KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > input > Checkbox


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.taglibs.input;
17
18 import java.util.Map JavaDoc;
19 import java.util.Vector JavaDoc;
20
21 import javax.servlet.ServletRequest JavaDoc;
22 import javax.servlet.jsp.JspException JavaDoc;
23 import javax.servlet.jsp.JspTagException JavaDoc;
24 import javax.servlet.jsp.JspWriter JavaDoc;
25 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
26
27 /**
28  *
29  * This class implements the <input:checkbox> tag, which presents an
30  * <input type="checkbox" ... /> form element.
31  *
32  * @version 0.90
33  * @author Shawn Bayern
34  * @author Lance Lavandowska
35  * @author Karl von Randow
36  */

37
38 public class Checkbox extends TagSupport JavaDoc {
39
40     private String JavaDoc name; // name of the checkbox group
41

42     private String JavaDoc value; // value of this particular button
43

44     private String JavaDoc dVal; // our single default value
45

46     private String JavaDoc[] dValArray; // our multiple default values
47

48     private Map JavaDoc attributes; // attributes of the <input> element
49

50     private String JavaDoc attributesText; // attributes of the <input> element as text
51

52     private String JavaDoc beanId; // bean id to get default values from
53

54     public void release() {
55         super.release();
56         name = null;
57         dVal = null;
58         dValArray = null;
59         attributes = null;
60         attributesText = null;
61         beanId = null;
62     }
63
64     public int doStartTag() throws JspException JavaDoc {
65         try {
66             // sanity check
67
if (name == null || name.equals(""))
68                 throw new JspTagException JavaDoc("invalid null or empty 'name'");
69
70             // Store beanId in a local variable as we change it
71
String JavaDoc beanId = this.beanId;
72
73             // Get default beanId
74
if (beanId == null) {
75                 beanId = Util.defaultFormBeanId(this);
76             } else if (beanId.length() == 0) {
77                 // An empty beanId means, do not use any bean - not even default
78
beanId = null;
79             }
80
81             // replace null value with "on"
82
if (value == null)
83                 value = "on";
84
85             // get what we need from the page
86
ServletRequest JavaDoc req = pageContext.getRequest();
87             JspWriter JavaDoc out = pageContext.getOut();
88
89             // construct a vector of default values
90
Vector JavaDoc dVals = new Vector JavaDoc();
91             if (dVal != null)
92                 dVals.add(dVal);
93             if (dValArray != null)
94                 for (int i = 0; i < dValArray.length; i++)
95                     if (dValArray[i] != null)
96                         dVals.add(dValArray[i]);
97
98             // start building up the tag
99
out.print("<input type=\"checkbox\" ");
100             out.print("name=\"" + Util.quote(name) + "\" ");
101             out.print("value=\"" + Util.quote(value) + "\" ");
102
103             // include any attributes we've got here
104
Util.printAttributes(out, attributes);
105             if (attributesText != null) {
106                 out.print(attributesText + " ");
107             }
108
109             /*
110              * Check this box (no pun intended) against potentially multiple
111              * selections. (No need for a hash table as in <select> because
112              * we're doing this exactly once per tag. We COULD cache stuff
113              * between tags, but I'm not sure that kind of extra performance
114              * would ever be called for.) We first check if there is a bean
115              * value associated with the checkbox, if there is we use it. Then
116              * note that we only use the "defaults" if the request is ENTIRELY
117              * empty; this is different from what we do with the other input
118              * types, checking "defaults" when there's no value for the specific
119              * field. This difference is the result of the underlying
120              * inconsistency between checkboxes and everything else. To achieve
121              * the default concept nicely with a bean, put the default as the
122              * initial value of the property in the bean when it is constructed
123              * rather than using the "default" attribute of the checkbox tag.
124              */

125
126             // First check beanValue, if available - then we don't worry about
127
// the defaults or request values etc
128
String JavaDoc[] beanValues = (beanId != null ? Util.beanPropertyValues(
129                     pageContext.findAttribute(beanId), name) : null);
130             if (beanValues != null) {
131                 for (int i = 0; i < beanValues.length; i++) {
132                     if (beanValues[i] != null && beanValues[i].equals(value)) {
133                         out.print("checked=\"checked\" ");
134                         break;
135                     }
136                 }
137             }
138             // No bean value, so check if the request is empty - and use
139
// defaults if it is
140
else if (!req.getParameterNames().hasMoreElements()) {
141                 // use "default" array if we got nothing from the request
142
if (dVals != null) {
143                     for (int i = 0; i < dVals.size(); i++) {
144                         if (dVals.get(i) == null
145                                 || !(dVals.get(i) instanceof String JavaDoc))
146                             throw new JspTagException JavaDoc(
147                                     "'default' array must only contain non-null "
148                                             + "Strings");
149                         if ((dVals.get(i)).equals(value)) {
150                             out.print("checked=\"checked\" ");
151                             break; // why go on?
152
}
153                     }
154                 }
155             } else {
156                 // Use values from the request
157
String JavaDoc[] checked = req.getParameterValues(name);
158                 if (checked != null) {
159                     // use the request if it says anything
160
for (int i = 0; i < checked.length; i++) {
161                         if (checked[i].equals(value)) {
162                             out.print("checked=\"checked\" ");
163                             break; // why go on?
164
}
165                     }
166                 }
167             }
168
169             // end the tag
170
out.print("/>");
171
172         } catch (Exception JavaDoc ex) {
173             throw new JspTagException JavaDoc(ex.getMessage());
174         }
175         return SKIP_BODY;
176     }
177
178     /**
179      * Getter for property name.
180      *
181      * @return Value of property name.
182      */

183     public String JavaDoc getName() {
184         return name;
185     }
186
187     public void setName(String JavaDoc x) {
188         name = x;
189     }
190
191     /**
192      * Getter for property value.
193      *
194      * @return Value of property value.
195      */

196     public String JavaDoc getValue() {
197         return value;
198     }
199
200     public void setValue(String JavaDoc x) {
201         value = x;
202     }
203
204     /**
205      * Getter for property defaults.
206      *
207      * @return Value of property defaults.
208      */

209     public String JavaDoc[] getDefaults() {
210         return dValArray;
211     }
212
213     public void setDefaults(String JavaDoc[] x) {
214         dValArray = x;
215     }
216
217     /**
218      * Getter for property default.
219      *
220      * @return Value of property default.
221      */

222     public String JavaDoc getDefault() {
223         return dVal;
224     }
225
226     public void setDefault(String JavaDoc x) {
227         dVal = x;
228     }
229
230     /**
231      * Getter for property bean.
232      *
233      * @return Value of property bean.
234      */

235     public String JavaDoc getBean() {
236         return beanId;
237     }
238
239     public void setBean(String JavaDoc x) {
240         beanId = x;
241     }
242
243     /**
244      * Getter for property attributesText.
245      *
246      * @return Value of property attributesText.
247      */

248     public String JavaDoc getAttributesText() {
249         return attributesText;
250     }
251
252     public void setAttributesText(String JavaDoc x) {
253         attributesText = x;
254     }
255
256     /**
257      * Getter for property attributes.
258      *
259      * @return Value of property attributes.
260      */

261     public Map JavaDoc getAttributes() {
262         return attributes;
263     }
264
265     public void setAttributes(Map JavaDoc x) {
266         attributes = x;
267     }
268
269 }
Popular Tags