Author: pbenedict Date: Sat Apr 7 22:45:54 2007 New Revision: 526528 URL: http://svn.apache.org/viewvc?view=rev&rev=526528 Log: STR-1305: Adding label tag
Added: struts/struts1/trunk/taglib/src/main/java/org/apache/struts/taglib/html/LabelTag.java (with props) Modified: struts/struts1/trunk/taglib/src/main/resources/META-INF/tld/struts-html.tld Added: struts/struts1/trunk/taglib/src/main/java/org/apache/struts/taglib/html/LabelTag.java URL: http://svn.apache.org/viewvc/struts/struts1/trunk/taglib/src/main/java/org/apache/struts/taglib/html/LabelTag.java?view=auto&rev=526528 ============================================================================== --- struts/struts1/trunk/taglib/src/main/java/org/apache/struts/taglib/html/LabelTag.java (added) +++ struts/struts1/trunk/taglib/src/main/java/org/apache/struts/taglib/html/LabelTag.java Sat Apr 7 22:45:54 2007 @@ -0,0 +1,203 @@ +/* + * $Id$ + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.struts.taglib.html; + +import org.apache.struts.taglib.TagUtils; + +import javax.servlet.jsp.JspException; + +/** + * Renders an HTML LABEL tag within the Struts framework. + * + * @version $Rev$ + * @since Struts 1.4 + */ +public class LabelTag extends BaseInputTag { + + // ----------------------------------------------------- Instance Variables + + protected String forId = null; + + protected String key = null; + + protected boolean required = false; + + /** + * The body content of this tag (if any). + */ + protected String text = null; + + // ------------------------------------------------------------- Properties + + public String getForId() { + return this.forId; + } + + public String getKey() { + return this.key; + } + + public boolean getRequired() { + return this.required; + } + + public void setForId(String forId) { + this.forId = forId; + } + + public void setKey(String key) { + this.key = key; + } + + public void setRequired(boolean required) { + this.required = required; + } + + // ----------------------------------------------------- Constructor + + public LabelTag() { + super(); + } + + // --------------------------------------------------------- Methods + + /** + * Render the beginning of the hyperlink. + * <p> + * Support for indexed property since Struts 1.1 + * + * @throws JspException if a JSP exception has occurred + */ + public int doStartTag() throws JspException { + // Evaluate the body of this tag + this.text = null; + + return (EVAL_BODY_BUFFERED); + } + + /** + * Save the associated label from the body content. + * + * @throws JspException if a JSP exception has occurred + */ + public int doAfterBody() throws JspException { + if (this.bodyContent != null) { + String value = this.bodyContent.getString().trim(); + if (value.length() > 0) { + this.text = value; + } + } + + return (SKIP_BODY); + } + + /** + * Render the end of the hyperlink. + * + * @throws JspException if a JSP exception has occurred + */ + public int doEndTag() throws JspException { + // Generate the opening element + StringBuffer results = new StringBuffer("<label"); + prepareAttribute(results, "accesskey", getAccesskey()); + prepareAttribute(results, "for", getForId() != null ? getForId() + : prepareName()); + prepareAttribute(results, "tabindex", getTabindex()); + prepareAttribute(results, "title", getTitle()); + results.append(prepareStyles()); + results.append(prepareEventHandlers()); + prepareFocusEvents(results); + prepareOtherAttributes(results); + results.append(">"); + + // Prepare the label value + this.value = message(this.text, this.key); + prepareValue(results); + + // End tag + results.append("</label>"); + TagUtils.getInstance().write(this.pageContext, results.toString()); + + return (EVAL_PAGE); + } + + /** + * Returns the CSS style class that indicates a "required" styling. If no + * styling is wanted, return <code>null</code>. + * + * @return the style class; can be <code>null</code> + * @see #prepareAttribute(StringBuffer, String, Object) + */ + protected String getRequiredStyleClass() { + return "required"; + } + + /** + * If this label is describes a required field, then the CSS style class + * attribute gets appended with the "required" style, if not null, which + * takes effect for both the normal and error style. + * + * @see #getRequiredStyleClass() + * @see #prepareValue(String) + */ + protected void prepareAttribute(StringBuffer handlers, String name, + Object value) { + + if ("class".equals(name) && this.required) { + String requiredStyleClass = getRequiredStyleClass(); + if (requiredStyleClass != null) { + value = (value != null) ? (value + " " + requiredStyleClass) + : requiredStyleClass; + } + } + super.prepareAttribute(handlers, name, value); + } + + /** + * Performs any pre-processing on the <code>value</code> property before + * printing it. The default behavior is to append an asterik inside a + * <code>span</code> tag with a CSS class attribute of + * [EMAIL PROTECTED] #getRequiredStyleClass()}, if the <code>required</code> + * property is set. + * + * @param handlers The StringBuffer that output will be appended to. + * @see #getRequiredStyleClass() + */ + protected void prepareValue(StringBuffer handlers) { + handlers.append(this.value); + if (this.required) { + handlers.append(" <span class=\""); + handlers.append(getRequiredStyleClass()); + handlers.append("\">*</span>"); + } + } + + /** + * Release any acquired resources. + */ + public void release() { + super.release(); + this.forId = null; + this.key = null; + this.required = false; + this.text = null; + } +} Propchange: struts/struts1/trunk/taglib/src/main/java/org/apache/struts/taglib/html/LabelTag.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: struts/struts1/trunk/taglib/src/main/java/org/apache/struts/taglib/html/LabelTag.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Modified: struts/struts1/trunk/taglib/src/main/resources/META-INF/tld/struts-html.tld URL: http://svn.apache.org/viewvc/struts/struts1/trunk/taglib/src/main/resources/META-INF/tld/struts-html.tld?view=diff&rev=526528&r1=526527&r2=526528 ============================================================================== --- struts/struts1/trunk/taglib/src/main/resources/META-INF/tld/struts-html.tld (original) +++ struts/struts1/trunk/taglib/src/main/resources/META-INF/tld/struts-html.tld Sat Apr 7 22:45:54 2007 @@ -4275,6 +4275,340 @@ </attribute> </tag> <tag> + <name>label</name> + <tag-class>org.apache.struts.taglib.html.LabelTag</tag-class> + <description> + <![CDATA[ + <p><strong>Render a Label</strong></p> + ]]> + </description> + <attribute> + <name>accesskey</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + <p>The keyboard character used to move focus immediately to this + element.</p> + ]]> + </description> + </attribute> + <attribute> + <name>bundle</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + The servlet context attributes key for the MessageResources + instance to use when printing the label value. If not + specified, defaults to the application resources configured + for our action servlet. + ]]> + </description> + </attribute> + <attribute> + <name>dir</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + <p>The direction for weak/neutral text for this element.</p> + ]]> + </description> + </attribute> + <attribute> + <name>errorStyle</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + <p>CSS styles to be applied to this HTML element if + an error exists for it.</p> + + <p><strong>N.B.</strong> If present, this overrides the + <code>style</code> attribute in the event of an error.</p> + ]]> + </description> + </attribute> + <attribute> + <name>errorStyleClass</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + <p>CSS stylesheet class to be applied to this HTML element if + an error exists for it (renders a "class" attribute).</p> + + <p><strong>N.B.</strong> If present, this overrides the + <code>styleClass</code> attribute in the event of an error.</p> + ]]> + </description> + </attribute> + <attribute> + <name>errorStyleId</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + <p>Identifier to be assigned to this HTML element if + an error exists for it (renders an "id" attribute).</p> + + <p><strong>N.B.</strong> If present, this overrides the + <code>styleId</code> attribute in the event of an error.</p> + ]]> + </description> + </attribute> + <attribute> + <name>forId</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + <p>Identifier to be assigned to this HTML element (renders + an "id" attribute).</p> + + <p><strong>N.B.</strong> If present, the <code>errorStyleId</code> + overrides this attribute in the event of an error for the element.</p> + ]]> + </description> + </attribute> + <attribute> + <name>key</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + If specified, defines the message key to be looked up in + the resource bundle specified by <code>bundle</code> for + the value displayed to the user for this label. If not + specified, the value to be displayed is taken from the body + content of this tag. + ]]> + </description> + </attribute> + <attribute> + <name>lang</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + <p>The language code for this element.</p> + <dl><dt><b>Since:</b></dt> + <dd>Struts 1.3.6</dd></dl> + ]]> + </description> + </attribute> + <attribute> + <name>name</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + The attribute name of the bean whose properties are consulted + when rendering the current value of this input field. If not + specified, the bean associated with the form tag we are nested + within is utilized. + ]]> + </description> + </attribute> + <attribute> + <name>onblur</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + JavaScript event handler executed when this element loses input + focus. + ]]> + </description> + </attribute> + <attribute> + <name>onclick</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + JavaScript event handler executed when this element receives a + mouse click. + ]]> + </description> + </attribute> + <attribute> + <name>ondblclick</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + JavaScript event handler executed when this element receives a + mouse double click. + ]]> + </description> + </attribute> + <attribute> + <name>onfocus</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + JavaScript event handler executed when this element receives input + focus. + ]]> + </description> + </attribute> + <attribute> + <name>onkeydown</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + JavaScript event handler executed when this element has focus and a + key is depressed. + ]]> + </description> + </attribute> + <attribute> + <name>onkeypress</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + JavaScript event handler executed when this element has focus and a + key is depressed and released. + ]]> + </description> + </attribute> + <attribute> + <name>onkeyup</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + JavaScript event handler executed when this element has focus and a + key is released. + ]]> + </description> + </attribute> + <attribute> + <name>onmousedown</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + JavaScript event handler executed when this element is under the mouse + pointer and a mouse button is depressed. + ]]> + </description> + </attribute> + <attribute> + <name>onmousemove</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + JavaScript event handler executed when this element is under the + mouse pointer and the pointer is moved. + ]]> + </description> + </attribute> + <attribute> + <name>onmouseout</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + JavaScript event handler executed when this element was under the + mouse pointer but the pointer was moved outside the element. + ]]> + </description> + </attribute> + <attribute> + <name>onmouseover</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + JavaScript event handler executed when this element was not under + the mouse pointer but the pointer is moved inside the element. + ]]> + </description> + </attribute> + <attribute> + <name>onmouseup</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + JavaScript event handler executed when this element is under the + mouse pointer and a mouse button is released. + ]]> + </description> + </attribute> + <attribute> + <name>property</name> + <required>true</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + Name of this input field, and the name of the corresponding bean + property if value is not specified. The corresponding bean property + (if any) must be of type String. + ]]> + </description> + </attribute> + <attribute> + <name>required</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <type>boolean</type> + <description> + <![CDATA[ + <p>Specifies that this label represents a required field.</p> + ]]> + </description> + </attribute> + <attribute> + <name>style</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + <p>CSS styles to be applied to this HTML element.</p> + + <p><strong>N.B.</strong> If present, the <code>errorStyle</code> + overrides this attribute in the event of an error for the element.</p> + ]]> + </description> + </attribute> + <attribute> + <name>styleClass</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + <p>CSS stylesheet class to be applied to this HTML element + (renders a "class" attribute).</p> + + <p><strong>N.B.</strong> If present, the <code>errorStyleClass</code> + overrides this attribute in the event of an error for the element.</p> + ]]> + </description> + </attribute> + <attribute> + <name>styleId</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + <description> + <![CDATA[ + <p>Identifier to be assigned to this HTML element (renders + an "id" attribute).</p> + + <p><strong>N.B.</strong> If present, the <code>errorStyleId</code> + overrides this attribute in the event of an error for the element.</p> + ]]> + </description> + </attribute> + </tag> + <tag> <name>link</name> <tag-class>org.apache.struts.taglib.html.LinkTag</tag-class> <body-content>JSP</body-content>