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