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.MissingFormElementIdAraneaJspException;
020: import org.araneaframework.jsp.tag.PresentationTag;
021: import org.araneaframework.jsp.util.JspUtil;
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 label tag.
029: *
030: * @author Oleg Mürk
031: */
032: public class BaseFormElementLabelTag extends PresentationTag {
033: protected FormWidget.ViewModel formViewModel;
034:
035: protected FormElement.ViewModel formElementViewModel;
036: protected Control.ViewModel controlViewModel;
037: protected String localizedLabel;
038: protected String accessKeyId;
039: protected String derivedId;
040:
041: //Attributes
042:
043: protected String id;
044: protected boolean showMandatory = true;
045: protected boolean showColon = true;
046: protected String accessKey;
047:
048: protected int doStartTag(Writer out) throws Exception {
049: super .doStartTag(out);
050:
051: // Get form data
052: formViewModel = (FormWidget.ViewModel) requireContextEntry(FormTag.FORM_VIEW_MODEL_KEY);
053: FormWidget form = (FormWidget) requireContextEntry(FormTag.FORM_KEY);
054:
055: //In case the tag is in formElement tag
056: derivedId = id;
057: if (derivedId == null
058: && getContextEntry(FormElementTag.ID_KEY) != null)
059: derivedId = (String) getContextEntry(FormElementTag.ID_KEY);
060:
061: if (derivedId == null)
062: throw new MissingFormElementIdAraneaJspException(this );
063:
064: formElementViewModel = (FormElement.ViewModel) JspWidgetUtil
065: .traverseToSubWidget(form, derivedId)._getViewable()
066: .getViewModel();
067:
068: // Get control
069: controlViewModel = (formElementViewModel).getControl();
070: localizedLabel = JspUtil.getResourceString(pageContext,
071: formElementViewModel.getLabel());
072:
073: if (accessKeyId == null) {
074:
075: // If controlViewModel.getLabel() did not specify a resource, we
076: // assume that controlViewModel.getLabel() + ".access-key" will also
077: // _not_ specify a legal resource.
078: accessKey = JspUtil.getResourceStringOrNull(pageContext,
079: formElementViewModel.getLabel() + ".access-key");
080: } else {
081: accessKey = JspUtil.getResourceStringOrNull(pageContext,
082: accessKeyId);
083: }
084: if (accessKey != null && accessKey.length() != 1)
085: accessKey = null;
086:
087: // Continue
088: return EVAL_BODY_INCLUDE;
089: }
090:
091: /* ***********************************************************************************
092: * Tag attributes
093: * ***********************************************************************************/
094:
095: /**
096: * @jsp.attribute
097: * type = "java.lang.String"
098: * required = "false"
099: * description = "Element id."
100: */
101: public void setId(String id) throws JspException {
102: this .id = (String) evaluateNotNull("id", id, String.class);
103: }
104:
105: /**
106: * @jsp.attribute
107: * type = "java.lang.String"
108: * required = "false"
109: * description = "Whether an asterisk is shown when the element is mandatory."
110: */
111: public void setShowMandatory(String showMandatory)
112: throws JspException {
113: this .showMandatory = ((Boolean) (evaluateNotNull(
114: "showMandatory", showMandatory, Boolean.class)))
115: .booleanValue();
116: }
117:
118: /**
119: * @jsp.attribute
120: * type = "java.lang.String"
121: * required = "false"
122: * description = "Whether a colon (":") is draw after the label."
123: */
124: public void setShowColon(String showColon) throws JspException {
125: this .showColon = ((Boolean) (evaluateNotNull("showColumn",
126: showColon, Boolean.class))).booleanValue();
127: }
128:
129: /**
130: * Set the id of the resource containing the access key for this label.
131: * It may be null. In this case the default resource id is used:
132: * <label-id>.access-key
133: * (where label-id is the resource id containing the label for the element.
134: * if it exists).
135: * If such resource exists and specifies a single character, the
136: * tag outputs an additional accesskey attribute for the HTML a <label> element.
137: * If given resource does not exist, it's also ok. Noone will die.
138: */
139: public void setAccessKeyId(String accessKeyId) throws JspException {
140: this .accessKeyId = (String) evaluate("accessKeyId",
141: accessKeyId, String.class);
142: }
143: }
|