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.main.web.demo;
016:
017: import java.lang.reflect.Method;
018: import org.araneaframework.example.main.TemplateBaseWidget;
019: import org.araneaframework.example.main.release.demos.DemoAutoCompletionWidget;
020: import org.araneaframework.framework.LocalizationContext;
021: import org.araneaframework.uilib.event.OnChangeEventListener;
022: import org.araneaframework.uilib.form.Control;
023: import org.araneaframework.uilib.form.FormElement;
024: import org.araneaframework.uilib.form.FormWidget;
025: import org.araneaframework.uilib.form.control.AutoCompleteTextControl;
026: import org.araneaframework.uilib.form.control.BaseControl;
027: import org.araneaframework.uilib.form.control.DateControl;
028: import org.araneaframework.uilib.form.control.DateTimeControl;
029: import org.araneaframework.uilib.form.control.FloatControl;
030: import org.araneaframework.uilib.form.control.TimeControl;
031: import org.araneaframework.uilib.form.data.BigDecimalData;
032: import org.araneaframework.uilib.form.data.DateData;
033: import org.araneaframework.uilib.form.data.StringData;
034:
035: /**
036: * Interactive onchange event listener test for the troublesome date/time controls
037: * and their JSP tags -- which should be buried.
038: *
039: * @author Taimo Peelo (taimo@araneaframework.org)
040: */
041: public class DemoOnChangeListenersWidget extends TemplateBaseWidget {
042: private static final long serialVersionUID = 1L;
043: private FormWidget simpleForm;
044:
045: protected void init() throws Exception {
046: setViewSelector("demo/demoOnChangeListeners");
047:
048: simpleForm = new FormWidget();
049:
050: simpleForm.addElement("dateTime1", "common.datetime",
051: buildControl(DateTimeControl.class), new DateData(),
052: false);
053: simpleForm.addElement("time1", "common.time",
054: buildControl(TimeControl.class), new DateData(), false);
055: simpleForm.addElement("date1", "common.date",
056: buildControl(DateControl.class), new DateData(), false);
057:
058: simpleForm.addElement("dateTime2", "common.datetime",
059: buildControl(DateTimeControl.class), new DateData(),
060: false);
061: simpleForm.addElement("time2", "common.time",
062: buildControl(TimeControl.class), new DateData(), false);
063: simpleForm.addElement("date2", "common.date",
064: buildControl(DateControl.class), new DateData(), false);
065:
066: simpleForm.addElement("suggestBox",
067: "demo.suggestive.textinput",
068: buildControl(AutoCompleteTextControl.class),
069: new StringData(), false);
070: ((AutoCompleteTextControl) simpleForm
071: .getControlByFullName("suggestBox"))
072: .setDataProvider(new DemoAutoCompletionWidget.DemoACDataProvider(
073: new DemoAutoCompletionWidget.LocalizationContextProvider() {
074: public LocalizationContext getL10nCtx() {
075: return DemoOnChangeListenersWidget.this
076: .getL10nCtx();
077: }
078: }));
079:
080: simpleForm.addElement("float1", "common.float",
081: buildControl(FloatControl.class), new BigDecimalData(),
082: false);
083: simpleForm.addElement("float2", "common.float",
084: buildControl(FloatControl.class), new BigDecimalData(),
085: false);
086:
087: addWidget("listenerForm", simpleForm);
088: }
089:
090: private Control buildControl(Class clazz) throws Exception {
091: Control c = (Control) clazz.newInstance();
092: Method m = clazz.getMethod("addOnChangeEventListener",
093: new Class[] { OnChangeEventListener.class });
094: m.invoke(c, new Object[] { new DemoChangeEventListener(c) });
095: return c;
096: }
097:
098: private class DemoChangeEventListener implements
099: OnChangeEventListener {
100: private static final long serialVersionUID = 1L;
101: private Control eventSource;
102:
103: public DemoChangeEventListener(Control eventSource) {
104: this .eventSource = eventSource;
105: }
106:
107: public void onChange() throws Exception {
108: FormElement element = (FormElement) ((BaseControl) eventSource)
109: .getFormElementCtx();
110: Object oldValue = element.getValue();
111: simpleForm.convert();
112: Object newValue = element.getValue();
113: getMessageCtx().showInfoMessage(
114: "'" + t(element.getLabel())
115: + "' triggered onChange: '" + oldValue
116: + "' --> '" + newValue + "'");
117: }
118: }
119: }
|