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.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
71
72
73
74
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
95
96
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 }