1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.common.ui.client.widgets.list; |
17 | |
|
18 | |
import com.google.gwt.event.dom.client.ClickEvent; |
19 | |
import com.google.gwt.event.dom.client.ClickHandler; |
20 | |
import com.google.gwt.event.logical.shared.CloseEvent; |
21 | |
import com.google.gwt.event.logical.shared.CloseHandler; |
22 | |
import com.google.gwt.event.shared.HandlerRegistration; |
23 | |
import com.google.gwt.user.client.Timer; |
24 | |
import com.google.gwt.user.client.ui.Composite; |
25 | |
import com.google.gwt.user.client.ui.HasName; |
26 | |
import com.google.gwt.user.client.ui.HorizontalPanel; |
27 | |
import com.google.gwt.user.client.ui.Widget; |
28 | |
|
29 | |
import org.kuali.student.common.assembly.data.Data; |
30 | |
import org.kuali.student.common.assembly.data.Data.DataValue; |
31 | |
import org.kuali.student.common.assembly.data.Data.Property; |
32 | |
import org.kuali.student.common.assembly.data.Data.StringKey; |
33 | |
import org.kuali.student.common.assembly.data.Data.Value; |
34 | |
import org.kuali.student.common.ui.client.configurable.mvc.WidgetConfigInfo; |
35 | |
import org.kuali.student.common.ui.client.mvc.*; |
36 | |
import org.kuali.student.common.ui.client.util.UtilConstants; |
37 | |
import org.kuali.student.common.ui.client.widgets.DataHelper; |
38 | |
import org.kuali.student.common.ui.client.widgets.HasInputWidget; |
39 | |
import org.kuali.student.common.ui.client.widgets.KSButton; |
40 | |
import org.kuali.student.common.ui.client.widgets.KSButtonAbstract.ButtonStyle; |
41 | |
import org.kuali.student.common.ui.client.widgets.KSItemLabel; |
42 | |
import org.kuali.student.common.ui.client.widgets.layout.VerticalFlowPanel; |
43 | |
import org.kuali.student.common.ui.client.widgets.menus.KSListPanel; |
44 | |
import org.kuali.student.common.ui.client.widgets.menus.KSListPanel.ListType; |
45 | |
import org.kuali.student.common.ui.client.widgets.search.KSPicker; |
46 | |
import org.kuali.student.common.ui.client.widgets.search.SelectedResults; |
47 | |
import org.kuali.student.common.ui.client.widgets.suggestbox.KSSuggestBox; |
48 | |
|
49 | |
import java.util.ArrayList; |
50 | |
import java.util.Iterator; |
51 | |
import java.util.List; |
52 | |
import java.util.Map; |
53 | |
import java.util.Map.Entry; |
54 | |
|
55 | 0 | public class KSSelectedList extends Composite implements HasDataValue, HasName, HasSelectionChangeHandlers, HasWidgetReadyCallback, TranslatableValueWidget, HasInputWidget, HasFocusLostCallbacks { |
56 | |
private static final String VALUE = "value"; |
57 | |
private static final String DISPLAY = "display"; |
58 | |
|
59 | |
private String name; |
60 | |
private boolean initialized; |
61 | |
private VerticalFlowPanel mainPanel; |
62 | |
private HorizontalPanel pickerPanel; |
63 | |
private KSPicker picker; |
64 | |
private KSButton addItemButton; |
65 | |
private KSListPanel valuesPanel; |
66 | |
|
67 | |
|
68 | |
|
69 | 0 | private List<KSItemLabel> selectedItems = new ArrayList<KSItemLabel>(); |
70 | 0 | private List<KSItemLabel> removedItems = new ArrayList<KSItemLabel>(); |
71 | 0 | public static ItemDataHelper itemDataHelper = new ItemDataHelper(); |
72 | |
|
73 | 0 | private final List<SelectionChangeHandler> selectionChangeHandlers = new ArrayList<SelectionChangeHandler>(); |
74 | 0 | private final List<Callback<Widget>> widgetReadyCallbacks = new ArrayList<Callback<Widget>>(); |
75 | 0 | private boolean hasDetails = false; |
76 | |
|
77 | |
private WidgetConfigInfo config; |
78 | |
|
79 | 0 | public KSSelectedList(WidgetConfigInfo config, boolean hasDetails) { |
80 | 0 | init(config, hasDetails); |
81 | 0 | } |
82 | |
|
83 | 0 | public KSSelectedList(WidgetConfigInfo config) { |
84 | 0 | init(config, false); |
85 | 0 | } |
86 | |
|
87 | |
public WidgetConfigInfo getConfig() { |
88 | 0 | return config; |
89 | |
} |
90 | |
|
91 | |
private void init(WidgetConfigInfo config, final boolean hasDetails) { |
92 | 0 | this.config = config; |
93 | 0 | this.hasDetails = hasDetails; |
94 | 0 | mainPanel = new VerticalFlowPanel(); |
95 | 0 | initWidget(mainPanel); |
96 | 0 | if (config.canEdit) { |
97 | 0 | pickerPanel = new HorizontalPanel(); |
98 | 0 | pickerPanel.addStyleName("ks-selected-list-picker"); |
99 | 0 | addItemButton = new KSButton("Add to list", ButtonStyle.SECONDARY_SMALL); |
100 | 0 | addItemButton.setEnabled(false); |
101 | 0 | picker = new KSPicker(config); |
102 | 0 | picker.setAdvancedSearchCallback(createAdvancedSearchCallback()); |
103 | 0 | addSelectionChangeHandler(); |
104 | |
|
105 | 0 | addItemButton.addClickHandler(new ClickHandler() { |
106 | |
@Override |
107 | |
public void onClick(ClickEvent event) { |
108 | 0 | DataValue v = (DataValue) picker.getValue(); |
109 | 0 | Data d = v.get(); |
110 | |
|
111 | 0 | Iterator<Property> iter = d.realPropertyIterator(); |
112 | 0 | while (iter.hasNext()) { |
113 | 0 | Property p = iter.next(); |
114 | 0 | String s = p.getValue(); |
115 | 0 | KSItemLabel selectedItem = createItem(s, picker.getDisplayValue(), |
116 | |
hasDetails); |
117 | 0 | addItem(selectedItem); |
118 | 0 | } |
119 | 0 | picker.clear(); |
120 | 0 | addItemButton.setEnabled(false); |
121 | |
|
122 | 0 | } |
123 | |
}); |
124 | |
|
125 | 0 | pickerPanel.add(picker); |
126 | 0 | pickerPanel.add(addItemButton); |
127 | |
|
128 | |
|
129 | 0 | mainPanel.add(pickerPanel); |
130 | |
} |
131 | |
|
132 | |
|
133 | 0 | valuesPanel = new KSListPanel(ListType.UNORDERED); |
134 | 0 | if (config.canEdit) { |
135 | 0 | valuesPanel.setStyleName("ks-selected-list"); |
136 | |
} else { |
137 | 0 | valuesPanel.setStyleName("ks-selected-list-readOnly"); |
138 | |
} |
139 | 0 | mainPanel.add(valuesPanel); |
140 | 0 | initialized = true; |
141 | 0 | widgetReady(); |
142 | 0 | } |
143 | |
|
144 | |
public KSListPanel separateValuesPanel() { |
145 | 0 | mainPanel.remove(valuesPanel); |
146 | 0 | return valuesPanel; |
147 | |
} |
148 | |
|
149 | |
private void widgetReady() { |
150 | 0 | for (Callback<Widget> callback : widgetReadyCallbacks) { |
151 | 0 | callback.exec(this); |
152 | |
} |
153 | 0 | } |
154 | |
|
155 | |
private Callback<List<SelectedResults>> createAdvancedSearchCallback() { |
156 | 0 | Callback<List<SelectedResults>> result = new Callback<List<SelectedResults>>() { |
157 | |
public void exec(List<SelectedResults> results) { |
158 | 0 | if (results.size() > 0) { |
159 | 0 | for (SelectedResults res : results) { |
160 | 0 | KSItemLabel item = createItem(res.getReturnKey(), res.getDisplayKey(), hasDetails); |
161 | 0 | addItem(item, false); |
162 | 0 | } |
163 | 0 | picker.clear(); |
164 | 0 | addItemButton.setEnabled(false); |
165 | 0 | selectionChanged(); |
166 | |
} |
167 | 0 | } |
168 | |
|
169 | |
}; |
170 | 0 | return result; |
171 | |
} |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
private void addSelectionChangeHandler() { |
178 | 0 | if (picker.getInputWidget() instanceof KSSuggestBox) { |
179 | 0 | KSSuggestBox suggestBox = (KSSuggestBox) picker.getInputWidget(); |
180 | 0 | suggestBox.addSelectionChangeHandler(new SelectionChangeHandler() { |
181 | |
@Override |
182 | |
public void onSelectionChange(SelectionChangeEvent event) { |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | 0 | String userValue = ((KSSuggestBox) picker.getInputWidget()).getText(); |
189 | 0 | String displayValue = picker.getDisplayValue(); |
190 | 0 | boolean enabled = displayValue != null && !displayValue.isEmpty() && displayValue.equals(userValue); |
191 | 0 | addItemButton.setEnabled(enabled); |
192 | 0 | } |
193 | |
}); |
194 | |
} |
195 | 0 | } |
196 | |
|
197 | |
public void addItem(final KSItemLabel item) { |
198 | 0 | addItem(item, true); |
199 | 0 | } |
200 | |
|
201 | |
public void addItem(final KSItemLabel item, boolean fireChangeListeners) { |
202 | 0 | if (removedItems.contains(item)) { |
203 | 0 | removedItems.remove(item); |
204 | |
} |
205 | 0 | if (!selectedItems.contains(item)) { |
206 | 0 | selectedItems.add(item); |
207 | |
} |
208 | 0 | valuesPanel.add(item); |
209 | 0 | if (fireChangeListeners) { |
210 | 0 | selectionChanged(); |
211 | |
} |
212 | 0 | if (config.canEdit && fireChangeListeners) { |
213 | 0 | item.setHighlighted(true); |
214 | 0 | new Timer() { |
215 | |
@Override |
216 | |
public void run() { |
217 | 0 | item.setHighlighted(false); |
218 | 0 | } |
219 | |
}.schedule(5000); |
220 | |
} else { |
221 | 0 | item.removeHighlight(); |
222 | |
} |
223 | 0 | } |
224 | |
|
225 | |
private void selectionChanged() { |
226 | 0 | for (SelectionChangeHandler handler : selectionChangeHandlers) { |
227 | 0 | handler.onSelectionChange(new SelectionChangeEvent(this)); |
228 | |
} |
229 | 0 | } |
230 | |
|
231 | |
public void clear() { |
232 | 0 | selectedItems = new ArrayList<KSItemLabel>(); |
233 | 0 | removedItems = new ArrayList<KSItemLabel>(); |
234 | 0 | valuesPanel.clear(); |
235 | 0 | } |
236 | |
|
237 | |
@Override |
238 | |
public String getName() { |
239 | |
|
240 | 0 | return name; |
241 | |
} |
242 | |
|
243 | |
@Override |
244 | |
public void setName(String name) { |
245 | 0 | this.name = name; |
246 | 0 | } |
247 | |
|
248 | |
|
249 | |
@Override |
250 | |
public HandlerRegistration addSelectionChangeHandler(final SelectionChangeHandler handler) { |
251 | 0 | selectionChangeHandlers.add(handler); |
252 | 0 | HandlerRegistration result = new HandlerRegistration() { |
253 | |
@Override |
254 | |
public void removeHandler() { |
255 | 0 | selectionChangeHandlers.remove(handler); |
256 | 0 | } |
257 | |
}; |
258 | 0 | return result; |
259 | |
} |
260 | |
|
261 | |
@Override |
262 | |
public void addWidgetReadyCallback(Callback<Widget> callback) { |
263 | 0 | widgetReadyCallbacks.add(callback); |
264 | 0 | } |
265 | |
|
266 | |
@Override |
267 | |
public boolean isInitialized() { |
268 | 0 | return initialized; |
269 | |
} |
270 | |
|
271 | |
@Override |
272 | |
public void setInitialized(boolean initialized) { |
273 | 0 | this.initialized = initialized; |
274 | 0 | } |
275 | |
|
276 | |
|
277 | |
@Override |
278 | |
public void addValueChangeCallback(Callback<Value> callback) { |
279 | |
|
280 | |
|
281 | 0 | } |
282 | |
|
283 | |
@Override |
284 | |
public Value getValue() { |
285 | 0 | Data data = new Data(); |
286 | 0 | for (KSItemLabel item : selectedItems) { |
287 | 0 | data.add(item.getKey()); |
288 | |
} |
289 | 0 | if (picker.getDisplayValue().equals(UtilConstants.IMPOSSIBLE_CHARACTERS)) { |
290 | 0 | data.add(UtilConstants.IMPOSSIBLE_CHARACTERS); |
291 | |
} |
292 | 0 | DataValue result = new DataValue(data); |
293 | 0 | return result; |
294 | |
} |
295 | |
|
296 | |
public List<KSItemLabel> getSelectedItems() { |
297 | 0 | return selectedItems; |
298 | |
} |
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
public Value getValueWithTranslations() { |
306 | 0 | Data data = new Data(); |
307 | 0 | Data _runtimeData = new Data(); |
308 | |
|
309 | 0 | for (KSItemLabel item : selectedItems) { |
310 | 0 | data.add(item.getKey()); |
311 | 0 | Data displayData = new Data(); |
312 | 0 | displayData.set("id-translation", item.getDisplayText()); |
313 | 0 | _runtimeData.add(displayData); |
314 | 0 | } |
315 | 0 | if (picker.getDisplayValue().equals(UtilConstants.IMPOSSIBLE_CHARACTERS)) { |
316 | 0 | data.add(UtilConstants.IMPOSSIBLE_CHARACTERS); |
317 | 0 | Data displayData = new Data(); |
318 | 0 | displayData.set("id-translation", UtilConstants.IMPOSSIBLE_CHARACTERS); |
319 | 0 | _runtimeData.add(displayData); |
320 | |
} |
321 | 0 | data.set(new StringKey("_runtimeData"), _runtimeData); |
322 | 0 | DataValue result = new DataValue(data); |
323 | 0 | return result; |
324 | |
} |
325 | |
|
326 | |
private KSItemLabel createItem(String value, String display, boolean hasDetails) { |
327 | 0 | Data itemData = toItemData(value, display); |
328 | 0 | DataValue itemDataValue = new DataValue(itemData); |
329 | 0 | KSItemLabel item = new KSItemLabel(KSSelectedList.this.config.canEdit, hasDetails, |
330 | |
itemDataHelper); |
331 | 0 | item.setValue(itemDataValue); |
332 | 0 | item.addCloseHandler(new CloseHandler<KSItemLabel>() { |
333 | |
@Override |
334 | |
public void onClose(CloseEvent<KSItemLabel> event) { |
335 | 0 | KSItemLabel itemToBeDeleted = event.getTarget(); |
336 | 0 | selectedItems.remove(itemToBeDeleted); |
337 | 0 | removedItems.add(itemToBeDeleted); |
338 | 0 | valuesPanel.remove(itemToBeDeleted); |
339 | 0 | selectionChanged(); |
340 | 0 | } |
341 | |
}); |
342 | 0 | return item; |
343 | |
} |
344 | |
|
345 | |
private Data toItemData(String value, String display) { |
346 | 0 | Data data = new Data(); |
347 | 0 | data.set(VALUE, value); |
348 | 0 | data.set(DISPLAY, display); |
349 | 0 | return data; |
350 | |
} |
351 | |
|
352 | |
@Override |
353 | |
public void setValue(Value value) { |
354 | 0 | clear(); |
355 | 0 | if (value != null) { |
356 | 0 | Data data = ((DataValue) value).get(); |
357 | 0 | Iterator<Property> iter = data.realPropertyIterator(); |
358 | 0 | while (iter.hasNext()) { |
359 | 0 | Property p = iter.next(); |
360 | 0 | String v = (String) p.getValue(); |
361 | |
|
362 | 0 | KSItemLabel item = createItem(v, "Display: " + v, hasDetails); |
363 | 0 | addItem(item); |
364 | 0 | } |
365 | |
} |
366 | 0 | } |
367 | |
|
368 | |
@Override |
369 | |
public void addFocusLostCallback(Callback<Boolean> callback) { |
370 | 0 | if (picker != null) |
371 | 0 | picker.addFocusLostCallback(callback); |
372 | 0 | } |
373 | |
|
374 | 0 | public static class ItemDataHelper implements DataHelper { |
375 | |
|
376 | |
@Override |
377 | |
public String parse(Data data) { |
378 | |
String result; |
379 | 0 | if (data == null) { |
380 | 0 | return null; |
381 | |
} |
382 | 0 | result = (String) data.get(DISPLAY); |
383 | 0 | return result; |
384 | |
} |
385 | |
|
386 | |
@Override |
387 | |
public String getKey(Data data) { |
388 | |
String result; |
389 | 0 | if (data == null) { |
390 | 0 | return null; |
391 | |
} |
392 | 0 | result = (String) data.get(VALUE); |
393 | 0 | return result; |
394 | |
} |
395 | |
|
396 | |
} |
397 | |
|
398 | |
@Override |
399 | |
public void setValue(String id, String translation) { |
400 | 0 | clear(); |
401 | 0 | if (id != null && translation != null) { |
402 | 0 | addItem(createItem(id, translation, hasDetails), false); |
403 | |
} |
404 | 0 | } |
405 | |
|
406 | |
@Override |
407 | |
public void setValue(Map<String, String> translations) { |
408 | 0 | clear(); |
409 | 0 | if (translations != null) { |
410 | 0 | for (Entry<String, String> e : translations.entrySet()) { |
411 | 0 | addItem(createItem(e.getKey(), e.getValue(), hasDetails), false); |
412 | |
} |
413 | |
} |
414 | 0 | } |
415 | |
|
416 | |
@Override |
417 | |
public Widget getInputWidget() { |
418 | 0 | if (picker == null) { |
419 | 0 | return valuesPanel; |
420 | |
} |
421 | 0 | return picker.getInputWidget(); |
422 | |
} |
423 | |
} |