1 | /** |
2 | * Copyright 2005-2011 Steve McDuff d-duff@users.sourceforge.net |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | package org.deduced.viewer.web.shared; |
17 | |
18 | import java.util.List; |
19 | |
20 | import org.gwt.advanced.client.datamodel.ComboBoxDataModel; |
21 | import org.gwt.advanced.client.ui.widget.ComboBox; |
22 | import org.gwt.advanced.client.ui.widget.combo.ListItemFactory; |
23 | |
24 | import com.google.gwt.event.dom.client.ChangeHandler; |
25 | import com.google.gwt.user.client.rpc.AsyncCallback; |
26 | import com.google.gwt.user.client.ui.Label; |
27 | import com.google.gwt.user.client.ui.UIObject; |
28 | import com.google.gwt.user.client.ui.Widget; |
29 | |
30 | /** |
31 | * ComboBoxModel |
32 | * |
33 | * @author Steve McDuff |
34 | * |
35 | */ |
36 | public class ComboBoxModel extends WidgetModel implements ModelContainer, |
37 | ChangeHandler |
38 | { |
39 | |
40 | /** |
41 | * serialVersionUID |
42 | */ |
43 | private static final long serialVersionUID = 4735427045608818356L; |
44 | |
45 | /** |
46 | * the ordered list of widgets in the combo box |
47 | */ |
48 | private OrderedListModel<OrderedComboBoxItemModel> orderedComboBoxItemList; |
49 | |
50 | /** |
51 | * the selected ComboBoxItemModel ID in the combo box |
52 | */ |
53 | private String selection; |
54 | |
55 | /** |
56 | * the number of visible rows. If left null, the default value of the combo |
57 | * box widget will be used. |
58 | */ |
59 | private Integer visibleRowCount; |
60 | |
61 | /** |
62 | * is the combo box enabled |
63 | */ |
64 | private boolean enabled; |
65 | |
66 | /** |
67 | * ComboBoxModel constructor |
68 | */ |
69 | public ComboBoxModel() |
70 | { |
71 | } |
72 | |
73 | /** |
74 | * set Ordered Component List |
75 | * |
76 | * @param newWidgetList the new widget list to use |
77 | */ |
78 | public void setOrderedComboBoxItemList( |
79 | OrderedListModel<OrderedComboBoxItemModel> newWidgetList) |
80 | { |
81 | orderedComboBoxItemList = newWidgetList; |
82 | } |
83 | |
84 | /** |
85 | * update Ordered Component List |
86 | * |
87 | * @param newWidgetList the new widget list to use |
88 | */ |
89 | public void updateOrderedComboBoxItemList( |
90 | OrderedListModel<OrderedComboBoxItemModel> newWidgetList) |
91 | { |
92 | setOrderedComboBoxItemList(updateModel(newWidgetList, |
93 | orderedComboBoxItemList, this)); |
94 | synchronizeWithUI(); |
95 | } |
96 | |
97 | /** |
98 | * (non-JSDoc) |
99 | * |
100 | * @see org.deduced.viewer.web.shared.WidgetModel#createUIObject() |
101 | */ |
102 | @Override |
103 | public UIObject createUIObject() |
104 | { |
105 | ComboBoxDataModel dataModel = new ComboBoxDataModel(); |
106 | ComboBox<ComboBoxDataModel> comboBox = |
107 | new ComboBox<ComboBoxDataModel>(); |
108 | comboBox.setListItemFactory(new ListItemFactory() |
109 | { |
110 | |
111 | @Override |
112 | public Widget createWidget( |
113 | Object value) |
114 | { |
115 | ComboBoxItemModel model = (ComboBoxItemModel) value; |
116 | Widget widget = getWidgetFromComboBoxItemModel(model); |
117 | if (widget == null) |
118 | { |
119 | widget = new Label(); |
120 | } |
121 | return widget; |
122 | } |
123 | |
124 | @Override |
125 | public String convert( |
126 | Object value) |
127 | { |
128 | String returnValue = null; |
129 | ComboBoxItemModel model = (ComboBoxItemModel) value; |
130 | |
131 | if (model != null) |
132 | { |
133 | returnValue = model.getText(); |
134 | } |
135 | |
136 | if (returnValue == null) |
137 | { |
138 | returnValue = ""; |
139 | } |
140 | return returnValue; |
141 | } |
142 | }); |
143 | |
144 | comboBox.setModel(dataModel); |
145 | |
146 | comboBox.addChangeHandler(this); |
147 | return comboBox; |
148 | } |
149 | |
150 | /** |
151 | * get Combo Box |
152 | * |
153 | * @return the combo box |
154 | */ |
155 | public ComboBox getComboBox() |
156 | { |
157 | return (ComboBox) getUIObject(); |
158 | } |
159 | |
160 | /** |
161 | * (non-JSDoc) |
162 | * |
163 | * @see org.deduced.viewer.web.shared.Model#registerChildModels() |
164 | */ |
165 | @Override |
166 | protected void registerChildModels() |
167 | { |
168 | registerChildModel(getOrderedComboBoxItemList(), this); |
169 | super.registerChildModels(); |
170 | } |
171 | |
172 | /** |
173 | * (non-JSDoc) |
174 | * |
175 | * @see org.deduced.viewer.web.shared.Model#deleteChildModels() |
176 | */ |
177 | @Override |
178 | protected void deleteChildModels() |
179 | { |
180 | deleteWidgetList(); |
181 | super.deleteChildModels(); |
182 | } |
183 | |
184 | /** |
185 | * delete the Widget List |
186 | */ |
187 | private void deleteWidgetList() |
188 | { |
189 | if (getOrderedComboBoxItemList() != null) |
190 | { |
191 | getOrderedComboBoxItemList().delete(); |
192 | setOrderedComboBoxItemList(null); |
193 | } |
194 | } |
195 | |
196 | /** |
197 | * (non-JSDoc) |
198 | * |
199 | * @see org.deduced.viewer.web.shared.UserInterfaceModel#internalSynchronizeWithUI() |
200 | */ |
201 | @Override |
202 | public void internalSynchronizeWithUI() |
203 | { |
204 | ComboBox comboBox = getComboBox(); |
205 | |
206 | if (getVisibleRowCount() != null) |
207 | { |
208 | comboBox.setVisibleRows(getVisibleRowCount().intValue()); |
209 | } |
210 | |
211 | synchronizeChildWidgetList(); |
212 | |
213 | String expectedSelection = getSelection(); |
214 | String currentSelectedId = comboBox.getSelectedId(); |
215 | String currentText = comboBox.getText(); |
216 | String expectedText = ""; |
217 | if (expectedSelection != null) |
218 | { |
219 | Model selectedModel = |
220 | getModelRegistry().getModel(expectedSelection); |
221 | if (selectedModel instanceof ComboBoxItemModel) |
222 | { |
223 | expectedText = ((ComboBoxItemModel) selectedModel).getText(); |
224 | } |
225 | } |
226 | |
227 | if (!Utilities.equals(expectedSelection, currentSelectedId) |
228 | || !Utilities.equals(expectedText, currentText)) |
229 | { |
230 | comboBox.setSelectedId(expectedSelection); |
231 | } |
232 | |
233 | comboBox.setEnabled(isEnabled()); |
234 | |
235 | super.internalSynchronizeWithUI(); |
236 | } |
237 | |
238 | /** |
239 | * synchronize Child Widget List |
240 | */ |
241 | private void synchronizeChildWidgetList() |
242 | { |
243 | if (!isUISynchronized()) |
244 | { |
245 | rebuildPanel(); |
246 | } |
247 | } |
248 | |
249 | /** |
250 | * check if the UI is synchronized with the model |
251 | * |
252 | * @return true if the UI is synchronized |
253 | */ |
254 | private boolean isUISynchronized() |
255 | { |
256 | boolean isUISynchronized = true; |
257 | |
258 | OrderedListModel<OrderedComboBoxItemModel> currentOrderedComboBoxItemList = |
259 | getOrderedComboBoxItemList(); |
260 | |
261 | ComboBoxDataModel dataModel = getDataModel(); |
262 | int widgetCount = dataModel.getCount(); |
263 | |
264 | if (currentOrderedComboBoxItemList == null) |
265 | { |
266 | isUISynchronized = widgetCount == 0; |
267 | } |
268 | else |
269 | { |
270 | List<OrderedComboBoxItemModel> widgetContainerList = |
271 | currentOrderedComboBoxItemList.getList(); |
272 | |
273 | int expectedWidgetCount = widgetContainerList.size(); |
274 | |
275 | if (widgetCount == expectedWidgetCount) |
276 | { |
277 | for (int i = 0; i < widgetCount; i++) |
278 | { |
279 | OrderedComboBoxItemModel orderedComboBoxItemModel = |
280 | widgetContainerList.get(i); |
281 | ComboBoxItemModel expectedModel = |
282 | orderedComboBoxItemModel.getComboBoxItem(); |
283 | |
284 | ComboBoxItemModel currentModel = |
285 | (ComboBoxItemModel) dataModel.get(i); |
286 | |
287 | if (expectedModel != null) |
288 | { |
289 | if (currentModel != expectedModel) |
290 | { |
291 | isUISynchronized = false; |
292 | break; |
293 | } |
294 | } |
295 | } |
296 | } |
297 | else |
298 | { |
299 | isUISynchronized = false; |
300 | } |
301 | } |
302 | return isUISynchronized; |
303 | } |
304 | |
305 | /** |
306 | * rebuild the widget list content |
307 | */ |
308 | private void rebuildPanel() |
309 | { |
310 | ComboBoxDataModel currentDataModel = getDataModel(); |
311 | currentDataModel.clear(); |
312 | |
313 | List<OrderedComboBoxItemModel> widgetContainerList = |
314 | getOrderedComboBoxItemList().getList(); |
315 | |
316 | for (OrderedComboBoxItemModel orderedComboBoxItemModel : widgetContainerList) |
317 | { |
318 | ComboBoxItemModel comboBoxItem = |
319 | orderedComboBoxItemModel.getComboBoxItem(); |
320 | String id = null; |
321 | if (comboBoxItem == null) |
322 | { |
323 | id = orderedComboBoxItemModel.getId(); |
324 | } |
325 | else |
326 | { |
327 | id = comboBoxItem.getId(); |
328 | } |
329 | currentDataModel.add(id, comboBoxItem); |
330 | } |
331 | } |
332 | |
333 | /** |
334 | * (non-JSDoc) |
335 | * |
336 | * @see org.deduced.viewer.web.shared.UserInterfaceModel#propertyChanged(org.deduced.viewer.web.shared.ChangeEvent) |
337 | */ |
338 | @SuppressWarnings("unchecked") |
339 | @Override |
340 | public void propertyChanged( |
341 | ChangeEvent event) |
342 | { |
343 | if (isEventNameEqual(event, "visible row count")) |
344 | { |
345 | setVisibleRowCount((Integer) event.getSerializableValue()); |
346 | synchronizeWithUI(); |
347 | } |
348 | else if (isEventNameEqual(event, "selection")) |
349 | { |
350 | setSelection((String) event.getSerializableValue()); |
351 | synchronizeWithUI(); |
352 | } |
353 | else if (isEventNameEqual(event, "ordered combo box item list")) |
354 | { |
355 | OrderedListModel<OrderedComboBoxItemModel> newList = |
356 | (OrderedListModel<OrderedComboBoxItemModel>) event |
357 | .getSerializableValue(); |
358 | updateOrderedComboBoxItemList(newList); |
359 | |
360 | } |
361 | else if (Utilities.equals(event.getName(), "enabled")) |
362 | { |
363 | setEnabled(((Boolean) event.getSerializableValue()).booleanValue()); |
364 | synchronizeWithUI(); |
365 | } |
366 | else |
367 | { |
368 | super.propertyChanged(event); |
369 | } |
370 | } |
371 | |
372 | /** |
373 | * @return the dataModel |
374 | */ |
375 | public ComboBoxDataModel getDataModel() |
376 | { |
377 | return (ComboBoxDataModel) getComboBox().getModel(); |
378 | } |
379 | |
380 | /** |
381 | * @param setEnabled the enabled to set |
382 | */ |
383 | public void setEnabled( |
384 | boolean setEnabled) |
385 | { |
386 | enabled = setEnabled; |
387 | } |
388 | |
389 | /** |
390 | * @return the enabled |
391 | */ |
392 | public boolean isEnabled() |
393 | { |
394 | return enabled; |
395 | } |
396 | |
397 | /** |
398 | * @param setVisibleRows the visibleRowCount to set |
399 | */ |
400 | public void setVisibleRowCount( |
401 | Integer setVisibleRows) |
402 | { |
403 | visibleRowCount = setVisibleRows; |
404 | } |
405 | |
406 | /** |
407 | * @return the visibleRowCount |
408 | */ |
409 | public Integer getVisibleRowCount() |
410 | { |
411 | return visibleRowCount; |
412 | } |
413 | |
414 | /** |
415 | * @return the orderedComboBoxItemList |
416 | */ |
417 | public OrderedListModel<OrderedComboBoxItemModel> getOrderedComboBoxItemList() |
418 | { |
419 | return orderedComboBoxItemList; |
420 | } |
421 | |
422 | /** |
423 | * @param setSelectedUserInterfaceElement the selection to set |
424 | */ |
425 | public void setSelection( |
426 | String setSelectedUserInterfaceElement) |
427 | { |
428 | selection = setSelectedUserInterfaceElement; |
429 | } |
430 | |
431 | /** |
432 | * @return the selection |
433 | */ |
434 | public String getSelection() |
435 | { |
436 | return selection; |
437 | } |
438 | |
439 | /** |
440 | * (non-JSDoc) |
441 | * |
442 | * @see org.deduced.viewer.web.shared.ModelContainer#modelChanged(org.deduced.viewer.web.shared.Model) |
443 | */ |
444 | @Override |
445 | public void modelChanged( |
446 | Model model) |
447 | { |
448 | synchronizeWithUI(); |
449 | } |
450 | |
451 | /** |
452 | * (non-JSDoc) |
453 | * |
454 | * @see com.google.gwt.event.dom.client.ChangeHandler#onChange(com.google.gwt.event.dom.client.ChangeEvent) |
455 | */ |
456 | @Override |
457 | public void onChange( |
458 | com.google.gwt.event.dom.client.ChangeEvent event) |
459 | { |
460 | String selectedId = getComboBox().getSelectedId(); |
461 | userSelectionChanged(selectedId); |
462 | } |
463 | |
464 | /** |
465 | * event called when the user Selection Changed |
466 | * |
467 | * @param selectedId the new selected ID |
468 | */ |
469 | public void userSelectionChanged( |
470 | String selectedId) |
471 | { |
472 | if (!Utilities.equals(selectedId, getSelection())) |
473 | { |
474 | setSelection(selectedId); |
475 | ModelRegistry modelRegistry = getModelRegistry(); |
476 | AsyncCallback<Void> defaultVoidCallback = |
477 | modelRegistry.getDefaultVoidCallback(); |
478 | modelRegistry.updateProperty(getId(), "selection", selectedId, |
479 | defaultVoidCallback); |
480 | } |
481 | } |
482 | |
483 | /** |
484 | * get Widget From Combo Box Item Model |
485 | * |
486 | * @param model the model from which to fetch the widget |
487 | * @return the widget associated with it. Null if it can't be found. |
488 | */ |
489 | public static Widget getWidgetFromComboBoxItemModel( |
490 | ComboBoxItemModel model) |
491 | { |
492 | Widget widget = null; |
493 | if (model != null) |
494 | { |
495 | WidgetModel component = model.getComponent(); |
496 | if (component != null) |
497 | { |
498 | widget = component.getWidget(); |
499 | } |
500 | } |
501 | return widget; |
502 | } |
503 | |
504 | } |