KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > tags > form > CheckboxTag


1 /*
2  * Copyright 2002-2007 the original author or authors.
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
17 package org.springframework.web.servlet.tags.form;
18
19 import java.util.Collection JavaDoc;
20
21 import javax.servlet.jsp.JspException JavaDoc;
22
23 import org.springframework.util.Assert;
24 import org.springframework.util.CollectionUtils;
25
26 /**
27  * Databinding-aware JSP tag for rendering an HTML '<code>input</code>'
28  * element with a '<code>type</code>' of '<code>checkbox</code>'.
29  *
30  * <p>May be used in one of three different approaches depending on the
31  * type of the {@link #getValue bound value}.
32  *
33  * <h3>Approach One</h3>
34  * When the bound value is of type {@link Boolean} then the '<code>input(checkbox)</code>'
35  * is marked as 'checked' if the bound value is <code>true</code>. The '<code>value</code>'
36  * attribute corresponds to the resolved value of the {@link #setValue(Object) value} property.
37  * <h3>Approach Two</h3>
38  * When the bound value is of type {@link Collection} then the '<code>input(checkbox)</code>'
39  * is marked as 'checked' if the configured {@link #setValue(Object) value} is present in
40  * the bound {@link Collection}.
41  * <h3>Approach Three</h3>
42  * For any other bound value type, the '<code>input(checkbox)</code>' is marked as 'checked'
43  * if the the configured {@link #setValue(Object) value} is equal to the bound value.
44  *
45  * @author Rob Harrop
46  * @since 2.0
47  */

48 public class CheckboxTag extends AbstractHtmlInputElementTag {
49
50     /**
51      * The value of the '<code>value</code>' attribute.
52      */

53     private Object JavaDoc value;
54
55
56     /**
57      * Set the value of the '<code>value</code>' attribute.
58      * May be a runtime expression.
59      */

60     public void setValue(Object JavaDoc value) {
61         Assert.notNull(value, "'value' must not be null");
62         this.value = value;
63     }
64
65     /**
66      * Get the value of the '<code>value</code>' attribute.
67      * May be a runtime expression.
68      */

69     protected Object JavaDoc getValue() {
70         return this.value;
71     }
72
73
74     /**
75      * Writes the '<code>input(checkbox)</code>' to the supplied {@link TagWriter},
76      * marking it as 'checked' if appropriate.
77      */

78     protected int writeTagContent(TagWriter tagWriter) throws JspException JavaDoc {
79         tagWriter.startTag("input");
80         writeDefaultAttributes(tagWriter);
81         tagWriter.writeAttribute("type", "checkbox");
82
83         Object JavaDoc boundValue = getBoundValue();
84         Class JavaDoc valueType = getBindStatus().getValueType();
85
86         if (Boolean JavaDoc.class.equals(valueType) || boolean.class.equals(valueType)) {
87             // the concrete type may not be a Boolean - can be String
88
if (boundValue instanceof String JavaDoc) {
89                 boundValue = Boolean.valueOf((String JavaDoc) boundValue);
90             }
91             Boolean JavaDoc booleanValue = (boundValue != null ? (Boolean JavaDoc) boundValue : Boolean.FALSE);
92             renderFromBoolean(booleanValue, tagWriter);
93         }
94
95         else {
96             Object JavaDoc value = getValue();
97             if (value == null) {
98                 throw new IllegalArgumentException JavaDoc("Attribute 'value' is required when binding to non-boolean values");
99             }
100
101             Object JavaDoc resolvedValue = (value instanceof String JavaDoc ? evaluate("value", (String JavaDoc)value) : value);
102
103             if (boundValue != null && boundValue.getClass().isArray()) {
104                 renderFromCollection(resolvedValue, CollectionUtils.arrayToList(boundValue), tagWriter);
105             }
106             else if (boundValue instanceof Collection JavaDoc) {
107                 renderFromCollection(resolvedValue, (Collection JavaDoc) boundValue, tagWriter);
108             }
109             else {
110                 renderSingleValue(resolvedValue, tagWriter);
111             }
112         }
113
114         tagWriter.endTag();
115
116         if (!isDisabled()) {
117             // write out the marker field
118
tagWriter.startTag("input");
119             tagWriter.writeAttribute("type", "hidden");
120             tagWriter.writeAttribute("value", "1");
121             tagWriter.writeAttribute("name", "_" + getName());
122             tagWriter.endTag();
123         }
124
125         return EVAL_PAGE;
126     }
127
128     /**
129      * Render the '<code>input(checkbox)</code>' with the supplied value, marking the
130      * '<code>input</code>' element as 'checked' if the supplied value matches the
131      * bound value.
132      */

133     private void renderSingleValue(Object JavaDoc resolvedValue, TagWriter tagWriter) throws JspException JavaDoc {
134         tagWriter.writeAttribute("value", getDisplayString(resolvedValue));
135
136         if (SelectedValueComparator.isSelected(getBindStatus(), resolvedValue)) {
137             tagWriter.writeAttribute("checked", "checked");
138         }
139     }
140
141     /**
142      * Render the '<code>input(checkbox)</code>' with the supplied value, marking
143      * the '<code>input</code>' element as 'checked' if the supplied value is
144      * present in the bound Collection value.
145      */

146     private void renderFromCollection(Object JavaDoc resolvedValue, Collection JavaDoc boundValue, TagWriter tagWriter) throws JspException JavaDoc {
147         tagWriter.writeAttribute("value", getDisplayString(resolvedValue));
148
149         if (SelectedValueComparator.isSelected(getBindStatus(), resolvedValue)) {
150             tagWriter.writeAttribute("checked", "checked");
151         }
152     }
153
154     /**
155      * Render the '<code>input(checkbox)</code>' with the supplied value, marking
156      * the '<code>input</code>' element as 'checked' if the supplied Boolean is
157      * <code>true</code>.
158      */

159     private void renderFromBoolean(Boolean JavaDoc boundValue, TagWriter tagWriter) throws JspException JavaDoc {
160         tagWriter.writeAttribute("value", "true");
161         if (boundValue.booleanValue()) {
162             tagWriter.writeAttribute("checked", "checked");
163         }
164     }
165
166     /**
167      * Return a unique ID for the bound name within the current PageContext.
168      */

169     protected String JavaDoc autogenerateId() throws JspException JavaDoc {
170         return TagIdGenerator.nextId(getName(), this.pageContext);
171     }
172
173 }
174
Popular Tags