KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Collection JavaDoc;
12 import java.util.Map JavaDoc;
13
14 import javax.servlet.jsp.JspException JavaDoc;
15
16 import org.apache.log4j.Category;
17
18 import com.inversoft.beans.BeanException;
19 import com.inversoft.beans.NestedBeanProperty;
20 import com.inversoft.util.ObjectTools;
21 import com.inversoft.verge.mvc.view.HtmlViewToolkit;
22 import com.inversoft.verge.mvc.view.jsp.JspTools;
23
24
25 /**
26  * This class is the option array tag used for an arry of
27  * options.
28  *
29  * @author Brian Pontarelli
30  */

31 public class OptionsArrayTag extends HtmlBaseTag {
32
33     /** The logger category */
34     private static final Category logger = Category.getInstance(OptionsArrayTag.class);
35
36     private Object JavaDoc items;
37     private Object JavaDoc localItems;
38     private String JavaDoc valueProperty;
39     private String JavaDoc textProperty;
40     protected SelectTag selectParent;
41
42
43     /**
44      * Retrieves the tag's items attribute
45      *
46      * @return Returns the tag's items attribute
47      */

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

57     public void setItems(Object JavaDoc items) {
58         this.items = items;
59     }
60
61     /**
62      * Retrieves the tag's value property attribute
63      *
64      * @return Returns the tag's value property attribute
65      */

66     public String JavaDoc getValueProperty() {
67         return valueProperty;
68     }
69
70     /**
71      * Populates the tag's value property attribute
72      *
73      * @param valueProperty The value of the tag's value property attribute
74      */

75     public void setValueProperty(String JavaDoc valueProperty) {
76         this.valueProperty = valueProperty;
77     }
78
79     /**
80      * Retrieves the tag's text property attribute
81      *
82      * @return Returns the tag's text property attribute
83      */

84     public String JavaDoc getTextProperty() {
85         return textProperty;
86     }
87
88     /**
89      * Populates the tag's text property attribute
90      *
91      * @param textProperty The value of the tag's text property attribute
92      */

93     public void setTextProperty(String JavaDoc textProperty) {
94         this.textProperty = textProperty;
95     }
96
97     /**
98      * Calls the super.initialize and also expands the items attribute if possible.
99      */

100     protected void initialize() throws JspException JavaDoc {
101         super.initialize();
102
103         selectParent = (SelectTag) findAncestorWithClass(this, SelectTag.class);
104         if (selectParent == null) {
105             throw new JspException JavaDoc("Option tags must be imbedded in select tags");
106         }
107
108         localItems = items;
109         if (items instanceof String JavaDoc) {
110             localItems = JspTools.expand("items", (String JavaDoc) items, Object JavaDoc.class,
111                 this, pageContext);
112         }
113     }
114
115     /**
116      * Outputs an option tags for each element in the array or collection or
117      * Map. This also checks with the SelectTag parent in order to determine if
118      * the select tags value is equal to the values of the options. If the values
119      * are equals, this method makes the option(s) selected.
120      *
121      * @return Always returns SKIP_BODY
122      * @throws JspException If there was any problems retrieve the data from the
123      * array
124      */

125     public int doStartTag() throws JspException JavaDoc {
126
127         // Setup the parents references including the parent tag
128
initialize();
129
130         // Convert the current bean value (which is whatever was referenced by the
131
// array attribute) and convert it to an array or throw an exception
132
Object JavaDoc [] finalArray = null;
133         boolean isMap = false;
134
135         if (localItems instanceof Object JavaDoc []) {
136             finalArray = (Object JavaDoc []) localItems;
137         } else if (localItems instanceof Collection JavaDoc) {
138             finalArray = ((Collection JavaDoc) localItems).toArray();
139         } else if (localItems instanceof Map JavaDoc) {
140             isMap = true;
141             finalArray = ((Map JavaDoc) localItems).keySet().toArray();
142         } else if (localItems == null) {
143             return SKIP_BODY;
144         } else {
145             throw new JspException JavaDoc("Array attribute must refer to an array or" +
146                 " Collection or Map");
147         }
148
149         // For each array position, output an option tag
150
NestedBeanProperty prop;
151         Object JavaDoc textObject;
152         Object JavaDoc valueObject;
153         Object JavaDoc text;
154         Object JavaDoc value;
155         boolean selected;
156         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
157
158         for (int i = 0; i < finalArray.length; i++) {
159
160             if (isMap) {
161                 textObject = ((Map JavaDoc) localItems).get(finalArray[i]);
162                 valueObject = finalArray[i];
163             } else {
164                 textObject = finalArray[i];
165                 valueObject = finalArray[i];
166             }
167
168             try {
169
170                 // Get the value
171
if (valueProperty == null) {
172                     value = valueObject;
173                 } else {
174                     prop = new NestedBeanProperty(valueProperty, valueObject.getClass());
175                     value = prop.getPropertyValue(valueObject);
176                 }
177
178                 // Get the text
179
if (textProperty == null) {
180                     text = textObject;
181                 } else {
182                     prop = new NestedBeanProperty(textProperty, textObject.getClass());
183                     text = prop.getPropertyValue(textObject);
184                 }
185             } catch (BeanException be) {
186                 throw new JspException JavaDoc(be.toString());
187             }
188
189             // Test if the value of the select tag is equal to this value
190
selected = ObjectTools.areObjectsEqual(value, selectParent.getLocalValue());
191             createOptionTag(buf, value.toString(), text.toString(), selected);
192             buf.append("\n");
193
194             if (logger.isDebugEnabled()) {
195                 logger.debug("The option tags: " + buf.toString());
196             }
197         }
198
199         try {
200             pageContext.getOut().print(buf.toString());
201         } catch (IOException JavaDoc ioe) {
202             throw new JspException JavaDoc(ioe.toString());
203         }
204
205         return SKIP_BODY;
206     }
207
208     /**
209      * Does the work of creating the tags body. This method can be overridden
210      * in sub-classes to change the attributes that are appended, etc. By default
211      * this creates an option tag with the name, value, attributes (single and named)
212      * and selected
213      *
214      * @param buf The StringBuffer to append to
215      */

216     protected void createOptionTag(StringBuffer JavaDoc buf, String JavaDoc value, String JavaDoc text,
217             boolean selected) {
218         HtmlViewToolkit.createOptionTag(buf, id, value, text, selected,
219             attributes, singleAttrs);
220     }
221 }
Popular Tags