KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > html > OptionTag


1 /*
2  * $Id: OptionTag.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.struts.taglib.html;
20
21 import java.util.Locale JavaDoc;
22
23 import javax.servlet.jsp.JspException JavaDoc;
24 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
25
26 import org.apache.struts.Globals;
27 import org.apache.struts.taglib.TagUtils;
28 import org.apache.struts.util.MessageResources;
29
30 /**
31  * Tag for select options. The body of this tag is presented to the user
32  * in the option list, while the value attribute is the value returned to
33  * the server if this option is selected.
34  *
35  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
36  */

37 public class OptionTag extends BodyTagSupport JavaDoc {
38
39     // ----------------------------------------------------- Instance Variables
40

41     /**
42      * The default locale for our server.
43      * @deprecated Use Locale.getDefault() directly.
44      */

45     protected static final Locale JavaDoc defaultLocale = Locale.getDefault();
46
47     /**
48      * The message resources for this package.
49      */

50     protected static MessageResources messages =
51         MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
52
53     /**
54      * The message text to be displayed to the user for this tag (if any)
55      */

56     protected String JavaDoc text = null;
57
58     // ------------------------------------------------------------- Properties
59

60     /**
61      * The name of the servlet context attribute containing our message
62      * resources.
63      */

64     protected String JavaDoc bundle = Globals.MESSAGES_KEY;
65
66     public String JavaDoc getBundle() {
67         return (this.bundle);
68     }
69
70     public void setBundle(String JavaDoc bundle) {
71         this.bundle = bundle;
72     }
73
74     /**
75      * Is this option disabled?
76      */

77     protected boolean disabled = false;
78
79     public boolean getDisabled() {
80         return (this.disabled);
81     }
82
83     public void setDisabled(boolean disabled) {
84         this.disabled = disabled;
85     }
86
87     /**
88      * The key used to look up the text displayed to the user for this
89      * option, if any.
90      */

91     protected String JavaDoc key = null;
92
93     public String JavaDoc getKey() {
94         return (this.key);
95     }
96
97     public void setKey(String JavaDoc key) {
98         this.key = key;
99     }
100
101     /**
102      * The name of the attribute containing the Locale to be used for
103      * looking up internationalized messages.
104      */

105     protected String JavaDoc locale = Globals.LOCALE_KEY;
106
107     public String JavaDoc getLocale() {
108         return (this.locale);
109     }
110
111     public void setLocale(String JavaDoc locale) {
112         this.locale = locale;
113     }
114
115     /**
116      * The style associated with this tag.
117      */

118     private String JavaDoc style = null;
119
120     public String JavaDoc getStyle() {
121         return style;
122     }
123
124     public void setStyle(String JavaDoc style) {
125         this.style = style;
126     }
127
128     /**
129      * The named style class associated with this tag.
130      */

131     private String JavaDoc styleClass = null;
132
133     public String JavaDoc getStyleClass() {
134         return styleClass;
135     }
136
137     public void setStyleClass(String JavaDoc styleClass) {
138         this.styleClass = styleClass;
139     }
140
141     /**
142      * The identifier associated with this tag.
143      */

144     protected String JavaDoc styleId = null;
145
146     /**
147      * Return the style identifier for this tag.
148      */

149     public String JavaDoc getStyleId() {
150
151         return (this.styleId);
152
153     }
154
155     /**
156      * Set the style identifier for this tag.
157      *
158      * @param styleId The new style identifier
159      */

160     public void setStyleId(String JavaDoc styleId) {
161
162         this.styleId = styleId;
163
164     }
165
166     /**
167      * The server value for this option, also used to match against the
168      * current property value to determine whether this option should be
169      * marked as selected.
170      */

171     protected String JavaDoc value = null;
172
173     public String JavaDoc getValue() {
174         return (this.value);
175     }
176
177     public void setValue(String JavaDoc value) {
178         this.value = value;
179     }
180
181     // --------------------------------------------------------- Public Methods
182

183     /**
184      * Process the start of this tag.
185      *
186      * @exception JspException if a JSP exception has occurred
187      */

188     public int doStartTag() throws JspException JavaDoc {
189
190         // Initialize the placeholder for our body content
191
this.text = null;
192
193         // Do nothing until doEndTag() is called
194
return (EVAL_BODY_TAG);
195
196     }
197
198     /**
199      * Process the body text of this tag (if any).
200      *
201      * @exception JspException if a JSP exception has occurred
202      */

203     public int doAfterBody() throws JspException JavaDoc {
204
205         if (bodyContent != null) {
206             String JavaDoc text = bodyContent.getString();
207             if (text != null) {
208                 text = text.trim();
209                 if (text.length() > 0) {
210                     this.text = text;
211                 }
212             }
213         }
214         return (SKIP_BODY);
215
216     }
217
218     /**
219      * Process the end of this tag.
220      *
221      * @exception JspException if a JSP exception has occurred
222      */

223     public int doEndTag() throws JspException JavaDoc {
224
225         TagUtils.getInstance().write(pageContext, this.renderOptionElement());
226
227         return (EVAL_PAGE);
228     }
229
230     /**
231      * Generate an HTML %lt;option> element.
232      * @throws JspException
233      * @since Struts 1.1
234      */

235     protected String JavaDoc renderOptionElement() throws JspException JavaDoc {
236         StringBuffer JavaDoc results = new StringBuffer JavaDoc("<option value=\"");
237         
238         results.append(this.value);
239         results.append("\"");
240         if (disabled) {
241             results.append(" disabled=\"disabled\"");
242         }
243         if (this.selectTag().isMatched(this.value)) {
244             results.append(" selected=\"selected\"");
245         }
246         if (style != null) {
247             results.append(" style=\"");
248             results.append(style);
249             results.append("\"");
250         }
251         if (styleId != null) {
252             results.append(" id=\"");
253             results.append(styleId);
254             results.append("\"");
255         }
256         if (styleClass != null) {
257             results.append(" class=\"");
258             results.append(styleClass);
259             results.append("\"");
260         }
261         results.append(">");
262
263         results.append(text());
264         
265         results.append("</option>");
266         return results.toString();
267     }
268
269     /**
270      * Acquire the select tag we are associated with.
271      * @throws JspException
272      */

273     private SelectTag selectTag() throws JspException JavaDoc {
274         SelectTag selectTag =
275             (SelectTag) pageContext.getAttribute(Constants.SELECT_KEY);
276             
277         if (selectTag == null) {
278             JspException JavaDoc e =
279                 new JspException JavaDoc(messages.getMessage("optionTag.select"));
280                 
281             TagUtils.getInstance().saveException(pageContext, e);
282             throw e;
283         }
284         
285         return selectTag;
286     }
287
288     /**
289      * Release any acquired resources.
290      */

291     public void release() {
292         super.release();
293         bundle = Globals.MESSAGES_KEY;
294         disabled = false;
295         key = null;
296         locale = Globals.LOCALE_KEY;
297         style = null;
298         styleClass = null;
299         text = null;
300         value = null;
301     }
302
303     // ------------------------------------------------------ Protected Methods
304

305     /**
306      * Return the text to be displayed to the user for this option (if any).
307      *
308      * @exception JspException if an error occurs
309      */

310     protected String JavaDoc text() throws JspException JavaDoc {
311         String JavaDoc optionText = this.text;
312
313         if ((optionText == null) && (this.key != null)) {
314             optionText =
315                 TagUtils.getInstance().message(pageContext, bundle, locale, key);
316         }
317
318         // no body text and no key to lookup so display the value
319
if (optionText == null) {
320             optionText = this.value;
321         }
322
323         return optionText;
324     }
325
326 }
327
Popular Tags