001: /*
002: * $Id: MultiboxTag.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.commons.beanutils.BeanUtils;
024: import org.apache.struts.Globals;
025: import org.apache.struts.taglib.TagUtils;
026: import org.apache.struts.util.MessageResources;
027:
028: import javax.servlet.jsp.JspException;
029: import javax.servlet.jsp.PageContext;
030:
031: import java.lang.reflect.InvocationTargetException;
032:
033: /**
034: * Tag for input fields of type "checkbox". This differs from CheckboxTag
035: * because it assumes that the underlying property is an array getter (of any
036: * supported primitive type, or String), and the checkbox is initialized to
037: * "checked" if the value listed for the "value" attribute is present in the
038: * values returned by the property getter.
039: *
040: * @version $Rev: 471754 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
041: * $
042: */
043: public class MultiboxTag extends BaseHandlerTag {
044: /**
045: * The message resources for this package.
046: */
047: protected static MessageResources messages = MessageResources
048: .getMessageResources(Constants.Package + ".LocalStrings");
049:
050: // ----------------------------------------------------- Instance Variables
051:
052: /**
053: * The constant String value to be returned when this checkbox is selected
054: * and the form is submitted.
055: */
056: protected String constant = null;
057:
058: /**
059: * The name of the bean containing our underlying property.
060: */
061: protected String name = Constants.BEAN_KEY;
062:
063: /**
064: * The property name for this field.
065: */
066: protected String property = null;
067:
068: /**
069: * The value which will mark this checkbox as "checked" if present in the
070: * array returned by our property getter.
071: */
072: protected String value = null;
073:
074: public String getName() {
075: return (this .name);
076: }
077:
078: public void setName(String name) {
079: this .name = name;
080: }
081:
082: // ------------------------------------------------------------- Properties
083:
084: /**
085: * Return the property name.
086: */
087: public String getProperty() {
088: return (this .property);
089: }
090:
091: /**
092: * Set the property name.
093: *
094: * @param property The new property name
095: */
096: public void setProperty(String property) {
097: this .property = property;
098: }
099:
100: /**
101: * Return the server value.
102: */
103: public String getValue() {
104: return (this .value);
105: }
106:
107: /**
108: * Set the server value.
109: *
110: * @param value The new server value
111: */
112: public void setValue(String value) {
113: this .value = value;
114: }
115:
116: // --------------------------------------------------------- Public Methods
117:
118: /**
119: * Process the beginning of this tag.
120: *
121: * @throws JspException if a JSP exception has occurred
122: */
123: public int doStartTag() throws JspException {
124: // Defer processing until the end of this tag is encountered
125: this .constant = null;
126:
127: return (EVAL_BODY_TAG);
128: }
129:
130: /**
131: * Save the body contents of this tag as the constant that we will be
132: * returning.
133: *
134: * @throws JspException if a JSP exception has occurred
135: */
136: public int doAfterBody() throws JspException {
137: if (bodyContent != null) {
138: this .constant = bodyContent.getString().trim();
139: }
140:
141: if ("".equals(this .constant)) {
142: this .constant = null;
143: }
144:
145: return SKIP_BODY;
146: }
147:
148: /**
149: * Render an input element for this tag.
150: *
151: * @throws JspException if a JSP exception has occurred
152: */
153: public int doEndTag() throws JspException {
154: // Create an appropriate "input" element based on our parameters
155: StringBuffer results = new StringBuffer(
156: "<input type=\"checkbox\"");
157:
158: prepareAttribute(results, "name", prepareName());
159: prepareAttribute(results, "accesskey", getAccesskey());
160: prepareAttribute(results, "tabindex", getTabindex());
161:
162: String value = prepareValue(results);
163:
164: prepareChecked(results, value);
165: results.append(prepareEventHandlers());
166: results.append(prepareStyles());
167: prepareOtherAttributes(results);
168: results.append(getElementClose());
169:
170: TagUtils.getInstance().write(pageContext, results.toString());
171:
172: return EVAL_PAGE;
173: }
174:
175: /**
176: * Prepare the name element
177: *
178: * @return The element name.
179: */
180: protected String prepareName() throws JspException {
181: return property;
182: }
183:
184: /**
185: * Render the value element
186: *
187: * @param results The StringBuffer that output will be appended to.
188: */
189: protected String prepareValue(StringBuffer results)
190: throws JspException {
191: String value = (this .value == null) ? this .constant
192: : this .value;
193:
194: if (value == null) {
195: JspException e = new JspException(messages
196: .getMessage("multiboxTag.value"));
197:
198: pageContext.setAttribute(Globals.EXCEPTION_KEY, e,
199: PageContext.REQUEST_SCOPE);
200: throw e;
201: }
202:
203: prepareAttribute(results, "value", TagUtils.getInstance()
204: .filter(value));
205:
206: return value;
207: }
208:
209: /**
210: * Render the checked element
211: *
212: * @param results The StringBuffer that output will be appended to.
213: */
214: protected void prepareChecked(StringBuffer results, String value)
215: throws JspException {
216: Object bean = TagUtils.getInstance().lookup(pageContext, name,
217: null);
218: String[] values = null;
219:
220: if (bean == null) {
221: throw new JspException(messages.getMessage("getter.bean",
222: name));
223: }
224:
225: try {
226: values = BeanUtils.getArrayProperty(bean, property);
227:
228: if (values == null) {
229: values = new String[0];
230: }
231: } catch (IllegalAccessException e) {
232: throw new JspException(messages.getMessage("getter.access",
233: property, name));
234: } catch (InvocationTargetException e) {
235: Throwable t = e.getTargetException();
236:
237: throw new JspException(messages.getMessage("getter.result",
238: property, t.toString()));
239: } catch (NoSuchMethodException e) {
240: throw new JspException(messages.getMessage("getter.method",
241: property, name));
242: }
243:
244: for (int i = 0; i < values.length; i++) {
245: if (value.equals(values[i])) {
246: results.append(" checked=\"checked\"");
247:
248: break;
249: }
250: }
251: }
252:
253: /**
254: * Release any acquired resources.
255: */
256: public void release() {
257: super.release();
258: constant = null;
259: name = Constants.BEAN_KEY;
260: property = null;
261: value = null;
262: }
263: }
|