KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > tags > form > OptionTag


1 /*
2  * Copyright 2002-2007 the original author or authors.
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
17 package org.springframework.web.servlet.tags.form;
18
19 import javax.servlet.jsp.JspException JavaDoc;
20 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
21 import javax.servlet.jsp.tagext.BodyTag JavaDoc;
22
23 import org.springframework.util.Assert;
24 import org.springframework.web.servlet.support.BindStatus;
25 import org.springframework.web.util.TagUtils;
26
27 /**
28  * JSP tag for rendering an HTML '<code>option</code>' tag.
29  *
30  * <p><b>Must be used nested inside a {@link SelectTag}.</b>
31  *
32  * <p>Provides full support for databinding by marking an
33  * '<code>option</code>' as 'selected' if the {@link #setValue value}
34  * matches the value bound to the out {@link SelectTag}.
35  *
36  * <p>The {@link #setValue value} property is required and corresponds to
37  * the '<code>value</code>' attribute of the rendered '<code>option</code>'.
38  *
39  * <p>An optional {@link #setLabel label} property can be specified, the
40  * value of which corresponds to inner text of the rendered
41  * '<code>option</code>' tag. If no {@link #setLabel label} is specified
42  * then the {@link #setValue value} property will be used when rendering
43  * the inner text.
44  *
45  * @author Rob Harrop
46  * @since 2.0
47  */

48 public class OptionTag extends AbstractHtmlElementBodyTag implements BodyTag JavaDoc {
49
50     /**
51      * The name of the JSP variable used to expose the value for this tag.
52      */

53     public static final String JavaDoc VALUE_VARIABLE_NAME = "value";
54
55     /**
56      * The name of the JSP variable used to expose the display value for this tag.
57      */

58     public static final String JavaDoc DISPLAY_VALUE_VARIABLE_NAME = "displayValue";
59
60
61     /**
62      * The name of the '<code>selected</code>' attribute.
63      */

64     private static final String JavaDoc SELECTED_ATTRIBUTE = "selected";
65
66     /**
67      * The name of the '<code>value</code>' attribute.
68      */

69     private static final String JavaDoc VALUE_ATTRIBUTE = VALUE_VARIABLE_NAME;
70     
71     /**
72      * The name of the '<code>disabled</code>' attribute.
73      */

74     private static final String JavaDoc DISABLED_ATTRIBUTE = "disabled";
75
76
77     /**
78      * The 'value' attribute of the rendered HTML <code>&lt;option&gt;</code> tag.
79      */

80     private Object JavaDoc value;
81
82     /**
83      * The text body of the rendered HTML <code>&lt;option&gt;</code> tag.
84      */

85     private String JavaDoc label;
86
87     private Object JavaDoc oldValue;
88
89     private Object JavaDoc oldDisplayValue;
90     
91     private String JavaDoc disabled;
92
93
94     /**
95      * Set the 'value' attribute of the rendered HTML <code>&lt;option&gt;</code> tag.
96      * <p>May be a runtime expression.
97      */

98     public void setValue(Object JavaDoc value) {
99         this.value = value;
100     }
101
102     /**
103      * Get the 'value' attribute of the rendered HTML <code>&lt;option&gt;</code> tag.
104      * <p>May be a runtime expression.
105      */

106     protected Object JavaDoc getValue() {
107         return this.value;
108     }
109     
110     /**
111      * Set the value of the '<code>disabled</code>' attribute.
112      * <p>May be a runtime expression.
113      * @param disabled the value of the '<code>disabled</code>' attribute
114      */

115     public void setDisabled(String JavaDoc disabled) {
116         this.disabled = disabled;
117     }
118
119     /**
120      * Get the value of the '<code>disabled</code>' attribute.
121      * <p>May be a runtime expression.
122      * @return the value of the '<code>disabled</code>' attribute
123      */

124     protected String JavaDoc getDisabled() {
125         return disabled;
126     }
127     
128     /**
129      * Is the current HTML tag disabled?
130      * @return <code>true</code> if this tag is disabled
131      */

132     protected boolean isDisabled() {
133         return "true".equals(getDisabled());
134     }
135
136     /**
137      * Set the text body of the rendered HTML <code>&lt;option&gt;</code> tag.
138      * <p>May be a runtime expression.
139      * @throws IllegalArgumentException if the supplied <code>label</code> is <code>null</code>
140      */

141     public void setLabel(String JavaDoc label) {
142         Assert.notNull(label, "'label' must not be null");
143         this.label = label;
144     }
145
146     /**
147      * Get the text body of the rendered HTML <code>&lt;option&gt;</code> tag.
148      * <p>May be a runtime expression.
149      */

150     protected String JavaDoc getLabel() {
151         return this.label;
152     }
153
154
155     protected void renderDefaultContent(TagWriter tagWriter) throws JspException JavaDoc {
156         Object JavaDoc value = this.pageContext.getAttribute(VALUE_VARIABLE_NAME);
157         String JavaDoc label = getLabelValue(value);
158         renderOption(value, label, tagWriter);
159     }
160
161     protected void renderFromBodyContent(BodyContent JavaDoc bodyContent, TagWriter tagWriter) throws JspException JavaDoc {
162         Object JavaDoc value = this.pageContext.getAttribute(VALUE_VARIABLE_NAME);
163         String JavaDoc label = bodyContent.getString();
164         renderOption(value, label, tagWriter);
165     }
166
167     /**
168      * Make sure we are under a '<code>select</code>' tag before proceeding.
169      */

170     protected void onWriteTagContent() {
171         assertUnderSelectTag();
172     }
173
174     protected void exposeAttributes() throws JspException JavaDoc {
175         Object JavaDoc value = resolveValue();
176         this.oldValue = this.pageContext.getAttribute(VALUE_VARIABLE_NAME);
177         this.pageContext.setAttribute(VALUE_VARIABLE_NAME, value);
178         this.oldDisplayValue = this.pageContext.getAttribute(DISPLAY_VALUE_VARIABLE_NAME);
179         this.pageContext.setAttribute(DISPLAY_VALUE_VARIABLE_NAME, getDisplayString(value, getBindStatus().getEditor()));
180     }
181
182     protected BindStatus getBindStatus() {
183         return (BindStatus) this.pageContext.getAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE);
184     }
185
186     protected void removeAttributes() {
187         if (this.oldValue != null) {
188             this.pageContext.setAttribute(VALUE_ATTRIBUTE, this.oldValue);
189             this.oldValue = null;
190         }
191         else {
192             this.pageContext.removeAttribute(VALUE_VARIABLE_NAME);
193         }
194
195         if (this.oldDisplayValue != null) {
196             this.pageContext.setAttribute(DISPLAY_VALUE_VARIABLE_NAME, this.oldDisplayValue);
197             this.oldDisplayValue = null;
198         }
199         else {
200             this.pageContext.removeAttribute(DISPLAY_VALUE_VARIABLE_NAME);
201         }
202     }
203
204
205     private void renderOption(Object JavaDoc value, String JavaDoc label, TagWriter tagWriter) throws JspException JavaDoc {
206         tagWriter.startTag("option");
207
208         String JavaDoc renderedValue = getDisplayString(value, getBindStatus().getEditor());
209
210
211         tagWriter.writeAttribute(VALUE_ATTRIBUTE, renderedValue);
212
213         if (isSelected(value)) {
214             tagWriter.writeAttribute(SELECTED_ATTRIBUTE, SELECTED_ATTRIBUTE);
215         }
216         if(isDisabled()) {
217             tagWriter.writeAttribute(DISABLED_ATTRIBUTE, "disabled");
218         }
219         tagWriter.appendValue(label);
220
221         tagWriter.endTag();
222     }
223
224     /**
225      * Returns the value of the label for this '<code>option</code>' element.
226      * If the {@link #setLabel label} property is set then the resolved value
227      * of that property is used, otherwise the value of the <code>resolvedValue</code>
228      * argument is used.
229      */

230     private String JavaDoc getLabelValue(Object JavaDoc resolvedValue) throws JspException JavaDoc {
231         String JavaDoc label = getLabel();
232         Object JavaDoc labelObj = (label == null ? resolvedValue : evaluate("label", label));
233         return getDisplayString(labelObj, getBindStatus().getEditor());
234     }
235
236     private void assertUnderSelectTag() {
237         TagUtils.assertHasAncestorOfType(this, SelectTag.class, "option", "select");
238     }
239
240     private boolean isSelected(Object JavaDoc resolvedValue) {
241         return SelectedValueComparator.isSelected(getBindStatus(), resolvedValue);
242     }
243
244     private Object JavaDoc resolveValue() throws JspException JavaDoc {
245         return evaluate(VALUE_VARIABLE_NAME, getValue());
246     }
247
248 }
249
Popular Tags