KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > input > Option


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

16 package org.apache.taglibs.input;
17
18 import java.util.Map JavaDoc;
19
20 import javax.servlet.jsp.JspException JavaDoc;
21 import javax.servlet.jsp.JspTagException JavaDoc;
22 import javax.servlet.jsp.JspWriter JavaDoc;
23 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
24
25 /**
26  *
27  * This class implements the <input:option> tag, which presents an
28  * <option> form element.
29  *
30  * @version 0.90
31  * @author Karl von Randow
32  */

33
34 public class Option extends BodyTagSupport JavaDoc {
35
36     private String JavaDoc value; // value of option
37

38     private Map JavaDoc attributes; // attributes of the <select> element
39

40     private String JavaDoc attributesText; // attributes of the <option> element as text
41

42     public void release() {
43         super.release();
44         value = null;
45         attributes = null;
46         attributesText = null;
47     }
48
49     public int doStartTag() throws JspException JavaDoc {
50         // It seems to be necessary to clear the body as release doesn't do
51
// this?
52
if (getBodyContent() != null) {
53             getBodyContent().clearBody();
54         }
55         return EVAL_BODY_BUFFERED;
56     }
57
58     public int doEndTag() throws JspException JavaDoc {
59         try {
60             String JavaDoc content = getBodyContent() != null ? getBodyContent()
61                     .getString() : null;
62
63             // get what we need from the page
64
JspWriter JavaDoc out = pageContext.getOut();
65
66             String JavaDoc value = this.value;
67             if (value == null && content != null) {
68                 value = content.trim();
69             }
70
71             // start building up the tag
72
out.print("<option ");
73             if (value != null) {
74                 out.print("value=\"" + Util.quote(value) + "\" ");
75             }
76
77             // include any attributes we've got here
78
Util.printAttributes(out, attributes);
79             if (attributesText != null) {
80                 out.print(attributesText + " ");
81             }
82
83             /*
84              * Print out our options, selecting one or more if appropriate. If
85              * there are multiple selections but the page doesn't call for a
86              * <select> that accepts them, ignore the selections. This is
87              * preferable to throwing a JspException because the (end) user can
88              * control input, and we don't want the user causing exceptions in
89              * our application.
90              */

91
92             if (testAndRemoveChosen(value)) {
93                 out.print("selected=\"selected\" ");
94             }
95
96             // end the starting tag
97
out.print(">");
98             if (content != null) {
99                 out.print(content);
100             }
101             out.print("</option>");
102         } catch (Exception JavaDoc ex) {
103             throw new JspTagException JavaDoc(ex.getMessage());
104         }
105         return EVAL_PAGE;
106     }
107
108     protected boolean testAndRemoveChosen(String JavaDoc value) throws JspException JavaDoc {
109         Select selectTag = (Select) findAncestorWithClass(this, Select.class);
110         if (selectTag != null) {
111             Map JavaDoc chosen = selectTag.getChosen();
112             if (value != null && chosen != null && chosen.containsKey(value)) {
113                 if (!selectTag.isMultiple()) {
114                     chosen.remove(value);
115                 }
116                 return true;
117             } else {
118                 return false;
119             }
120         } else {
121             throw new JspTagException JavaDoc("option tag used outside a select tag");
122         }
123     }
124
125     public void setValue(String JavaDoc x) {
126         value = x;
127     }
128
129     /**
130      * Getter for property value.
131      *
132      * @return Value of property value.
133      */

134     public String JavaDoc getValue() {
135         return value;
136     }
137
138     public void setAttributes(Map JavaDoc x) {
139         attributes = x;
140     }
141
142     /**
143      * Getter for property attributes.
144      *
145      * @return Value of property attributes.
146      */

147     public Map JavaDoc getAttributes() {
148         return attributes;
149     }
150
151     public void setAttributesText(String JavaDoc x) {
152         attributesText = x;
153     }
154
155     /**
156      * Getter for property attributesText.
157      *
158      * @return Value of property attributesText.
159      */

160     public String JavaDoc getAttributesText() {
161         return attributesText;
162     }
163
164 }
Popular Tags