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