View Javadoc

1   package mx.com.brained.taglib;
2   
3   import java.util.*;
4   
5   import javax.servlet.jsp.*;
6   import javax.servlet.jsp.tagext.*;
7   
8   import org.apache.struts.*;
9   import org.apache.struts.taglib.*;
10  
11  /***
12   * @jsp.tag
13   *   name="checkboxGridElement"
14   *   body-content="empty"
15   *   description="Tag that renders a checkbox field for the grid"
16   */
17  public class CheckBoxGridElement extends TagSupport {
18      private String name;
19      private String property;
20      private String title;
21      private String type = GridConstants.CHECK;
22      private String bundle;
23      private String locale = Globals.LOCALE_KEY;
24      private String trueValue;
25      private String falseValue;
26  
27  
28      public CheckBoxGridElement() {
29      }
30  
31      /***
32       * Set the name of the bean containing the value to be rendered
33       * @param name String
34       */
35      public void setName(String name) {
36          this.name = name;
37      }
38  
39      /***
40       * Set the property of the bean rendered as a checkbox
41       * @param property String
42       */
43      public void setProperty(String property) {
44          this.property = property;
45      }
46  
47      /***
48       * Set the resource bundle key to be rendered as the header
49       * @param title String
50       */
51      public void setTitle(String title) {
52          this.title = title;
53      }
54  
55      /***
56       * Set the bundle to which the key belongs to
57       * @param bundle String
58       */
59      public void setBundle(String bundle) {
60          this.bundle = bundle;
61      }
62  
63      /***
64       * Set the locale to be rendered
65       * @param locale String
66       */
67      public void setLocale(String locale) {
68          this.locale = locale;
69      }
70  
71      /***
72       * Set the value that will be rendered as a checked checkbox
73       * @param trueValue String
74       */
75      public void setTrueValue(String trueValue) {
76          this.trueValue = trueValue;
77      }
78  
79      /***
80       * Set the value that will be rendered as an unchecked checkbox
81       * @param falseValue String
82       */
83      public void setFalseValue(String falseValue) {
84          this.falseValue = falseValue;
85      }
86  
87      /***
88       * Returns the name of the bean containing the value to be rendered
89       * @return String
90       * @jsp.attribute
91       *    required="true"
92       *    rtexprvalue="false"
93       */
94      public String getName() {
95          return name;
96      }
97  
98      /***
99       * Return the property of the bean rendered as a checkbox
100      * @return String
101      * @jsp.attribute
102      *    required="true"
103      *    rtexprvalue="false"
104      */
105     public String getProperty() {
106         return property;
107     }
108 
109     /***
110      * Return the resource bundle key to be rendered as the header
111      * @return String
112      * @jsp.attribute
113      *    required="true"
114      *    rtexprvalue="false"
115      */
116     public String getTitle() {
117         return title;
118     }
119 
120     public String getType() {
121         return type;
122     }
123 
124     /***
125      * Return the bundle to which the key belongs to
126      * @return String
127      * @jsp.attribute
128      *    required="false"
129      *    rtexprvalue="false"
130      */
131     public String getBundle() {
132         return bundle;
133     }
134 
135     /***
136      * Return the locale that was set
137      * @return String
138      * @jsp.attribute
139      *    required="false"
140      *    rtexprvalue="false"
141      */
142     public String getLocale() {
143         return locale;
144     }
145 
146     /***
147      * Return the value that will be rendered as a checked checkbox
148      * @return String
149      * @jsp.attribute
150      *    required="true"
151      *    rtexprvalue="false"
152      */
153     public String getTrueValue() {
154         return trueValue;
155     }
156 
157     /***
158      * Return the value that will be rendered as an unchecked checkbox
159      * @return String
160      * @jsp.attribute
161      *    required="true"
162      *    rtexprvalue="false"
163      */
164     public String getFalseValue() {
165         return falseValue;
166     }
167 
168     /***
169      * This method renders the checkbox based on the properties set
170      * @return int
171      * @throws JspException
172      */
173     public int doStartTag() throws JspException {
174         Tag tag = this.getParent();
175         GridIteratorTag parent;
176         GridUtil util;
177         if (tag != null && tag instanceof GridIteratorTag) {
178             parent = (GridIteratorTag) tag;
179             util = parent.getGridUtil();
180             if (parent.getIndex() == 0) {
181                 String message =
182                         TagUtils.getInstance().message(
183                                 pageContext,
184                                 this.bundle,
185                                 locale,
186                                 title,
187                                 null);
188                 HashMap map = new HashMap();
189                 map.put(GridConstants.TYPE, type);
190                 map.put(GridConstants.TRUE_VALUE, trueValue);
191                 map.put(GridConstants.FALSE_VALUE, falseValue);
192                 map.put(GridConstants.PROPERTY, property);
193                 map.put(GridConstants.HEADER, message);
194                 util.addTitle(map);
195             }
196                 Object value = null;
197                 try {
198                     value = TagUtils.getInstance().lookup(pageContext, name,
199                             property, null);
200                 } catch (JspException ex) {
201                     util.setEmptyCollection();
202                     value=null; //if not found
203                 }
204 
205             if (value != null) {
206                 util.setData(value.toString());
207             } else {
208                 util.setData("");
209             }
210         }
211         return this.SKIP_BODY;
212     }
213 
214     /***
215      * Reset the properties to be reused
216      */
217     public void release() {
218         name = null;
219         property = null;
220         title = null;
221         type = GridConstants.READONLY;
222         bundle = null;
223         trueValue = null;
224         falseValue = null;
225         locale = Globals.LOCALE_KEY;
226     }
227 }