001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.example.common.framework;
016:
017: import java.util.ArrayList;
018: import java.util.List;
019: import java.util.Locale;
020: import org.araneaframework.Widget;
021: import org.araneaframework.framework.LocalizationContext;
022: import org.araneaframework.framework.LocalizationContext.LocaleChangeListener;
023: import org.araneaframework.uilib.core.BaseMenuWidget;
024: import org.araneaframework.uilib.event.OnChangeEventListener;
025: import org.araneaframework.uilib.form.FormElement;
026: import org.araneaframework.uilib.form.FormWidget;
027: import org.araneaframework.uilib.form.control.SelectControl;
028: import org.araneaframework.uilib.form.data.StringData;
029: import org.araneaframework.uilib.list.util.FormUtil;
030: import org.araneaframework.uilib.support.DisplayItem;
031:
032: /**
033: * @author Taimo Peelo (taimo@araneaframework.org)
034: */
035: public abstract class TemplateMenuWidget extends BaseMenuWidget
036: implements LocaleChangeListener {
037: private FormWidget form;
038: private FormElement langSelect;
039:
040: // CONSTRUCTOR
041: public TemplateMenuWidget(Widget topWidget) throws Exception {
042: super (topWidget);
043: }
044:
045: protected void init() throws Exception {
046: super .init();
047:
048: form = new FormWidget();
049: langSelect = FormUtil.createElement("#", new SelectControl(),
050: new StringData(), false);
051: addWidget("form", form);
052: form.addWidget("langSelect", langSelect);
053:
054: createLangSelect();
055: getL10nCtx().addLocaleChangeListener(this );
056: }
057:
058: public void createLangSelect() throws Exception {
059: SelectControl select = new SelectControl();
060:
061: select.addOnChangeEventListener(new OnChangeEventListener() {
062: public void onChange() throws Exception {
063: if (langSelect.convertAndValidate()) {
064: String lang = (String) langSelect.getValue();
065: getL10nCtx().setLocale(new Locale(lang, ""));
066: }
067: }
068: });
069:
070: select.addItems(getLocales());
071: langSelect.setControl(select);
072: langSelect.setValue(getL10nCtx().getLocale().getLanguage());
073: }
074:
075: public void onLocaleChange(Locale oldLocale, Locale newLocale) {
076: String lang = (String) langSelect.getValue();
077: ((SelectControl) langSelect.getControl()).clearItems();
078: ((SelectControl) langSelect.getControl())
079: .addItems(getLocales());
080: langSelect.setValue(lang);
081: }
082:
083: public List getLocales() {
084: List result = new ArrayList();
085: result.add(new DisplayItem("en", getL10nCtx().localize(
086: "EnglishLang")));
087: result.add(new DisplayItem("et", getL10nCtx().localize(
088: "EstonianLang")));
089: return result;
090: }
091:
092: protected LocalizationContext getL10nCtx() {
093: return (LocalizationContext) getEnvironment().getEntry(
094: LocalizationContext.class);
095: }
096:
097: // returns the name of currently running flow class,
098: // so that its source could be located and shown to user
099: public String getFlowClassName() {
100: String result = null;
101:
102: try {
103: result = ((CallFrame) callStack.getFirst()).getWidget()
104: .getClass().getName();
105: } catch (Exception e) {
106: }
107:
108: return result;
109: }
110:
111: // returns the name of currently running flow's view selector,
112: // so that its source could be located and shown to user
113: public String getFlowViewSelector() {
114: String result = null;
115:
116: try {
117: result = ((ViewSelectorAware) ((CallFrame) callStack
118: .getFirst()).getWidget()).getViewSelector();
119: } catch (Exception e) {
120: }
121:
122: return result;
123: }
124: }
|