1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
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
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
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
131 TagUtils.getInstance().write(pageContext, results.toString());
132
133
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
188 if (text != null) {
189 TagUtils.getInstance().write(pageContext, text);
190 }
191
192
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
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 }