1
2
3
4
5
6
7
8
9
10
11
12
13
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
35
36
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
77
78
79
80
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
101
102
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 }