View Javadoc

1   package mx.com.brained.taglib;
2   
3   import java.lang.reflect.*;
4   import java.util.*;
5   
6   import javax.servlet.http.*;
7   import javax.servlet.jsp.*;
8   import javax.servlet.jsp.tagext.*;
9   
10  import org.apache.commons.beanutils.*;
11  import org.apache.commons.collections.*;
12  import org.apache.struts.*;
13  import org.apache.struts.taglib.*;
14  import org.apache.struts.taglib.html.*;
15  /***
16   *
17   * <p>Title: AwTaglib</p>
18   *
19   * <p>Description: ComboBox Grid Element Tag</p>
20   *
21   * <p>Copyright: Copyright (c) 2005</p>
22   *
23   * <p>Company: Brain-ED</p>
24   *
25   * @author Isidoro Treviņo, Eliseo Partida
26   * @version 1.0
27   * @jsp.tag
28   *   name="comboGridElement"
29   *   body-content="empty"
30   *   description="Tag that renders a combobox"
31   *
32   */
33  public class ComboGridElement extends TagSupport{
34      private String name;
35      private String property;
36      private String title;
37      private String type=GridConstants.COMBOBOX;
38      private String bundle;
39      private String locale = Globals.LOCALE_KEY;
40      private String action="";
41      private String options;
42      private String labelProperty;
43      private String labelValue;
44      private String hiddenValues="false";
45  
46  
47      public ComboGridElement() {
48      }
49      /***
50       * This method renders the combobox based on the properties set
51       * @return int
52       * @throws JspException
53       */
54      public int doStartTag() throws JspException {
55          Tag tag = this.getParent();
56          GridIteratorTag parent;
57          GridUtil util;
58          HttpServletResponse response =
59                      (HttpServletResponse)this.pageContext.getResponse();
60          if (tag != null && tag instanceof GridIteratorTag) {
61              parent = (GridIteratorTag) tag;
62              util = parent.getGridUtil();
63              if (parent.getIndex() == 0) {
64                  String message =
65                          TagUtils.getInstance().message(
66                                  pageContext,
67                                  this.bundle,
68                                  locale,
69                                  title,
70                                  null);
71                  HashMap map = new HashMap();
72                  map.put(GridConstants.PROPERTY,property);
73                  map.put(GridConstants.HEADER,message);
74                  map.put(GridConstants.TYPE,type);
75                  map.put(GridConstants.HIDDEN_VALUES,hiddenValues);
76                  util.addTitle(map);
77                  try {
78                      map.put(GridConstants.COMBOBOX_OPTIONS, getOptions(getIterator(options, null)));
79                      if(hiddenValues.equals(Boolean.toString(true))){
80                          map.put(GridConstants.DOT_OPTIONS,getDotOptions(getIterator(options,null)));
81                      }
82                  } catch (Exception ex) {
83                      throw new JspException(ex);
84                  }
85                  if(action!=""){
86                      action=response.encodeURL(
87                      TagUtils.getInstance().getActionMappingURL(
88                              action,
89                              this.pageContext));
90                  }
91                  map.put(GridConstants.ACTION,action);
92              }
93              Object value = null;
94              try {
95                  value = TagUtils.getInstance().lookup(pageContext, name,
96                          property, null);
97              } catch (JspException ex1) {
98                  util.setEmptyCollection();
99                  value=null;
100             }
101             if(hiddenValues.equals(Boolean.toString(true))){
102                 try {
103                     if(value==null){
104                       value="";
105                     }
106                     Map map = getInternalMap(getIterator(options, null));
107                     value = map.get(value.toString());
108                     if(value==null){
109                         value="";
110                     }
111                 } catch (Exception ex) {
112                     throw new JspException(ex);
113                 }
114             }
115             util.setData(value.toString());
116         }
117         return this.SKIP_BODY;
118     }
119 
120     /***
121      * getInternalMap
122      *
123      * @param iterator Iterator
124      * @return Object
125      */
126     private Map getInternalMap(Iterator iterator) throws NoSuchMethodException,
127             InvocationTargetException, IllegalAccessException {
128         Map map = new HashMap();
129         Object objeto;
130         if(iterator.hasNext()){
131             while (iterator.hasNext()) {
132                 objeto = iterator.next();
133                 map.put(PropertyUtils.getProperty(objeto, labelValue).toString(),PropertyUtils.getProperty(objeto, labelProperty).toString());
134             }
135         }
136         return map;
137     }
138 
139     /***
140      * getDotOptions
141      *
142      * @param iterator Iterator
143      * @return Object
144      */
145     private Object getDotOptions(Iterator iterator) throws
146             NoSuchMethodException, InvocationTargetException,
147             IllegalAccessException {
148         StringBuffer buffer = new StringBuffer();
149         Object objeto;
150         if(iterator.hasNext()){
151             while (iterator.hasNext()) {
152                 objeto = iterator.next();
153                 buffer.append("\"");
154                 buffer.append(PropertyUtils.getProperty(objeto, labelProperty));
155                 buffer.append("\":\"");
156                 buffer.append(PropertyUtils.getProperty(objeto, labelValue));
157                 buffer.append("\",");
158             }
159             buffer.deleteCharAt(buffer.length() - 1);
160         }
161         return buffer.toString();
162     }
163 
164     /***
165      * getOptions
166      *
167      * @param iterator Iterator
168      * @return Object
169      */
170     private Object getOptions(Iterator iterator) throws NoSuchMethodException,
171             InvocationTargetException, IllegalAccessException {
172         StringBuffer buffer = new StringBuffer();
173         Object objeto;
174         if(iterator.hasNext()){
175             while (iterator.hasNext()) {
176                 objeto = iterator.next();
177                 buffer.append("'<option value=\"");
178                 if(hiddenValues.equals(Boolean.toString(true))){
179                     buffer.append(PropertyUtils.getProperty(objeto,
180                             labelProperty));
181                 }
182                 else{
183                     buffer.append(PropertyUtils.getProperty(objeto, labelValue));
184                 }
185                 buffer.append("\">");
186                 buffer.append(PropertyUtils.getProperty(objeto, labelProperty));
187                 buffer.append("</option>'+");
188             }
189             buffer.deleteCharAt(buffer.length() - 1);
190         }
191         return buffer.toString();
192     }
193     /***
194      * Set the name of the bean containing the value to be rendered
195      * @param name String
196      */
197     public void setName(String name) {
198         this.name = name;
199     }
200     /***
201      * Set the property of the bean rendered as a combobox
202      * @param property String
203      */
204     public void setProperty(String property) {
205         this.property = property;
206     }
207     /***
208      * Set the resource bundle key to be rendered as the header
209      * @param title String
210      */
211     public void setTitle(String title) {
212         this.title = title;
213     }
214     /***
215      * Set the bundle to which the key belongs to
216      * @param bundle String
217      */
218     public void setBundle(String bundle) {
219         this.bundle = bundle;
220     }
221     /***
222      * Set the locale to be rendered
223      * @param locale String
224      */
225     public void setLocale(String locale) {
226         this.locale = locale;
227     }
228     /***
229      * Set the action to be called when the combobox changes its value
230      * @param action String
231      */
232     public void setAction(String action) {
233         this.action = action;
234     }
235     /***
236      * Sets the array containing the beans to be rendered as options in the combobox
237      * @param options String
238      */
239     public void setOptions(String options) {
240         this.options = options;
241     }
242     /***
243      * Sets the property to be rendered as the label of the combobox option
244      * @param labelProperty String
245      */
246     public void setLabelProperty(String labelProperty) {
247         this.labelProperty = labelProperty;
248     }
249     /***
250      * Sets the property to be rendered as the value of the combobox option
251      * @param labelValue String
252      */
253     public void setLabelValue(String labelValue) {
254         this.labelValue = labelValue;
255     }
256     /***
257      * If true, the value will be hidden to the user until submit time
258      * @param hiddenValue String
259      */
260     public void setHiddenValues(String hiddenValue) {
261         this.hiddenValues = hiddenValue;
262     }
263 
264     /***
265      * Returns the name of the bean containing the value to be rendered
266      * @return String
267      * @jsp.attribute
268      *    required="true"
269      *    rtexprvalue="false"
270      */
271     public String getName() {
272         return name;
273     }
274     /***
275      * Return the property of the bean rendered as a combobox
276      * @return String
277      * @jsp.attribute
278      *    required="true"
279      *    rtexprvalue="false"
280      *
281      */
282     public String getProperty() {
283         return property;
284     }
285     /***
286       * Return the resource bundle key to be rendered as the header
287       * @return String
288       * @jsp.attribute
289       *    required="true"
290       *    rtexprvalue="false"
291       *
292      */
293     public String getTitle() {
294         return title;
295     }
296     /***
297      * Return the bundle to which the key belongs to
298      * @return String
299      * @jsp.attribute
300      *    required="false"
301      *    rtexprvalue="false"
302      */
303     public String getBundle() {
304         return bundle;
305     }
306     /***
307      * Return the locale that was set
308      * @return String
309      * @jsp.attribute
310      *    required="false"
311      *    rtexprvalue="false"
312      */
313     public String getLocale() {
314         return locale;
315     }
316     /***
317      * Get the action to be called when the combobox changes its value
318      * @return String
319      * @jsp.attribute
320      *    required="false"
321      *    rtexprvalue="true"
322      */
323     public String getAction() {
324         return action;
325     }
326     /***
327      * Get the name of the array containing the beans to be rendered as options in the combobox
328      * @return String
329      * @jsp.attribute
330      *    required="true"
331      *    rtexprvalue="true"
332      */
333     public String getOptions() {
334         return options;
335     }
336     /***
337      * Get the property to be rendered as the label of the combobox option
338      * @return String
339      * @jsp.attribute
340      *    required="true"
341      *    rtexprvalue="true"
342      */
343     public String getLabelProperty() {
344         return labelProperty;
345     }
346     /***
347      * Sets the property to be rendered as the value of the combobox option
348      * @return String
349      * @jsp.attribute
350      *    required="true"
351      *    rtexprvalue="true"
352      */
353     public String getLabelValue() {
354         return labelValue;
355     }
356     /***
357      *
358      * @return String
359      * @jsp.attribute
360      *    required="false"
361      *    rtexprvalue="true"
362      */
363     public String getHiddenValues() {
364         return hiddenValues;
365     }
366 
367     protected Iterator getIterator(String name, String property) throws JspException {
368 
369         // Identify the bean containing our collection
370         String beanName = name;
371         if (beanName == null) {
372             beanName = Constants.BEAN_KEY;
373         }
374 
375         Object bean = TagUtils.getInstance().lookup(pageContext, beanName, null);
376         if (bean == null) {
377             throw new JspException("Bean " +name+" doesn't exist");
378         }
379 
380         // Identify the collection itself
381         Object collection = bean;
382         if (property != null) {
383             try {
384                 collection = PropertyUtils.getProperty(bean, property);
385                 if (collection == null) {
386                     throw new JspException("Property " + property + "doesn't exist");
387                 }
388             } catch (IllegalAccessException e) {
389                 throw new JspException(e);
390             } catch (InvocationTargetException e) {
391                 throw new JspException(e);
392             } catch (NoSuchMethodException e) {
393                 throw new JspException(e);
394             }
395         }
396 
397         // Construct and return an appropriate iterator
398         if (collection.getClass().isArray()) {
399             collection = Arrays.asList((Object[]) collection);
400         }
401 
402         if (collection instanceof Collection) {
403             return (((Collection) collection).iterator());
404 
405         } else if (collection instanceof Iterator) {
406             return ((Iterator) collection);
407 
408         } else if (collection instanceof Map) {
409             return (((Map) collection).entrySet().iterator());
410 
411         } else if (collection instanceof Enumeration) {
412             return IteratorUtils.asIterator((Enumeration) collection);
413 
414         } else {
415             throw new JspException("This object isn't a collection: "+collection);
416         }
417     }
418     /***
419      * Reset the properties to be reused
420      */
421     public void release(){
422         name=null;
423         property=null;
424         title=null;
425         type=GridConstants.COMBOBOX;
426         bundle=null;
427         locale=null;
428         action="";
429         options=null;
430         labelProperty=null;
431         labelValue=null;
432         hiddenValues="false";
433     }
434 }