001: package org.araneaframework.example.main.release.demos;
002:
003: import java.io.Serializable;
004: import java.util.ArrayList;
005: import java.util.Arrays;
006: import java.util.Iterator;
007: import java.util.List;
008: import java.util.Locale;
009: import org.araneaframework.example.main.TemplateBaseWidget;
010: import org.araneaframework.framework.LocalizationContext;
011: import org.araneaframework.uilib.event.OnChangeEventListener;
012: import org.araneaframework.uilib.form.FormWidget;
013: import org.araneaframework.uilib.form.control.AutoCompleteTextControl;
014: import org.araneaframework.uilib.form.data.StringData;
015:
016: /**
017: * @author Steven Jentson (steven@webmedia.ee)
018: * @author Taimo Peelo (taimo@araneaframework.org)
019: */
020: public class DemoAutoCompletionWidget extends TemplateBaseWidget {
021: private static final long serialVersionUID = 1L;
022: private FormWidget form;
023:
024: public void init() throws Exception {
025: setViewSelector("release/demos/demoAutoCompletion");
026: form = new FormWidget();
027:
028: AutoCompleteTextControl actc = new AutoCompleteTextControl();
029: actc.setDataProvider(new DemoACDataProvider(
030: new LocalizationContextProvider() {
031: public LocalizationContext getL10nCtx() {
032: return DemoAutoCompletionWidget.this
033: .getL10nCtx();
034: }
035: }));
036: actc.addOnChangeEventListener(new OnChangeEventListener() {
037: public void onChange() throws Exception {
038: form.convertAndValidate();
039: getMessageCtx().showInfoMessage(
040: t("release.ac.onchangetext") + " "
041: + form.getValueByFullName("acinput"));
042: }
043: });
044:
045: form.addElement("acinput", "common.Country", actc,
046: new StringData(), false);
047:
048: addWidget("testform", form);
049: }
050:
051: public void handleEventTest() throws Exception {
052: if (form.convertAndValidate()) {
053: getMessageCtx().showInfoMessage(
054: t("release.ac.submitvalid") + " "
055: + form.getValueByFullName("acinput"));
056: }
057: }
058:
059: public static interface LocalizationContextProvider extends
060: Serializable {
061: public LocalizationContext getL10nCtx();
062: }
063:
064: public static final class DemoACDataProvider implements
065: AutoCompleteTextControl.DataProvider {
066: private static final long serialVersionUID = 1L;
067: private List allSuggestions = new ArrayList();
068:
069: private String language;
070: private LocalizationContextProvider locCtxProvider;
071:
072: public DemoACDataProvider(
073: LocalizationContextProvider locCtxProvider) {
074: this .locCtxProvider = locCtxProvider;
075: fetchData();
076: }
077:
078: private void fetchData() {
079: allSuggestions.clear();
080: language = locCtxProvider.getL10nCtx().getLocale()
081: .getLanguage();
082: for (Iterator i = Arrays.asList(Locale.getISOCountries())
083: .iterator(); i.hasNext();) {
084: allSuggestions.add(new Locale(language, (String) i
085: .next()).getDisplayCountry(locCtxProvider
086: .getL10nCtx().getLocale()));
087: }
088: }
089:
090: private boolean isLanguageChanged() {
091: return !language.equals(locCtxProvider.getL10nCtx()
092: .getLocale().getLanguage());
093: }
094:
095: public List getSuggestions(String input) {
096: List results = new ArrayList();
097: if (input == null)
098: return results;
099:
100: if (isLanguageChanged())
101: fetchData();
102:
103: for (Iterator i = allSuggestions.iterator(); i.hasNext();) {
104: String suggestion = (String) i.next();
105: if (suggestion.length() >= input.length()
106: && suggestion.regionMatches(true, 0, input, 0,
107: input.length()))
108: results.add(suggestion);
109: }
110: return results;
111: }
112: }
113: }
|