~ubuntu-branches/ubuntu/trusty/libstruts1.2-java/trusty-proposed

« back to all changes in this revision

Viewing changes to src/share/org/apache/struts/taglib/html/MultiboxTag.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2004-11-19 15:35:25 UTC
  • Revision ID: james.westby@ubuntu.com-20041119153525-mdu08a76z4zo67xt
Tags: upstream-1.2.4
ImportĀ upstreamĀ versionĀ 1.2.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/MultiboxTag.java,v 1.25 2004/03/14 06:23:46 sraeburn Exp $
 
3
 * $Revision: 1.25 $
 
4
 * $Date: 2004/03/14 06:23:46 $
 
5
 *
 
6
 * Copyright 2001-2004 The Apache Software Foundation.
 
7
 * 
 
8
 * Licensed under the Apache License, Version 2.0 (the "License");
 
9
 * you may not use this file except in compliance with the License.
 
10
 * You may obtain a copy of the License at
 
11
 * 
 
12
 *      http://www.apache.org/licenses/LICENSE-2.0
 
13
 * 
 
14
 * Unless required by applicable law or agreed to in writing, software
 
15
 * distributed under the License is distributed on an "AS IS" BASIS,
 
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
17
 * See the License for the specific language governing permissions and
 
18
 * limitations under the License.
 
19
 */
 
20
 
 
21
package org.apache.struts.taglib.html;
 
22
 
 
23
import java.lang.reflect.InvocationTargetException;
 
24
 
 
25
import javax.servlet.jsp.JspException;
 
26
import javax.servlet.jsp.PageContext;
 
27
 
 
28
import org.apache.commons.beanutils.BeanUtils;
 
29
import org.apache.struts.Globals;
 
30
import org.apache.struts.taglib.TagUtils;
 
31
import org.apache.struts.util.MessageResources;
 
32
 
 
33
/**
 
34
 * Tag for input fields of type "checkbox".  This differs from CheckboxTag
 
35
 * because it assumes that the underlying property is an array getter (of any
 
36
 * supported primitive type, or String), and the checkbox is initialized to
 
37
 * "checked" if the value listed for the "value" attribute is present in the
 
38
 * values returned by the property getter.
 
39
 *
 
40
 * @version $Revision: 1.25 $ $Date: 2004/03/14 06:23:46 $
 
41
 */
 
42
 
 
43
public class MultiboxTag extends BaseHandlerTag {
 
44
 
 
45
    // ----------------------------------------------------- Instance Variables
 
46
 
 
47
    /**
 
48
     * The constant String value to be returned when this checkbox is
 
49
     * selected and the form is submitted.
 
50
     */
 
51
    protected String constant = null;
 
52
 
 
53
    /**
 
54
     * The message resources for this package.
 
55
     */
 
56
    protected static MessageResources messages =
 
57
        MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
 
58
 
 
59
    /**
 
60
     * The name of the bean containing our underlying property.
 
61
     */
 
62
    protected String name = Constants.BEAN_KEY;
 
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
     * The property name for this field.
 
74
     */
 
75
    protected String property = null;
 
76
 
 
77
    /**
 
78
     * The value which will mark this checkbox as "checked" if present
 
79
     * in the array returned by our property getter.
 
80
     */
 
81
    protected String value = null;
 
82
 
 
83
    // ------------------------------------------------------------- Properties
 
84
 
 
85
    /**
 
86
     * Return the property name.
 
87
     */
 
88
    public String getProperty() {
 
89
 
 
90
        return (this.property);
 
91
 
 
92
    }
 
93
 
 
94
    /**
 
95
     * Set the property name.
 
96
     *
 
97
     * @param property The new property name
 
98
     */
 
99
    public void setProperty(String property) {
 
100
 
 
101
        this.property = property;
 
102
 
 
103
    }
 
104
 
 
105
    /**
 
106
     * Return the server value.
 
107
     */
 
108
    public String getValue() {
 
109
 
 
110
        return (this.value);
 
111
 
 
112
    }
 
113
 
 
114
    /**
 
115
     * Set the server value.
 
116
     *
 
117
     * @param value The new server value
 
118
     */
 
119
    public void setValue(String value) {
 
120
 
 
121
        this.value = value;
 
122
 
 
123
    }
 
124
 
 
125
    // --------------------------------------------------------- Public Methods
 
126
 
 
127
    /**
 
128
     * Process the beginning of this tag.
 
129
     *
 
130
     * @exception JspException if a JSP exception has occurred
 
131
     */
 
132
    public int doStartTag() throws JspException {
 
133
 
 
134
        // Defer processing until the end of this tag is encountered
 
135
        this.constant = null;
 
136
        return (EVAL_BODY_TAG);
 
137
 
 
138
    }
 
139
 
 
140
    /**
 
141
     * Save the body contents of this tag as the constant that we will
 
142
     * be returning.
 
143
     *
 
144
     * @exception JspException if a JSP exception has occurred
 
145
     */
 
146
    public int doAfterBody() throws JspException {
 
147
 
 
148
        if (bodyContent != null) {
 
149
            this.constant = bodyContent.getString().trim();
 
150
        }
 
151
            
 
152
        if ("".equals(this.constant)) {
 
153
            this.constant = null;
 
154
        }
 
155
            
 
156
        return SKIP_BODY;
 
157
    }
 
158
 
 
159
    /**
 
160
     * Render an input element for this tag.
 
161
     *
 
162
     * @exception JspException if a JSP exception has occurred
 
163
     */
 
164
    public int doEndTag() throws JspException {
 
165
 
 
166
        // Create an appropriate "input" element based on our parameters
 
167
        StringBuffer results = new StringBuffer("<input type=\"checkbox\"");
 
168
        results.append(" name=\"");
 
169
        results.append(this.property);
 
170
        results.append("\"");
 
171
        if (accesskey != null) {
 
172
            results.append(" accesskey=\"");
 
173
            results.append(accesskey);
 
174
            results.append("\"");
 
175
        }
 
176
        if (tabindex != null) {
 
177
            results.append(" tabindex=\"");
 
178
            results.append(tabindex);
 
179
            results.append("\"");
 
180
        }
 
181
        results.append(" value=\"");
 
182
        String value = (this.value == null) ? this.constant : this.value;
 
183
            
 
184
        if (value == null) {
 
185
            JspException e = new JspException(messages.getMessage("multiboxTag.value"));
 
186
            pageContext.setAttribute(Globals.EXCEPTION_KEY, e, PageContext.REQUEST_SCOPE);
 
187
            throw e;
 
188
        }
 
189
        results.append(TagUtils.getInstance().filter(value));
 
190
        results.append("\"");
 
191
        Object bean = TagUtils.getInstance().lookup(pageContext, name, null);
 
192
        String values[] = null;
 
193
        
 
194
        if (bean == null) {
 
195
            throw new JspException(messages.getMessage("getter.bean", name));
 
196
        }
 
197
            
 
198
        try {
 
199
            values = BeanUtils.getArrayProperty(bean, property);
 
200
            if (values == null) {
 
201
                values = new String[0];
 
202
            }
 
203
                
 
204
        } catch (IllegalAccessException e) {
 
205
            throw new JspException(messages.getMessage("getter.access", property, name));
 
206
        } catch (InvocationTargetException e) {
 
207
            Throwable t = e.getTargetException();
 
208
            throw new JspException(messages.getMessage("getter.result", property, t.toString()));
 
209
        } catch (NoSuchMethodException e) {
 
210
            throw new JspException(messages.getMessage("getter.method", property, name));
 
211
        }
 
212
        
 
213
        for (int i = 0; i < values.length; i++) {
 
214
            if (value.equals(values[i])) {
 
215
                results.append(" checked=\"checked\"");
 
216
                break;
 
217
            }
 
218
        }
 
219
        
 
220
        results.append(prepareEventHandlers());
 
221
        results.append(prepareStyles());
 
222
        results.append(getElementClose());
 
223
 
 
224
        TagUtils.getInstance().write(pageContext, results.toString());
 
225
 
 
226
        return EVAL_PAGE;
 
227
    }
 
228
 
 
229
    /**
 
230
     * Release any acquired resources.
 
231
     */
 
232
    public void release() {
 
233
 
 
234
        super.release();
 
235
        constant = null;
 
236
        name = Constants.BEAN_KEY;
 
237
        property = null;
 
238
        value = null;
 
239
 
 
240
    }
 
241
 
 
242
}