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