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