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.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  public class SelectItemWidgetBinding extends ModelWidgetBindingSupport<KSSelectItemWidgetAbstract> {
40  
41      public static SelectItemWidgetBinding INSTANCE = new SelectItemWidgetBinding();
42  
43      private SelectItemWidgetBinding() {}
44  
45      @Override
46      public void setModelValue(KSSelectItemWidgetAbstract object, DataModel model, String path) {
47          QueryPath qPath = QueryPath.parse(path);
48  
49  
50          if (object.isMultipleSelect()) {
51          	Data newValue = (Data)getWidgetValue(object).get();
52              Data oldValue = model.get(qPath);
53              if (!nullsafeEquals(oldValue, newValue)) {
54                  model.set(qPath, newValue);
55                  setDirtyFlag(model, qPath);
56              }
57          } else {
58              String newValue = object.getSelectedItem();
59              String oldValue = model.get(qPath);
60              if (!nullsafeEquals(oldValue, newValue)) {
61                  model.set(qPath, object.getSelectedItem());
62                  setDirtyFlag(model, qPath);
63              }
64          }
65  
66      }
67  
68      @Override
69      public void setWidgetValue(KSSelectItemWidgetAbstract object, final DataModel model, final String path) {
70          QueryPath qPath = QueryPath.parse(path);
71          Object value = model.get(qPath);
72          setWidgetValue(object, value);
73      }
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          if (object.isMultipleSelect()) {
86          	Data data = new Data();
87          	List<String> selectedItems = object.getSelectedItems();
88  	        for (String stringItem : selectedItems) {
89  	           data.add(stringItem);
90  	        }
91  	        value = new Data.DataValue(data);
92          } else {
93          	value = new Data.StringValue(object.getSelectedItem());
94          }
95  
96          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     	Callback<Widget> selectListItemsCallback = new Callback<Widget>(){
106             @Override
107             public void exec(Widget widget) {
108 
109                 ((KSSelectItemWidgetAbstract)widget).clear();
110                 if (value != null){
111 	                if (value instanceof String || value instanceof StringValue) {
112 	                	String itemId = (String)(value instanceof String ? value:((StringValue)value).get());
113 	                    ((KSSelectItemWidgetAbstract)widget).selectItem(itemId);
114 	                } else if (value instanceof Data || value instanceof DataValue) {
115 	                	Data data = (Data)(value instanceof Data ? value:((DataValue)value).get());
116 	                    for (Iterator itr = data.iterator(); itr.hasNext();) {
117 	                        Property p = (Property) itr.next();
118 	                        ((KSSelectItemWidgetAbstract)widget).selectItem((String) p.getValue());
119 	                    }
120 	                }
121                 }
122             }            
123         };
124 
125         if (!object.isInitialized()){
126             object.addWidgetReadyCallback(selectListItemsCallback);
127         } else{
128             selectListItemsCallback.exec(object);
129         }    	
130     }
131     
132 
133 }