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