Coverage Report - org.kuali.student.common.ui.client.configurable.mvc.binding.SelectItemWidgetBinding
 
Classes in this File Line Coverage Branch Coverage Complexity
SelectItemWidgetBinding
0%
0/35
0%
0/12
3.333
SelectItemWidgetBinding$1
0%
0/13
0%
0/16
3.333
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.student.common.ui.client.configurable.mvc.binding;
 17  
 
 18  
 import java.util.Iterator;
 19  
 import java.util.List;
 20  
 
 21  
 import org.kuali.student.common.assembly.data.Data;
 22  
 import org.kuali.student.common.assembly.data.QueryPath;
 23  
 import org.kuali.student.common.assembly.data.Data.DataValue;
 24  
 import org.kuali.student.common.assembly.data.Data.Property;
 25  
 import org.kuali.student.common.assembly.data.Data.StringValue;
 26  
 import org.kuali.student.common.assembly.data.Data.Value;
 27  
 import org.kuali.student.common.ui.client.mvc.Callback;
 28  
 import org.kuali.student.common.ui.client.mvc.DataModel;
 29  
 import org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract;
 30  
 
 31  
 import com.google.gwt.user.client.ui.Widget;
 32  
 
 33  
 /**
 34  
  * Model widget binding for KSSelectItemWidgetAbstract widgets
 35  
  * 
 36  
  * @author Kuali Student Team
 37  
  *
 38  
  */
 39  0
 public class SelectItemWidgetBinding extends ModelWidgetBindingSupport<KSSelectItemWidgetAbstract> {
 40  
 
 41  0
     public static SelectItemWidgetBinding INSTANCE = new SelectItemWidgetBinding();
 42  
 
 43  0
     private SelectItemWidgetBinding() {}
 44  
 
 45  
     @Override
 46  
     public void setModelValue(KSSelectItemWidgetAbstract object, DataModel model, String path) {
 47  0
         QueryPath qPath = QueryPath.parse(path);
 48  
 
 49  
 
 50  0
         if (object.isMultipleSelect()) {
 51  0
                 Data newValue = (Data)getWidgetValue(object).get();
 52  0
             Data oldValue = model.get(qPath);
 53  0
             if (!nullsafeEquals(oldValue, newValue)) {
 54  0
                 model.set(qPath, newValue);
 55  0
                 setDirtyFlag(model, qPath);
 56  
             }
 57  0
         } else {
 58  0
             String newValue = object.getSelectedItem();
 59  0
             String oldValue = model.get(qPath);
 60  0
             if (!nullsafeEquals(oldValue, newValue)) {
 61  0
                 model.set(qPath, object.getSelectedItem());
 62  0
                 setDirtyFlag(model, qPath);
 63  
             }
 64  
         }
 65  
 
 66  0
     }
 67  
 
 68  
     @Override
 69  
     public void setWidgetValue(KSSelectItemWidgetAbstract object, final DataModel model, final String path) {
 70  0
         QueryPath qPath = QueryPath.parse(path);
 71  0
         Object value = model.get(qPath);
 72  0
         setWidgetValue(object, value);
 73  0
     }
 74  
     
 75  
     /**
 76  
      * Helper method get list item widget's values as a Data object
 77  
      * 
 78  
      * @param object
 79  
      * @param value
 80  
      * @return
 81  
      */
 82  
     public Value getWidgetValue(KSSelectItemWidgetAbstract object){
 83  
             Value value;
 84  
             
 85  0
         if (object.isMultipleSelect()) {
 86  0
                 Data data = new Data();
 87  0
                 List<String> selectedItems = object.getSelectedItems();
 88  0
                 for (String stringItem : selectedItems) {
 89  0
                    data.add(stringItem);
 90  
                 }
 91  0
                 value = new Data.DataValue(data);
 92  0
         } else {
 93  0
                 value = new Data.StringValue(object.getSelectedItem());
 94  
         }
 95  
 
 96  0
         return value;
 97  
     }
 98  
     
 99  
     /**
 100  
      * Helper method to set Data object to a list item widget
 101  
      * @param object
 102  
      * @param value
 103  
      */
 104  
     public void setWidgetValue(KSSelectItemWidgetAbstract object, final Object value){
 105  0
             Callback<Widget> selectListItemsCallback = new Callback<Widget>(){
 106  
             @Override
 107  
             public void exec(Widget widget) {
 108  
 
 109  0
                 ((KSSelectItemWidgetAbstract)widget).clear();
 110  0
                 if (value != null){
 111  0
                         if (value instanceof String || value instanceof StringValue) {
 112  0
                                 String itemId = (String)(value instanceof String ? value:((StringValue)value).get());
 113  0
                             ((KSSelectItemWidgetAbstract)widget).selectItem(itemId);
 114  0
                         } else if (value instanceof Data || value instanceof DataValue) {
 115  0
                                 Data data = (Data)(value instanceof Data ? value:((DataValue)value).get());
 116  0
                             for (Iterator itr = data.iterator(); itr.hasNext();) {
 117  0
                                 Property p = (Property) itr.next();
 118  0
                                 ((KSSelectItemWidgetAbstract)widget).selectItem((String) p.getValue());
 119  0
                             }
 120  
                         }
 121  
                 }
 122  0
             }            
 123  
         };
 124  
 
 125  0
         if (!object.isInitialized()){
 126  0
             object.addWidgetReadyCallback(selectListItemsCallback);
 127  
         } else{
 128  0
             selectListItemsCallback.exec(object);
 129  
         }            
 130  0
     }
 131  
     
 132  
 
 133  
 }