001: /*
002: * $Id: RadioTag.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.taglib.html;
022:
023: import org.apache.struts.taglib.TagUtils;
024: import org.apache.struts.util.MessageResources;
025:
026: import javax.servlet.jsp.JspException;
027:
028: /**
029: * Tag for input fields of type "radio".
030: *
031: * @version $Rev: 471754 $ $Date: 2005-06-14 14:26:17 -0400 (Tue, 14 Jun 2005)
032: * $
033: */
034: public class RadioTag extends BaseHandlerTag {
035: // ----------------------------------------------------- Instance Variables
036:
037: /**
038: * The message resources for this package.
039: */
040: protected static MessageResources messages = MessageResources
041: .getMessageResources(Constants.Package + ".LocalStrings");
042:
043: /**
044: * The name of the bean containing our underlying property.
045: */
046: protected String name = Constants.BEAN_KEY;
047:
048: /**
049: * The property name for this field.
050: */
051: protected String property = null;
052:
053: /**
054: * The body content of this tag (if any).
055: */
056: protected String text = null;
057:
058: /**
059: * The server value for this option.
060: */
061: protected String value = null;
062:
063: /**
064: * Name of the bean (in some scope) that will return the value of the
065: * radio tag. <p> If an iterator is used to render a series of radio tags,
066: * this field may be used to specify the name of the bean exposed by the
067: * iterator. In this case, the value attribute is used as the name of a
068: * property on the <code>idName</code> bean that returns the value of the
069: * radio tag in this iteration.
070: */
071: protected String idName = null;
072:
073: public String getName() {
074: return (this .name);
075: }
076:
077: public void setName(String name) {
078: this .name = name;
079: }
080:
081: // ------------------------------------------------------------- Properties
082:
083: /**
084: * Return the property name.
085: */
086: public String getProperty() {
087: return (this .property);
088: }
089:
090: /**
091: * Set the property name.
092: *
093: * @param property The new property name
094: */
095: public void setProperty(String property) {
096: this .property = property;
097: }
098:
099: /**
100: * Return the server value.
101: */
102: public String getValue() {
103: return (this .value);
104: }
105:
106: /**
107: * Set the server value.
108: *
109: * @param value The new server value
110: */
111: public void setValue(String value) {
112: this .value = value;
113: }
114:
115: /**
116: * Return the idName.
117: *
118: * @since Struts 1.1
119: */
120: public String getIdName() {
121: return (this .idName);
122: }
123:
124: /**
125: * Set the idName.
126: *
127: * @param idName The new idName
128: * @since Struts 1.1
129: */
130: public void setIdName(String idName) {
131: this .idName = idName;
132: }
133:
134: // --------------------------------------------------------- Public Methods
135:
136: /**
137: * Generate the required input tag. [Indexed property since Struts 1.1]
138: *
139: * @throws JspException if a JSP exception has occurred
140: */
141: public int doStartTag() throws JspException {
142: String radioTag = renderRadioElement(serverValue(),
143: currentValue());
144:
145: TagUtils.getInstance().write(pageContext, radioTag);
146:
147: this .text = null;
148:
149: return (EVAL_BODY_TAG);
150: }
151:
152: /**
153: * Return the String to be used in the radio tag's <code>value</code>
154: * attribute that gets sent to the server on form submission.
155: *
156: * @throws JspException
157: */
158: private String serverValue() throws JspException {
159: // Not using indexed radio buttons
160: if (this .idName == null) {
161: return this .value;
162: }
163:
164: String serverValue = this .lookupProperty(this .idName,
165: this .value);
166:
167: return (serverValue == null) ? "" : serverValue;
168: }
169:
170: /**
171: * Acquire the current value of the bean specified by the
172: * <code>name</code> attribute and the property specified by the
173: * <code>property</code> attribute. This radio button with this value will
174: * be checked.
175: *
176: * @throws JspException
177: */
178: private String currentValue() throws JspException {
179: String current = this .lookupProperty(this .name, this .property);
180:
181: return (current == null) ? "" : current;
182: }
183:
184: /**
185: * Renders an HTML <input type="radio"> element.
186: *
187: * @param serverValue The data to be used in the tag's <code>value</code>
188: * attribute and sent to the server when the form is
189: * submitted.
190: * @param checkedValue If the serverValue equals this value the radio
191: * button will be checked.
192: * @return A radio input element.
193: * @throws JspException
194: * @since Struts 1.1
195: */
196: protected String renderRadioElement(String serverValue,
197: String checkedValue) throws JspException {
198: StringBuffer results = new StringBuffer("<input type=\"radio\"");
199:
200: prepareAttribute(results, "name", prepareName());
201: prepareAttribute(results, "accesskey", getAccesskey());
202: prepareAttribute(results, "tabindex", getTabindex());
203: prepareAttribute(results, "value", TagUtils.getInstance()
204: .filter(serverValue));
205:
206: if (serverValue.equals(checkedValue)) {
207: results.append(" checked=\"checked\"");
208: }
209:
210: results.append(prepareEventHandlers());
211: results.append(prepareStyles());
212: prepareOtherAttributes(results);
213: results.append(getElementClose());
214:
215: return results.toString();
216: }
217:
218: /**
219: * Save the associated label from the body content.
220: *
221: * @throws JspException if a JSP exception has occurred
222: */
223: public int doAfterBody() throws JspException {
224: if (this .bodyContent != null) {
225: String value = this .bodyContent.getString().trim();
226:
227: if (value.length() > 0) {
228: this .text = value;
229: }
230: }
231:
232: return (SKIP_BODY);
233: }
234:
235: /**
236: * Optionally render the associated label from the body content.
237: *
238: * @throws JspException if a JSP exception has occurred
239: */
240: public int doEndTag() throws JspException {
241: // Render any description for this radio button
242: if (this .text != null) {
243: TagUtils.getInstance().write(pageContext, text);
244: }
245:
246: return (EVAL_PAGE);
247: }
248:
249: /**
250: * Prepare the name element
251: *
252: * @return The element name.
253: */
254: protected String prepareName() throws JspException {
255: if (property == null) {
256: return null;
257: }
258:
259: // * @since Struts 1.1
260: if (indexed) {
261: StringBuffer results = new StringBuffer();
262:
263: prepareIndex(results, name);
264: results.append(property);
265:
266: return results.toString();
267: }
268:
269: return property;
270: }
271:
272: /**
273: * Release any acquired resources.
274: */
275: public void release() {
276: super.release();
277: idName = null;
278: name = Constants.BEAN_KEY;
279: property = null;
280: text = null;
281: value = null;
282: }
283: }
|