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