KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 import org.springframework.util.Assert;
22 import org.springframework.util.ObjectUtils;
23 import org.springframework.web.servlet.support.BindStatus;
24 import org.springframework.web.util.TagUtils;
25
26 /**
27  * Convenient tag that allows one to supply a collection of objects
28  * that are to be rendered as '<code>option</code>' tags within a
29  * '<code>select</code>' tag.
30  *
31  * <p><i>Must</i> be used within a {@link SelectTag 'select' tag}.
32  *
33  * @author Rob Harrop
34  * @since 2.0
35  */

36 public class OptionsTag extends AbstractFormTag {
37     
38     /**
39      * The {@link java.util.Collection}, {@link java.util.Map} or array of
40      * objects used to generate the inner '<code>option</code>' tags.
41      */

42     private Object JavaDoc items;
43
44     /**
45      * The name of the property mapped to the '<code>value</code>' attribute
46      * of the '<code>option</code>' tag.
47      */

48     private String JavaDoc itemValue;
49
50     /**
51      * The name of the property mapped to the inner text of the
52      * '<code>option</code>' tag.
53      */

54     private String JavaDoc itemLabel;
55
56
57     /**
58      * Set the {@link java.util.Collection}, {@link java.util.Map} or array
59      * of objects used to generate the inner '<code>option</code>' tags.
60      * <p>Required when wishing to render '<code>option</code>' tags from an
61      * array, {@link java.util.Collection} or {@link java.util.Map}.
62      * <p>Typically a runtime expression.
63      * @param items said items
64      * @throws IllegalArgumentException if the supplied <code>items</code> instance is <code>null</code>
65      */

66     public void setItems(Object JavaDoc items) {
67         this.items = items;
68     }
69
70     /**
71      * Get the {@link java.util.Collection}, {@link java.util.Map} or array
72      * of objects used to generate the inner '<code>option</code>' tags.
73      * <p>Typically a runtime expression.
74      */

75     protected Object JavaDoc getItems() {
76         return this.items;
77     }
78
79     /**
80      * Set the name of the property mapped to the '<code>value</code>'
81      * attribute of the '<code>option</code>' tag.
82      * <p>Required when wishing to render '<code>option</code>' tags from
83      * an array or {@link java.util.Collection}.
84      * <p>May be a runtime expression.
85      */

86     public void setItemValue(String JavaDoc itemValue) {
87         Assert.hasText(itemValue, "'itemValue' must not be empty");
88         this.itemValue = itemValue;
89     }
90
91     protected String JavaDoc getItemValue() {
92         return this.itemValue;
93     }
94
95     /**
96      * Set the name of the property mapped to the label (inner text) of the
97      * '<code>option</code>' tag.
98      * <p>May be a runtime expression.
99      */

100     public void setItemLabel(String JavaDoc itemLabel) {
101         Assert.hasText(itemLabel, "'itemLabel' must not be empty");
102         this.itemLabel = itemLabel;
103     }
104
105     /**
106      * Get the name of the property mapped to the label (inner text) of the
107      * '<code>option</code>' tag.
108      * <p>May be a runtime expression.
109      */

110     protected String JavaDoc getItemLabel() {
111         return this.itemLabel;
112     }
113
114
115     protected int writeTagContent(TagWriter tagWriter) throws JspException JavaDoc {
116         assertUnderSelectTag();
117         Object JavaDoc items = getItems();
118         Object JavaDoc itemsObject = (items instanceof String JavaDoc ? evaluate("items", (String JavaDoc) items) : items);
119
120         if (itemsObject != null) {
121             String JavaDoc itemValue = getItemValue();
122             String JavaDoc itemLabel = getItemLabel();
123
124             String JavaDoc valueProperty =
125                     (itemValue != null ? ObjectUtils.getDisplayString(evaluate("itemValue", itemValue)) : null);
126             String JavaDoc labelProperty =
127                     (itemLabel != null ? ObjectUtils.getDisplayString(evaluate("itemLabel", itemLabel)) : null);
128
129             OptionWriter optionWriter =
130                     new OptionWriter(itemsObject, getBindStatus(), valueProperty, labelProperty, isHtmlEscape());
131             optionWriter.writeOptions(tagWriter);
132         }
133
134         return EVAL_PAGE;
135     }
136
137
138     private void assertUnderSelectTag() {
139         TagUtils.assertHasAncestorOfType(this, SelectTag.class, "options", "select");
140     }
141
142     private BindStatus getBindStatus() {
143         return (BindStatus) this.pageContext.getAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE);
144     }
145
146 }
147
Popular Tags