KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > html > CheckboxTag


1 /*
2  * $Id: CheckboxTag.java 54929 2004-10-16 16:38:42Z germuska $
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
19 package org.apache.struts.taglib.html;
20
21 import javax.servlet.jsp.JspException JavaDoc;
22
23 import org.apache.struts.taglib.TagUtils;
24 import org.apache.struts.util.MessageResources;
25
26 /**
27  * Tag for input fields of type "checkbox".
28  *
29  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
30  */

31 public class CheckboxTag extends BaseHandlerTag {
32
33     // ----------------------------------------------------- Instance Variables
34

35     /**
36      * The message resources for this package.
37      */

38     protected static MessageResources messages =
39         MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
40
41     /**
42      * The name of the bean containing our underlying property.
43      */

44     protected String JavaDoc name = Constants.BEAN_KEY;
45
46     public String JavaDoc getName() {
47         return (this.name);
48     }
49
50     public void setName(String JavaDoc name) {
51         this.name = name;
52     }
53
54     /**
55      * The property name for this field.
56      */

57     protected String JavaDoc property = null;
58
59     /**
60      * The body content of this tag (if any).
61      */

62     protected String JavaDoc text = null;
63
64     /**
65      * The server value for this option.
66      */

67     protected String JavaDoc value = null;
68
69     // ------------------------------------------------------------- Properties
70

71     /**
72      * Return the property name.
73      */

74     public String JavaDoc getProperty() {
75
76         return (this.property);
77
78     }
79
80     /**
81      * Set the property name.
82      *
83      * @param property The new property name
84      */

85     public void setProperty(String JavaDoc property) {
86
87         this.property = property;
88
89     }
90
91     /**
92      * Return the server value.
93      */

94     public String JavaDoc getValue() {
95
96         return value == null ? "on" : value;
97
98     }
99
100     /**
101      * Set the server value.
102      *
103      * @param value The new server value
104      */

105     public void setValue(String JavaDoc value) {
106
107         this.value = value;
108
109     }
110
111     // --------------------------------------------------------- Public Methods
112

113     /**
114      * Generate the required input tag.
115      * <p>
116      * Support for indexed property since Struts 1.1
117      *
118      * @exception JspException if a JSP exception has occurred
119      */

120     public int doStartTag() throws JspException JavaDoc {
121
122         // Create an appropriate "input" element based on our parameters
123
StringBuffer JavaDoc results = new StringBuffer JavaDoc("<input type=\"checkbox\"");
124         prepareAttribute(results, "name", prepareName());
125         prepareAttribute(results, "accesskey", getAccesskey());
126         prepareAttribute(results, "tabindex", getTabindex());
127
128         prepareAttribute(results, "value", getValue());
129         if (isChecked()) {
130             results.append(" checked=\"checked\"");
131         }
132
133         results.append(prepareEventHandlers());
134         results.append(prepareStyles());
135         prepareOtherAttributes(results);
136         results.append(getElementClose());
137
138         // Print this field to our output writer
139
TagUtils.getInstance().write(pageContext, results.toString());
140
141         // Continue processing this page
142
this.text = null;
143         return (EVAL_BODY_TAG);
144
145     }
146     
147     /**
148      * Determines if the checkbox should be checked.
149      * @return true if checked="checked" should be rendered.
150      * @throws JspException
151      * @since Struts 1.2
152      */

153     protected boolean isChecked() throws JspException JavaDoc {
154         Object JavaDoc result =
155             TagUtils.getInstance().lookup(pageContext, name, property, null);
156
157         if (result == null) {
158             result = "";
159         }
160
161         result = result.toString();
162
163         String JavaDoc checked = (String JavaDoc) result;
164         return (
165             checked.equalsIgnoreCase(this.value)
166                 || checked.equalsIgnoreCase("true")
167                 || checked.equalsIgnoreCase("yes")
168                 || checked.equalsIgnoreCase("on"));
169
170     }
171
172     /**
173      * Save the associated label from the body content.
174      *
175      * @exception JspException if a JSP exception has occurred
176      */

177     public int doAfterBody() throws JspException JavaDoc {
178
179         if (bodyContent != null) {
180             String JavaDoc value = bodyContent.getString().trim();
181             if (value.length() > 0) {
182                 text = value;
183             }
184         }
185         return (SKIP_BODY);
186
187     }
188
189     /**
190      * Process the remainder of this page normally.
191      *
192      * @exception JspException if a JSP exception has occurred
193      */

194     public int doEndTag() throws JspException JavaDoc {
195
196         // Render any description for this checkbox
197
if (text != null) {
198             TagUtils.getInstance().write(pageContext, text);
199         }
200
201         // Evaluate the remainder of this page
202
return (EVAL_PAGE);
203
204     }
205
206
207     /**
208      * Prepare the name element
209      * @return The element name.
210      */

211     protected String JavaDoc prepareName() throws JspException JavaDoc {
212
213         if (property == null) {
214             return null;
215         }
216
217         // * @since Struts 1.1
218
if(indexed) {
219             StringBuffer JavaDoc results = new StringBuffer JavaDoc();
220             prepareIndex(results, name);
221             results.append(property);
222             return results.toString();
223         }
224
225         return property;
226
227     }
228
229     /**
230      * Release any acquired resources.
231      */

232     public void release() {
233
234         super.release();
235         name = Constants.BEAN_KEY;
236         property = null;
237         text = null;
238         value = null;
239
240     }
241
242 }
243
Popular Tags