1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 javax.servlet.jsp.JspException;
27
28
29
30
31
32
33
34 public class CheckboxTag extends BaseHandlerTag {
35
36
37
38
39
40 protected static MessageResources messages =
41 MessageResources.getMessageResources(Constants.Package
42 + ".LocalStrings");
43
44
45
46
47 protected String name = Constants.BEAN_KEY;
48
49
50
51
52 protected String property = null;
53
54
55
56
57 protected String text = null;
58
59
60
61
62 protected String value = null;
63
64 public String getName() {
65 return (this.name);
66 }
67
68 public void setName(String name) {
69 this.name = name;
70 }
71
72
73
74
75
76
77 public String getProperty() {
78 return (this.property);
79 }
80
81
82
83
84
85
86 public void setProperty(String property) {
87 this.property = property;
88 }
89
90
91
92
93 public String getValue() {
94 return (value == null) ? "on" : value;
95 }
96
97
98
99
100
101
102 public void setValue(String value) {
103 this.value = value;
104 }
105
106
107
108
109
110
111
112
113
114 public int doStartTag() throws JspException {
115
116 StringBuffer results = new StringBuffer("<input type=\"checkbox\"");
117
118 prepareAttribute(results, "name", prepareName());
119 prepareAttribute(results, "accesskey", getAccesskey());
120 prepareAttribute(results, "tabindex", getTabindex());
121
122 prepareAttribute(results, "value", getValue());
123
124 if (isChecked()) {
125 results.append(" checked=\"checked\"");
126 }
127
128 results.append(prepareEventHandlers());
129 results.append(prepareStyles());
130 prepareOtherAttributes(results);
131 results.append(getElementClose());
132
133
134 TagUtils.getInstance().write(pageContext, results.toString());
135
136
137 this.text = null;
138
139 return (EVAL_BODY_TAG);
140 }
141
142
143
144
145
146
147
148
149 protected boolean isChecked()
150 throws JspException {
151 Object result =
152 TagUtils.getInstance().lookup(pageContext, name, property, null);
153
154 if (result == null) {
155 result = "";
156 }
157
158 result = result.toString();
159
160 String checked = (String) result;
161
162 return (checked.equalsIgnoreCase(this.value)
163 || checked.equalsIgnoreCase("true") || checked.equalsIgnoreCase("yes")
164 || checked.equalsIgnoreCase("on"));
165 }
166
167
168
169
170
171
172 public int doAfterBody() throws JspException {
173 if (bodyContent != null) {
174 String value = bodyContent.getString().trim();
175
176 if (value.length() > 0) {
177 text = value;
178 }
179 }
180
181 return (SKIP_BODY);
182 }
183
184
185
186
187
188
189 public int doEndTag() throws JspException {
190
191 if (text != null) {
192 TagUtils.getInstance().write(pageContext, text);
193 }
194
195
196 return (EVAL_PAGE);
197 }
198
199
200
201
202
203
204 protected String prepareName()
205 throws JspException {
206 if (property == null) {
207 return null;
208 }
209
210
211 if (indexed) {
212 StringBuffer results = new StringBuffer();
213
214 prepareIndex(results, name);
215 results.append(property);
216
217 return results.toString();
218 }
219
220 return property;
221 }
222
223
224
225
226 public void release() {
227 super.release();
228 name = Constants.BEAN_KEY;
229 property = null;
230 text = null;
231 value = null;
232 }
233 }