KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > view > jsp > html > OptionTag


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.view.jsp.html;
8
9
10 import java.io.IOException JavaDoc;
11
12 import javax.servlet.jsp.JspException JavaDoc;
13
14 import org.apache.log4j.Logger;
15
16 import com.inversoft.util.ObjectTools;
17 import com.inversoft.verge.mvc.view.HtmlConstants;
18 import com.inversoft.verge.mvc.view.HtmlViewToolkit;
19 import com.inversoft.verge.mvc.view.jsp.JspTools;
20
21
22 /**
23  * This class is the single option tag used for inserting a
24  * single option into the page.
25  *
26  * @author Brian Pontarelli
27  */

28 public class OptionTag extends HtmlBaseTag {
29
30     /**
31      * This classes logger
32      */

33     private static final Logger logger = Logger.getLogger(OptionTag.class);
34
35     private Object JavaDoc value;
36     protected Object JavaDoc localValue;
37     private String JavaDoc label;
38     protected String JavaDoc localLabel;
39     private Boolean JavaDoc selected;
40     protected Boolean JavaDoc localSelected;
41     Boolean JavaDoc disabled;
42     protected SelectTag selectParent;
43
44
45     /**
46      * Retrieves the tag's value attribute
47      *
48      * @return Returns the tag's value attribute
49      */

50     public Object JavaDoc getValue() {
51         return value;
52     }
53
54     /**
55      * Populates the tag's value attribute
56      *
57      * @param value The value of the tag's value attribute
58      */

59     public void setValue(Object JavaDoc value) {
60         this.value = value;
61     }
62
63     /**
64      * Retrieves the tag's selected attribute
65      *
66      * @return Returns the tag's selected attribute
67      */

68     public Boolean JavaDoc getSelected() {
69         return selected;
70     }
71
72     /**
73      * Populates the tag's selected attribute
74      *
75      * @param selected The value of the tag's selected attribute
76      */

77     public void setSelected(Boolean JavaDoc selected) {
78         this.selected = selected;
79     }
80
81     /**
82      * Retrieves the tag's disabled attribute
83      *
84      * @return Returns the tag's disabled attribute
85      */

86     public Boolean JavaDoc getDisabled() {
87         return disabled;
88     }
89
90     /**
91      * Populates the tag's disabled attribute
92      *
93      * @param disabled The value of the tag's disabled attribute
94      */

95     public void setDisabled(Boolean JavaDoc disabled) {
96         this.disabled = disabled;
97     }
98
99     /**
100      * Retrieves the tag's label attribute
101      *
102      * @return Returns the tag's label attribute
103      */

104     public String JavaDoc getLabel() {
105         return label;
106     }
107
108     /**
109      * Populates the tag's label attribute
110      *
111      * @param label The value of the tag's label attribute
112      */

113     public void setLabel(String JavaDoc label) {
114         this.label = label;
115     }
116
117     /**
118      * Initializes the tag by determining if the option tag is in a select tag
119      *
120      * @throws JspException If the option tag is not in a selected tag
121      */

122     protected void initialize() throws JspException JavaDoc {
123         super.initialize();
124
125         localLabel = label;
126         localValue = value;
127         localSelected = selected;
128
129         if (!JspTools.JSP_20) {
130             localLabel = (String JavaDoc) JspTools.expand("label", label, String JavaDoc.class,
131                 this, pageContext);
132
133             if (value instanceof String JavaDoc) {
134                 localValue = JspTools.expand("value", (String JavaDoc) value, Object JavaDoc.class,
135                     this, pageContext);
136             }
137         }
138
139         if (disabled != null && disabled.booleanValue()) {
140             singleAttrs.add(HtmlConstants.DISABLED);
141         }
142
143         attributes.put(HtmlConstants.LABEL, localLabel);
144
145         // Check with the parent for selectedness
146
selectParent = (SelectTag) findAncestorWithClass(this, SelectTag.class);
147         if (selectParent == null) {
148             throw new JspException JavaDoc("Option tags must be imbedded in select tags");
149         }
150
151         Object JavaDoc parentValue = selectParent.getLocalValue();
152         if (parentValue != null) {
153             // Test if the value of the select tag is equal to this value
154
localSelected =
155                 new Boolean JavaDoc(ObjectTools.areObjectsEqual(localValue, parentValue));
156         }
157     }
158
159     /**
160      * Outputs the options tag. This also checks with the select tag parent to
161      * determine if this options value is equal to the select tags value. If
162      * it is, then the option is selected, otherwise it is not. The selected
163      * property of this class is used first and the comparison is used second.
164      *
165      * @return Always returns EVAL_BODY_TAG
166      */

167     public int doStartTag() throws JspException JavaDoc {
168
169         initialize();
170
171         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
172         createOptionTag(buf, getId(), localValue, localSelected);
173
174         if (logger.isDebugEnabled()) {
175             logger.debug("The option tag: " + buf.toString());
176         }
177
178         try {
179             pageContext.getOut().print(buf.toString());
180         } catch (IOException JavaDoc ioe) {
181             throw new JspException JavaDoc(ioe.toString());
182         }
183
184         return EVAL_BODY_INCLUDE;
185     }
186
187     /**
188      * Outputs the options tag end tag
189      *
190      * @return Always returns EVAL_PAGE
191      */

192     public int doEndTag() throws JspException JavaDoc {
193         try {
194             pageContext.getOut().print(HtmlConstants.OPTION_END_TAG);
195         } catch (IOException JavaDoc ioe) {
196             throw new JspException JavaDoc(ioe.toString());
197         }
198
199         // Reset the tag's attributes that are important so that the tag instance
200
// can be re-used by the container and won't screw anything up
201
selected = null;
202
203         return EVAL_PAGE;
204     }
205
206     /**
207      * Appends the option tag to the StringBuffer.
208      *
209      * @param buf The StringBuffer to append to
210      * @param id The id of the option tag
211      * @param value The value of the option tag
212      * @param selected Whether or not the option tag is selected
213      */

214     protected void createOptionTag(StringBuffer JavaDoc buf, String JavaDoc id, Object JavaDoc value,
215             Boolean JavaDoc selected) {
216         boolean selectedBool = (selected == null) ? false : selected.booleanValue();
217         String JavaDoc valueStr = ObjectTools.toString(value);
218         HtmlViewToolkit.createOptionStartTag(buf, valueStr, selectedBool,
219             attributes, singleAttrs);
220     }
221 }
Popular Tags