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 java.util.ArrayList; |
19 | |
import java.util.HashSet; |
20 | |
import java.util.Iterator; |
21 | |
import java.util.List; |
22 | |
import java.util.Map; |
23 | |
import java.util.Map.Entry; |
24 | |
|
25 | |
import org.kuali.student.common.assembly.data.Data; |
26 | |
import org.kuali.student.common.assembly.data.Data.DataValue; |
27 | |
import org.kuali.student.common.assembly.data.Data.Property; |
28 | |
import org.kuali.student.common.assembly.data.Data.StringKey; |
29 | |
import org.kuali.student.common.assembly.data.Data.Value; |
30 | |
import org.kuali.student.common.ui.client.configurable.mvc.WidgetConfigInfo; |
31 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
32 | |
import org.kuali.student.common.ui.client.mvc.HasCrossConstraints; |
33 | |
import org.kuali.student.common.ui.client.mvc.HasDataValue; |
34 | |
import org.kuali.student.common.ui.client.mvc.HasFocusLostCallbacks; |
35 | |
import org.kuali.student.common.ui.client.mvc.HasWidgetReadyCallback; |
36 | |
import org.kuali.student.common.ui.client.mvc.TranslatableValueWidget; |
37 | |
import org.kuali.student.common.ui.client.util.UtilConstants; |
38 | |
import org.kuali.student.common.ui.client.widgets.DataHelper; |
39 | |
import org.kuali.student.common.ui.client.widgets.HasInputWidget; |
40 | |
import org.kuali.student.common.ui.client.widgets.KSButton; |
41 | |
import org.kuali.student.common.ui.client.widgets.KSButtonAbstract.ButtonStyle; |
42 | |
import org.kuali.student.common.ui.client.widgets.KSDropDown; |
43 | |
import org.kuali.student.common.ui.client.widgets.KSItemLabel; |
44 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.ValidationProcessable; |
45 | |
import org.kuali.student.common.ui.client.widgets.layout.VerticalFlowPanel; |
46 | |
import org.kuali.student.common.ui.client.widgets.menus.KSListPanel; |
47 | |
import org.kuali.student.common.ui.client.widgets.menus.KSListPanel.ListType; |
48 | |
import org.kuali.student.common.ui.client.widgets.search.KSPicker; |
49 | |
import org.kuali.student.common.ui.client.widgets.search.SelectedResults; |
50 | |
import org.kuali.student.common.ui.client.widgets.suggestbox.KSSuggestBox; |
51 | |
import org.kuali.student.common.validation.dto.ValidationResultInfo; |
52 | |
import org.kuali.student.common.validation.dto.ValidationResultInfo.ErrorLevel; |
53 | |
|
54 | |
import com.google.gwt.event.dom.client.ClickEvent; |
55 | |
import com.google.gwt.event.dom.client.ClickHandler; |
56 | |
import com.google.gwt.event.logical.shared.CloseEvent; |
57 | |
import com.google.gwt.event.logical.shared.CloseHandler; |
58 | |
import com.google.gwt.event.shared.HandlerRegistration; |
59 | |
import com.google.gwt.user.client.Timer; |
60 | |
import com.google.gwt.user.client.ui.Composite; |
61 | |
import com.google.gwt.user.client.ui.HasName; |
62 | |
import com.google.gwt.user.client.ui.HorizontalPanel; |
63 | |
import com.google.gwt.user.client.ui.Widget; |
64 | |
|
65 | 0 | public class KSSelectedList extends Composite implements HasDataValue, HasName, HasSelectionChangeHandlers, HasWidgetReadyCallback, TranslatableValueWidget, HasInputWidget, HasFocusLostCallbacks, HasCrossConstraints, ValidationProcessable { |
66 | |
private static final String VALUE = "value"; |
67 | |
private static final String DISPLAY = "display"; |
68 | |
|
69 | |
private String name; |
70 | |
private boolean initialized; |
71 | |
private VerticalFlowPanel mainPanel; |
72 | |
private HorizontalPanel pickerPanel; |
73 | |
private KSPicker picker; |
74 | |
private KSButton addItemButton; |
75 | |
private KSListPanel valuesPanel; |
76 | |
|
77 | |
|
78 | |
|
79 | 0 | private List<KSItemLabel> selectedItems = new ArrayList<KSItemLabel>(); |
80 | 0 | private List<KSItemLabel> removedItems = new ArrayList<KSItemLabel>(); |
81 | 0 | public static ItemDataHelper itemDataHelper = new ItemDataHelper(); |
82 | |
|
83 | 0 | private final List<SelectionChangeHandler> selectionChangeHandlers = new ArrayList<SelectionChangeHandler>(); |
84 | 0 | private final List<Callback<Widget>> widgetReadyCallbacks = new ArrayList<Callback<Widget>>(); |
85 | 0 | private boolean hasDetails = false; |
86 | |
|
87 | |
private WidgetConfigInfo config; |
88 | |
|
89 | 0 | public KSSelectedList(WidgetConfigInfo config, boolean hasDetails) { |
90 | 0 | init(config, hasDetails); |
91 | 0 | } |
92 | |
|
93 | 0 | public KSSelectedList(WidgetConfigInfo config) { |
94 | 0 | init(config, false); |
95 | 0 | } |
96 | |
|
97 | |
public WidgetConfigInfo getConfig() { |
98 | 0 | return config; |
99 | |
} |
100 | |
|
101 | |
private void init(WidgetConfigInfo config, final boolean hasDetails) { |
102 | 0 | this.config = config; |
103 | 0 | this.hasDetails = hasDetails; |
104 | 0 | mainPanel = new VerticalFlowPanel(); |
105 | 0 | initWidget(mainPanel); |
106 | 0 | if (config.canEdit) { |
107 | 0 | pickerPanel = new HorizontalPanel(); |
108 | 0 | pickerPanel.addStyleName("ks-selected-list-picker"); |
109 | 0 | addItemButton = new KSButton("Add to list", ButtonStyle.SECONDARY_SMALL); |
110 | 0 | addItemButton.setEnabled(false); |
111 | 0 | picker = new KSPicker(config); |
112 | 0 | picker.setAdvancedSearchCallback(createAdvancedSearchCallback()); |
113 | 0 | addSelectionChangeHandler(); |
114 | |
|
115 | 0 | addItemButton.addClickHandler(new ClickHandler() { |
116 | |
@Override |
117 | |
public void onClick(ClickEvent event) { |
118 | 0 | Value v = picker.getValue(); |
119 | 0 | if(v instanceof DataValue){ |
120 | 0 | Data d = ((DataValue) picker.getValue()).get(); |
121 | |
|
122 | 0 | Iterator<Property> iter = d.realPropertyIterator(); |
123 | 0 | while (iter.hasNext()) { |
124 | 0 | Property p = iter.next(); |
125 | 0 | String s = p.getValue(); |
126 | 0 | KSItemLabel selectedItem = createItem(s, picker.getDisplayValue(), |
127 | |
hasDetails); |
128 | 0 | addItem(selectedItem); |
129 | 0 | } |
130 | 0 | }else{ |
131 | 0 | String s = v.get(); |
132 | 0 | KSItemLabel selectedItem = createItem(s, picker.getDisplayValue(), |
133 | |
hasDetails); |
134 | 0 | addItem(selectedItem); |
135 | |
} |
136 | 0 | picker.clear(); |
137 | 0 | addItemButton.setEnabled(false); |
138 | 0 | } |
139 | |
}); |
140 | |
|
141 | 0 | pickerPanel.add(picker); |
142 | 0 | pickerPanel.add(addItemButton); |
143 | |
|
144 | |
|
145 | 0 | mainPanel.add(pickerPanel); |
146 | |
} |
147 | |
|
148 | |
|
149 | 0 | valuesPanel = new KSListPanel(ListType.UNORDERED); |
150 | 0 | if (config.canEdit) { |
151 | 0 | valuesPanel.setStyleName("ks-selected-list"); |
152 | |
} else { |
153 | 0 | valuesPanel.setStyleName("ks-selected-list-readOnly"); |
154 | |
} |
155 | 0 | mainPanel.add(valuesPanel); |
156 | 0 | initialized = true; |
157 | 0 | widgetReady(); |
158 | 0 | } |
159 | |
|
160 | |
public KSListPanel separateValuesPanel() { |
161 | 0 | mainPanel.remove(valuesPanel); |
162 | 0 | return valuesPanel; |
163 | |
} |
164 | |
|
165 | |
private void widgetReady() { |
166 | 0 | for (Callback<Widget> callback : widgetReadyCallbacks) { |
167 | 0 | callback.exec(this); |
168 | |
} |
169 | 0 | } |
170 | |
|
171 | |
private Callback<List<SelectedResults>> createAdvancedSearchCallback() { |
172 | 0 | Callback<List<SelectedResults>> result = new Callback<List<SelectedResults>>() { |
173 | |
public void exec(List<SelectedResults> results) { |
174 | 0 | if (results.size() > 0) { |
175 | 0 | for (SelectedResults res : results) { |
176 | 0 | KSItemLabel item = createItem(res.getReturnKey(), res.getDisplayKey(), hasDetails); |
177 | 0 | addItem(item, false); |
178 | 0 | } |
179 | 0 | picker.clear(); |
180 | 0 | addItemButton.setEnabled(false); |
181 | 0 | selectionChanged(); |
182 | |
} |
183 | 0 | } |
184 | |
|
185 | |
}; |
186 | 0 | return result; |
187 | |
} |
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
private void addSelectionChangeHandler() { |
194 | 0 | if (picker.getInputWidget() instanceof KSSuggestBox) { |
195 | 0 | KSSuggestBox suggestBox = (KSSuggestBox) picker.getInputWidget(); |
196 | 0 | suggestBox.addSelectionChangeHandler(new SelectionChangeHandler() { |
197 | |
@Override |
198 | |
public void onSelectionChange(SelectionChangeEvent event) { |
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | 0 | String userValue = ((KSSuggestBox) picker.getInputWidget()).getText(); |
205 | 0 | String displayValue = picker.getDisplayValue(); |
206 | 0 | boolean enabled = displayValue != null && !displayValue.isEmpty() && displayValue.equals(userValue); |
207 | 0 | addItemButton.setEnabled(enabled); |
208 | 0 | } |
209 | |
}); |
210 | 0 | } else if(picker.getInputWidget() instanceof KSDropDown){ |
211 | 0 | KSDropDown dropDown = (KSDropDown) picker.getInputWidget(); |
212 | 0 | dropDown.addSelectionChangeHandler(new SelectionChangeHandler() { |
213 | |
@Override |
214 | |
public void onSelectionChange(SelectionChangeEvent event) { |
215 | |
|
216 | 0 | String displayValue = picker.getDisplayValue(); |
217 | 0 | boolean enabled = displayValue != null && !displayValue.isEmpty(); |
218 | 0 | addItemButton.setEnabled(enabled); |
219 | 0 | } |
220 | |
}); |
221 | |
} |
222 | 0 | } |
223 | |
|
224 | |
public void addItem(String value, String display) { |
225 | 0 | addItem(createItem(value, display, hasDetails)); |
226 | 0 | } |
227 | |
|
228 | |
public void addItem(final KSItemLabel item) { |
229 | 0 | addItem(item, true); |
230 | 0 | } |
231 | |
|
232 | |
public void addItem(final KSItemLabel item, boolean fireChangeListeners) { |
233 | 0 | if (removedItems.contains(item)) { |
234 | 0 | removedItems.remove(item); |
235 | |
} |
236 | 0 | if (!selectedItems.contains(item)) { |
237 | 0 | selectedItems.add(item); |
238 | |
|
239 | 0 | valuesPanel.add(item); |
240 | 0 | if (fireChangeListeners) { |
241 | 0 | selectionChanged(); |
242 | |
} |
243 | 0 | if (config.canEdit && fireChangeListeners) { |
244 | 0 | item.setHighlighted(true); |
245 | 0 | new Timer() { |
246 | |
@Override |
247 | |
public void run() { |
248 | 0 | item.setHighlighted(false); |
249 | 0 | } |
250 | |
}.schedule(5000); |
251 | |
} else { |
252 | 0 | item.removeHighlight(); |
253 | |
} |
254 | |
} |
255 | 0 | } |
256 | |
|
257 | |
private void selectionChanged() { |
258 | 0 | for (SelectionChangeHandler handler : selectionChangeHandlers) { |
259 | 0 | handler.onSelectionChange(new SelectionChangeEvent(this)); |
260 | |
} |
261 | 0 | } |
262 | |
|
263 | |
public void clear() { |
264 | 0 | selectedItems = new ArrayList<KSItemLabel>(); |
265 | 0 | removedItems = new ArrayList<KSItemLabel>(); |
266 | 0 | valuesPanel.clear(); |
267 | 0 | } |
268 | |
|
269 | |
@Override |
270 | |
public String getName() { |
271 | |
|
272 | 0 | return name; |
273 | |
} |
274 | |
|
275 | |
@Override |
276 | |
public void setName(String name) { |
277 | 0 | this.name = name; |
278 | 0 | } |
279 | |
|
280 | |
|
281 | |
@Override |
282 | |
public HandlerRegistration addSelectionChangeHandler(final SelectionChangeHandler handler) { |
283 | 0 | selectionChangeHandlers.add(handler); |
284 | 0 | HandlerRegistration result = new HandlerRegistration() { |
285 | |
@Override |
286 | |
public void removeHandler() { |
287 | 0 | selectionChangeHandlers.remove(handler); |
288 | 0 | } |
289 | |
}; |
290 | 0 | return result; |
291 | |
} |
292 | |
|
293 | |
@Override |
294 | |
public void addWidgetReadyCallback(Callback<Widget> callback) { |
295 | 0 | widgetReadyCallbacks.add(callback); |
296 | 0 | } |
297 | |
|
298 | |
@Override |
299 | |
public boolean isInitialized() { |
300 | 0 | return initialized; |
301 | |
} |
302 | |
|
303 | |
@Override |
304 | |
public void setInitialized(boolean initialized) { |
305 | 0 | this.initialized = initialized; |
306 | 0 | } |
307 | |
|
308 | |
|
309 | |
@Override |
310 | |
public void addValueChangeCallback(Callback<Value> callback) { |
311 | |
|
312 | |
|
313 | 0 | } |
314 | |
|
315 | |
@Override |
316 | |
public Value getValue() { |
317 | 0 | Data data = new Data(); |
318 | 0 | for (KSItemLabel item : selectedItems) { |
319 | 0 | data.add(item.getKey()); |
320 | |
} |
321 | 0 | if (picker.getDisplayValue().equals(UtilConstants.IMPOSSIBLE_CHARACTERS)) { |
322 | 0 | data.add(UtilConstants.IMPOSSIBLE_CHARACTERS); |
323 | |
} |
324 | 0 | DataValue result = new DataValue(data); |
325 | 0 | return result; |
326 | |
} |
327 | |
|
328 | |
public List<KSItemLabel> getSelectedItems() { |
329 | 0 | return selectedItems; |
330 | |
} |
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
public Value getValueWithTranslations() { |
338 | 0 | Data data = new Data(); |
339 | 0 | Data _runtimeData = new Data(); |
340 | |
|
341 | 0 | for (KSItemLabel item : selectedItems) { |
342 | 0 | data.add(item.getKey()); |
343 | 0 | Data displayData = new Data(); |
344 | 0 | displayData.set("id-translation", item.getDisplayText()); |
345 | 0 | _runtimeData.add(displayData); |
346 | 0 | } |
347 | 0 | if (picker.getDisplayValue().equals(UtilConstants.IMPOSSIBLE_CHARACTERS)) { |
348 | 0 | data.add(UtilConstants.IMPOSSIBLE_CHARACTERS); |
349 | 0 | Data displayData = new Data(); |
350 | 0 | displayData.set("id-translation", UtilConstants.IMPOSSIBLE_CHARACTERS); |
351 | 0 | _runtimeData.add(displayData); |
352 | |
} |
353 | 0 | data.set(new StringKey("_runtimeData"), _runtimeData); |
354 | 0 | DataValue result = new DataValue(data); |
355 | 0 | return result; |
356 | |
} |
357 | |
|
358 | |
private KSItemLabel createItem(String value, String display, boolean hasDetails) { |
359 | 0 | Data itemData = toItemData(value, display); |
360 | 0 | DataValue itemDataValue = new DataValue(itemData); |
361 | 0 | KSItemLabel item = new KSItemLabel(KSSelectedList.this.config.canEdit, hasDetails, |
362 | |
itemDataHelper); |
363 | 0 | item.setValue(itemDataValue); |
364 | 0 | item.addCloseHandler(new CloseHandler<KSItemLabel>() { |
365 | |
@Override |
366 | |
public void onClose(CloseEvent<KSItemLabel> event) { |
367 | 0 | KSItemLabel itemToBeDeleted = event.getTarget(); |
368 | 0 | selectedItems.remove(itemToBeDeleted); |
369 | 0 | removedItems.add(itemToBeDeleted); |
370 | 0 | valuesPanel.remove(itemToBeDeleted); |
371 | 0 | selectionChanged(); |
372 | 0 | } |
373 | |
}); |
374 | 0 | return item; |
375 | |
} |
376 | |
|
377 | |
private Data toItemData(String value, String display) { |
378 | 0 | Data data = new Data(); |
379 | 0 | data.set(VALUE, value); |
380 | 0 | data.set(DISPLAY, display); |
381 | 0 | return data; |
382 | |
} |
383 | |
|
384 | |
@Override |
385 | |
public void setValue(Value value) { |
386 | 0 | clear(); |
387 | 0 | if (value != null) { |
388 | |
try{ |
389 | 0 | if(value instanceof DataValue){ |
390 | 0 | Data data = ((DataValue) value).get(); |
391 | 0 | Iterator<Property> iter = data.realPropertyIterator(); |
392 | 0 | while (iter.hasNext()) { |
393 | 0 | Property p = iter.next(); |
394 | 0 | setSingleValue(p.getValue()); |
395 | 0 | } |
396 | 0 | }else{ |
397 | 0 | setSingleValue(value.get()); |
398 | |
} |
399 | 0 | }catch(ClassCastException cce){ |
400 | 0 | String errorMsg = "ClassCastException in KSSelectedList.java::setValue \n " + |
401 | |
"Trying to cast Value from: " + value.getClass().getName() + "\n " + |
402 | |
"TO DataValue.java. \n Value was of type " + value.getType().getName() + "\n "; |
403 | |
try{ |
404 | 0 | errorMsg += "Value=["+ value.get().toString() +"]"; |
405 | 0 | }catch(Exception ex){ |
406 | 0 | errorMsg += "Error printing error value: " + ex.getMessage(); |
407 | 0 | } |
408 | |
|
409 | 0 | throw new ClassCastException(errorMsg); |
410 | 0 | } |
411 | |
} |
412 | 0 | } |
413 | |
|
414 | |
private void setSingleValue(Object pVal) { |
415 | 0 | String v = null; |
416 | 0 | if(pVal != null){ |
417 | 0 | v = pVal.toString(); |
418 | |
} |
419 | |
|
420 | 0 | KSItemLabel item = createItem(v, "Display: " + v, hasDetails); |
421 | 0 | addItem(item); |
422 | 0 | } |
423 | |
|
424 | |
@Override |
425 | |
public void addFocusLostCallback(Callback<Boolean> callback) { |
426 | 0 | if (picker != null) |
427 | 0 | picker.addFocusLostCallback(callback); |
428 | 0 | } |
429 | |
|
430 | 0 | public static class ItemDataHelper implements DataHelper { |
431 | |
|
432 | |
@Override |
433 | |
public String parse(Data data) { |
434 | |
String result; |
435 | 0 | if (data == null) { |
436 | 0 | return null; |
437 | |
} |
438 | 0 | result = (String) data.get(DISPLAY); |
439 | 0 | return result; |
440 | |
} |
441 | |
|
442 | |
@Override |
443 | |
public String getKey(Data data) { |
444 | |
String result; |
445 | 0 | if (data == null) { |
446 | 0 | return null; |
447 | |
} |
448 | 0 | result = (String) data.get(VALUE); |
449 | 0 | return result; |
450 | |
} |
451 | |
|
452 | |
} |
453 | |
|
454 | |
@Override |
455 | |
public void setValue(String id, String translation) { |
456 | 0 | clear(); |
457 | 0 | if (id != null && translation != null) { |
458 | 0 | addItem(createItem(id, translation, hasDetails), false); |
459 | |
} |
460 | 0 | } |
461 | |
|
462 | |
@Override |
463 | |
public void setValue(Map<String, String> translations) { |
464 | 0 | clear(); |
465 | 0 | if (translations != null) { |
466 | 0 | for (Entry<String, String> e : translations.entrySet()) { |
467 | 0 | addItem(createItem(e.getKey(), e.getValue(), hasDetails), false); |
468 | |
} |
469 | |
} |
470 | 0 | } |
471 | |
|
472 | |
@Override |
473 | |
public Widget getInputWidget() { |
474 | 0 | if (picker == null) { |
475 | 0 | return valuesPanel; |
476 | |
} |
477 | 0 | return picker.getInputWidget(); |
478 | |
} |
479 | |
|
480 | |
@Override |
481 | |
public HashSet<String> getCrossConstraints() { |
482 | 0 | if(picker!=null){ |
483 | 0 | return picker.getCrossConstraints(); |
484 | |
} |
485 | 0 | return new HashSet<String>(); |
486 | |
} |
487 | |
|
488 | |
@Override |
489 | |
public void reprocessWithUpdatedConstraints() { |
490 | 0 | if(picker!=null){ |
491 | 0 | picker.reprocessWithUpdatedConstraints(); |
492 | |
} |
493 | 0 | } |
494 | |
|
495 | |
public KSButton getAddItemButton() { |
496 | 0 | return addItemButton; |
497 | |
} |
498 | |
|
499 | |
public VerticalFlowPanel getMainPanel() { |
500 | 0 | return mainPanel; |
501 | |
} |
502 | |
|
503 | |
public KSPicker getPicker() { |
504 | 0 | return picker; |
505 | |
} |
506 | |
|
507 | |
@Override |
508 | |
public ErrorLevel processValidationResult(ValidationResultInfo vr) { |
509 | |
|
510 | 0 | String indexNumber=vr.getElement().substring(vr.getElement().lastIndexOf('/')+1); |
511 | 0 | int index = Integer.parseInt(indexNumber); |
512 | 0 | if(index<getSelectedItems().size()){ |
513 | 0 | KSItemLabel itemLabel = getSelectedItems().get(index); |
514 | 0 | return itemLabel.processValidationResult(vr, vr.getElement().substring(0, vr.getElement().lastIndexOf('/'))); |
515 | |
}else{ |
516 | 0 | return ErrorLevel.OK; |
517 | |
} |
518 | |
} |
519 | |
|
520 | |
@Override |
521 | |
public boolean shouldProcessValidationResult(ValidationResultInfo vr) { |
522 | |
|
523 | 0 | if(vr.getElement()!=null&&vr.getElement().matches("^\\S+/[0-9]+$")){ |
524 | 0 | return true; |
525 | |
} |
526 | 0 | return false; |
527 | |
} |
528 | |
|
529 | |
} |