001: /*
002: * $Id: OptionTag.java 479633 2006-11-27 14:25:35Z pbenedict $
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.Globals;
024: import org.apache.struts.taglib.TagUtils;
025: import org.apache.struts.util.MessageResources;
026:
027: import javax.servlet.jsp.JspException;
028: import javax.servlet.jsp.tagext.BodyTagSupport;
029:
030: /**
031: * Tag for select options. The body of this tag is presented to the user in
032: * the option list, while the value attribute is the value returned to the
033: * server if this option is selected.
034: *
035: * @version $Rev: 479633 $ $Date: 2005-08-21 19:08:45 -0400 (Sun, 21 Aug 2005)
036: * $
037: */
038: public class OptionTag extends BodyTagSupport {
039: // ----------------------------------------------------- Instance Variables
040:
041: /**
042: * The message resources for this package.
043: */
044: protected static MessageResources messages = MessageResources
045: .getMessageResources(Constants.Package + ".LocalStrings");
046:
047: /**
048: * The message text to be displayed to the user for this tag (if any)
049: */
050: protected String text = null;
051:
052: // ------------------------------------------------------------- Properties
053:
054: /**
055: * The name of the servlet context attribute containing our message
056: * resources.
057: */
058: protected String bundle = Globals.MESSAGES_KEY;
059:
060: /**
061: * Is this option disabled?
062: */
063: protected boolean disabled = false;
064:
065: /**
066: * Should the label be filtered for HTML sensitive characters?
067: */
068: protected boolean filter = false;
069:
070: /**
071: * The key used to look up the text displayed to the user for this option,
072: * if any.
073: */
074: protected String key = null;
075:
076: /**
077: * The name of the attribute containing the Locale to be used for looking
078: * up internationalized messages.
079: */
080: protected String locale = Globals.LOCALE_KEY;
081:
082: /**
083: * The style associated with this tag.
084: */
085: private String style = null;
086:
087: /**
088: * The named style class associated with this tag.
089: */
090: private String styleClass = null;
091:
092: /**
093: * The identifier associated with this tag.
094: */
095: protected String styleId = null;
096:
097: /**
098: * The language code of this element.
099: */
100: private String lang = null;
101:
102: /**
103: * The direction for weak/neutral text of this element.
104: */
105: private String dir = null;
106:
107: /**
108: * The server value for this option, also used to match against the
109: * current property value to determine whether this option should be
110: * marked as selected.
111: */
112: protected String value = null;
113:
114: public String getBundle() {
115: return (this .bundle);
116: }
117:
118: public void setBundle(String bundle) {
119: this .bundle = bundle;
120: }
121:
122: public boolean getDisabled() {
123: return (this .disabled);
124: }
125:
126: public void setDisabled(boolean disabled) {
127: this .disabled = disabled;
128: }
129:
130: public boolean getFilter() {
131: return (this .filter);
132: }
133:
134: public void setFilter(boolean filter) {
135: this .filter = filter;
136: }
137:
138: public String getKey() {
139: return (this .key);
140: }
141:
142: public void setKey(String key) {
143: this .key = key;
144: }
145:
146: public String getLocale() {
147: return (this .locale);
148: }
149:
150: public void setLocale(String locale) {
151: this .locale = locale;
152: }
153:
154: public String getStyle() {
155: return style;
156: }
157:
158: public void setStyle(String style) {
159: this .style = style;
160: }
161:
162: public String getStyleClass() {
163: return styleClass;
164: }
165:
166: public void setStyleClass(String styleClass) {
167: this .styleClass = styleClass;
168: }
169:
170: /**
171: * Return the style identifier for this tag.
172: */
173: public String getStyleId() {
174: return (this .styleId);
175: }
176:
177: /**
178: * Set the style identifier for this tag.
179: *
180: * @param styleId The new style identifier
181: */
182: public void setStyleId(String styleId) {
183: this .styleId = styleId;
184: }
185:
186: public String getValue() {
187: return (this .value);
188: }
189:
190: public void setValue(String value) {
191: this .value = value;
192: }
193:
194: /**
195: * Returns the language code of this element.
196: *
197: * @since Struts 1.3.6
198: */
199: public String getLang() {
200: return this .lang;
201: }
202:
203: /**
204: * Sets the language code of this element.
205: *
206: * @since Struts 1.3.6
207: */
208: public void setLang(String lang) {
209: this .lang = lang;
210: }
211:
212: /**
213: * Returns the direction for weak/neutral text this element.
214: *
215: * @since Struts 1.3.6
216: */
217: public String getDir() {
218: return this .dir;
219: }
220:
221: /**
222: * Sets the direction for weak/neutral text of this element.
223: *
224: * @since Struts 1.3.6
225: */
226: public void setDir(String dir) {
227: this .dir = dir;
228: }
229:
230: // --------------------------------------------------------- Public Methods
231:
232: /**
233: * Process the start of this tag.
234: *
235: * @throws JspException if a JSP exception has occurred
236: */
237: public int doStartTag() throws JspException {
238: // Initialize the placeholder for our body content
239: this .text = null;
240:
241: // Do nothing until doEndTag() is called
242: return (EVAL_BODY_TAG);
243: }
244:
245: /**
246: * Process the body text of this tag (if any).
247: *
248: * @throws JspException if a JSP exception has occurred
249: */
250: public int doAfterBody() throws JspException {
251: if (bodyContent != null) {
252: String text = bodyContent.getString();
253:
254: if (text != null) {
255: text = text.trim();
256:
257: if (text.length() > 0) {
258: this .text = text;
259: }
260: }
261: }
262:
263: return (SKIP_BODY);
264: }
265:
266: /**
267: * Process the end of this tag.
268: *
269: * @throws JspException if a JSP exception has occurred
270: */
271: public int doEndTag() throws JspException {
272: TagUtils.getInstance().write(pageContext,
273: this .renderOptionElement());
274:
275: return (EVAL_PAGE);
276: }
277:
278: /**
279: * Generate an HTML %lt;option> element.
280: *
281: * @throws JspException
282: * @since Struts 1.1
283: */
284: protected String renderOptionElement() throws JspException {
285: StringBuffer results = new StringBuffer("<option value=\"");
286:
287: if (filter) {
288: results.append(TagUtils.getInstance().filter(this .value));
289: } else {
290: results.append(this .value);
291: }
292: results.append("\"");
293:
294: if (disabled) {
295: results.append(" disabled=\"disabled\"");
296: }
297:
298: if (this .selectTag().isMatched(this .value)) {
299: results.append(" selected=\"selected\"");
300: }
301:
302: if (style != null) {
303: results.append(" style=\"");
304: results.append(style);
305: results.append("\"");
306: }
307:
308: if (styleId != null) {
309: results.append(" id=\"");
310: results.append(styleId);
311: results.append("\"");
312: }
313:
314: if (styleClass != null) {
315: results.append(" class=\"");
316: results.append(styleClass);
317: results.append("\"");
318: }
319:
320: if (dir != null) {
321: results.append(" dir=\"");
322: results.append(dir);
323: results.append("\"");
324: }
325:
326: if (lang != null) {
327: results.append(" lang=\"");
328: results.append(lang);
329: results.append("\"");
330: }
331:
332: results.append(">");
333:
334: results.append(text());
335:
336: results.append("</option>");
337:
338: return results.toString();
339: }
340:
341: /**
342: * Acquire the select tag we are associated with.
343: *
344: * @throws JspException
345: */
346: private SelectTag selectTag() throws JspException {
347: SelectTag selectTag = (SelectTag) pageContext
348: .getAttribute(Constants.SELECT_KEY);
349:
350: if (selectTag == null) {
351: JspException e = new JspException(messages
352: .getMessage("optionTag.select"));
353:
354: TagUtils.getInstance().saveException(pageContext, e);
355: throw e;
356: }
357:
358: return selectTag;
359: }
360:
361: /**
362: * Release any acquired resources.
363: */
364: public void release() {
365: super .release();
366: bundle = Globals.MESSAGES_KEY;
367: dir = null;
368: disabled = false;
369: key = null;
370: lang = null;
371: locale = Globals.LOCALE_KEY;
372: style = null;
373: styleClass = null;
374: text = null;
375: value = null;
376: }
377:
378: // ------------------------------------------------------ Protected Methods
379:
380: /**
381: * Return the text to be displayed to the user for this option (if any).
382: *
383: * @throws JspException if an error occurs
384: */
385: protected String text() throws JspException {
386: String optionText = this .text;
387:
388: if ((optionText == null) && (this .key != null)) {
389: optionText = TagUtils.getInstance().message(pageContext,
390: bundle, locale, key);
391: }
392:
393: // no body text and no key to lookup so display the value
394: if (optionText == null) {
395: optionText = this.value;
396: }
397:
398: return optionText;
399: }
400: }
|