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.ui.client.mvc.Callback;
 22  
 import org.kuali.student.common.ui.client.mvc.DataModel;
 23  
 import org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract;
 24  
 import org.kuali.student.core.assembly.data.Data;
 25  
 import org.kuali.student.core.assembly.data.QueryPath;
 26  
 import org.kuali.student.core.assembly.data.Data.DataValue;
 27  
 import org.kuali.student.core.assembly.data.Data.Property;
 28  
 import org.kuali.student.core.assembly.data.Data.StringValue;
 29  
 import org.kuali.student.core.assembly.data.Data.Value;
 30  
 
 31  
 import com.google.gwt.user.client.ui.Widget;
 32  
 
 33  0
 public class SelectItemWidgetBinding extends ModelWidgetBindingSupport<KSSelectItemWidgetAbstract> {
 34  
 
 35  0
     public static SelectItemWidgetBinding INSTANCE = new SelectItemWidgetBinding();
 36  
 
 37  0
     private SelectItemWidgetBinding() {}
 38  
 
 39  
     @Override
 40  
     public void setModelValue(KSSelectItemWidgetAbstract object, DataModel model, String path) {
 41  0
         QueryPath qPath = QueryPath.parse(path);
 42  
 
 43  
 
 44  0
         if (object.isMultipleSelect()) {
 45  0
                 Data newValue = (Data)getWidgetValue(object).get();
 46  0
             Data oldValue = model.get(qPath);
 47  0
             if (!nullsafeEquals(oldValue, newValue)) {
 48  0
                 model.set(qPath, newValue);
 49  0
                 setDirtyFlag(model, qPath);
 50  
             }
 51  0
         } else {
 52  0
             String newValue = object.getSelectedItem();
 53  0
             String oldValue = model.get(qPath);
 54  0
             if (!nullsafeEquals(oldValue, newValue)) {
 55  0
                 model.set(qPath, object.getSelectedItem());
 56  0
                 setDirtyFlag(model, qPath);
 57  
             }
 58  
         }
 59  
 
 60  0
     }
 61  
 
 62  
     @Override
 63  
     public void setWidgetValue(KSSelectItemWidgetAbstract object, final DataModel model, final String path) {
 64  0
         QueryPath qPath = QueryPath.parse(path);
 65  0
         Object value = model.get(qPath);
 66  0
         setWidgetValue(object, value);
 67  0
     }
 68  
     
 69  
     /**
 70  
      * Helper method get list item widget's values as a Data object
 71  
      * 
 72  
      * @param object
 73  
      * @param value
 74  
      * @return
 75  
      */
 76  
     public Value getWidgetValue(KSSelectItemWidgetAbstract object){
 77  
             Value value;
 78  
             
 79  0
         if (object.isMultipleSelect()) {
 80  0
                 Data data = new Data();
 81  0
                 List<String> selectedItems = object.getSelectedItems();
 82  0
                 for (String stringItem : selectedItems) {
 83  0
                    data.add(stringItem);
 84  
                 }
 85  0
                 value = new Data.DataValue(data);
 86  0
         } else {
 87  0
                 value = new Data.StringValue(object.getSelectedItem());
 88  
         }
 89  
 
 90  0
         return value;
 91  
     }
 92  
     
 93  
     /**
 94  
      * Helper method to set Data object to a list item widget
 95  
      * @param object
 96  
      * @param value
 97  
      */
 98  
     public void setWidgetValue(KSSelectItemWidgetAbstract object, final Object value){
 99  0
             Callback<Widget> selectListItemsCallback = new Callback<Widget>(){
 100  
             @Override
 101  
             public void exec(Widget widget) {
 102  
 
 103  0
                 ((KSSelectItemWidgetAbstract)widget).clear();
 104  0
                 if (value != null){
 105  0
                         if (value instanceof String || value instanceof StringValue) {
 106  0
                                 String itemId = (String)(value instanceof String ? value:((StringValue)value).get());
 107  0
                             ((KSSelectItemWidgetAbstract)widget).selectItem(itemId);
 108  0
                         } else if (value instanceof Data || value instanceof DataValue) {
 109  0
                                 Data data = (Data)(value instanceof Data ? value:((DataValue)value).get());
 110  0
                             for (Iterator itr = data.iterator(); itr.hasNext();) {
 111  0
                                 Property p = (Property) itr.next();
 112  0
                                 ((KSSelectItemWidgetAbstract)widget).selectItem((String) p.getValue());
 113  0
                             }
 114  
                         }
 115  
                 }
 116  0
             }            
 117  
         };
 118  
 
 119  0
         if (!object.isInitialized()){
 120  0
             object.addWidgetReadyCallback(selectListItemsCallback);
 121  
         } else{
 122  0
             selectListItemsCallback.exec(object);
 123  
         }            
 124  0
     }
 125  
     
 126  
 
 127  
 }