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.util.ArrayList;
018: import java.util.Arrays;
019: import java.util.Collection;
020: import java.util.Iterator;
021: import java.util.List;
022: import org.araneaframework.example.main.TemplateBaseWidget;
023: import org.araneaframework.uilib.event.OnChangeEventListener;
024: import org.araneaframework.uilib.form.FormWidget;
025: import org.araneaframework.uilib.form.control.DisplayControl;
026: import org.araneaframework.uilib.form.control.MultiSelectControl;
027: import org.araneaframework.uilib.form.control.SelectControl;
028: import org.araneaframework.uilib.form.data.StringData;
029: import org.araneaframework.uilib.form.data.StringListData;
030: import org.araneaframework.uilib.support.DisplayItem;
031:
032: /**
033: * @author Taimo Peelo (taimo@araneaframework.org)
034: */
035: public class DemoComplexForm extends TemplateBaseWidget {
036: private static final long serialVersionUID = 1L;
037: /* Different controls and widgets we want to be accessible all the time are
038: made instance variables by convention. */
039: private FormWidget complexForm;
040: /* SelectControl - control which provides various selections from which one must be picked. */
041: private SelectControl beastSelectionControl;
042: /* MultiSelectControl - provides various selections from which zero to many can be picked */
043: private MultiSelectControl concreteBeastMultiSelectionControl;
044:
045: protected void init() throws Exception {
046: setViewSelector("demo/demoComplexForm");
047: putViewData("formLabel", "Complex_Form");
048:
049: beastSelectionControl = new SelectControl();
050: /* SelectControls can be added DisplayItems, one by one ... */
051: beastSelectionControl.addItem(new DisplayItem(null,
052: t("select.choose")));
053: /* or whole collections of value objects, which must have getters for specified value
054: * and displayString fields (here, for sampleValue and sampleDisplayString). Note that
055: * both value and displayString must be of String class */
056: beastSelectionControl.addFromBeanCollection(getSelectItems(),
057: "sampleValue", "sampleDisplayString");
058:
059: /* Adds the onChange event listener to selectControl */
060: beastSelectionControl
061: .addOnChangeEventListener(new OnChangeEventListener() {
062: private static final long serialVersionUID = 1L;
063:
064: public void onChange() throws Exception {
065: /* Form must be converted before new values can be read from form.
066: As we want to be sure that entered data is valid (no random strings
067: where numbers are expected, length and content constraints are met)
068: we usually also validate data before using it for anything. */
069: if (complexForm.convertAndValidate()) {
070: // DemoComplexForm.this.getMessageCtx().showInfoMessage("Value in multiselect has changed to " + (String)beastSelectionControl.getRawValue() + ".");
071: // get the value from control (aka what beast was selected).
072: String selectedBeast = (String) beastSelectionControl
073: .getRawValue();
074:
075: // if no beast is selected in our select control, we remove the other
076: // elements from form that depend directly on selection being made -
077: // the controls providing possibily for more specific beast selection.
078: if (selectedBeast == null) {
079: complexForm
080: .removeElement("concreteBeastControl");
081: complexForm
082: .removeElement("selectedBeastDesc");
083: return;
084: }
085:
086: // create the multiselectcontrol allowing selection of some beasts of selected type.
087: concreteBeastMultiSelectionControl = new MultiSelectControl();
088: for (Iterator i = getMultiSelectItems(
089: selectedBeast).iterator(); i
090: .hasNext();) {
091: String current = (String) i.next();
092: concreteBeastMultiSelectionControl
093: .addItem(new DisplayItem(
094: current, current));
095: }
096:
097: // finally add both beast group description and beast selection control to this widget.
098: complexForm.addElement(
099: "concreteBeastControl", "#"
100: + t("common.Choose") + " "
101: + selectedBeast,
102: concreteBeastMultiSelectionControl,
103: new StringListData(), false);
104: complexForm.addElement("selectedBeastDesc",
105: "#" + t("common.Description"),
106: new DisplayControl(),
107: new StringData(), false);
108: // if not dealing with beanforms, form element values are typically set this way
109: complexForm.setValueByFullName(
110: "selectedBeastDesc",
111: new SelectItem(selectedBeast)
112: .getDescription());
113: }
114: }
115: });
116:
117: complexForm = new FormWidget();
118: complexForm.addElement("beastSelection", "natures.beasts",
119: beastSelectionControl, new StringData(), false);
120:
121: addWidget("complexForm", complexForm);
122: }
123:
124: // HELPER METHODS AND CLASSES
125: private Collection getSelectItems() {
126: List list = new ArrayList();
127: // note that SelectItem is just an inner class and not anything special
128: list.add(new SelectItem(" Bird "));
129: list.add(new SelectItem(" Animal "));
130: list.add(new SelectItem(" Fish "));
131: list.add(new SelectItem(" Dragon "));
132: return list;
133: }
134:
135: private static final String[] birds = new String[] { "Chicken",
136: "Goose", "Duck", "Swan" };
137: private static final String[] animals = new String[] { "Piglet",
138: "Pooh", "Tiger", "Cangaroo" };
139: private static final String[] fishes = new String[] { "Willy",
140: "Nemo", "Dory", "Marlin" };
141: private static final String[] dragons = new String[] { "Smaug",
142: "Chrysophylax", "Devon & Cornwall" };
143:
144: private Collection getMultiSelectItems(String selectItem) {
145: List result = new ArrayList();
146: if (selectItem.equals("Bird")) {
147: result.addAll(Arrays.asList(birds));
148: } else if (selectItem.equals("Animal")) {
149: result.addAll(Arrays.asList(animals));
150: } else if (selectItem.equals("Fish")) {
151: result.addAll(Arrays.asList(fishes));
152: } else if (selectItem.equals("Dragon")) {
153: result.addAll(Arrays.asList(dragons));
154: }
155:
156: return result;
157: }
158:
159: public static class SelectItem {
160: public String sampleValue;
161: public String sampleDisplayString;
162:
163: public SelectItem(String value) {
164: this .sampleValue = value.trim();
165: this .sampleDisplayString = "> " + value + " <";
166: }
167:
168: public String getSampleDisplayString() {
169: return sampleDisplayString;
170: }
171:
172: public void setSampleDisplayString(String sampleDisplayString) {
173: this .sampleDisplayString = sampleDisplayString;
174: }
175:
176: public String getSampleValue() {
177: return sampleValue;
178: }
179:
180: public void setSampleValue(String sampleValue) {
181: this .sampleValue = sampleValue;
182: }
183:
184: public String getDescription() {
185: String result = "";
186: if (sampleValue.equals("Bird")) {
187: result = "Birds are bipedal, warm-blooded, oviparous vertebrates characterized primarily by feathers, forelimbs modified as wings, and hollow bones.";
188: } else if (sampleValue.equals("Animal")) {
189: result = "Animals are a major group of organisms, classified as the kingdom Animalia or Metazoa. In general they are multicellular, capable of locomotion and responsive to their environment, and feed by consuming other organisms. Their body plan becomes fixed as they develop, usually early on in their development as embryos, although some undergo a process of metamorphosis later on.";
190: } else if (sampleValue.equals("Fish")) {
191: result = "A fish is a poikilothermic (cold-blooded) water-dwelling vertebrate with gills.";
192: } else if (sampleValue.equals("Dragon")) {
193: result = "A dragon is a legendary creature, typically depicted as a large and powerful serpent or other reptile, with magical or spiritual qualities.";
194: }
195: return result;
196: }
197: }
198: }
|