View Javadoc

1   /*
2    * $Id: CheckboxTag.java 376841 2006-02-10 21:01:28Z husted $
3    *
4    * Copyright 1999-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts.taglib.html;
19  
20  import org.apache.struts.taglib.TagUtils;
21  import org.apache.struts.util.MessageResources;
22  
23  import javax.servlet.jsp.JspException;
24  
25  /***
26   * Tag for input fields of type "checkbox".
27   *
28   * @version $Rev: 376841 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
29   *          $
30   */
31  public class CheckboxTag extends BaseHandlerTag {
32      // ----------------------------------------------------- Instance Variables
33  
34      /***
35       * The message resources for this package.
36       */
37      protected static MessageResources messages =
38          MessageResources.getMessageResources(Constants.Package
39              + ".LocalStrings");
40  
41      /***
42       * The name of the bean containing our underlying property.
43       */
44      protected String name = Constants.BEAN_KEY;
45  
46      /***
47       * The property name for this field.
48       */
49      protected String property = null;
50  
51      /***
52       * The body content of this tag (if any).
53       */
54      protected String text = null;
55  
56      /***
57       * The server value for this option.
58       */
59      protected String value = null;
60  
61      public String getName() {
62          return (this.name);
63      }
64  
65      public void setName(String name) {
66          this.name = name;
67      }
68  
69      // ------------------------------------------------------------- Properties
70  
71      /***
72       * Return the property name.
73       */
74      public String getProperty() {
75          return (this.property);
76      }
77  
78      /***
79       * Set the property name.
80       *
81       * @param property The new property name
82       */
83      public void setProperty(String property) {
84          this.property = property;
85      }
86  
87      /***
88       * Return the server value.
89       */
90      public String getValue() {
91          return (value == null) ? "on" : value;
92      }
93  
94      /***
95       * Set the server value.
96       *
97       * @param value The new server value
98       */
99      public void setValue(String value) {
100         this.value = value;
101     }
102 
103     // --------------------------------------------------------- Public Methods
104 
105     /***
106      * Generate the required input tag. <p> Support for indexed property since
107      * Struts 1.1
108      *
109      * @throws JspException if a JSP exception has occurred
110      */
111     public int doStartTag() throws JspException {
112         // Create an appropriate "input" element based on our parameters
113         StringBuffer results = new StringBuffer("<input type=\"checkbox\"");
114 
115         prepareAttribute(results, "name", prepareName());
116         prepareAttribute(results, "accesskey", getAccesskey());
117         prepareAttribute(results, "tabindex", getTabindex());
118 
119         prepareAttribute(results, "value", getValue());
120 
121         if (isChecked()) {
122             results.append(" checked=\"checked\"");
123         }
124 
125         results.append(prepareEventHandlers());
126         results.append(prepareStyles());
127         prepareOtherAttributes(results);
128         results.append(getElementClose());
129 
130         // Print this field to our output writer
131         TagUtils.getInstance().write(pageContext, results.toString());
132 
133         // Continue processing this page
134         this.text = null;
135 
136         return (EVAL_BODY_TAG);
137     }
138 
139     /***
140      * Determines if the checkbox should be checked.
141      *
142      * @return true if checked="checked" should be rendered.
143      * @throws JspException
144      * @since Struts 1.2
145      */
146     protected boolean isChecked()
147         throws JspException {
148         Object result =
149             TagUtils.getInstance().lookup(pageContext, name, property, null);
150 
151         if (result == null) {
152             result = "";
153         }
154 
155         result = result.toString();
156 
157         String checked = (String) result;
158 
159         return (checked.equalsIgnoreCase(this.value)
160         || checked.equalsIgnoreCase("true") || checked.equalsIgnoreCase("yes")
161         || checked.equalsIgnoreCase("on"));
162     }
163 
164     /***
165      * Save the associated label from the body content.
166      *
167      * @throws JspException if a JSP exception has occurred
168      */
169     public int doAfterBody() throws JspException {
170         if (bodyContent != null) {
171             String value = bodyContent.getString().trim();
172 
173             if (value.length() > 0) {
174                 text = value;
175             }
176         }
177 
178         return (SKIP_BODY);
179     }
180 
181     /***
182      * Process the remainder of this page normally.
183      *
184      * @throws JspException if a JSP exception has occurred
185      */
186     public int doEndTag() throws JspException {
187         // Render any description for this checkbox
188         if (text != null) {
189             TagUtils.getInstance().write(pageContext, text);
190         }
191 
192         // Evaluate the remainder of this page
193         return (EVAL_PAGE);
194     }
195 
196     /***
197      * Prepare the name element
198      *
199      * @return The element name.
200      */
201     protected String prepareName()
202         throws JspException {
203         if (property == null) {
204             return null;
205         }
206 
207         // * @since Struts 1.1
208         if (indexed) {
209             StringBuffer results = new StringBuffer();
210 
211             prepareIndex(results, name);
212             results.append(property);
213 
214             return results.toString();
215         }
216 
217         return property;
218     }
219 
220     /***
221      * Release any acquired resources.
222      */
223     public void release() {
224         super.release();
225         name = Constants.BEAN_KEY;
226         property = null;
227         text = null;
228         value = null;
229     }
230 }