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.tag.uilib.form.element;
016:
017: import java.io.Writer;
018: import java.lang.reflect.Method;
019: import java.util.Iterator;
020: import java.util.Map;
021: import javax.servlet.jsp.JspException;
022: import org.araneaframework.core.util.ClassLoaderUtil;
023: import org.araneaframework.http.JspContext;
024: import org.araneaframework.jsp.exception.MissingFormElementIdAraneaJspException;
025: import org.araneaframework.jsp.support.FormElementViewSelector;
026: import org.araneaframework.jsp.support.TagInfo;
027: import org.araneaframework.jsp.tag.BaseTag;
028: import org.araneaframework.jsp.tag.uilib.form.FormElementTag;
029: import org.araneaframework.jsp.tag.uilib.form.FormElementTagInterface;
030: import org.araneaframework.jsp.tag.uilib.form.FormTag;
031: import org.araneaframework.jsp.util.JspWidgetUtil;
032: import org.araneaframework.uilib.form.Control;
033: import org.araneaframework.uilib.form.FormElement;
034: import org.araneaframework.uilib.form.FormWidget;
035:
036: /**
037: * Automatic form element tag. Chooses the tag to draw the control based on the information supplied in the
038: * component.
039: *
040: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
041: *
042: * @jsp.tag
043: * name = "automaticFormElement"
044: * body-content = "JSP"
045: * description = "Automatic form element which dynamically draws correct form element tag."
046: */
047: public class AutomaticTagFormElementTag extends BaseTag {
048: protected String derivedId;
049: private String id;
050: protected String events;
051: protected String validateOnEvent;
052: protected String tabindex;
053: protected String styleClass;
054: protected String updateRegions;
055: protected String globalUpdateRegions;
056:
057: protected FormWidget.ViewModel formViewModel;
058:
059: protected FormElement.ViewModel formElementViewModel;
060: protected Control.ViewModel controlViewModel;
061:
062: protected FormElementTagInterface controlTag;
063:
064: protected int doStartTag(Writer out) throws Exception {
065: super .doStartTag(out);
066:
067: formViewModel = (FormWidget.ViewModel) requireContextEntry(FormTag.FORM_VIEW_MODEL_KEY);
068: FormWidget form = (FormWidget) requireContextEntry(FormTag.FORM_KEY);
069:
070: //In case the tag is in formElement tag
071: derivedId = id;
072: if (derivedId == null
073: && getContextEntry(FormElementTag.ID_KEY) != null)
074: derivedId = (String) getContextEntry(FormElementTag.ID_KEY);
075: if (derivedId == null)
076: throw new MissingFormElementIdAraneaJspException(this );
077: formElementViewModel = (FormElement.ViewModel) JspWidgetUtil
078: .traverseToSubWidget(form, derivedId)._getViewable()
079: .getViewModel();
080:
081: // Get control
082: controlViewModel = formElementViewModel.getControl();
083:
084: FormElementViewSelector viewSelector = (FormElementViewSelector) formElementViewModel
085: .getProperties()
086: .get(
087: FormElementViewSelector.FORM_ELEMENT_VIEW_SELECTOR_PROPERTY);
088:
089: if (viewSelector == null)
090: throw new JspException(
091: "The form element view selector was not passed!.");
092:
093: JspContext config = (JspContext) getEnvironment().requireEntry(
094: JspContext.class);
095: Map tagMapping = config.getTagMapping(viewSelector.getUri());
096:
097: if (tagMapping == null)
098: throw new JspException("The tag mapping was not found!.");
099:
100: TagInfo tagInfo = (TagInfo) tagMapping.get(viewSelector
101: .getTag());
102:
103: if (tagInfo == null)
104: throw new JspException(
105: "Unexistant tag was passed to form element view selector!.");
106:
107: controlTag = (FormElementTagInterface) ClassLoaderUtil
108: .loadClass(tagInfo.getTagClassName()).newInstance();
109: Class tagClass = controlTag.getClass();
110:
111: registerSubtag(controlTag);
112:
113: initTagAttributes(tagClass, controlTag, viewSelector
114: .getAttributes());
115:
116: controlTag.setId(derivedId);
117: if (events != null)
118: controlTag.setEvents(events);
119: if (validateOnEvent != null)
120: controlTag.setValidateOnEvent(validateOnEvent);
121: if (styleClass != null)
122: controlTag.setStyleClass(styleClass);
123: if (tabindex != null)
124: controlTag.setTabindex(tabindex);
125: if (updateRegions != null)
126: controlTag.setUpdateRegions(updateRegions);
127: if (globalUpdateRegions != null)
128: controlTag.setGlobalUpdateRegions(globalUpdateRegions);
129:
130: executeStartSubtag(controlTag);
131:
132: return EVAL_BODY_INCLUDE;
133: }
134:
135: protected int doEndTag(Writer out) throws Exception {
136: executeEndSubtag(controlTag);
137: unregisterSubtag(controlTag);
138:
139: return super .doEndTag(out);
140: }
141:
142: /* ***********************************************************************************
143: * Tag attributes
144: * ***********************************************************************************/
145:
146: /**
147: * @jsp.attribute
148: * type = "java.lang.String"
149: * required = "false"
150: * description = "Element id, can also be inherited."
151: */
152: public void setId(String id) throws JspException {
153: this .id = (String) evaluateNotNull("id", id, String.class);
154: }
155:
156: /**
157: * @jsp.attribute
158: * type = "java.lang.String"
159: * required = "false"
160: * description = "Whether the element will send the events that are registered by server-side (by default "true")."
161: */
162: public void setEvents(String events) throws JspException {
163: this .events = events;
164: }
165:
166: /**
167: * @jsp.attribute
168: * type = "java.lang.String"
169: * required = "false"
170: * description = "Whether the form will be validated on the client-side when the element generates an event (by default "false")."
171: */
172: public void setValidateOnEvent(String validateOnEvent)
173: throws JspException {
174: this .validateOnEvent = validateOnEvent;
175: }
176:
177: /**
178: * @jsp.attribute
179: * type = "java.lang.String"
180: * required = "false"
181: * description = "Element tabindex."
182: */
183: public void setTabindex(String tabindex) throws JspException {
184: this .tabindex = tabindex;
185: }
186:
187: /**
188: * @jsp.attribute
189: * type = "java.lang.String"
190: * required = "false"
191: * description = "CSS class without prefix of the dynamically selected tag."
192: */
193: public void setStyleClass(String styleClass) throws JspException {
194: this .styleClass = styleClass;
195: }
196:
197: /**
198: * @jsp.attribute
199: * type = "java.lang.String"
200: * required = "false"
201: * description = "Enumerates the regions of markup to be updated in this widget scope. Please see <code><ui:updateRegion></code> for details."
202: */
203: public void setUpdateRegions(String updateRegions)
204: throws JspException {
205: this .updateRegions = updateRegions;
206: }
207:
208: /**
209: * @jsp.attribute
210: * type = "java.lang.String"
211: * required = "false"
212: * description = "Enumerates the regions of markup to be updated globally. Please see <code><ui:updateRegion></code> for details."
213: */
214: public void setGlobalUpdateRegions(String globalUpdateRegions)
215: throws JspException {
216: this .globalUpdateRegions = globalUpdateRegions;
217: }
218:
219: protected void initTagAttributes(Class tagClass, Object tag,
220: Map attributes) throws Exception {
221: for (Iterator i = attributes.entrySet().iterator(); i.hasNext();) {
222: Map.Entry entry = (Map.Entry) i.next();
223:
224: String attributeName = (String) entry.getKey();
225: String setterMethodName = "set"
226: + attributeName.substring(0, 1).toUpperCase()
227: + attributeName.substring(1);
228:
229: Method setter = tagClass.getMethod(setterMethodName,
230: new Class[] { entry.getValue().getClass() });
231: setter.invoke(tag, new Object[] { entry.getValue() });
232: }
233: }
234: }
|