View Javadoc

1   /*
2    * $Id: OptionTag.java 376841 2006-02-10 21:01:28Z husted $
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  package org.apache.struts.taglib.html;
19  
20  import org.apache.struts.Globals;
21  import org.apache.struts.taglib.TagUtils;
22  import org.apache.struts.util.MessageResources;
23  
24  import javax.servlet.jsp.JspException;
25  import javax.servlet.jsp.tagext.BodyTagSupport;
26  
27  /***
28   * Tag for select options.  The body of this tag is presented to the user in
29   * the option list, while the value attribute is the value returned to the
30   * server if this option is selected.
31   *
32   * @version $Rev: 376841 $ $Date: 2005-08-21 19:08:45 -0400 (Sun, 21 Aug 2005)
33   *          $
34   */
35  public class OptionTag extends BodyTagSupport {
36      // ----------------------------------------------------- Instance Variables
37  
38      /***
39       * The message resources for this package.
40       */
41      protected static MessageResources messages =
42          MessageResources.getMessageResources(Constants.Package
43              + ".LocalStrings");
44  
45      /***
46       * The message text to be displayed to the user for this tag (if any)
47       */
48      protected String text = null;
49  
50      // ------------------------------------------------------------- Properties
51  
52      /***
53       * The name of the servlet context attribute containing our message
54       * resources.
55       */
56      protected String bundle = Globals.MESSAGES_KEY;
57  
58      /***
59       * Is this option disabled?
60       */
61      protected boolean disabled = false;
62  
63      /***
64       * The key used to look up the text displayed to the user for this option,
65       * if any.
66       */
67      protected String key = null;
68  
69      /***
70       * The name of the attribute containing the Locale to be used for looking
71       * up internationalized messages.
72       */
73      protected String locale = Globals.LOCALE_KEY;
74  
75      /***
76       * The style associated with this tag.
77       */
78      private String style = null;
79  
80      /***
81       * The named style class associated with this tag.
82       */
83      private String styleClass = null;
84  
85      /***
86       * The identifier associated with this tag.
87       */
88      protected String styleId = null;
89  
90      /***
91       * The server value for this option, also used to match against the
92       * current property value to determine whether this option should be
93       * marked as selected.
94       */
95      protected String value = null;
96  
97      public String getBundle() {
98          return (this.bundle);
99      }
100 
101     public void setBundle(String bundle) {
102         this.bundle = bundle;
103     }
104 
105     public boolean getDisabled() {
106         return (this.disabled);
107     }
108 
109     public void setDisabled(boolean disabled) {
110         this.disabled = disabled;
111     }
112 
113     public String getKey() {
114         return (this.key);
115     }
116 
117     public void setKey(String key) {
118         this.key = key;
119     }
120 
121     public String getLocale() {
122         return (this.locale);
123     }
124 
125     public void setLocale(String locale) {
126         this.locale = locale;
127     }
128 
129     public String getStyle() {
130         return style;
131     }
132 
133     public void setStyle(String style) {
134         this.style = style;
135     }
136 
137     public String getStyleClass() {
138         return styleClass;
139     }
140 
141     public void setStyleClass(String styleClass) {
142         this.styleClass = styleClass;
143     }
144 
145     /***
146      * Return the style identifier for this tag.
147      */
148     public String getStyleId() {
149         return (this.styleId);
150     }
151 
152     /***
153      * Set the style identifier for this tag.
154      *
155      * @param styleId The new style identifier
156      */
157     public void setStyleId(String styleId) {
158         this.styleId = styleId;
159     }
160 
161     public String getValue() {
162         return (this.value);
163     }
164 
165     public void setValue(String value) {
166         this.value = value;
167     }
168 
169     // --------------------------------------------------------- Public Methods
170 
171     /***
172      * Process the start of this tag.
173      *
174      * @throws JspException if a JSP exception has occurred
175      */
176     public int doStartTag() throws JspException {
177         // Initialize the placeholder for our body content
178         this.text = null;
179 
180         // Do nothing until doEndTag() is called
181         return (EVAL_BODY_TAG);
182     }
183 
184     /***
185      * Process the body text of this tag (if any).
186      *
187      * @throws JspException if a JSP exception has occurred
188      */
189     public int doAfterBody() throws JspException {
190         if (bodyContent != null) {
191             String text = bodyContent.getString();
192 
193             if (text != null) {
194                 text = text.trim();
195 
196                 if (text.length() > 0) {
197                     this.text = text;
198                 }
199             }
200         }
201 
202         return (SKIP_BODY);
203     }
204 
205     /***
206      * Process the end of this tag.
207      *
208      * @throws JspException if a JSP exception has occurred
209      */
210     public int doEndTag() throws JspException {
211         TagUtils.getInstance().write(pageContext, this.renderOptionElement());
212 
213         return (EVAL_PAGE);
214     }
215 
216     /***
217      * Generate an HTML %lt;option> element.
218      *
219      * @throws JspException
220      * @since Struts 1.1
221      */
222     protected String renderOptionElement()
223         throws JspException {
224         StringBuffer results = new StringBuffer("<option value=\"");
225 
226         results.append(this.value);
227         results.append("\"");
228 
229         if (disabled) {
230             results.append(" disabled=\"disabled\"");
231         }
232 
233         if (this.selectTag().isMatched(this.value)) {
234             results.append(" selected=\"selected\"");
235         }
236 
237         if (style != null) {
238             results.append(" style=\"");
239             results.append(style);
240             results.append("\"");
241         }
242 
243         if (styleId != null) {
244             results.append(" id=\"");
245             results.append(styleId);
246             results.append("\"");
247         }
248 
249         if (styleClass != null) {
250             results.append(" class=\"");
251             results.append(styleClass);
252             results.append("\"");
253         }
254 
255         results.append(">");
256 
257         results.append(text());
258 
259         results.append("</option>");
260 
261         return results.toString();
262     }
263 
264     /***
265      * Acquire the select tag we are associated with.
266      *
267      * @throws JspException
268      */
269     private SelectTag selectTag()
270         throws JspException {
271         SelectTag selectTag =
272             (SelectTag) pageContext.getAttribute(Constants.SELECT_KEY);
273 
274         if (selectTag == null) {
275             JspException e =
276                 new JspException(messages.getMessage("optionTag.select"));
277 
278             TagUtils.getInstance().saveException(pageContext, e);
279             throw e;
280         }
281 
282         return selectTag;
283     }
284 
285     /***
286      * Release any acquired resources.
287      */
288     public void release() {
289         super.release();
290         bundle = Globals.MESSAGES_KEY;
291         disabled = false;
292         key = null;
293         locale = Globals.LOCALE_KEY;
294         style = null;
295         styleClass = null;
296         text = null;
297         value = null;
298     }
299 
300     // ------------------------------------------------------ Protected Methods
301 
302     /***
303      * Return the text to be displayed to the user for this option (if any).
304      *
305      * @throws JspException if an error occurs
306      */
307     protected String text() throws JspException {
308         String optionText = this.text;
309 
310         if ((optionText == null) && (this.key != null)) {
311             optionText =
312                 TagUtils.getInstance().message(pageContext, bundle, locale, key);
313         }
314 
315         // no body text and no key to lookup so display the value
316         if (optionText == null) {
317             optionText = this.value;
318         }
319 
320         return optionText;
321     }
322 }