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.IOException;
018: import java.io.Writer;
019: import javax.servlet.jsp.JspException;
020: import javax.servlet.jsp.PageContext;
021: import org.apache.commons.lang.StringEscapeUtils;
022: import org.araneaframework.jsp.tag.PresentationTag;
023: import org.araneaframework.jsp.util.JspStringUtil;
024: import org.araneaframework.jsp.util.JspUtil;
025: import org.araneaframework.jsp.util.JspWidgetUtil;
026: import org.araneaframework.uilib.form.FormElement;
027: import org.araneaframework.uilib.form.FormWidget;
028:
029: /**
030: * Standard simple label tag.
031: *
032: * @author Oleg Mürk
033: *
034: * @jsp.tag
035: * name = "simpleLabel"
036: * body-content = "JSP"
037: * description = "Represents localizable label."
038: */
039: public class FormSimpleLabelHtmlTag extends PresentationTag {
040: /** @since 1.1 */
041: public static final String LABEL_SPAN_PREFIX = "label-";
042:
043: public final static String ASTERISK_STYLE = "aranea-label-asterisk";
044:
045: protected boolean showColon = true;
046: protected String labelId;
047: protected boolean mandatory = false;
048: protected String forElementId;
049: protected String accessKeyId;
050:
051: protected int doStartTag(Writer out) throws Exception {
052: super .doStartTag(out);
053:
054: if (accessKeyId == null)
055: accessKeyId = labelId + ".access-key";
056: String accessKey = JspUtil.getResourceStringOrNull(pageContext,
057: accessKeyId);
058: writeLabel(out,
059: JspUtil.getResourceString(pageContext, labelId),
060: mandatory, getStyleClass(), forElementId, pageContext,
061: showColon, accessKey, getStyle());
062:
063: return EVAL_BODY_INCLUDE;
064: }
065:
066: /* ***********************************************************************************
067: * Tag attributes
068: * ***********************************************************************************/
069:
070: /**
071: * @jsp.attribute
072: * type = "java.lang.String"
073: * required = "true"
074: * description = "Label id."
075: */
076: public void setLabelId(String labelId) throws JspException {
077: this .labelId = (String) evaluateNotNull("labelId", labelId,
078: String.class);
079: }
080:
081: /**
082: * @jsp.attribute
083: * type = "java.lang.String"
084: * required = "false"
085: * description = "Whether the label should display the asterisk, <code>false<code> by default."
086: */
087: public void setShowMandatory(String mandatory) throws JspException {
088: this .mandatory = ((Boolean) (evaluateNotNull("mandatory",
089: mandatory, Boolean.class))).booleanValue();
090: }
091:
092: /**
093: * @jsp.attribute
094: * type = "java.lang.String"
095: * required = "false"
096: * description = "Whether a colon (":") is shown after the label."
097: */
098: public void setShowColon(String showColon) throws JspException {
099: this .showColon = ((Boolean) (evaluateNotNull("showColumn",
100: showColon, Boolean.class))).booleanValue();
101: }
102:
103: /**
104: * @jsp.attribute
105: * type = "java.lang.String"
106: * required = "false"
107: * description = "ID of the form element for which the label is created"
108: */
109: public void setFor(String elementName) throws JspException {
110: this .forElementId = (String) evaluate("for", elementName,
111: String.class);
112: }
113:
114: /**
115: * Sets the resource id containing the access key for this label.
116: * This attribute is optional, if it is not specified, the resource id is constructed as
117: * <label-id>.access-key.
118: * When this resource exists and contains a single character, this character is used as an
119: * access key for the label. Otherwise no access key is used.
120: */
121: public void setAccessKeyId(String accessKeyId) throws JspException {
122: this .accessKeyId = (String) evaluate("accessKeyId",
123: accessKeyId, String.class);
124: }
125:
126: /* ***********************************************************************************
127: * STATIC label writing functions
128: * ***********************************************************************************/
129:
130: /**
131: * @deprecated Use {@link #writeLabel(Writer,String,boolean,String,String,PageContext,boolean,String,String)} instead
132: */
133: public static void writeLabel(Writer out, String label,
134: boolean mandatory, String styleClass, String formElementId,
135: PageContext pageContext, boolean showColon, String accessKey)
136: throws Exception {
137: writeLabel(out, label, mandatory, styleClass, formElementId,
138: pageContext, showColon, accessKey, null);
139: }
140:
141: public static void writeLabel(Writer out, String label,
142: boolean mandatory, String styleClass, String formElementId,
143: PageContext pageContext, boolean showColon,
144: String accessKey, String style) throws Exception {
145:
146: // Allow accessKey only if it is one-character long
147: if (accessKey != null && accessKey.length() != 1)
148: accessKey = null;
149:
150: String fullFormElementId = null; // Need this for the "for" attribute of the <label> tag
151:
152: if (formElementId != null) {
153: // Find surrounding form's id
154: String formId = (String) JspUtil.requireContextEntry(
155: pageContext, FormTag.FORM_FULL_ID_KEY);
156: // XXX: even if formElementContext JS should be invoked when writing label, formelement should not be marked as present
157: BaseFormElementHtmlTag.writeFormElementContextOpen(out,
158: formId, formElementId, false, pageContext,
159: LABEL_SPAN_PREFIX);
160: fullFormElementId = formId + "." + formElementId;
161: }
162:
163: if (mandatory) {
164: JspUtil.writeOpenStartTag(out, "span");
165: JspUtil.writeAttribute(out, "class", ASTERISK_STYLE);
166: JspUtil.writeCloseStartTag_SS(out);
167: out.write("* ");
168: JspUtil.writeEndTag_SS(out, "span");
169: }
170:
171: // Write <span class=..>
172: JspUtil.writeOpenStartTag(out, "span");
173: JspUtil.writeAttribute(out, "class", styleClass);
174: JspUtil.writeAttribute(out, "style", style);
175: JspUtil.writeCloseStartTag_SS(out);
176:
177: // Write <label ...>
178: JspUtil.writeOpenStartTag(out, "label");
179: if (fullFormElementId != null)
180: JspUtil.writeAttribute(out, "for", fullFormElementId);
181: if (accessKey != null)
182: JspUtil.writeAttribute(out, "accesskey", accessKey);
183: JspUtil.writeCloseStartTag_SS(out);
184:
185: // Write label itself, underlining the accesskey if there is one
186: String escapedLabel = StringEscapeUtils.escapeHtml(label);
187: out.write(JspStringUtil.underlineAccessKey(escapedLabel,
188: accessKey));
189: if (showColon)
190: out.write(":");
191:
192: // Close </label></span>
193: JspUtil.writeEndTag_SS(out, "label");
194: JspUtil.writeEndTag(out, "span");
195:
196: if (formElementId != null) {
197: BaseFormElementHtmlTag.writeFormElementContextClose(out);
198: }
199:
200: if (fullFormElementId != null) {
201: FormWidget formWidget = (FormWidget) JspUtil
202: .requireContextEntry(pageContext, FormTag.FORM_KEY);
203: FormElement f = (FormElement) JspWidgetUtil
204: .traverseToSubWidget(formWidget, formElementId);
205: BaseFormElementHtmlTag.writeFormElementValidityMarkers(out,
206: f.isValid(), LABEL_SPAN_PREFIX + fullFormElementId);
207: }
208: }
209:
210: public static void writeSelectLabel(Writer out, String label,
211: String styleClass) throws JspException, IOException {
212: JspUtil.writeOpenStartTag(out, "span");
213: JspUtil.writeAttribute(out, "class", styleClass);
214: JspUtil.writeCloseStartTag_SS(out);
215: JspUtil.writeEscaped(out, label);
216: JspUtil.writeEndTag(out, "span");
217: }
218: }
|