1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.lum.common.client.lo; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.Arrays; |
20 | |
import java.util.HashMap; |
21 | |
import java.util.List; |
22 | |
import java.util.Map; |
23 | |
|
24 | |
import org.kuali.student.common.assembly.data.Data; |
25 | |
import org.kuali.student.common.assembly.data.Data.DataValue; |
26 | |
import org.kuali.student.common.ui.client.application.Application; |
27 | |
import org.kuali.student.common.ui.client.application.KSAsyncCallback; |
28 | |
import org.kuali.student.common.ui.client.configurable.mvc.SectionTitle; |
29 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
30 | |
import org.kuali.student.common.ui.client.service.DataSaveResult; |
31 | |
import org.kuali.student.common.ui.client.util.UtilConstants; |
32 | |
import org.kuali.student.common.ui.client.widgets.DataHelper; |
33 | |
import org.kuali.student.common.ui.client.widgets.KSButton; |
34 | |
import org.kuali.student.common.ui.client.widgets.KSDropDown; |
35 | |
import org.kuali.student.common.ui.client.widgets.KSItemLabel; |
36 | |
import org.kuali.student.common.ui.client.widgets.KSLabel; |
37 | |
import org.kuali.student.common.ui.client.widgets.KSLightBox; |
38 | |
import org.kuali.student.common.ui.client.widgets.KSTextBox; |
39 | |
import org.kuali.student.common.ui.client.widgets.KSButtonAbstract.ButtonStyle; |
40 | |
import org.kuali.student.common.ui.client.widgets.buttongroups.ButtonGroup; |
41 | |
import org.kuali.student.common.ui.client.widgets.buttongroups.ButtonEnumerations.ButtonEnum; |
42 | |
import org.kuali.student.common.ui.client.widgets.buttonlayout.ButtonRow; |
43 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.AbbrButton; |
44 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.LabelPanel; |
45 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.AbbrButton.AbbrButtonType; |
46 | |
import org.kuali.student.common.ui.client.widgets.field.layout.layouts.FieldLayoutComponent; |
47 | |
import org.kuali.student.common.ui.client.widgets.focus.FocusGroup; |
48 | |
import org.kuali.student.common.ui.client.widgets.list.ListItems; |
49 | |
import org.kuali.student.common.ui.client.widgets.list.SelectionChangeHandler; |
50 | |
import org.kuali.student.common.ui.client.widgets.notification.KSNotification; |
51 | |
import org.kuali.student.common.ui.client.widgets.notification.KSNotifier; |
52 | |
import org.kuali.student.common.ui.client.widgets.progress.BlockingTask; |
53 | |
import org.kuali.student.common.ui.client.widgets.progress.KSBlockingProgressIndicator; |
54 | |
import org.kuali.student.common.ui.client.widgets.suggestbox.KSSuggestBox; |
55 | |
import org.kuali.student.common.ui.client.widgets.suggestbox.SearchSuggestOracle; |
56 | |
import org.kuali.student.common.ui.client.widgets.suggestbox.SuggestPicker; |
57 | |
import org.kuali.student.lum.common.client.lo.rpc.LoCategoryRpcService; |
58 | |
import org.kuali.student.lum.common.client.lo.rpc.LoCategoryRpcServiceAsync; |
59 | |
import org.kuali.student.lum.common.client.lu.LUUIConstants; |
60 | |
import org.kuali.student.lum.lo.dto.LoCategoryInfo; |
61 | |
import org.kuali.student.lum.lo.dto.LoCategoryTypeInfo; |
62 | |
|
63 | |
import com.google.gwt.core.client.GWT; |
64 | |
import com.google.gwt.event.dom.client.BlurHandler; |
65 | |
import com.google.gwt.event.dom.client.ClickEvent; |
66 | |
import com.google.gwt.event.dom.client.ClickHandler; |
67 | |
import com.google.gwt.event.dom.client.FocusHandler; |
68 | |
import com.google.gwt.event.logical.shared.CloseEvent; |
69 | |
import com.google.gwt.event.logical.shared.CloseHandler; |
70 | |
import com.google.gwt.event.logical.shared.ValueChangeEvent; |
71 | |
import com.google.gwt.event.logical.shared.ValueChangeHandler; |
72 | |
import com.google.gwt.event.shared.GwtEvent; |
73 | |
import com.google.gwt.event.shared.HandlerRegistration; |
74 | |
import com.google.gwt.user.client.Window; |
75 | |
import com.google.gwt.user.client.ui.Anchor; |
76 | |
import com.google.gwt.user.client.ui.Composite; |
77 | |
import com.google.gwt.user.client.ui.FlexTable; |
78 | |
import com.google.gwt.user.client.ui.FlowPanel; |
79 | |
import com.google.gwt.user.client.ui.HTMLPanel; |
80 | |
import com.google.gwt.user.client.ui.HasValue; |
81 | |
import com.google.gwt.user.client.ui.HorizontalPanel; |
82 | |
import com.google.gwt.user.client.ui.VerticalPanel; |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | 0 | public class LOCategoryBuilder extends Composite implements HasValue<List<LoCategoryInfo>> { |
92 | |
|
93 | |
private String type; |
94 | |
private String state; |
95 | |
private String repoKey; |
96 | |
private String messageGroup; |
97 | |
|
98 | |
private LoCategoryRpcServiceAsync loCatRpcServiceAsync; |
99 | |
private LOCategoryPicker picker; |
100 | 0 | private AbbrButton help = new AbbrButton(AbbrButtonType.HELP); |
101 | |
|
102 | |
LOCategoryListNew categoryList; |
103 | |
Map<String, LoCategoryTypeInfo> categoryTypeMap; |
104 | |
|
105 | 0 | VerticalPanel root = new VerticalPanel(); |
106 | |
|
107 | 0 | private KSButton addButton = new KSButton("Add", ButtonStyle.SECONDARY); |
108 | |
|
109 | |
private KSLightBox createCategoryWindow; |
110 | 0 | Anchor browseCategoryLink = new Anchor("Browse for categories"); |
111 | 0 | private final BlockingTask saving = new BlockingTask("Saving"); |
112 | |
|
113 | |
public LOCategoryBuilder(String messageGroup, String type, String state, String loRepoKey) { |
114 | 0 | super(); |
115 | |
|
116 | 0 | this.type = type; |
117 | 0 | this.state = state; |
118 | 0 | this.repoKey = loRepoKey; |
119 | 0 | this.messageGroup = messageGroup; |
120 | |
|
121 | 0 | loCatRpcServiceAsync = GWT.create(LoCategoryRpcService.class); |
122 | 0 | picker = new LOCategoryPicker(); |
123 | 0 | categoryList = new LOCategoryListNew(); |
124 | |
|
125 | 0 | initWidget(root); |
126 | |
|
127 | 0 | final FlowPanel selectedPanel = new FlowPanel(); |
128 | 0 | selectedPanel.setStyleName("KS-LOSelectedCategories"); |
129 | 0 | addButton.addClickHandler(new ClickHandler() { |
130 | |
@Override |
131 | |
public void onClick(ClickEvent event) { |
132 | 0 | addEnteredCategory(); |
133 | 0 | } |
134 | |
}); |
135 | 0 | browseCategoryLink.addClickHandler(new ClickHandler() { |
136 | |
@Override |
137 | |
public void onClick(ClickEvent event) { |
138 | |
|
139 | 0 | List<LoCategoryInfo> categoriesInPicker = categoryList.getValue(); |
140 | 0 | final CategoryManagement categoryManagement = new CategoryManagement(true, true, categoriesInPicker); |
141 | 0 | categoryManagement.setDeleteButtonEnabled(false); |
142 | 0 | categoryManagement.setInsertButtonEnabled(false); |
143 | 0 | categoryManagement.setUpdateButtonEnabled(false); |
144 | |
|
145 | 0 | final KSLightBox pop = new KSLightBox(); |
146 | 0 | pop.setSize(750, 600); |
147 | 0 | KSButton addButton = new KSButton("Add"); |
148 | 0 | KSButton cancelButton = new KSButton("Cancel", ButtonStyle.ANCHOR_LARGE_CENTERED); |
149 | |
|
150 | 0 | pop.addButton(addButton); |
151 | 0 | pop.addButton(cancelButton); |
152 | |
|
153 | 0 | FlowPanel mainPanel = new FlowPanel(); |
154 | 0 | mainPanel.add(SectionTitle.generateH2Title("Select Categories")); |
155 | 0 | mainPanel.add(categoryManagement); |
156 | |
|
157 | 0 | addButton.addClickHandler(new ClickHandler() { |
158 | |
@Override |
159 | |
public void onClick(ClickEvent event) { |
160 | 0 | List<LoCategoryInfo> list = categoryManagement.getSelectedCategoryList(); |
161 | 0 | for (LoCategoryInfo info : list) { |
162 | 0 | addCategory(info); |
163 | |
} |
164 | 0 | pop.hide(); |
165 | 0 | } |
166 | |
}); |
167 | |
|
168 | 0 | cancelButton.addClickHandler(new ClickHandler() { |
169 | |
@Override |
170 | |
public void onClick(ClickEvent event) { |
171 | 0 | pop.hide(); |
172 | 0 | } |
173 | |
}); |
174 | |
|
175 | |
|
176 | 0 | pop.setWidget(mainPanel); |
177 | 0 | pop.show(); |
178 | |
|
179 | |
|
180 | 0 | } |
181 | |
}); |
182 | |
|
183 | 0 | VerticalPanel main = new VerticalPanel(); |
184 | 0 | HorizontalPanel suggestPanel = new HorizontalPanel(); |
185 | 0 | suggestPanel.add(picker); |
186 | 0 | suggestPanel.add(addButton); |
187 | |
|
188 | 0 | VerticalPanel suggestAndBrowsePanel = new VerticalPanel(); |
189 | 0 | suggestAndBrowsePanel.add(suggestPanel); |
190 | 0 | suggestAndBrowsePanel.add(browseCategoryLink); |
191 | |
|
192 | |
|
193 | 0 | selectedPanel.add(categoryList); |
194 | |
|
195 | 0 | String fieldHTMLId = HTMLPanel.createUniqueId(); |
196 | 0 | String title = getLabelText(LUUIConstants.LO_CATEGORY_KEY); |
197 | 0 | String helpText = getLabelText(LUUIConstants.LO_CATEGORY_KEY + FieldLayoutComponent.HELP_MESSAGE_KEY); |
198 | 0 | LabelPanel fieldTitle = new LabelPanel(title, fieldHTMLId); |
199 | |
|
200 | 0 | if (helpText != null) { |
201 | 0 | setHelp(helpText); |
202 | |
} else { |
203 | 0 | help.setVisible(false); |
204 | |
} |
205 | |
|
206 | 0 | fieldTitle.add(help); |
207 | 0 | main.add(fieldTitle); |
208 | 0 | main.add(suggestAndBrowsePanel); |
209 | 0 | main.add(selectedPanel); |
210 | 0 | root.add(main); |
211 | |
|
212 | 0 | main.addStyleName("KS-LOCategoryPicker"); |
213 | 0 | addButton.addStyleName("KS-LOCategoryPicker-Button"); |
214 | |
|
215 | 0 | } |
216 | |
|
217 | |
public void setHelp(final String html) { |
218 | 0 | if (html != null && !html.trim().equals("")) { |
219 | 0 | help.setVisible(true); |
220 | 0 | help.setHoverHTML(html); |
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
} else { |
231 | 0 | help.setVisible(false); |
232 | |
} |
233 | 0 | } |
234 | |
|
235 | |
private void addEnteredCategory() { |
236 | |
|
237 | 0 | if (categoryList == null) |
238 | 0 | categoryList = new LOCategoryListNew(); |
239 | |
|
240 | 0 | String selectedId = picker.getSelectedId(); |
241 | 0 | if (selectedId.trim().equals("") || selectedId.equals(UtilConstants.IMPOSSIBLE_CHARACTERS)) { |
242 | 0 | showNewCategoryWindow(); |
243 | |
} else { |
244 | 0 | loCatRpcServiceAsync.getData(picker.getSelectedId(), new KSAsyncCallback<Data>() { |
245 | |
|
246 | |
@Override |
247 | |
public void handleFailure(Throwable caught) { |
248 | 0 | Window.alert("getLoCategory failed " + caught.getMessage()); |
249 | 0 | } |
250 | |
|
251 | |
@Override |
252 | |
public void onSuccess(Data result) { |
253 | 0 | final LoCategoryInfo newCategory = CategoryDataUtil.toLoCategoryInfo(result); |
254 | 0 | addCategory(newCategory); |
255 | |
|
256 | 0 | } |
257 | |
}); |
258 | |
} |
259 | 0 | } |
260 | |
|
261 | |
private void showNewCategoryWindow() { |
262 | |
|
263 | 0 | final VerticalPanel main = new VerticalPanel(); |
264 | 0 | final KSDropDown typesDropDown = new KSDropDown(); |
265 | 0 | createCategoryWindow = new KSLightBox(); |
266 | |
|
267 | 0 | FlexTable layoutTable = new FlexTable(); |
268 | 0 | final KSTextBox nameTextBox = new KSTextBox(); |
269 | 0 | nameTextBox.setText(picker.getText()); |
270 | |
|
271 | 0 | layoutTable.setWidget(0, 0, new KSLabel("Category")); |
272 | 0 | layoutTable.setWidget(0, 1, new KSLabel("Type")); |
273 | 0 | layoutTable.setWidget(1, 0, nameTextBox); |
274 | 0 | layoutTable.setWidget(1, 1, typesDropDown); |
275 | |
|
276 | 0 | SectionTitle sectionTitle = SectionTitle.generateH2Title("Create New Category"); |
277 | |
|
278 | 0 | main.add(sectionTitle); |
279 | 0 | main.add(layoutTable); |
280 | |
|
281 | 0 | loCatRpcServiceAsync.getLoCategoryTypes(new KSAsyncCallback<List<LoCategoryTypeInfo>>() { |
282 | |
|
283 | |
@Override |
284 | |
public void handleFailure(Throwable caught) { |
285 | 0 | Window.alert("getLoCategoryTypes failed " + caught.getMessage()); |
286 | 0 | } |
287 | |
|
288 | |
@Override |
289 | |
public void onSuccess(List<LoCategoryTypeInfo> result) { |
290 | 0 | final LOCategoryTypeInfoList list = new LOCategoryTypeInfoList(result); |
291 | 0 | typesDropDown.setListItems(list); |
292 | 0 | if (categoryTypeMap == null) { |
293 | 0 | loadCategoryTypes(result); |
294 | |
} |
295 | |
|
296 | 0 | CreateLoCancelGroup buttonPanel = new CreateLoCancelGroup(new Callback<LoCancelEnum>() { |
297 | |
|
298 | |
@Override |
299 | |
public void exec(LoCancelEnum result) { |
300 | 0 | switch (result) { |
301 | |
case CREATE: |
302 | |
|
303 | 0 | LoCategoryInfoHelper catHelper = new LoCategoryInfoHelper(new Data()); |
304 | 0 | catHelper.setName(nameTextBox.getText()); |
305 | 0 | catHelper.setState("active"); |
306 | 0 | catHelper.setLoRepository(repoKey); |
307 | 0 | catHelper.setType(typesDropDown.getSelectedItem()); |
308 | |
|
309 | 0 | loCatRpcServiceAsync.saveData(catHelper.getData(), new KSAsyncCallback<DataSaveResult>() { |
310 | |
@Override |
311 | |
public void handleFailure(Throwable caught) { |
312 | 0 | Window.alert("Create LO Category failed: " + caught.getMessage()); |
313 | 0 | } |
314 | |
|
315 | |
@Override |
316 | |
public void onSuccess(DataSaveResult result) { |
317 | 0 | KSBlockingProgressIndicator.removeTask(saving); |
318 | |
|
319 | 0 | if (result.getValidationResults() != null && !result.getValidationResults().isEmpty()) { |
320 | 0 | Window.alert("Create LO Category failed: " + result.getValidationResults().get(0).getMessage()); |
321 | |
} else { |
322 | |
|
323 | 0 | final LoCategoryInfo newCategory = CategoryDataUtil.toLoCategoryInfo(result.getValue()); |
324 | 0 | addCategory(newCategory); |
325 | 0 | createCategoryWindow.hide(); |
326 | 0 | KSNotifier.add(new KSNotification("Create LO Category Successful", false, 3000)); |
327 | |
} |
328 | 0 | } |
329 | |
}); |
330 | 0 | break; |
331 | |
case CANCEL: |
332 | 0 | createCategoryWindow.hide(); |
333 | |
break; |
334 | |
} |
335 | 0 | } |
336 | |
}); |
337 | |
|
338 | 0 | main.add(buttonPanel); |
339 | 0 | main.setPixelSize(300, 300); |
340 | 0 | createCategoryWindow.setWidget(main); |
341 | 0 | createCategoryWindow.show(); |
342 | 0 | } |
343 | |
|
344 | |
|
345 | |
}); |
346 | 0 | } |
347 | |
|
348 | |
private void loadCategoryTypes(List<LoCategoryTypeInfo> categoryTypes) { |
349 | 0 | if (categoryTypeMap == null) { |
350 | 0 | categoryTypeMap = new HashMap<String, LoCategoryTypeInfo>(); |
351 | |
} |
352 | 0 | if (categoryTypes != null) { |
353 | 0 | for (LoCategoryTypeInfo i : categoryTypes) { |
354 | 0 | categoryTypeMap.put(i.getId(), i); |
355 | |
} |
356 | |
} |
357 | 0 | } |
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
|
367 | |
private boolean isCategoryAlreadyAddedToPicker(LoCategoryInfo categoryToCheck){ |
368 | |
|
369 | 0 | List<LoCategoryInfo> categoriesInPicker = categoryList.getValue(); |
370 | 0 | if (categoriesInPicker != null && categoryToCheck != null){ |
371 | 0 | for (LoCategoryInfo pickerCategory : categoriesInPicker) { |
372 | 0 | boolean namesMatch = pickerCategory.getName().equalsIgnoreCase(categoryToCheck.getName()); |
373 | 0 | boolean typesMatch = pickerCategory.getType().equalsIgnoreCase(categoryToCheck.getType()); |
374 | 0 | if (namesMatch && typesMatch){ |
375 | 0 | return true; |
376 | |
} |
377 | 0 | } |
378 | |
} |
379 | 0 | return false; |
380 | |
} |
381 | |
private void addCategory(final LoCategoryInfo category) { |
382 | 0 | if (categoryTypeMap == null) { |
383 | 0 | categoryTypeMap = new HashMap<String, LoCategoryTypeInfo>(); |
384 | |
} |
385 | |
|
386 | 0 | if (categoryTypeMap.containsKey(category.getType())) { |
387 | |
|
388 | 0 | if (!isCategoryAlreadyAddedToPicker(category)){ |
389 | 0 | categoryList.addItem(category); |
390 | |
} |
391 | 0 | picker.reset(); |
392 | |
} else { |
393 | 0 | loCatRpcServiceAsync.getLoCategoryType(category.getType(), new KSAsyncCallback<LoCategoryTypeInfo>() { |
394 | |
|
395 | |
@Override |
396 | |
public void handleFailure(Throwable caught) { |
397 | 0 | Window.alert("getLoCategoryType failed " + caught.getMessage()); |
398 | 0 | } |
399 | |
|
400 | |
@Override |
401 | |
public void onSuccess(LoCategoryTypeInfo result) { |
402 | 0 | categoryTypeMap.put(result.getId(), result); |
403 | |
|
404 | 0 | if (!isCategoryAlreadyAddedToPicker(category)){ |
405 | 0 | categoryList.addItem(category); |
406 | |
} |
407 | 0 | picker.reset(); |
408 | |
|
409 | 0 | } |
410 | |
|
411 | |
}); |
412 | |
} |
413 | |
|
414 | 0 | } |
415 | |
|
416 | |
private String getLabelText(String labelKey) { |
417 | 0 | return Application.getApplicationContext().getUILabel(messageGroup, type, state, labelKey); |
418 | |
} |
419 | |
|
420 | |
@Override |
421 | |
public void setValue(List<LoCategoryInfo> categories) { |
422 | 0 | categoryList.setValue(categories); |
423 | |
|
424 | 0 | } |
425 | |
|
426 | |
@Override |
427 | |
public void setValue(List<LoCategoryInfo> value, boolean fireEvents) { |
428 | 0 | setValue(value); |
429 | 0 | } |
430 | |
|
431 | |
|
432 | |
|
433 | |
|
434 | |
@Override |
435 | |
public List<LoCategoryInfo> getValue() { |
436 | 0 | return categoryList.getValue(); |
437 | |
} |
438 | |
|
439 | |
|
440 | |
|
441 | |
|
442 | |
@Override |
443 | |
public HandlerRegistration addValueChangeHandler(ValueChangeHandler<List<LoCategoryInfo>> handler) { |
444 | 0 | return categoryList.addValueChangeHandler(handler); |
445 | |
} |
446 | |
|
447 | |
|
448 | |
|
449 | |
|
450 | |
|
451 | |
|
452 | 0 | private class LOCategoryPicker extends Composite implements SuggestPicker { |
453 | |
|
454 | |
|
455 | |
|
456 | 0 | final SearchSuggestOracle loSearchOracle = new SearchSuggestOracle( |
457 | |
"lo.search.loCategories", |
458 | |
"lo.queryParam.loOptionalCategoryName", |
459 | |
"lo.queryParam.loCategoryId", |
460 | |
"lo.resultColumn.categoryId", |
461 | |
"lo.resultColumn.categoryNameAndType"); |
462 | |
|
463 | 0 | final KSSuggestBox suggestBox = new KSSuggestBox(loSearchOracle); |
464 | |
|
465 | 0 | private final FocusGroup focus = new FocusGroup(this); |
466 | |
|
467 | 0 | private VerticalPanel main = new VerticalPanel(); |
468 | |
|
469 | 0 | protected LOCategoryPicker() { |
470 | 0 | super(); |
471 | 0 | init(); |
472 | 0 | } |
473 | |
|
474 | |
public String getSelectedId() { |
475 | 0 | return suggestBox.getSelectedId(); |
476 | |
} |
477 | |
|
478 | |
private void init() { |
479 | 0 | focus.addWidget(suggestBox); |
480 | 0 | loSearchOracle.setTextWidget(suggestBox.getTextBox()); |
481 | 0 | main.add(suggestBox); |
482 | 0 | initWidget(main); |
483 | 0 | } |
484 | |
|
485 | |
@Override |
486 | |
public String getValue() { |
487 | 0 | return suggestBox.getSelectedId(); |
488 | |
} |
489 | |
|
490 | |
@Override |
491 | |
public void setValue(String value) { |
492 | 0 | setValue(value, true); |
493 | 0 | } |
494 | |
|
495 | |
@Override |
496 | |
public void setValue(String value, boolean fireEvents) { |
497 | 0 | suggestBox.reset(); |
498 | 0 | suggestBox.setValue(value, fireEvents); |
499 | 0 | } |
500 | |
|
501 | |
|
502 | |
@Override |
503 | |
public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) { |
504 | 0 | return suggestBox.addValueChangeHandler(handler); |
505 | |
} |
506 | |
|
507 | |
@Override |
508 | |
public void fireEvent(GwtEvent<?> event) { |
509 | 0 | super.fireEvent(event); |
510 | 0 | } |
511 | |
|
512 | |
public void reset() { |
513 | 0 | suggestBox.reset(); |
514 | 0 | } |
515 | |
|
516 | |
@Override |
517 | |
public HandlerRegistration addFocusHandler(FocusHandler handler) { |
518 | 0 | return focus.addFocusHandler(handler); |
519 | |
} |
520 | |
|
521 | |
@Override |
522 | |
public HandlerRegistration addBlurHandler(BlurHandler handler) { |
523 | 0 | return focus.addBlurHandler(handler); |
524 | |
} |
525 | |
|
526 | |
public String getText() { |
527 | 0 | return suggestBox.getText(); |
528 | |
} |
529 | |
|
530 | |
@Override |
531 | |
public HandlerRegistration addSelectionChangeHandler(SelectionChangeHandler handler) { |
532 | 0 | return suggestBox.addSelectionChangeHandler(handler); |
533 | |
} |
534 | |
} |
535 | |
|
536 | |
private class LOCategoryTypeInfoList implements ListItems { |
537 | 0 | Map<String, LoCategoryTypeInfo> loTypeMap = new HashMap<String, LoCategoryTypeInfo>(); |
538 | |
|
539 | 0 | public LOCategoryTypeInfoList(List<LoCategoryTypeInfo> loTypes) { |
540 | 0 | for (LoCategoryTypeInfo type : loTypes) { |
541 | 0 | loTypeMap.put(type.getId(), type); |
542 | |
} |
543 | 0 | } |
544 | |
|
545 | |
public List<String> getAttrKeys() { |
546 | 0 | return Arrays.asList("Name"); |
547 | |
} |
548 | |
|
549 | |
public String getItemAttribute(String id, String attrkey) { |
550 | 0 | LoCategoryTypeInfo lo = loTypeMap.get(id); |
551 | |
|
552 | 0 | if (attrkey.equals("Name")) { |
553 | 0 | return lo.getName(); |
554 | |
} |
555 | |
|
556 | 0 | return null; |
557 | |
} |
558 | |
|
559 | |
public int getItemCount() { |
560 | 0 | return loTypeMap.size(); |
561 | |
} |
562 | |
|
563 | |
public List<String> getItemIds() { |
564 | 0 | List<String> keys = new ArrayList<String>(); |
565 | |
|
566 | 0 | for (String s : loTypeMap.keySet()) { |
567 | 0 | keys.add(s); |
568 | |
} |
569 | |
|
570 | 0 | return keys; |
571 | |
} |
572 | |
|
573 | |
public String getItemText(String id) { |
574 | 0 | return ((LoCategoryTypeInfo) loTypeMap.get(id)).getName(); |
575 | |
} |
576 | |
} |
577 | |
|
578 | 0 | public class LOCategoryListNew extends Composite implements HasValue<List<LoCategoryInfo>> { |
579 | |
private static final String CATEGORY_TYPE_SEPARATOR = " - "; |
580 | |
private VerticalPanel listPanel; |
581 | 0 | private VerticalPanel main = new VerticalPanel(); |
582 | 0 | protected List<LoCategoryInfo> categories = new ArrayList<LoCategoryInfo>(); |
583 | |
|
584 | 0 | final CloseHandler<KSItemLabel> deleteHandler = new CloseHandler<KSItemLabel>() { |
585 | |
@Override |
586 | |
public void onClose(CloseEvent<KSItemLabel> event) { |
587 | 0 | KSItemLabel itemLabel = event.getTarget(); |
588 | 0 | String itemText = itemLabel.getDeletedKey(); |
589 | 0 | categoryList.removeItem(itemText); |
590 | 0 | categoryList.redraw(); |
591 | 0 | } |
592 | |
}; |
593 | |
|
594 | 0 | public LOCategoryListNew() { |
595 | 0 | listPanel = new VerticalPanel(); |
596 | 0 | main.add(listPanel); |
597 | 0 | super.initWidget(main); |
598 | 0 | } |
599 | |
|
600 | |
public HandlerRegistration addValueChangeHandler(ValueChangeHandler<List<LoCategoryInfo>> handler) { |
601 | 0 | return addHandler(handler, ValueChangeEvent.getType()); |
602 | |
} |
603 | |
|
604 | |
private void fireChangeEvent() { |
605 | 0 | ValueChangeEvent.fire(this, categories); |
606 | 0 | } |
607 | |
|
608 | |
public void redraw() { |
609 | |
|
610 | 0 | if (null == categoryTypeMap || categoryTypeMap.isEmpty()) { |
611 | |
|
612 | 0 | loCatRpcServiceAsync.getLoCategoryTypes(new KSAsyncCallback<List<LoCategoryTypeInfo>>() { |
613 | |
|
614 | |
@Override |
615 | |
public void handleFailure(Throwable caught) { |
616 | 0 | Window.alert("getLoCategoryTypes failed " + caught.getMessage()); |
617 | 0 | } |
618 | |
|
619 | |
@Override |
620 | |
public void onSuccess(List<LoCategoryTypeInfo> result) { |
621 | 0 | if (categoryTypeMap == null) { |
622 | 0 | loadCategoryTypes(result); |
623 | |
} |
624 | 0 | redrawCategoryTable(); |
625 | 0 | } |
626 | |
|
627 | |
}); |
628 | |
} else { |
629 | 0 | redrawCategoryTable(); |
630 | |
} |
631 | 0 | } |
632 | |
|
633 | |
private void redrawCategoryTable() { |
634 | 0 | listPanel.clear(); |
635 | |
|
636 | 0 | for (int i = 0; i < categories.size(); i++) { |
637 | 0 | String name = categories.get(i).getName(); |
638 | 0 | String typeKey = categories.get(i).getType(); |
639 | |
|
640 | 0 | KSItemLabel newItemLabel = new KSItemLabel(true, new CategoryDataParser()); |
641 | 0 | Data categoryData = CategoryDataUtil.toData(categories.get(i)); |
642 | 0 | newItemLabel.setValue(new DataValue(categoryData)); |
643 | 0 | newItemLabel.addCloseHandler(deleteHandler); |
644 | |
|
645 | 0 | listPanel.add(newItemLabel); |
646 | |
|
647 | |
|
648 | |
|
649 | |
|
650 | |
|
651 | |
|
652 | |
|
653 | |
|
654 | |
|
655 | |
|
656 | |
|
657 | |
|
658 | |
|
659 | |
|
660 | |
} |
661 | 0 | } |
662 | |
|
663 | |
public List<LoCategoryInfo> getValue() { |
664 | 0 | return categories; |
665 | |
} |
666 | |
|
667 | |
public void setValue(List<LoCategoryInfo> categories) { |
668 | 0 | this.categories = categories; |
669 | 0 | redraw(); |
670 | 0 | } |
671 | |
|
672 | |
@Override |
673 | |
public void setValue(List<LoCategoryInfo> value, boolean fireEvents) { |
674 | 0 | setValue(value); |
675 | 0 | } |
676 | |
|
677 | |
|
678 | |
public void removeItem(String text) { |
679 | |
|
680 | 0 | int a = text.indexOf(CATEGORY_TYPE_SEPARATOR); |
681 | 0 | text = text.substring(0, a); |
682 | |
|
683 | 0 | int i = 0; |
684 | 0 | for (LoCategoryInfo catInfo : categories) { |
685 | 0 | String name = catInfo.getName(); |
686 | |
|
687 | 0 | if (name.equals(text)) { |
688 | 0 | categories.remove(i); |
689 | 0 | fireChangeEvent(); |
690 | 0 | break; |
691 | |
} |
692 | 0 | i++; |
693 | 0 | } |
694 | 0 | redraw(); |
695 | 0 | } |
696 | |
|
697 | |
public void addItem(LoCategoryInfo category) { |
698 | 0 | categories.add(category); |
699 | 0 | fireChangeEvent(); |
700 | 0 | redraw(); |
701 | 0 | } |
702 | |
} |
703 | |
|
704 | |
public class CategoryDataParser implements DataHelper { |
705 | |
|
706 | 0 | public CategoryDataParser() { |
707 | 0 | } |
708 | |
|
709 | |
; |
710 | |
|
711 | |
@Override |
712 | |
public String getKey(Data data) { |
713 | 0 | return parse(data); |
714 | |
} |
715 | |
|
716 | |
@Override |
717 | |
public String parse(Data data) { |
718 | 0 | String result = null; |
719 | 0 | if (data != null) { |
720 | 0 | LoCategoryInfo loCategoryInfo = CategoryDataUtil.toLoCategoryInfo(data); |
721 | 0 | String typeName = "ERROR: uninitialized categoryTypeMap"; |
722 | 0 | if (null != categoryTypeMap) { |
723 | 0 | typeName = categoryTypeMap.get(loCategoryInfo.getType()).getName(); |
724 | |
} |
725 | 0 | result = loCategoryInfo.getName() + |
726 | |
LOCategoryBuilder.LOCategoryListNew.CATEGORY_TYPE_SEPARATOR + typeName; |
727 | 0 | } else { |
728 | 0 | result = ""; |
729 | |
} |
730 | 0 | return result; |
731 | |
} |
732 | |
|
733 | |
} |
734 | |
|
735 | 0 | public class CreateLoCancelGroup extends ButtonGroup<LoCancelEnum> { |
736 | 0 | public CreateLoCancelGroup(Callback<LoCancelEnum> callback) { |
737 | 0 | layout = new ButtonRow(); |
738 | 0 | this.addCallback(callback); |
739 | |
|
740 | 0 | addButton(LoCancelEnum.CANCEL); |
741 | 0 | addButtonToSecondaryGroup(LoCancelEnum.CREATE); |
742 | |
|
743 | 0 | this.initWidget(layout); |
744 | 0 | } |
745 | |
|
746 | |
private void addButton(final LoCancelEnum type) { |
747 | 0 | KSButton button = new KSButton(type.getText(), new ClickHandler() { |
748 | |
|
749 | |
@Override |
750 | |
public void onClick(ClickEvent event) { |
751 | 0 | sendCallbacks(type); |
752 | 0 | } |
753 | |
}); |
754 | 0 | layout.addButton(button); |
755 | 0 | buttonMap.put(type, button); |
756 | 0 | } |
757 | |
|
758 | |
private void addButtonToSecondaryGroup(final LoCancelEnum type) { |
759 | 0 | KSButton button = new KSButton(type.getText(), new ClickHandler() { |
760 | |
|
761 | |
@Override |
762 | |
public void onClick(ClickEvent event) { |
763 | 0 | sendCallbacks(type); |
764 | 0 | } |
765 | |
}); |
766 | 0 | ((ButtonRow) layout).addButtonToSecondaryGroup(button); |
767 | 0 | buttonMap.put(type, button); |
768 | 0 | } |
769 | |
} |
770 | |
|
771 | 0 | public static enum LoCancelEnum implements ButtonEnum { |
772 | 0 | CREATE, CANCEL; |
773 | |
|
774 | |
@Override |
775 | |
public ButtonEnum getActionType() { |
776 | 0 | return CREATE; |
777 | |
} |
778 | |
|
779 | |
@Override |
780 | |
public ButtonEnum getCancelType() { |
781 | 0 | return CANCEL; |
782 | |
} |
783 | |
|
784 | |
@Override |
785 | |
public ButtonStyle getStyle() { |
786 | 0 | return ButtonStyle.PRIMARY; |
787 | |
} |
788 | |
|
789 | |
@Override |
790 | |
public String getText() { |
791 | 0 | switch (this) { |
792 | |
case CREATE: |
793 | 0 | return "Create"; |
794 | |
case CANCEL: |
795 | 0 | return "Cancel"; |
796 | |
} |
797 | 0 | return null; |
798 | |
} |
799 | |
} |
800 | |
} |