1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.dbforms.taglib;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import org.dbforms.event.AbstractWebEvent;
30 import org.dbforms.event.eventtype.EventType;
31 import org.dbforms.interfaces.IDataContainer;
32 import org.dbforms.interfaces.StaticData;
33
34 import org.dbforms.util.*;
35
36 import java.util.*;
37
38 import javax.servlet.jsp.*;
39
40
41
42 /***
43 * <p>
44 * This tag renders a html CHECKBOX element or a whole group of them
45 * </p>
46 *
47 * @author Joachim Peer
48 */
49 public class DbCheckboxTag extends AbstractDbBaseHandlerTag implements IDataContainer,
50 javax.servlet.jsp.tagext.TryCatchFinally {
51 private static Log logCat = LogFactory.getLog(DbCheckboxTag.class);
52 private List embeddedData = null;
53 private String checked;
54 private String growDirection;
55 private String growSize = "0";
56 private String noValue;
57 private String value;
58 private String force = "false";
59
60 /***
61 * DOCUMENT ME!
62 *
63 * @param checked DOCUMENT ME!
64 */
65 public void setChecked(String checked) {
66 this.checked = checked;
67 }
68
69
70 /***
71 * This method is a "hookup" for EmbeddedData - Tags which can assign the
72 * lines of data they loaded (by querying a database, or by rendering
73 * data-subelements, etc. etc.) and make the data available to this tag.
74 * [this method is defined in Interface DataContainer]
75 *
76 * @param embeddedData DOCUMENT ME!
77 */
78 public void setEmbeddedData(List embeddedData) {
79 this.embeddedData = embeddedData;
80 }
81
82
83 /***
84 * DOCUMENT ME!
85 *
86 * @param b
87 */
88 public void setForce(String b) {
89 force = b;
90 }
91
92
93 /***
94 * DOCUMENT ME!
95 *
96 * @return
97 */
98 public String getForce() {
99 return force;
100 }
101
102
103 /***
104 * DOCUMENT ME!
105 *
106 * @param growDirection DOCUMENT ME!
107 */
108 public void setGrowDirection(String growDirection) {
109 this.growDirection = growDirection;
110 }
111
112
113 /***
114 * DOCUMENT ME!
115 *
116 * @return DOCUMENT ME!
117 */
118 public String getGrowDirection() {
119 return growDirection;
120 }
121
122
123 /***
124 * DOCUMENT ME!
125 *
126 * @param growSize DOCUMENT ME!
127 */
128 public void setGrowSize(String growSize) {
129 try {
130 int grow = Integer.parseInt(growSize);
131
132 if (grow > 0) {
133 this.growSize = growSize;
134 } else {
135 this.growSize = "0";
136 }
137 } catch (NumberFormatException nfe) {
138 logCat.warn(" setGrowSize(" + growSize + ") NumberFormatException : "
139 + nfe.getMessage());
140 this.growSize = "0";
141 }
142 }
143
144
145 /***
146 * DOCUMENT ME!
147 *
148 * @return DOCUMENT ME!
149 */
150 public String getGrowSize() {
151 return growSize;
152 }
153
154
155 /***
156 * Sets the noValue.
157 *
158 * @param noValue The noValue to set
159 */
160 public void setNovalue(String noValue) {
161 this.noValue = noValue;
162 }
163
164
165 /***
166 * Returns the noValue.
167 *
168 * @return String
169 */
170 public String getNovalue() {
171 return noValue;
172 }
173
174
175 /***
176 * DOCUMENT ME!
177 *
178 * @param string
179 */
180 public void setValue(String string) {
181 value = string;
182 }
183
184
185 /***
186 * DOCUMENT ME!
187 *
188 * @return
189 */
190 public String getValue() {
191 return value;
192 }
193
194
195 /***
196 * DOCUMENT ME!
197 *
198 * @return DOCUMENT ME!
199 *
200 * @throws javax.servlet.jsp.JspException DOCUMENT ME!
201 * @throws JspException DOCUMENT ME!
202 */
203 public int doEndTag() throws javax.servlet.jsp.JspException {
204 StringBuffer tagBuf = new StringBuffer();
205 AbstractWebEvent we = getParentForm()
206 .getWebEvent();
207
208
209 String currentValue = getFormFieldValue();
210
211
212
213 String onclick = (getOnClick() != null) ? getOnClick()
214 : "";
215
216 if (onclick.lastIndexOf(";") != (onclick.length() - 1)) {
217 onclick += ";";
218 }
219
220
221 getParentForm()
222 .addChildName(getName(), getFormFieldName());
223
224 if (embeddedData == null) {
225
226
227 boolean isSelected = ((!getParentForm()
228 .isFooterReached()
229 || ((we != null)
230 && we.getType()
231 .equals(EventType.EVENT_NAVIGATION_RELOAD)))
232 && (getValue() != null)
233 && getValue()
234 .equals(currentValue))
235 || (getParentForm()
236 .isFooterReached() && hasCheckedSet());
237
238
239 if (Util.getTrue(getForce())) {
240 isSelected = hasCheckedSet();
241 }
242
243 if (hasReadOnlySet() || getParentForm()
244 .hasReadOnlySet()) {
245 setOnClick("this.checked=" + isSelected + ";" + onclick);
246 }
247
248 tagBuf.append(generateTagString(getValue(), "", isSelected));
249 } else {
250 int embeddedDataSize = embeddedData.size();
251 int maxSize = Integer.parseInt(getGrowSize());
252
253 tagBuf.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr valign=\"top\">");
254
255 for (int i = 0; i < embeddedDataSize; i++) {
256 StaticData aKeyValuePair = (StaticData) embeddedData.get(i);
257 String aKey = aKeyValuePair.getKey();
258 String aValue = aKeyValuePair.getValue();
259
260
261 boolean isSelected = aKey.equals(currentValue);
262
263 if (hasReadOnlySet() || getParentForm()
264 .hasReadOnlySet()) {
265 setOnClick("this.checked=" + isSelected + ";" + onclick);
266 }
267
268 if ("horizontal".equals(getGrowDirection())
269 && (maxSize != 0)
270 && ((i % maxSize) == 0)
271 && (i != 0)) {
272 tagBuf.append("</tr><tr valign=\"top\">");
273 }
274
275 if ("vertical".equals(getGrowDirection()) && (i != 0)) {
276 tagBuf.append("</tr><tr valign=\"top\">");
277 }
278
279 tagBuf.append("<td>")
280 .append(generateTagString(aKey, aValue, isSelected))
281 .append(" </td>");
282 }
283
284 tagBuf.append("</tr></table>");
285 }
286
287 if (!Util.isNull(getNovalue())) {
288
289
290
291 tagBuf.append("<input type=\"hidden\" name=\"");
292 tagBuf.append(getFormFieldName());
293 tagBuf.append("\" value =\"");
294 tagBuf.append(getNovalue());
295 tagBuf.append("\" ");
296 tagBuf.append("/>");
297 }
298
299 try {
300 pageContext.getOut()
301 .write(tagBuf.toString());
302
303
304 writeOutSpecialValues();
305 } catch (java.io.IOException ioe) {
306 throw new JspException("IO Error: " + ioe.getMessage());
307 }
308
309 return EVAL_PAGE;
310 }
311
312
313 /***
314 * DOCUMENT ME!
315 */
316 public void doFinally() {
317 embeddedData = null;
318 checked = null;
319 growDirection = null;
320 growSize = "0";
321 noValue = null;
322 value = null;
323 force = "false";
324 super.doFinally();
325 }
326
327
328 /***
329 * DOCUMENT ME!
330 *
331 * @return DOCUMENT ME!
332 *
333 * @throws javax.servlet.jsp.JspException DOCUMENT ME!
334 */
335 public int doStartTag() throws javax.servlet.jsp.JspException {
336 return EVAL_BODY_BUFFERED;
337 }
338
339
340 /***
341 * DOCUMENT ME!
342 *
343 * @return DOCUMENT ME!
344 */
345 public boolean hasCheckedSet() {
346 return Util.getTrue(checked);
347 }
348
349
350 private String generateTagString(String avalue,
351 String description,
352 boolean selected) {
353 StringBuffer tagBuf = new StringBuffer();
354
355 tagBuf.append("<input type=\"checkbox\" name=\"");
356 tagBuf.append(getFormFieldName());
357 tagBuf.append("\" value =\"");
358 tagBuf.append(avalue);
359 tagBuf.append("\" ");
360
361 if (selected) {
362 tagBuf.append(" checked=\"checked\" ");
363 }
364
365 if (getAccessKey() != null) {
366 tagBuf.append(" accesskey=\"");
367 tagBuf.append(getAccessKey());
368 tagBuf.append("\"");
369 }
370
371 if (getTabIndex() != null) {
372 tagBuf.append(" tabindex=\"");
373 tagBuf.append(getTabIndex());
374 tagBuf.append("\"");
375 }
376
377 tagBuf.append(prepareStyles());
378 tagBuf.append(prepareEventHandlers());
379 tagBuf.append(">");
380 tagBuf.append(description);
381 tagBuf.append("</input>");
382
383 return tagBuf.toString();
384 }
385 }