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