001: /*
002: * $Id: CheckboxTag.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 "checkbox".
030: *
031: * @version $Rev: 471754 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
032: * $
033: */
034: public class CheckboxTag 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: public String getName() {
064: return (this .name);
065: }
066:
067: public void setName(String name) {
068: this .name = name;
069: }
070:
071: // ------------------------------------------------------------- Properties
072:
073: /**
074: * Return the property name.
075: */
076: public String getProperty() {
077: return (this .property);
078: }
079:
080: /**
081: * Set the property name.
082: *
083: * @param property The new property name
084: */
085: public void setProperty(String property) {
086: this .property = property;
087: }
088:
089: /**
090: * Return the server value.
091: */
092: public String getValue() {
093: return (value == null) ? "on" : value;
094: }
095:
096: /**
097: * Set the server value.
098: *
099: * @param value The new server value
100: */
101: public void setValue(String value) {
102: this .value = value;
103: }
104:
105: // --------------------------------------------------------- Public Methods
106:
107: /**
108: * Generate the required input tag. <p> Support for indexed property since
109: * Struts 1.1
110: *
111: * @throws JspException if a JSP exception has occurred
112: */
113: public int doStartTag() throws JspException {
114: // Create an appropriate "input" element based on our parameters
115: StringBuffer results = new StringBuffer(
116: "<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: // Print this field to our output writer
134: TagUtils.getInstance().write(pageContext, results.toString());
135:
136: // Continue processing this page
137: this .text = null;
138:
139: return (EVAL_BODY_TAG);
140: }
141:
142: /**
143: * Determines if the checkbox should be checked.
144: *
145: * @return true if checked="checked" should be rendered.
146: * @throws JspException
147: * @since Struts 1.2
148: */
149: protected boolean isChecked() throws JspException {
150: Object result = TagUtils.getInstance().lookup(pageContext,
151: name, property, null);
152:
153: if (result == null) {
154: result = "";
155: }
156:
157: result = result.toString();
158:
159: String checked = (String) result;
160:
161: return (checked.equalsIgnoreCase(this .value)
162: || checked.equalsIgnoreCase("true")
163: || checked.equalsIgnoreCase("yes") || checked
164: .equalsIgnoreCase("on"));
165: }
166:
167: /**
168: * Save the associated label from the body content.
169: *
170: * @throws JspException if a JSP exception has occurred
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: * Process the remainder of this page normally.
186: *
187: * @throws JspException if a JSP exception has occurred
188: */
189: public int doEndTag() throws JspException {
190: // Render any description for this checkbox
191: if (text != null) {
192: TagUtils.getInstance().write(pageContext, text);
193: }
194:
195: // Evaluate the remainder of this page
196: return (EVAL_PAGE);
197: }
198:
199: /**
200: * Prepare the name element
201: *
202: * @return The element name.
203: */
204: protected String prepareName() throws JspException {
205: if (property == null) {
206: return null;
207: }
208:
209: // * @since Struts 1.1
210: if (indexed) {
211: StringBuffer results = new StringBuffer();
212:
213: prepareIndex(results, name);
214: results.append(property);
215:
216: return results.toString();
217: }
218:
219: return property;
220: }
221:
222: /**
223: * Release any acquired resources.
224: */
225: public void release() {
226: super.release();
227: name = Constants.BEAN_KEY;
228: property = null;
229: text = null;
230: value = null;
231: }
232: }
|