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;
016:
017: import java.io.Writer;
018: import javax.servlet.jsp.JspException;
019: import org.araneaframework.jsp.exception.AraneaJspException;
020: import org.araneaframework.jsp.exception.MissingFormElementIdAraneaJspException;
021: import org.araneaframework.jsp.tag.PresentationTag;
022: import org.araneaframework.jsp.util.JspWidgetUtil;
023: import org.araneaframework.uilib.form.Control;
024: import org.araneaframework.uilib.form.FormElement;
025: import org.araneaframework.uilib.form.FormWidget;
026:
027: /**
028: * Base form element tag.
029: *
030: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
031: */
032: public class BaseFormElementDisplayTag extends PresentationTag
033: implements FormElementTagInterface {
034: protected FormWidget.ViewModel formViewModel;
035: protected FormElement.ViewModel formElementViewModel;
036: protected Control.ViewModel controlViewModel;
037:
038: private String id;
039: protected String derivedId;
040: protected String tabindex;
041: protected boolean events = true;
042: protected boolean validate = true;
043: protected boolean validateOnEvent = false;
044:
045: protected int doStartTag(Writer out) throws Exception {
046: super .doStartTag(out);
047:
048: formViewModel = (FormWidget.ViewModel) requireContextEntry(FormTag.FORM_VIEW_MODEL_KEY);
049: FormWidget form = (FormWidget) requireContextEntry(FormTag.FORM_KEY);
050:
051: // In case the tag is in formElement tag
052: derivedId = id;
053: if (derivedId == null
054: && getContextEntry(FormElementTag.ID_KEY) != null)
055: derivedId = (String) getContextEntry(FormElementTag.ID_KEY);
056:
057: if (derivedId == null)
058: throw new MissingFormElementIdAraneaJspException(this );
059:
060: // get control and formelement viewmodels
061: formElementViewModel = (FormElement.ViewModel) JspWidgetUtil
062: .traverseToSubWidget(form, derivedId)._getViewable()
063: .getViewModel();
064: controlViewModel = (formElementViewModel).getControl();
065:
066: return EVAL_BODY_INCLUDE;
067: }
068:
069: protected int doEndTag(Writer out) throws Exception {
070: super .doEndTag(out);
071: return EVAL_PAGE;
072: }
073:
074: /* ***********************************************************************************
075: * Tag attributes
076: * ***********************************************************************************/
077:
078: /**
079: * @jsp.attribute
080: * type = "java.lang.String"
081: * required = "false"
082: * description = "Element id, can also be inherited."
083: */
084: public void setId(String id) throws JspException {
085: this .id = (String) evaluateNotNull("id", id, String.class);
086: }
087:
088: public void setEvents(String events) throws JspException {
089: this .events = ((Boolean) evaluateNotNull("events", events,
090: Boolean.class)).booleanValue();
091: }
092:
093: public void setValidate(String validate) throws JspException {
094: this .validate = ((Boolean) evaluateNotNull("validate",
095: validate, Boolean.class)).booleanValue();
096: }
097:
098: public void setValidateOnEvent(String validateOnEvent)
099: throws JspException {
100: this .validateOnEvent = ((Boolean) evaluateNotNull(
101: "validateOnEvent", validateOnEvent, Boolean.class))
102: .booleanValue();
103: }
104:
105: public void setTabindex(String tabindex) throws JspException {
106: this .tabindex = (String) evaluateNotNull("tabindex", tabindex,
107: String.class);
108: }
109:
110: public void setUpdateRegions(String updateRegions)
111: throws JspException {
112: }
113:
114: public void setGlobalUpdateRegions(String globalUpdateRegions)
115: throws JspException {
116: }
117:
118: /* ***********************************************************************************
119: * Control type assertion method to be used by subclasses.
120: * ***********************************************************************************/
121:
122: /**
123: * Asserts that associated control is of given type. If the
124: * condition does not hold, throws exception.
125: */
126: protected void assertControlType(String type) throws JspException {
127: if (!controlViewModel.getControlType().equals(type))
128: throw new AraneaJspException("Control of type '" + type
129: + "' expected in form element '" + id
130: + "' instead of '"
131: + controlViewModel.getControlType() + "'");
132: }
133: }
|