KickJava   Java API By Example, From Geeks To Geeks.

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


1 package fr.improve.struts.taglib.layout.field;
2
3 import java.lang.reflect.InvocationTargetException JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 import javax.servlet.jsp.JspException JavaDoc;
7 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
8
9 import org.apache.commons.beanutils.PropertyUtils;
10 import org.apache.struts.taglib.html.Constants;
11 import org.apache.struts.util.MessageResources;
12
13 import fr.improve.struts.taglib.layout.util.LayoutUtils;
14 import fr.improve.struts.taglib.layout.util.TagUtils;
15
16 /**
17  * Tag for creating multiple <select> options from a collection. The
18  * collection may be part of the enclosing form, or may be independent of
19  * the form. Each element of the collection must expose a 'label' and a
20  * 'value', the property names of which are configurable by attributes of
21  * this tag.
22  * <p>
23  * The collection may be an array of objects, a Collection, an Enumeration,
24  * an Iterator, or a Map.
25  * <p>
26  * This tag is the struts-layout version of the struts-html tag.
27  * <p>
28  * <b>NOTE</b> - This tag requires a Java2 (JDK 1.2 or later) platform.
29  *
30  * @author JN Ribette
31  * @author Martin Cooper
32  */

33
34 public class OptionsCollectionTag extends TagSupport JavaDoc implements Choice {
35
36     // ----------------------------------------------------- Instance Variables
37

38     /**
39      * The message resources for this package.
40      */

41     protected static MessageResources messages =
42         MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
43
44     /**
45      * The name of the bean property containing the label.
46      */

47     protected String JavaDoc label = "label";
48     
49     
50     /**
51      * The computed label.
52      */

53     protected String JavaDoc choiceLabel;
54     
55     /**
56      * The computed value.
57      */

58     protected String JavaDoc choiceValue;
59     
60
61     public String JavaDoc getLabel() {
62         return label;
63     }
64
65     public void setLabel(String JavaDoc label) {
66         this.label = label;
67     }
68
69     /**
70      * The name of the bean containing the values collection.
71      */

72     protected String JavaDoc name = Constants.BEAN_KEY;
73
74     public String JavaDoc getName() {
75         return name;
76     }
77
78     public void setName(String JavaDoc name) {
79         this.name = name;
80     }
81
82     /**
83      * The name of the property to use to build the values collection.
84      */

85     protected String JavaDoc property = null;
86
87     public String JavaDoc getProperty() {
88         return property;
89     }
90
91     public void setProperty(String JavaDoc property) {
92         this.property = property;
93     }
94
95     /**
96      * The name of the bean property containing the value.
97      */

98     protected String JavaDoc value = "value";
99
100     public String JavaDoc getValue() {
101         return value;
102     }
103
104     public void setValue(String JavaDoc value) {
105         this.value = value;
106     }
107
108     // --------------------------------------------------------- Public Methods
109

110     /**
111      * Process the start of this tag.
112      *
113      * @exception JspException if a JSP exception has occurred
114      */

115     public int doStartTag() throws JspException JavaDoc {
116
117     
118         ChoiceTag choiceTag;
119         // Acquire the select or radios tag we are associated with
120
choiceTag = (ChoiceTag) findAncestorWithClass(this, ChoiceTag.class);
121         if (choiceTag==null) {
122             throw new JspException JavaDoc(messages.getMessage("optionsTag.select"));
123         }
124         
125         // Acquire the collection containing our options
126
Object JavaDoc collection = TagUtils.lookup(pageContext, name, property, null);
127
128         if (collection == null) {
129             JspException JavaDoc e =
130                 new JspException JavaDoc(messages.getMessage("optionsCollectionTag.collection"));
131             TagUtils.saveException(pageContext, e);
132             throw e;
133         }
134
135         // Acquire an iterator over the options collection
136
Iterator JavaDoc iter = LayoutUtils.getIterator(collection);
137
138         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
139
140         // Render the options
141
while (iter.hasNext()) {
142
143             Object JavaDoc bean = iter.next();
144             Object JavaDoc beanLabel = null;
145             Object JavaDoc beanValue = null;
146
147             // Get the label for this option
148
try {
149                 beanLabel = PropertyUtils.getProperty(bean, label);
150                 if (beanLabel == null) {
151                     beanLabel = "";
152                 }
153             } catch (IllegalAccessException JavaDoc e) {
154                 JspException JavaDoc jspe =
155                     new JspException JavaDoc(messages.getMessage("getter.access", label, bean));
156                 TagUtils.saveException(pageContext, jspe);
157                 throw jspe;
158             } catch (InvocationTargetException JavaDoc e) {
159                 Throwable JavaDoc t = e.getTargetException();
160                 JspException JavaDoc jspe =
161                     new JspException JavaDoc(messages.getMessage("getter.result", label, t.toString()));
162                 TagUtils.saveException(pageContext, jspe);
163                 throw jspe;
164             } catch (NoSuchMethodException JavaDoc e) {
165                 JspException JavaDoc jspe =
166                     new JspException JavaDoc(messages.getMessage("getter.method", label, bean));
167                 TagUtils.saveException(pageContext, jspe);
168                 throw jspe;
169             }
170
171             // Get the value for this option
172
try {
173                 beanValue = PropertyUtils.getProperty(bean, value);
174                 if (beanValue == null) {
175                     beanValue = "";
176                 }
177             } catch (IllegalAccessException JavaDoc e) {
178                 JspException JavaDoc jspe =
179                     new JspException JavaDoc(messages.getMessage("getter.access", value, bean));
180                 TagUtils.saveException(pageContext, jspe);
181                 throw jspe;
182             } catch (InvocationTargetException JavaDoc e) {
183                 Throwable JavaDoc t = e.getTargetException();
184                 JspException JavaDoc jspe =
185                     new JspException JavaDoc(messages.getMessage("getter.result", value, t.toString()));
186                 TagUtils.saveException(pageContext, jspe);
187                 throw jspe;
188             } catch (NoSuchMethodException JavaDoc e) {
189                 JspException JavaDoc jspe =
190                     new JspException JavaDoc(messages.getMessage("getter.method", value, bean));
191                 TagUtils.saveException(pageContext, jspe);
192                 throw jspe;
193             }
194
195             choiceLabel = beanLabel.toString();
196             choiceValue = beanValue.toString();
197
198             // Render this option
199
choiceTag.addChoice(sb, this);
200         }
201
202         // Render this element to our writer
203
TagUtils.write(pageContext, sb.toString());
204
205         return SKIP_BODY;
206     }
207
208     /**
209      * Release any acquired resources.
210      */

211     public void release() {
212         super.release();
213         label = "label";
214         name = Constants.BEAN_KEY;
215         property = null;
216         value = "value";
217     }
218
219     /**
220      * @see fr.improve.struts.taglib.layout.field.Choice#getChoiceLabel()
221      */

222     public String JavaDoc getChoiceLabel() {
223         return choiceLabel;
224     }
225     /**
226      * @see fr.improve.struts.taglib.layout.field.Choice#getChoiceTooltip()
227      */

228     public String JavaDoc getChoiceTooltip() {
229         return null;
230     }
231     /**
232      * @see fr.improve.struts.taglib.layout.field.Choice#getChoiceValue()
233      */

234     public String JavaDoc getChoiceValue() {
235         return choiceValue;
236     }
237 }
238
Popular Tags