View Javadoc

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  public class SelectItemWidgetBinding extends ModelWidgetBindingSupport<KSSelectItemWidgetAbstract> {
34  
35      public static SelectItemWidgetBinding INSTANCE = new SelectItemWidgetBinding();
36  
37      private SelectItemWidgetBinding() {}
38  
39      @Override
40      public void setModelValue(KSSelectItemWidgetAbstract object, DataModel model, String path) {
41          QueryPath qPath = QueryPath.parse(path);
42  
43  
44          if (object.isMultipleSelect()) {
45          	Data newValue = (Data)getWidgetValue(object).get();
46              Data oldValue = model.get(qPath);
47              if (!nullsafeEquals(oldValue, newValue)) {
48                  model.set(qPath, newValue);
49                  setDirtyFlag(model, qPath);
50              }
51          } else {
52              String newValue = object.getSelectedItem();
53              String oldValue = model.get(qPath);
54              if (!nullsafeEquals(oldValue, newValue)) {
55                  model.set(qPath, object.getSelectedItem());
56                  setDirtyFlag(model, qPath);
57              }
58          }
59  
60      }
61  
62      @Override
63      public void setWidgetValue(KSSelectItemWidgetAbstract object, final DataModel model, final String path) {
64          QueryPath qPath = QueryPath.parse(path);
65          Object value = model.get(qPath);
66          setWidgetValue(object, value);
67      }
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          if (object.isMultipleSelect()) {
80          	Data data = new Data();
81          	List<String> selectedItems = object.getSelectedItems();
82  	        for (String stringItem : selectedItems) {
83  	           data.add(stringItem);
84  	        }
85  	        value = new Data.DataValue(data);
86          } else {
87          	value = new Data.StringValue(object.getSelectedItem());
88          }
89  
90          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      	Callback<Widget> selectListItemsCallback = new Callback<Widget>(){
100             @Override
101             public void exec(Widget widget) {
102 
103                 ((KSSelectItemWidgetAbstract)widget).clear();
104                 if (value != null){
105 	                if (value instanceof String || value instanceof StringValue) {
106 	                	String itemId = (String)(value instanceof String ? value:((StringValue)value).get());
107 	                    ((KSSelectItemWidgetAbstract)widget).selectItem(itemId);
108 	                } else if (value instanceof Data || value instanceof DataValue) {
109 	                	Data data = (Data)(value instanceof Data ? value:((DataValue)value).get());
110 	                    for (Iterator itr = data.iterator(); itr.hasNext();) {
111 	                        Property p = (Property) itr.next();
112 	                        ((KSSelectItemWidgetAbstract)widget).selectItem((String) p.getValue());
113 	                    }
114 	                }
115                 }
116             }            
117         };
118 
119         if (!object.isInitialized()){
120             object.addWidgetReadyCallback(selectListItemsCallback);
121         } else{
122             selectListItemsCallback.exec(object);
123         }    	
124     }
125     
126 
127 }