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