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.sample;
016:
017: import java.util.Date;
018: import org.araneaframework.example.main.TemplateBaseWidget;
019: import org.araneaframework.uilib.event.ProxyOnClickEventListener;
020: import org.araneaframework.uilib.form.FormElement;
021: import org.araneaframework.uilib.form.FormWidget;
022: import org.araneaframework.uilib.form.constraint.NotEmptyConstraint;
023: import org.araneaframework.uilib.form.control.ButtonControl;
024: import org.araneaframework.uilib.form.control.CheckboxControl;
025: import org.araneaframework.uilib.form.control.DateControl;
026: import org.araneaframework.uilib.form.control.DateTimeControl;
027: import org.araneaframework.uilib.form.control.FloatControl;
028: import org.araneaframework.uilib.form.control.TextControl;
029: import org.araneaframework.uilib.form.control.TimeControl;
030: import org.araneaframework.uilib.form.data.BigDecimalData;
031: import org.araneaframework.uilib.form.data.BooleanData;
032: import org.araneaframework.uilib.form.data.DateData;
033: import org.araneaframework.uilib.form.data.StringData;
034:
035: /**
036: * Simple form component. A form with one checkbox, one textbox and
037: * three kinds of different timeinputs (DateInput, Timeinput and
038: * DateTimeInput) and a button.
039: *
040: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
041: */
042: public class SimpleFormWidget extends TemplateBaseWidget {
043: private static final long serialVersionUID = 1L;
044: private FormWidget simpleForm;
045:
046: /**
047: * Builds the form.
048: */
049: protected void init() throws Exception {
050: setViewSelector("sample/simpleForm");
051:
052: // creation of new form
053: simpleForm = new FormWidget();
054:
055: // Now that we have created a form, we will need to add form elements.
056: // form elements consist of four basic things - label, Control that implements
057: // form element functionality and Data holding values that form element can have.
058: // Note that the first sample with FormWidget's createElement method is not the
059: // way form elements are usually added to the form, but rather emphasises the
060: // fact that everything you add to FormWidget is a FormElement.
061:
062: // createElement(String labelId, Control control, Data data, boolean mandatory)
063: FormElement el = simpleForm.createElement("common.Textbox",
064: new TextControl(), new StringData(), false);
065: simpleForm.addElement("textbox1", el);
066:
067: // and here we add form elements to form without the extra step taken previously.
068: simpleForm.addElement("checkbox1", "Checkbox",
069: new CheckboxControl(), new BooleanData(), false);
070: simpleForm.addElement("dateTime", "common.datetime",
071: new DateTimeControl(), new DateData(), false);
072: simpleForm.addElement("time", "common.time", new TimeControl(),
073: new DateData(), false);
074: simpleForm.addElement("date", "common.date", new DateControl(),
075: new DateData(), false);
076: simpleForm.addElement("number", "common.float",
077: new FloatControl(), new BigDecimalData(), false);
078: // require the number input field to be filled. It could have been achieved already
079: // on formelement creation by setting mandatory attribute to true
080: simpleForm.getElement("number").setConstraint(
081: new NotEmptyConstraint());
082: // sets initial value of form element
083: simpleForm.setValueByFullName("dateTime", new Date());
084:
085: // now we construct a button, that is also Control. Reason why we cannot just add it
086: // to form is obvious, we want to add a specific listener to button before.
087: ButtonControl button = new ButtonControl();
088: button.addOnClickEventListener(new ProxyOnClickEventListener(
089: this , "testSimpleForm"));
090: // add the button to form. As the button does not hold any value, Data will be null.
091: simpleForm.addElement("button", "#Button", button, null, false);
092:
093: // the usual, add the created widget to main widget.
094: addWidget("simpleForm", simpleForm);
095: }
096:
097: /**
098: * A test action, invoked when button is pressed. It adds the values of
099: * formelements to message context, and they end up at the top of user screen
100: * at the end of the request.
101: */
102: public void handleEventTestSimpleForm() throws Exception {
103: // if form is not invalid, do not try to show form element values
104: // (error messages are added automatically to the messagecontext
105: // though, user will not be without feedback)
106: if (simpleForm.convertAndValidate()) {
107: // long way to check form element value ...
108: getMessageCtx().showInfoMessage(
109: "Checkbox value is: "
110: + ((FormElement) simpleForm
111: .getElement("checkbox1")).getData()
112: .getValue());
113: // and a shorter one
114: getMessageCtx()
115: .showInfoMessage(
116: "Textbox value is: "
117: + simpleForm
118: .getValueByFullName("textbox1"));
119: getMessageCtx()
120: .showInfoMessage(
121: "DateTime value is: "
122: + simpleForm
123: .getValueByFullName("dateTime"));
124: getMessageCtx().showInfoMessage(
125: "Time value is: "
126: + simpleForm.getValueByFullName("time"));
127: getMessageCtx().showInfoMessage(
128: "Date value is: "
129: + simpleForm.getValueByFullName("date"));
130: getMessageCtx().showInfoMessage(
131: "Number value is: "
132: + simpleForm.getValueByFullName("number"));
133: }
134: }
135: }
|