1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.common.ui.client.widgets.search;
17
18 import java.util.ArrayList;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.kuali.student.common.ui.client.application.Application;
24 import org.kuali.student.common.ui.client.application.KSAsyncCallback;
25 import org.kuali.student.common.ui.client.configurable.mvc.WidgetConfigInfo;
26 import org.kuali.student.common.ui.client.configurable.mvc.binding.SelectItemWidgetBinding;
27 import org.kuali.student.common.ui.client.mvc.Callback;
28 import org.kuali.student.common.ui.client.mvc.HasCrossConstraints;
29 import org.kuali.student.common.ui.client.mvc.HasDataValue;
30 import org.kuali.student.common.ui.client.mvc.HasFocusLostCallbacks;
31 import org.kuali.student.common.ui.client.mvc.TranslatableValueWidget;
32 import org.kuali.student.common.ui.client.service.CachingSearchService;
33 import org.kuali.student.common.ui.client.util.SearchUtils;
34 import org.kuali.student.common.ui.client.util.SearchUtils.SearchRequestWrapper;
35 import org.kuali.student.common.ui.client.widgets.HasInputWidget;
36 import org.kuali.student.common.ui.client.widgets.KSDropDown;
37 import org.kuali.student.common.ui.client.widgets.KSErrorDialog;
38 import org.kuali.student.common.ui.client.widgets.KSLabel;
39 import org.kuali.student.common.ui.client.widgets.KSTextBox;
40 import org.kuali.student.common.ui.client.widgets.list.KSCheckBoxList;
41 import org.kuali.student.common.ui.client.widgets.list.KSLabelList;
42 import org.kuali.student.common.ui.client.widgets.list.KSRadioButtonList;
43 import org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract;
44 import org.kuali.student.common.ui.client.widgets.list.ListItems;
45 import org.kuali.student.common.ui.client.widgets.list.SearchResultListItems;
46 import org.kuali.student.common.ui.client.widgets.list.SelectionChangeEvent;
47 import org.kuali.student.common.ui.client.widgets.list.SelectionChangeHandler;
48 import org.kuali.student.common.ui.client.widgets.suggestbox.IdableSuggestOracle.IdableSuggestion;
49 import org.kuali.student.common.ui.client.widgets.suggestbox.KSSuggestBox;
50 import org.kuali.student.common.ui.client.widgets.suggestbox.SearchSuggestOracle;
51 import org.kuali.student.r1.common.assembly.data.Data;
52 import org.kuali.student.r1.common.assembly.data.Data.DataValue;
53 import org.kuali.student.r1.common.assembly.data.Data.StringValue;
54 import org.kuali.student.r1.common.assembly.data.Data.Value;
55 import org.kuali.student.r1.common.assembly.data.LookupMetadata;
56 import org.kuali.student.r1.common.assembly.data.QueryPath;
57 import org.kuali.student.r2.core.search.dto.SearchRequestInfo;
58 import org.kuali.student.r2.core.search.dto.SearchResultInfo;
59
60 import com.google.gwt.core.client.GWT;
61 import com.google.gwt.dom.client.Element;
62 import com.google.gwt.event.dom.client.BlurEvent;
63 import com.google.gwt.event.dom.client.BlurHandler;
64 import com.google.gwt.event.dom.client.ClickEvent;
65 import com.google.gwt.event.dom.client.ClickHandler;
66 import com.google.gwt.event.dom.client.KeyUpEvent;
67 import com.google.gwt.event.dom.client.KeyUpHandler;
68 import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
69 import com.google.gwt.event.logical.shared.ValueChangeEvent;
70 import com.google.gwt.event.logical.shared.ValueChangeHandler;
71 import com.google.gwt.event.shared.HandlerRegistration;
72 import com.google.gwt.user.client.DOM;
73 import com.google.gwt.user.client.ui.Anchor;
74 import com.google.gwt.user.client.ui.Composite;
75 import com.google.gwt.user.client.ui.FlowPanel;
76 import com.google.gwt.user.client.ui.HasText;
77 import com.google.gwt.user.client.ui.SuggestBox;
78 import com.google.gwt.user.client.ui.Widget;
79
80 public class KSPicker extends Composite implements HasFocusLostCallbacks, HasValueChangeHandlers<String>, HasDataValue, TranslatableValueWidget, HasInputWidget, HasCrossConstraints {
81
82 protected FlowPanel layout = new FlowPanel();
83 protected BasicWidget basicWidget;
84 private Anchor advSearchLink = new Anchor(getMessage("advSearch"));
85 private AdvancedSearchWindow advSearchWindow = null;
86 private SearchPanel searchPanel;
87 public SearchPanel getSearchPanel() {
88 return searchPanel;
89 }
90
91 protected WidgetConfigInfo config;
92 private Callback<List<SelectedResults>> advancedSearchCallback;
93 private List<Callback<SelectedResults>> basicSelectionCallbacks =
94 new ArrayList<Callback<SelectedResults>>();
95 private List<Callback<String>> basicSelectionTextChangeCallbacks =
96 new ArrayList<Callback<String>>();
97 private CachingSearchService cachingSearchService = CachingSearchService.getSearchService();
98
99 protected SearchRequestWrapper searchRequestWrapper = new SearchRequestWrapper();
100
101 public KSPicker() {}
102
103 public void init(WidgetConfigInfo config) {
104 this.config = config;
105 init(config.lookupMeta, config.additionalLookups);
106 }
107
108 @Override
109 public Widget getInputWidget(){
110 if(basicWidget != null){
111 return basicWidget.get();
112 }
113 return null;
114
115 }
116
117 public void init(LookupMetadata inLookupMetadata, List<LookupMetadata> additionalLookupMetadata) {
118 this.initWidget(layout);
119 if (inLookupMetadata == null) {
120 KSErrorDialog.show(new Throwable(getMessage("invalidLookupConfig")));
121 return;
122 }
123
124 if(config == null) {
125 config = new WidgetConfigInfo();
126 }
127
128 setupBasicSearch(inLookupMetadata);
129 setupAdvancedSearch(additionalLookupMetadata);
130 }
131
132 private static String getMessage(final String messageId) {
133 return Application.getApplicationContext().getMessage(messageId);
134 }
135
136 private void setupBasicSearch(LookupMetadata inLookupMetadata) {
137
138
139 if (inLookupMetadata.getWidget() != null){
140 switch (inLookupMetadata.getWidget()){
141 case SUGGEST_BOX:
142 setupSuggestBox(inLookupMetadata);
143 break;
144 case DROP_DOWN:
145 case CHECKBOX_LIST:
146 case RADIO:
147 setupListWidget(inLookupMetadata);
148 break;
149 case NO_WIDGET:
150 if(getMessage(inLookupMetadata.getId()+"-name")!=null){
151 advSearchLink.setText(getMessage(inLookupMetadata.getId()+"-name"));
152 }
153 else
154 if ((inLookupMetadata.getName() != null) && (inLookupMetadata.getName().trim().length() > 0)) {
155 advSearchLink.setText(inLookupMetadata.getName().trim());
156 }
157 basicWidget = new BasicWidget(new SelectionContainerWidget());
158 break;
159 default:
160 setupDefaultWidget(inLookupMetadata);
161 }
162 } else {
163 setupDefaultWidget(inLookupMetadata);
164 }
165 }
166
167 private void setupDefaultWidget(LookupMetadata inLookupMetadata){
168 basicWidget = new BasicWidget(config != null && config.canEdit ? new KSTextBox() : new KSLabel());
169 GWT.log("KSTextBox for " + inLookupMetadata.getSearchTypeId(), null);
170 layout.add(basicWidget.get());
171 }
172
173 private void setupSuggestBox(LookupMetadata inLookupMetadata){
174 if(config.canEdit) {
175 final SearchSuggestOracle orgSearchOracle = new SearchSuggestOracle(inLookupMetadata);
176 final KSSuggestBox suggestBox = new KSSuggestBox(orgSearchOracle, true);
177 suggestBox.addKeyUpHandler(new KeyUpHandler() {
178 @Override
179 public void onKeyUp(KeyUpEvent event) {
180 for(Callback<String> basicSelectionTextChangeCallback: basicSelectionTextChangeCallbacks){
181 basicSelectionTextChangeCallback.exec("Text Changed");
182 }
183 }
184 });
185 suggestBox.addSelectionChangeHandler(new SelectionChangeHandler(){
186
187 @Override
188 public void onSelectionChange(SelectionChangeEvent event) {
189 IdableSuggestion currentSuggestion = suggestBox.getSelectedSuggestion();
190 SelectedResults selectedResults = new SelectedResults(
191 currentSuggestion.getReplacementString(), currentSuggestion.getId());
192 for(Callback<SelectedResults> basicSelectionCallback: basicSelectionCallbacks){
193 basicSelectionCallback.exec(selectedResults);
194 }
195 }
196 });
197 basicWidget = new BasicWidget(suggestBox);
198 ((KSSuggestBox) basicWidget.get()).setAutoSelectEnabled(false);
199 orgSearchOracle.setTextWidget(((SuggestBox) basicWidget.get()).getTextBox());
200 } else {
201 basicWidget = new BasicWidget(new KSLabel());
202 }
203 layout.add(basicWidget.get());
204 }
205
206 protected void setupListWidget(LookupMetadata inLookupMetadata) {
207
208
209 if(config.canEdit) {
210
211 KSSelectItemWidgetAbstract listItemWidget = null;
212 switch (inLookupMetadata.getWidget()){
213 case DROP_DOWN:
214 listItemWidget = new KSDropDown();
215 break;
216 case CHECKBOX_LIST:
217 listItemWidget = new KSCheckBoxList();
218 ((KSCheckBoxList)listItemWidget).setIgnoreMultipleAttributes(true);
219 break;
220 case RADIO:
221 listItemWidget = new KSRadioButtonList();
222 break;
223 }
224 basicWidget = new BasicWidget(listItemWidget);
225 SearchUtils.initializeSearchRequest(inLookupMetadata, searchRequestWrapper);
226 if(!searchRequestWrapper.isDeferSearch()) populateListWidget(searchRequestWrapper.getSearchRequest());
227 } else {
228 if (inLookupMetadata.getWidget() == LookupMetadata.Widget.DROP_DOWN || inLookupMetadata.getWidget() == LookupMetadata.Widget.RADIO){
229 basicWidget = new BasicWidget(new KSLabel());
230 } else {
231
232 basicWidget = new BasicWidget(new KSLabelList());
233 SearchUtils.initializeSearchRequest(inLookupMetadata, searchRequestWrapper);
234 if(!searchRequestWrapper.isDeferSearch()) populateListWidget(searchRequestWrapper.getSearchRequest());
235 }
236 layout.add(basicWidget.get());
237 }
238 }
239
240 private void setupAdvancedSearch(List<LookupMetadata> additionalLookupMetadata) {
241
242
243 List<LookupMetadata> advancedLightboxLookupdata = getLookupMetadataBasedOnWidget(additionalLookupMetadata, LookupMetadata.Widget.ADVANCED_LIGHTBOX);
244 if ((advancedLightboxLookupdata != null) && config.canEdit) {
245
246
247 String advSearchTitle;
248 advSearchTitle = getMessage(advancedLightboxLookupdata.get(0).getId()+"-title");
249 if (basicWidget.get() instanceof SelectionContainerWidget){
250 if(advSearchTitle==null)
251 advSearchTitle = advancedLightboxLookupdata.get(0).getTitle();
252 } else {
253 if(advSearchTitle==null)
254 advSearchTitle = "Advanced Search: " + advancedLightboxLookupdata.get(0).getTitle();
255 else
256 advSearchTitle = "Advanced Search: " + advSearchTitle;
257 }
258
259
260 if (advancedLightboxLookupdata.size() == 1) {
261 String actionLabel = advancedLightboxLookupdata.get(0).getWidgetOptionValue(LookupMetadata.WidgetOption.ADVANCED_LIGHTBOX_ACTION_LABEL);
262 searchPanel = new SearchPanel(advancedLightboxLookupdata.get(0));
263 searchPanel.setActionLabel(actionLabel);
264 advSearchWindow = new AdvancedSearchWindow(advSearchTitle, searchPanel);
265 } else {
266 searchPanel = new SearchPanel(advancedLightboxLookupdata);
267 advSearchWindow = new AdvancedSearchWindow(advSearchTitle, searchPanel);
268 searchPanel.addLookupChangedCallback(new Callback<LookupMetadata>() {
269 @Override
270 public void exec(LookupMetadata selectedLookup) {
271 String actionLabel = (selectedLookup == null)? null : selectedLookup
272 .getWidgetOptionValue(LookupMetadata.WidgetOption.ADVANCED_LIGHTBOX_ACTION_LABEL);
273 searchPanel.setActionLabel(actionLabel);
274 }
275 });
276 LookupMetadata initialLookupMetaData = advancedLightboxLookupdata.get(0);
277 String actionLabel = (initialLookupMetaData == null)? null : initialLookupMetaData
278 .getWidgetOptionValue(LookupMetadata.WidgetOption.ADVANCED_LIGHTBOX_ACTION_LABEL);
279 searchPanel.setActionLabel(actionLabel);
280 }
281 searchPanel.setMultiSelect(true);
282
283 String previewMode = additionalLookupMetadata.get(0).getWidgetOptionValue(LookupMetadata.WidgetOption.ADVANCED_LIGHTBOX_PREVIEW_MODE);
284 if (previewMode != null && previewMode.equals("true")) {
285 searchPanel.setActionLabel("Preview");
286 }
287
288 searchPanel.addSelectionCompleteCallback(new Callback<List<SelectedResults>>() {
289 public void exec(List<SelectedResults> results) {
290 if (results != null && results.size() > 0) {
291 if (advancedSearchCallback != null) {
292 advancedSearchCallback.exec(results);
293 }
294 else {
295 basicWidget.setResults(results);
296 }
297 advSearchWindow.hide();
298 }
299 }
300 });
301
302 advSearchLink.addClickHandler(new ClickHandler(){
303 @Override
304 public void onClick(ClickEvent event) {
305 if(advSearchWindow != null){
306 advSearchWindow.show();
307 }
308 }
309 });
310 layout.add(advSearchLink);
311 }
312 }
313
314 protected void populateListWidget(SearchRequestInfo sr) {
315
316 cachingSearchService.search(sr, new KSAsyncCallback<SearchResultInfo>(){
317
318 @Override
319 public void onSuccess(SearchResultInfo results) {
320 if(results != null){
321 ((KSSelectItemWidgetAbstract)basicWidget.get()).setListItems(new SearchResultListItems(results.getRows(), config.lookupMeta));
322 } else {
323 ((KSSelectItemWidgetAbstract)basicWidget.get()).setListItems(new SearchResultListItems(null, config.lookupMeta));
324
325 }
326
327 ((KSSelectItemWidgetAbstract)basicWidget.get()).setInitialized(true);
328 layout.add(basicWidget.get());
329 }
330 });
331 }
332
333 private List<LookupMetadata> getLookupMetadataBasedOnWidget(List<LookupMetadata> additionalLookupMetadata, LookupMetadata.Widget widgetType) {
334
335
336 if (additionalLookupMetadata == null) {
337 return null;
338 }
339
340 List<LookupMetadata> lookups = new ArrayList<LookupMetadata>();
341 for (LookupMetadata addLookupData : additionalLookupMetadata) {
342 if (addLookupData.getWidget() == widgetType) {
343 lookups.add(addLookupData);
344 }
345 }
346 return (lookups.size() > 0 ? lookups : null);
347 }
348
349 public class BasicWidget implements HasDataValue, HasFocusLostCallbacks, TranslatableValueWidget {
350 private Widget basicWidget;
351
352 public BasicWidget(Widget basicWidget){
353 this.basicWidget = basicWidget;
354 initAccessibility();
355 }
356
357 private void initAccessibility() {
358 Element element = basicWidget.getElement();
359 element.setAttribute("role", "combobox");
360 element.setAttribute("aria-autocomplete", "list");
361 element.setAttribute("aria-haspopup", "true");
362 }
363
364 public void setResults(List<SelectedResults> results) {
365 if (basicWidget instanceof KSTextBox) {
366 ((KSTextBox)basicWidget).setText(results.get(0).getDisplayKey());
367 } else if (basicWidget.getClass().getName().contains("ContainerWidget")) {
368 List<String> selections = new ArrayList<String>();
369 for (SelectedResults result: results) {
370
371 selections.add(result.getReturnKey());
372 }
373 ((SelectionContainerWidget) basicWidget).setSelections(selections);
374 } else if (basicWidget instanceof KSSuggestBox) {
375 IdableSuggestion theSuggestion = new IdableSuggestion();
376 theSuggestion.setReplacementString(results.get(0).getDisplayKey());
377 theSuggestion.setId(results.get(0).getReturnKey());
378 theSuggestion.setAttrMap(results.get(0).getResultRow().getColumnValues());
379 ((KSSuggestBox) basicWidget).setValue(theSuggestion);
380 }
381 }
382 public void clear() {
383 if(basicWidget instanceof KSTextBox) {
384 ((KSTextBox)basicWidget).setText(null);
385 } else if(basicWidget instanceof KSSuggestBox) {
386 ((KSSuggestBox) basicWidget).setValue((String)null);
387 } else if(basicWidget instanceof KSLabel) {
388 ((KSLabel)basicWidget).setText(null);
389 } else if(basicWidget instanceof KSDropDown) {
390 ((KSDropDown)basicWidget).clear();
391 } else {
392 ((KSSuggestBox) basicWidget).setValue(null, false);
393 }
394 }
395 public void setValue(final String id, String translation) {
396 if(basicWidget instanceof KSTextBox) {
397 ((KSTextBox)basicWidget).setText(translation);
398 } else if(basicWidget instanceof KSSuggestBox) {
399 ((KSSuggestBox) basicWidget).setValue(id, translation);
400 } else if(basicWidget instanceof KSLabel) {
401 ((KSLabel)basicWidget).setText(translation);
402 } else if(basicWidget instanceof KSSelectItemWidgetAbstract) {
403 ListItems searchResults = ((KSSelectItemWidgetAbstract)basicWidget).getListItems();
404 if (searchResults != null) {
405 ((KSSelectItemWidgetAbstract)basicWidget).selectItem(id);
406 }
407 else {
408 ((KSSelectItemWidgetAbstract)basicWidget).addWidgetReadyCallback(new Callback<Widget>() {
409 @Override
410 public void exec(Widget widget) {
411 final ListItems searchResults = ((KSSelectItemWidgetAbstract)widget).getListItems();
412
413 if(id != null){
414 for (String itemId : searchResults.getItemIds()) {
415 if (itemId.equals(id.trim())) {
416 ((KSSelectItemWidgetAbstract)basicWidget).selectItem(itemId);
417 break;
418 }
419 }
420 } else {
421 ((KSSelectItemWidgetAbstract)basicWidget).clear();
422 }
423 }
424 });
425 }
426
427 } else {
428 ((KSSuggestBox) basicWidget).setValue("", true);
429 }
430 }
431
432 public void setValue(final Value value, boolean fireEvents) {
433 if (basicWidget instanceof KSTextBox || basicWidget instanceof KSLabel) {
434 if(value != null){
435 ((HasText)basicWidget).setText((String) value.get());
436 }
437 else{
438 ((HasText)basicWidget).setText("");
439 }
440 } else if (basicWidget instanceof KSSuggestBox) {
441
442 if(value != null){
443 if(!config.isRepeating){
444 ((KSSuggestBox) basicWidget).setValue((String)value.get(), fireEvents);
445 }
446 else{
447 DataValue dataValue = (DataValue)value;
448 Data d = dataValue.get();
449 QueryPath path = QueryPath.parse("0");
450 String v = d.query(path);
451 ((KSSuggestBox) basicWidget).setValue((String)v, fireEvents);
452 }
453 }
454 else{
455 ((KSSuggestBox) basicWidget).setValue("", fireEvents);
456 }
457 } else if(basicWidget instanceof KSSelectItemWidgetAbstract) {
458 SelectItemWidgetBinding.INSTANCE.setWidgetValue((KSSelectItemWidgetAbstract)basicWidget, value);
459 }
460 }
461 public String getDisplayValue() {
462 String result = "";
463 if (basicWidget instanceof KSTextBox) {
464 result = ((KSTextBox)basicWidget).getText();
465 } else if (basicWidget instanceof KSSuggestBox) {
466 IdableSuggestion suggestion = ((KSSuggestBox)basicWidget).getCurrentSuggestion();
467 if(suggestion != null) {
468 result = suggestion.getReplacementString();
469 }
470 } else if (basicWidget instanceof KSDropDown) {
471 KSDropDown dropDown = (KSDropDown)basicWidget;
472 StringValue value = new StringValue(((KSDropDown) basicWidget).getSelectedItem());
473 String itemId = dropDown.getSelectedItem();
474
475 if(itemId != null && !itemId.isEmpty()) {
476 result = dropDown.getListItems().getItemText(itemId);
477 }
478 }
479 return result;
480 }
481 public Value getValue() {
482 if (basicWidget instanceof KSTextBox) {
483 StringValue value = new StringValue(((KSTextBox)basicWidget).getText());
484 return value;
485 } else if (basicWidget instanceof KSSuggestBox) {
486
487 if(!config.isRepeating){
488 StringValue value = new StringValue(((KSSuggestBox) basicWidget).getValue());
489 return value;
490 }
491 else{
492 Data data = new Data();
493 data.set(new Data.IntegerKey(0),((KSSuggestBox) basicWidget).getValue());
494 DataValue value = new DataValue(data);
495 return value;
496 }
497 } else if (basicWidget instanceof KSSelectItemWidgetAbstract) {
498 return SelectItemWidgetBinding.INSTANCE.getWidgetValue((KSSelectItemWidgetAbstract)basicWidget);
499 }
500
501 return null;
502 }
503
504 public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
505 if (basicWidget instanceof KSTextBox) {
506 return ((KSTextBox)basicWidget).addValueChangeHandler(handler);
507 } else if (basicWidget instanceof KSSuggestBox) {
508 return ((KSSuggestBox) basicWidget).addValueChangeHandler(handler);
509 }
510 return null;
511 }
512
513 public void addValuesChangeHandler(ValueChangeHandler<List<String>> handler) {
514 if (basicWidget.getClass().getName().contains("ContainerWidget")) {
515 ((SelectionContainerWidget) basicWidget).addValueChangeHandler(handler);
516 }
517 }
518 public void addSelectionChangeHandler(SelectionChangeHandler handler) {
519 if (basicWidget instanceof KSSelectItemWidgetAbstract) {
520 ((KSSelectItemWidgetAbstract) basicWidget).addSelectionChangeHandler(handler);
521 }else if(config!=null&&!config.isRepeating&&basicWidget instanceof KSSuggestBox){
522 ((KSSuggestBox) basicWidget).addSelectionChangeHandler(handler);
523 }
524 }
525
526 @Override
527 public void addValueChangeCallback(final Callback<Value> callback) {
528 ValueChangeHandler<String> handler = new ValueChangeHandler<String>(){
529 @Override
530 public void onValueChange(ValueChangeEvent<String> event) {
531 StringValue value = new StringValue(event.getValue());
532 callback.exec(value);
533 }
534 };
535 addValueChangeHandler(handler);
536 }
537
538 @Override
539 public void addFocusLostCallback(final Callback<Boolean> callback) {
540 if (basicWidget instanceof KSTextBox) {
541 ((KSTextBox)basicWidget).addBlurHandler(new BlurHandler(){
542 @Override
543 public void onBlur(BlurEvent event) {
544 callback.exec(true);
545
546 }
547 });
548 } else if (basicWidget instanceof KSSuggestBox) {
549 ((KSSuggestBox) basicWidget).getTextBox().addBlurHandler(new BlurHandler(){
550 @Override
551 public void onBlur(BlurEvent event) {
552
553 if(!((KSSuggestBox) basicWidget).isSuggestionListShowing()){
554 callback.exec(true);
555 }
556 }
557 });
558 } else if (basicWidget instanceof KSSelectItemWidgetAbstract) {
559 ((KSSelectItemWidgetAbstract) basicWidget).addBlurHandler(new BlurHandler(){
560 @Override
561 public void onBlur(BlurEvent event) {
562 callback.exec(true);
563 }
564 });
565 }
566 }
567
568 @Override
569 public void setValue(Value value) {
570 this.setValue(value, true);
571 }
572
573 public Widget get() {
574 return basicWidget;
575 }
576
577 @Override
578 public void setValue(final Map<String, String> translations) {
579
580 if (basicWidget instanceof KSSelectItemWidgetAbstract){
581 Callback<Widget> widgetReadyCallback = new Callback<Widget>(){
582 public void exec(Widget widget) {
583 ((KSSelectItemWidgetAbstract)widget).clear();
584 for (String id:translations.keySet()){
585 ((KSSelectItemWidgetAbstract)widget).selectItem(id);
586 }
587 }
588 };
589 if (!((KSSelectItemWidgetAbstract)basicWidget).isInitialized()){
590 ((KSSelectItemWidgetAbstract)basicWidget).addWidgetReadyCallback(widgetReadyCallback);
591 } else{
592 widgetReadyCallback.exec(basicWidget);
593 }
594 } else {
595 GWT.log("Basic picker widget not coded to handle multiple translations", null);
596 }
597 }
598
599 }
600
601
602
603
604
605
606
607 private class SelectionContainerWidget extends Widget implements HasValueChangeHandlers<List<String>> {
608 private List<String> selections = new ArrayList<String>();
609
610 public SelectionContainerWidget(){
611 this.setElement(DOM.createSpan());
612 }
613
614 public List<String> getSelections() {
615 return selections;
616 }
617
618 public void setSelections(List<String> selections) {
619 this.selections = selections;
620 ValueChangeEvent.fire(this, this.selections);
621 }
622
623 @Override
624 public HandlerRegistration addValueChangeHandler(ValueChangeHandler<List<String>> handler) {
625 return super.addHandler(handler, ValueChangeEvent.getType());
626 }
627 }
628
629
630 public AdvancedSearchWindow getSearchWindow(){
631 return advSearchWindow;
632 }
633
634 public void addBasicSelectionCompletedCallback(Callback<SelectedResults> callback) {
635 basicSelectionCallbacks.add(callback);
636 }
637
638 public void addBasicSelectionTextChangeCallback(Callback<String> callback) {
639 basicSelectionTextChangeCallbacks.add(callback);
640 }
641
642 @Override
643 public void addValueChangeCallback(Callback<Value> callback) {
644 basicWidget.addValueChangeCallback(callback);
645 }
646
647 @Override
648 public void setValue(Value value) {
649 setValue(value, true);
650 }
651
652 public void setValue(String value){
653 basicWidget.setValue(new StringValue(value));
654 }
655
656 public void setValue(Value value, boolean fireEvents) {
657
658 basicWidget.setValue(value, fireEvents);
659 }
660 public void clear() {
661 basicWidget.clear();
662 }
663
664 @Override
665 public void setValue(String id, String translation) {
666 basicWidget.setValue(id, translation);
667 }
668
669 @Override
670 public Value getValue() {
671 return basicWidget.getValue();
672 }
673 public String getDisplayValue() {
674 return basicWidget.getDisplayValue();
675 }
676 @Override
677 public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
678 return basicWidget.addValueChangeHandler(handler);
679 }
680
681 public void addValuesChangeHandler(ValueChangeHandler<List<String>> handler) {
682 if(basicWidget != null)
683 basicWidget.addValuesChangeHandler(handler);
684 }
685
686 public void addSelectionChangeHandler(SelectionChangeHandler handler) {
687 if(basicWidget != null)
688 basicWidget.addSelectionChangeHandler(handler);
689 }
690
691
692 @Override
693 public void addFocusLostCallback(Callback<Boolean> callback) {
694 basicWidget.addFocusLostCallback(callback);
695 }
696
697 public void setAdvancedSearchCallback(Callback<List<SelectedResults>> advancedSearchCallback) {
698 this.advancedSearchCallback = advancedSearchCallback;
699 }
700
701 @Override
702 public void setValue(Map<String, String> translations) {
703 basicWidget.setValue(translations);
704 }
705
706 @Override
707 public HashSet<String> getCrossConstraints() {
708 return searchRequestWrapper.getCrossConstraints();
709 }
710
711 @Override
712 public void reprocessWithUpdatedConstraints() {
713 SearchUtils.initializeSearchRequest(config.lookupMeta, searchRequestWrapper);
714 populateListWidget(searchRequestWrapper.getSearchRequest());
715 }
716
717 public Callback<List<SelectedResults>> getAdvancedSearchCallback() {
718 return advancedSearchCallback;
719 }
720
721 @Override
722 protected void onEnsureDebugId(String baseID) {
723 super.onEnsureDebugId(baseID);
724 Widget basicInputWidget = getInputWidget();
725 if (basicInputWidget != null) {
726 basicInputWidget.ensureDebugId(baseID + "-KSPicker-widget");
727 }
728 advSearchLink.ensureDebugId(baseID + "-Advanced-Search-anchor");
729 }
730
731 }