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 "radio".
27 *
28 * @version $Rev: 376841 $ $Date: 2005-06-14 14:26:17 -0400 (Tue, 14 Jun 2005)
29 * $
30 */
31 public class RadioTag 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 /***
62 * Name of the bean (in some scope) that will return the value of the
63 * radio tag. <p> If an iterator is used to render a series of radio tags,
64 * this field may be used to specify the name of the bean exposed by the
65 * iterator. In this case, the value attribute is used as the name of a
66 * property on the <code>idName</code> bean that returns the value of the
67 * radio tag in this iteration.
68 */
69 protected String idName = null;
70
71 public String getName() {
72 return (this.name);
73 }
74
75 public void setName(String name) {
76 this.name = name;
77 }
78
79
80
81 /***
82 * Return the property name.
83 */
84 public String getProperty() {
85 return (this.property);
86 }
87
88 /***
89 * Set the property name.
90 *
91 * @param property The new property name
92 */
93 public void setProperty(String property) {
94 this.property = property;
95 }
96
97 /***
98 * Return the server value.
99 */
100 public String getValue() {
101 return (this.value);
102 }
103
104 /***
105 * Set the server value.
106 *
107 * @param value The new server value
108 */
109 public void setValue(String value) {
110 this.value = value;
111 }
112
113 /***
114 * Return the idName.
115 *
116 * @since Struts 1.1
117 */
118 public String getIdName() {
119 return (this.idName);
120 }
121
122 /***
123 * Set the idName.
124 *
125 * @param idName The new idName
126 * @since Struts 1.1
127 */
128 public void setIdName(String idName) {
129 this.idName = idName;
130 }
131
132
133
134 /***
135 * Generate the required input tag. [Indexed property since Struts 1.1]
136 *
137 * @throws JspException if a JSP exception has occurred
138 */
139 public int doStartTag() throws JspException {
140 String radioTag = renderRadioElement(serverValue(), currentValue());
141
142 TagUtils.getInstance().write(pageContext, radioTag);
143
144 this.text = null;
145
146 return (EVAL_BODY_TAG);
147 }
148
149 /***
150 * Return the String to be used in the radio tag's <code>value</code>
151 * attribute that gets sent to the server on form submission.
152 *
153 * @throws JspException
154 */
155 private String serverValue()
156 throws JspException {
157
158 if (this.idName == null) {
159 return this.value;
160 }
161
162 String serverValue = this.lookupProperty(this.idName, this.value);
163
164 return (serverValue == null) ? "" : serverValue;
165 }
166
167 /***
168 * Acquire the current value of the bean specified by the
169 * <code>name</code> attribute and the property specified by the
170 * <code>property</code> attribute. This radio button with this value will
171 * be checked.
172 *
173 * @throws JspException
174 */
175 private String currentValue()
176 throws JspException {
177 String current = this.lookupProperty(this.name, this.property);
178
179 return (current == null) ? "" : current;
180 }
181
182 /***
183 * Renders an HTML <input type="radio"> element.
184 *
185 * @param serverValue The data to be used in the tag's <code>value</code>
186 * attribute and sent to the server when the form is
187 * submitted.
188 * @param checkedValue If the serverValue equals this value the radio
189 * button will be checked.
190 * @return A radio input element.
191 * @throws JspException
192 * @since Struts 1.1
193 */
194 protected String renderRadioElement(String serverValue, String checkedValue)
195 throws JspException {
196 StringBuffer results = new StringBuffer("<input type=\"radio\"");
197
198 prepareAttribute(results, "name", prepareName());
199 prepareAttribute(results, "accesskey", getAccesskey());
200 prepareAttribute(results, "tabindex", getTabindex());
201 prepareAttribute(results, "value",
202 TagUtils.getInstance().filter(serverValue));
203
204 if (serverValue.equals(checkedValue)) {
205 results.append(" checked=\"checked\"");
206 }
207
208 results.append(prepareEventHandlers());
209 results.append(prepareStyles());
210 prepareOtherAttributes(results);
211 results.append(getElementClose());
212
213 return results.toString();
214 }
215
216 /***
217 * Save the associated label from the body content.
218 *
219 * @throws JspException if a JSP exception has occurred
220 */
221 public int doAfterBody() throws JspException {
222 if (this.bodyContent != null) {
223 String value = this.bodyContent.getString().trim();
224
225 if (value.length() > 0) {
226 this.text = value;
227 }
228 }
229
230 return (SKIP_BODY);
231 }
232
233 /***
234 * Optionally render the associated label from the body content.
235 *
236 * @throws JspException if a JSP exception has occurred
237 */
238 public int doEndTag() throws JspException {
239
240 if (this.text != null) {
241 TagUtils.getInstance().write(pageContext, text);
242 }
243
244 return (EVAL_PAGE);
245 }
246
247 /***
248 * Prepare the name element
249 *
250 * @return The element name.
251 */
252 protected String prepareName()
253 throws JspException {
254 if (property == null) {
255 return null;
256 }
257
258
259 if (indexed) {
260 StringBuffer results = new StringBuffer();
261
262 prepareIndex(results, name);
263 results.append(property);
264
265 return results.toString();
266 }
267
268 return property;
269 }
270
271 /***
272 * Release any acquired resources.
273 */
274 public void release() {
275 super.release();
276 idName = null;
277 name = Constants.BEAN_KEY;
278 property = null;
279 text = null;
280 value = null;
281 }
282 }