KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > field > OptionsTag


1 package fr.improve.struts.taglib.layout.field;
2
3 import java.util.Iterator JavaDoc;
4
5 import javax.servlet.jsp.JspException JavaDoc;
6 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
7
8 import org.apache.struts.taglib.html.Constants;
9 import org.apache.struts.util.MessageResources;
10
11 import fr.improve.struts.taglib.layout.el.Expression;
12 import fr.improve.struts.taglib.layout.util.LayoutUtils;
13 import fr.improve.struts.taglib.layout.util.TagUtils;
14
15 /**
16  * Tag for creating multiple <select> options from a collection. The
17  * associated values displayed to the user may optionally be specified by a
18  * second collection, or will be the same as the values themselves. Each
19  * collection may be an array of objects, a Collection, an Iterator, or a
20  * Map. <b>NOTE</b> - This tag requires a Java2 (JDK 1.2 or later) platform.
21  *
22  * Tag modified to use the ChoiceTag interface of struts-layout
23  *
24  * @author Florent Carpentier
25  * @author Craig McClanahan
26  * @author Jean-Noel Ribette
27  */

28
29 public class OptionsTag extends TagSupport JavaDoc implements Choice {
30
31     /**
32      * The message resources for this package.
33      */

34     protected static MessageResources messages =
35      MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
36
37     /**
38      * The name of the collection containing beans that have properties to
39      * provide both the values and the labels (identified by the
40      * <code>property</code> and <code>labelProperty</code> attributes).
41      */

42     protected String JavaDoc collection = null;
43
44     /**
45      * The name of the bean containing the labels collection.
46      */

47     protected String JavaDoc labelName = null;
48
49     /**
50      * The bean property containing the labels collection.
51      */

52     protected String JavaDoc labelProperty = null;
53
54     /**
55      * The name of the bean containing the values collection.
56      */

57     protected String JavaDoc name=null;
58
59     /**
60      * The name of the property to use to build the values collection.
61      */

62     protected String JavaDoc property=null;
63     protected String JavaDoc jspProperty = null;
64     
65     /**
66      * The name of the other select tags whose values depends of values definied by this options tag.
67      */

68     protected String JavaDoc sourceOf;
69     
70     protected String JavaDoc choiceLabel;
71     protected String JavaDoc choiceValue;
72
73     /**
74      * Process the end of this tag.
75      *
76      * @exception JspException if a JSP exception has occurred
77      */

78     public int doEndTag() throws JspException JavaDoc {
79
80         ChoiceTag choiceTag;
81     // Acquire the select or radios tag we are associated with
82
try {
83             choiceTag = (ChoiceTag) getParent();
84         } catch (ClassCastException JavaDoc e) {
85             throw new JspException JavaDoc(messages.getMessage("optionsTag.select"));
86         }
87         
88         // Compute dynamic values.
89
initDynamicValues();
90
91     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
92     // If a collection was specified, use that mode to render options
93
if (collection != null) {
94             Iterator JavaDoc collIterator = getIterator(collection, null);
95             while (collIterator.hasNext()) {
96                 Object JavaDoc bean = collIterator.next();
97                 Object JavaDoc value = null;
98                 Object JavaDoc label = null;;
99
100                     value = getValueFromBean(bean, property);
101                     if (value == null)
102                         value = "";
103                     if (labelProperty != null)
104                         label = getLabelFromBean(bean, labelProperty);
105                     else
106                         label = value;
107                     if (label == null)
108                         label = "";
109                 choiceLabel = label.toString();
110                 choiceValue = value.toString();
111                 choiceTag.addChoice(sb, this);
112             }
113             if (sourceOf!=null) {
114                 initDependentCombo();
115             }
116         }
117
118         // Otherwise, use the separate iterators mode to render options
119
else {
120
121               // Construct iterators for the values and labels collections
122
Iterator JavaDoc valuesIterator = getIterator(name, property);
123               Iterator JavaDoc labelsIterator = null;
124               if ((labelName == null) && (labelProperty == null))
125                   labelsIterator = getIterator(name, property); // Same coll.
126
else
127                   labelsIterator = getIterator(labelName, labelProperty);
128
129               // Render the options tags for each element of the values coll.
130
while (valuesIterator.hasNext()) {
131                   Object JavaDoc obj = valuesIterator.next();
132                   choiceValue = obj==null ? null : obj.toString();
133                   choiceLabel = choiceValue;
134                   if (labelsIterator.hasNext()) {
135                       obj = labelsIterator.next();
136                       choiceLabel = obj==null ? null : obj.toString();
137                   }
138                   choiceTag.addChoice(sb, this);
139               }
140         }
141
142     // Render this element to our writer
143
TagUtils.write(pageContext, sb.toString());
144     
145     // Reset
146
reset();
147
148     return EVAL_PAGE;
149
150     }
151         
152     /**
153      * Init dynamic values.
154      */

155     protected void initDynamicValues() {
156         jspProperty = property;
157         property = Expression.evaluate(jspProperty, pageContext);
158     }
159     
160     /**
161      * Reset dynamic values.
162      */

163     protected void reset() {
164         property = jspProperty;
165         jspProperty = null;
166     }
167
168     /**
169      * make two combo interdependent.
170      */

171     protected void initDependentCombo() {
172         SelectTag lc_select = (SelectTag) findAncestorWithClass(this, SelectTag.class);
173         OptionsDependentTag.DependentInfo lc_info = (OptionsDependentTag.DependentInfo) OptionsDependentTag.getDependentInfo(lc_select.getProperty(), pageContext);
174         lc_info.mainCollectionName = collection;
175         lc_info.mainCollectionProperty = null;
176         lc_info.mainCollectionBeanProperty = property;
177     }
178
179     protected Object JavaDoc getLabelFromBean(Object JavaDoc in_bean, String JavaDoc in_labelProperty) throws JspException JavaDoc {
180         return LayoutUtils.getProperty(in_bean, in_labelProperty);
181     }
182     protected Object JavaDoc getValueFromBean(Object JavaDoc in_bean, String JavaDoc in_property) throws JspException JavaDoc {
183         return LayoutUtils.getProperty(in_bean, in_property);
184     }
185     /**
186      * Process the start of this tag.
187      *
188      * @exception JspException if a JSP exception has occurred
189      */

190
191     public int doStartTag() throws JspException JavaDoc {
192     return SKIP_BODY;
193     }
194     public String JavaDoc getCollection() {
195         return (this.collection);
196     }
197     /**
198      * Return an iterator for the option labels or values, based on our
199      * configured properties.
200      *
201      * @param name Name of the bean attribute (if any)
202      * @param property Name of the bean property (if any)
203      *
204      * @exception JspException if an error occurs
205      */

206     protected Iterator JavaDoc getIterator(String JavaDoc name, String JavaDoc property) throws JspException JavaDoc {
207         String JavaDoc lc_beanName = name;
208         if (lc_beanName == null) {
209             lc_beanName = Constants.BEAN_KEY;
210         }
211         return LayoutUtils.getIterator(pageContext, lc_beanName, property);
212             
213     }
214     public String JavaDoc getLabelName() {
215         return labelName;
216     }
217     public String JavaDoc getLabelProperty() {
218         return labelProperty;
219     }
220     public String JavaDoc getName() {
221         return name;
222     }
223     public String JavaDoc getProperty() {
224         return property;
225     }
226     /**
227      * Release any acquired resources.
228      */

229     public void release() {
230         super.release();
231         collection = null;
232         labelName = null;
233         labelProperty = null;
234         name = null;
235         property = null;
236         sourceOf = null;
237     }
238     public void setCollection(String JavaDoc collection) {
239         this.collection = collection;
240     }
241     public void setLabelName(String JavaDoc labelName) {
242         this.labelName = labelName;
243     }
244     public void setLabelProperty(String JavaDoc labelProperty) {
245         this.labelProperty = labelProperty;
246     }
247     public void setName(String JavaDoc name) {
248         this.name = name;
249     }
250     public void setProperty(String JavaDoc property) {
251         this.property = property;
252     }
253     /**
254      * @return Returns the sourceOf.
255      */

256     public String JavaDoc getSourceOf() {
257         return sourceOf;
258     }
259     /**
260      * @param sourceOf The sourceOf to set.
261      */

262     public void setSourceOf(String JavaDoc sourceOf) {
263         this.sourceOf = sourceOf;
264     }
265     
266     /**
267      * @see fr.improve.struts.taglib.layout.field.Choice#getChoiceLabel()
268      */

269     public String JavaDoc getChoiceLabel() {
270         return choiceLabel;
271     }
272     /**
273      * @see fr.improve.struts.taglib.layout.field.Choice#getChoiceTooltip()
274      */

275     public String JavaDoc getChoiceTooltip() {
276         return null;
277     }
278     /**
279      * @see fr.improve.struts.taglib.layout.field.Choice#getChoiceValue()
280      */

281     public String JavaDoc getChoiceValue() {
282         return choiceValue;
283     }
284 }
285
Popular Tags