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.jsp.util;
016:
017: import java.util.Collections;
018: import java.util.HashMap;
019: import java.util.Iterator;
020: import java.util.Map;
021: import org.araneaframework.jsp.support.FormElementViewSelector;
022: import org.araneaframework.jsp.support.TagAttr;
023: import org.araneaframework.jsp.tag.uilib.form.element.AutomaticTagFormElementTag;
024: import org.araneaframework.uilib.form.FormElement;
025: import org.araneaframework.uilib.form.FormWidget;
026: import org.araneaframework.uilib.form.GenericFormElement;
027: import org.araneaframework.uilib.form.control.ButtonControl;
028: import org.araneaframework.uilib.form.control.CheckboxControl;
029: import org.araneaframework.uilib.form.control.DateControl;
030: import org.araneaframework.uilib.form.control.DateTimeControl;
031: import org.araneaframework.uilib.form.control.FileUploadControl;
032: import org.araneaframework.uilib.form.control.FloatControl;
033: import org.araneaframework.uilib.form.control.MultiSelectControl;
034: import org.araneaframework.uilib.form.control.NumberControl;
035: import org.araneaframework.uilib.form.control.SelectControl;
036: import org.araneaframework.uilib.form.control.TextControl;
037: import org.araneaframework.uilib.form.control.TextareaControl;
038: import org.araneaframework.uilib.form.control.TimeControl;
039:
040: /**
041: * Utility class for setting {@link FormElement} properties that are needed for
042: * rendering them in Aranea JSP with
043: * {@link org.araneaframework.jsp.tag.uilib.form.element.AutomaticTagFormElementTag}.
044: *
045: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
046: */
047: public class AutomaticFormElementUtil {
048: public static final Map CONTROLS_TO_EDITABLE_TAGS = new HashMap();
049: public static final Map CONTROLS_TO_DISPLAY_TAGS = new HashMap();
050:
051: static {
052: CONTROLS_TO_EDITABLE_TAGS.put(ButtonControl.class, "button");
053:
054: CONTROLS_TO_EDITABLE_TAGS
055: .put(CheckboxControl.class, "checkbox");
056:
057: CONTROLS_TO_EDITABLE_TAGS.put(FileUploadControl.class,
058: "fileUpload");
059:
060: CONTROLS_TO_EDITABLE_TAGS.put(MultiSelectControl.class,
061: "multiSelect");
062: CONTROLS_TO_EDITABLE_TAGS.put(SelectControl.class, "select");
063:
064: CONTROLS_TO_EDITABLE_TAGS
065: .put(TextareaControl.class, "textarea");
066: CONTROLS_TO_EDITABLE_TAGS.put(TextControl.class, "textInput");
067: CONTROLS_TO_EDITABLE_TAGS.put(NumberControl.class,
068: "numberInput");
069: CONTROLS_TO_EDITABLE_TAGS.put(FloatControl.class, "floatInput");
070:
071: CONTROLS_TO_EDITABLE_TAGS.put(DateControl.class, "dateInput");
072: CONTROLS_TO_EDITABLE_TAGS.put(DateTimeControl.class,
073: "dateTimeInput");
074: CONTROLS_TO_EDITABLE_TAGS.put(TimeControl.class, "timeInput");
075:
076: //-------------------------------------------------------------
077: CONTROLS_TO_DISPLAY_TAGS.put(CheckboxControl.class,
078: "checkboxDisplay");
079:
080: CONTROLS_TO_DISPLAY_TAGS.put(MultiSelectControl.class,
081: "multiSelectDisplay");
082: CONTROLS_TO_DISPLAY_TAGS.put(SelectControl.class,
083: "selectDisplay");
084:
085: CONTROLS_TO_DISPLAY_TAGS.put(TextareaControl.class,
086: "textareaDisplay");
087: CONTROLS_TO_DISPLAY_TAGS.put(TextControl.class,
088: "textInputDisplay");
089: CONTROLS_TO_DISPLAY_TAGS.put(NumberControl.class,
090: "numberInputDisplay");
091: CONTROLS_TO_DISPLAY_TAGS.put(FloatControl.class,
092: "floatInputDisplay");
093:
094: CONTROLS_TO_DISPLAY_TAGS.put(DateControl.class,
095: "dateInputDisplay");
096: CONTROLS_TO_DISPLAY_TAGS.put(DateTimeControl.class,
097: "dateTimeInputDisplay");
098: CONTROLS_TO_DISPLAY_TAGS.put(TimeControl.class,
099: "timeInputDisplay");
100: }
101:
102: private AutomaticFormElementUtil() {
103: }
104:
105: /**
106: * Assigns a {@link FormElementViewSelector} <code>viewSelector</code> to the specified {@link FormElement}.
107: */
108: public static void setFormElementViewSelector(
109: FormElement formElement,
110: FormElementViewSelector viewSelector) {
111: formElement
112: .setProperty(
113: FormElementViewSelector.FORM_ELEMENT_VIEW_SELECTOR_PROPERTY,
114: viewSelector);
115: }
116:
117: /**
118: * Assigns a {@link FormElementViewSelector} <code>viewSelector</code> to the {@link FormElement} that
119: * belongs to <code>form</code> and has id <code>formElementId</code>.
120: */
121: public static void setFormElementViewSelector(FormWidget form,
122: String formElementId, FormElementViewSelector viewSelector) {
123: setFormElementViewSelector(form
124: .getElementByFullName(formElementId), viewSelector);
125: }
126:
127: /**
128: * Associates {@link FormElement} with a JSP tag that {@link AutomaticTagFormElementTag} will use
129: * for actual rendering of the {@link FormElement}.
130: *
131: * @param formElement
132: * @param tagName name of the tag, without any namespace
133: * @param tagAttributes Map <attributeName, attributeValue>
134: */
135: public static void setFormElementTag(FormElement formElement,
136: String tagName, Map tagAttributes) {
137: setFormElementViewSelector(formElement,
138: new FormElementViewSelector(tagName, tagAttributes));
139: }
140:
141: /**
142: * Assigns a tag to the specified element.
143: *
144: * @param form parent form or composite element.
145: * @param formElementId id of the simple element.
146: * @param tagName name of the tag that will be used to render the element.
147: * @param tagAttributes tag custom attributes.
148: */
149: public static void setFormElementTag(FormWidget form,
150: String formElementId, String tagName, Map tagAttributes) {
151: setFormElementViewSelector(form, formElementId,
152: new FormElementViewSelector(tagName, tagAttributes));
153: }
154:
155: /**
156: * Assigns an attributeless tag to the specified {@link FormElement}.
157: *
158: * @param formElement id of the simple element.
159: * @param tagName name of the tag that will be used to render the element, without any namespace.
160: */
161: public static void setFormElementTag(FormElement formElement,
162: String tagName) {
163: setFormElementTag(formElement, tagName, Collections.EMPTY_MAP);
164: }
165:
166: /**
167: * Assigns an attributeless tag to the specified to the {@link FormElement} that
168: * belongs to <code>form</code> and has id <code>formElementId</code>.
169: *
170: * @param form parent form or composite element.
171: * @param formElementId id of the simple element.
172: * @param tagName name of the tag that will be used to render the element, without any namespace.
173: */
174: public static void setFormElementTag(FormWidget form,
175: String formElementId, String tagName) {
176: setFormElementTag(form.getElementByFullName(formElementId),
177: tagName);
178: }
179:
180: /**
181: * Assigns specified tag and attributes to the {@link FormElement}.
182: *
183: * @param formElement
184: * @param tagName name of the tag that will be used to render the element, without any namespace
185: * @param attributePairs tag attributes.
186: */
187: public static void setFormElementTag(FormElement formElement,
188: String tagName, TagAttr[] attributePairs) {
189: Map attributes = new HashMap(attributePairs.length);
190:
191: for (int i = 0; i < attributePairs.length; i++)
192: attributes.put(attributePairs[i].getName(),
193: attributePairs[i].getValue());
194:
195: setFormElementTag(formElement, tagName, attributes);
196: }
197:
198: /**
199: * Assigns tag with specified attributes to the specified to the {@link FormElement} that
200: * belongs to <code>form</code> and has id <code>formElementId</code>.
201: *
202: * @param form parent {@link FormWidget}
203: * @param formElementId id of the form element
204: * @param tagName name of the tag that will be used to render the element, without any namespace
205: * @param attributePairs tag attributes.
206: */
207: public static void setFormElementTag(FormWidget form,
208: String formElementId, String tagName,
209: TagAttr[] attributePairs) {
210: setFormElementTag(form.getElementByFullName(formElementId),
211: tagName, attributePairs);
212: }
213:
214: /**
215: * Assigns the default editable (aka input) tags to given form element.
216: * @since 1.0.7
217: */
218: public static void setFormElementDefaultEditableTag(
219: FormElement element) {
220: setFormElementTag(element, (String) CONTROLS_TO_EDITABLE_TAGS
221: .get(element.getControl().getClass()));
222: }
223:
224: /**
225: * Assigns the default editable (aka input) tags to given form element.
226: * @since 1.0.7
227: */
228: public static void setFormElementDefaultDisplayTag(
229: FormElement element) {
230: setFormElementTag(element, (String) CONTROLS_TO_DISPLAY_TAGS
231: .get(element.getControl().getClass()));
232: }
233:
234: /**
235: * Assigns the default editable (aka input) tags to all of the elements of the form.
236: * @param form parent form or composite element.
237: */
238: public static void setFormElementDefaultEditableTags(FormWidget form) {
239: for (Iterator i = form.getElements().entrySet().iterator(); i
240: .hasNext();) {
241: Map.Entry entry = (Map.Entry) i.next();
242: GenericFormElement element = (GenericFormElement) entry
243: .getValue();
244:
245: if (element instanceof FormWidget)
246: setFormElementDefaultEditableTags((FormWidget) element);
247: else if (element instanceof FormElement) {
248: setFormElementDefaultEditableTag((FormElement) element);
249: }
250: }
251: }
252:
253: /**
254: * Assigns the default display (aka read-only) tags to all of the elements of the form.
255: * @param form parent form or composite element.
256: */
257: public static void setFormElementDefaultDisplayTags(FormWidget form) {
258: for (Iterator i = form.getElements().entrySet().iterator(); i
259: .hasNext();) {
260: Map.Entry entry = (Map.Entry) i.next();
261: GenericFormElement element = (GenericFormElement) entry
262: .getValue();
263:
264: if (element instanceof FormWidget)
265: setFormElementDefaultDisplayTags((FormWidget) element);
266: else if (element instanceof FormElement) {
267: setFormElementDefaultDisplayTag((FormElement) element);
268: }
269: }
270: }
271: }
|