KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > taglib > DbCheckboxTag


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/DbCheckboxTag.java,v 1.31 2005/02/20 10:27:44 hkollmann Exp $
3  * $Revision: 1.31 $
4  * $Date: 2005/02/20 10:27:44 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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.WebEvent;
30 import org.dbforms.event.eventtype.EventType;
31
32 import org.dbforms.util.*;
33
34 import java.util.*;
35
36 import javax.servlet.jsp.*;
37
38
39
40 /**
41  * <p>
42  * This tag renders a html CHECKBOX element or a whole group of them
43  * </p>
44  *
45  * @author Joachim Peer
46  */

47 public class DbCheckboxTag extends DbBaseHandlerTag implements DataContainer,
48                                                                javax.servlet.jsp.tagext.TryCatchFinally JavaDoc {
49    private static Log logCat = LogFactory.getLog(DbCheckboxTag.class); // logging category for this class
50
private List embeddedData = null;
51    private String JavaDoc checked; // only needed if parentForm is in "insert-mode", otherwise the DbForms-Framework determinates whether a checkbox should be selected or not.
52
private String JavaDoc growDirection; // only needed if we habe a whole "group" of DbRadioTags; default = null == horizontal
53
private String JavaDoc growSize = "0"; // only needed if we habe a whole "group" of DbRadioTags; default = 1
54
private String JavaDoc noValue;
55    private String JavaDoc value;
56    private String JavaDoc force = "false";
57
58    /**
59     * DOCUMENT ME!
60     *
61     * @param checked DOCUMENT ME!
62     */

63    public void setChecked(String JavaDoc checked) {
64       this.checked = checked;
65    }
66
67
68    /**
69     * This method is a "hookup" for EmbeddedData - Tags which can assign the
70     * lines of data they loaded (by querying a database, or by rendering
71     * data-subelements, etc. etc.) and make the data available to this tag.
72     * [this method is defined in Interface DataContainer]
73     *
74     * @param embeddedData DOCUMENT ME!
75     */

76    public void setEmbeddedData(List embeddedData) {
77       this.embeddedData = embeddedData;
78    }
79
80
81    /**
82     * DOCUMENT ME!
83     *
84     * @param b
85     */

86    public void setForce(String JavaDoc b) {
87       force = b;
88    }
89
90
91    /**
92     * DOCUMENT ME!
93     *
94     * @return
95     */

96    public String JavaDoc getForce() {
97       return force;
98    }
99
100
101    /**
102     * DOCUMENT ME!
103     *
104     * @param growDirection DOCUMENT ME!
105     */

106    public void setGrowDirection(String JavaDoc growDirection) {
107       this.growDirection = growDirection;
108    }
109
110
111    /**
112     * DOCUMENT ME!
113     *
114     * @return DOCUMENT ME!
115     */

116    public String JavaDoc getGrowDirection() {
117       return growDirection;
118    }
119
120
121    /**
122     * DOCUMENT ME!
123     *
124     * @param growSize DOCUMENT ME!
125     */

126    public void setGrowSize(String JavaDoc growSize) {
127       try {
128          int grow = Integer.parseInt(growSize);
129
130          if (grow > 0) {
131             this.growSize = growSize;
132          } else {
133             this.growSize = "0";
134          }
135       } catch (NumberFormatException JavaDoc nfe) {
136          logCat.warn(" setGrowSize(" + growSize + ") NumberFormatException : "
137                      + nfe.getMessage());
138          this.growSize = "0";
139       }
140    }
141
142
143    /**
144     * DOCUMENT ME!
145     *
146     * @return DOCUMENT ME!
147     */

148    public String JavaDoc getGrowSize() {
149       return growSize;
150    }
151
152
153    /**
154     * Sets the noValue.
155     *
156     * @param noValue The noValue to set
157     */

158    public void setNovalue(String JavaDoc noValue) {
159       this.noValue = noValue;
160    }
161
162
163    /**
164     * Returns the noValue.
165     *
166     * @return String
167     */

168    public String JavaDoc getNovalue() {
169       return noValue;
170    }
171
172
173    /**
174     * DOCUMENT ME!
175     *
176     * @param string
177     */

178    public void setValue(String JavaDoc string) {
179       value = string;
180    }
181
182
183    /**
184     * DOCUMENT ME!
185     *
186     * @return
187     */

188    public String JavaDoc getValue() {
189       return value;
190    }
191
192
193    /**
194     * @see javax.servlet.jsp.tagext.TryCatchFinally#doCatch(java.lang.Throwable)
195     */

196    public void doCatch(Throwable JavaDoc t) throws Throwable JavaDoc {
197       throw t;
198    }
199
200
201    /**
202     * DOCUMENT ME!
203     *
204     * @return DOCUMENT ME!
205     *
206     * @throws javax.servlet.jsp.JspException DOCUMENT ME!
207     * @throws JspException DOCUMENT ME!
208     */

209    public int doEndTag() throws javax.servlet.jsp.JspException JavaDoc {
210       StringBuffer JavaDoc tagBuf = new StringBuffer JavaDoc();
211       WebEvent we = getParentForm()
212                            .getWebEvent();
213
214       // current Value from Database; or if no data: explicitly set by user; or ""
215
String JavaDoc currentValue = getFormFieldValue();
216
217       // Because it can generate more than one checkbox, and to avoid wrong concatenation
218
// ex: onclick="this.checked=true; this.checked=false; this.checked=true ..."
219
String JavaDoc onclick = (getOnClick() != null) ? getOnClick()
220                                               : "";
221
222       if (onclick.lastIndexOf(";") != (onclick.length() - 1)) {
223          onclick += ";"; // be sure javascript end with ";"
224
}
225
226       // For generation Javascript Validation. Need all original and modified fields name
227
getParentForm()
228          .addChildName(getName(), getFormFieldName());
229
230       if (embeddedData == null) { // no embedded data is nested in this tag
231

232          // select, if datadriven and data matches with current value OR if explicitly set by user
233
boolean isSelected = ((!getParentForm()
234                                     .isFooterReached()
235                               || ((we != null)
236                               && we.getType()
237                                    .equals(EventType.EVENT_NAVIGATION_RELOAD)))
238                               && (getValue() != null)
239                               && getValue()
240                                     .equals(currentValue))
241                               || (getParentForm()
242                                      .isFooterReached() && hasCheckedSet());
243
244          // nk, check if need to force it on. Useful w/ custom controller or javascript
245
if (Util.getTrue(getForce())) {
246             isSelected = hasCheckedSet();
247          }
248
249          if (hasReadOnlySet() || getParentForm()
250                                           .hasReadOnlySet()) {
251             setOnClick("this.checked=" + isSelected + ";" + onclick);
252          }
253
254          tagBuf.append(generateTagString(getValue(), "", isSelected));
255       } else {
256          int embeddedDataSize = embeddedData.size();
257          int maxSize = Integer.parseInt(getGrowSize());
258
259          tagBuf.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr valign=\"top\">");
260
261          for (int i = 0; i < embeddedDataSize; i++) {
262             KeyValuePair aKeyValuePair = (KeyValuePair) embeddedData.get(i);
263             String JavaDoc aKey = aKeyValuePair.getKey();
264             String JavaDoc aValue = aKeyValuePair.getValue();
265
266             // select, if datadriven and data matches with current value OR if explicitly set by user
267
boolean isSelected = aKey.equals(currentValue);
268
269             if (hasReadOnlySet() || getParentForm()
270                                              .hasReadOnlySet()) {
271                setOnClick("this.checked=" + isSelected + ";" + onclick);
272             }
273
274             if ("horizontal".equals(getGrowDirection())
275                       && (maxSize != 0)
276                       && ((i % maxSize) == 0)
277                       && (i != 0)) {
278                tagBuf.append("</tr><tr valign=\"top\">");
279             }
280
281             if ("vertical".equals(getGrowDirection()) && (i != 0)) {
282                tagBuf.append("</tr><tr valign=\"top\">");
283             }
284
285             tagBuf.append("<td>")
286                   .append(generateTagString(aKey, aValue, isSelected))
287                   .append("&nbsp;</td>");
288          }
289
290          tagBuf.append("</tr></table>");
291       }
292
293       if (!Util.isNull(getNovalue())) {
294          // Write noValue last. During parameter parsing the
295
// first written value will be returned.
296
// This the setted value!!!
297
tagBuf.append("<input type=\"hidden\" name=\"");
298          tagBuf.append(getFormFieldName());
299          tagBuf.append("\" value =\"");
300          tagBuf.append(getNovalue());
301          tagBuf.append("\" ");
302          tagBuf.append("/>");
303       }
304
305       try {
306          pageContext.getOut()
307                     .write(tagBuf.toString());
308
309          // Writes out the old field value
310
writeOutSpecialValues();
311       } catch (java.io.IOException JavaDoc ioe) {
312          throw new JspException("IO Error: " + ioe.getMessage());
313       }
314
315       return EVAL_PAGE;
316    }
317
318
319    /**
320     * DOCUMENT ME!
321     */

322    public void doFinally() {
323       embeddedData = null;
324       checked = null;
325       growDirection = null;
326       growSize = "0";
327       noValue = null;
328       value = null;
329       force = "false";
330       super.doFinally();
331    }
332
333
334    /**
335     * DOCUMENT ME!
336     *
337     * @return DOCUMENT ME!
338     *
339     * @throws javax.servlet.jsp.JspException DOCUMENT ME!
340     */

341    public int doStartTag() throws javax.servlet.jsp.JspException JavaDoc {
342       return EVAL_BODY_BUFFERED;
343    }
344
345
346    /**
347     * DOCUMENT ME!
348     *
349     * @return DOCUMENT ME!
350     */

351    public boolean hasCheckedSet() {
352       return Util.getTrue(checked);
353    }
354
355
356    private String JavaDoc generateTagString(String JavaDoc avalue,
357                                     String JavaDoc description,
358                                     boolean selected) {
359       StringBuffer JavaDoc tagBuf = new StringBuffer JavaDoc();
360
361       tagBuf.append("<input type=\"checkbox\" name=\"");
362       tagBuf.append(getFormFieldName());
363       tagBuf.append("\" value =\"");
364       tagBuf.append(avalue);
365       tagBuf.append("\" ");
366
367       if (selected) {
368          tagBuf.append(" checked=\"checked\" ");
369       }
370
371       if (getAccessKey() != null) {
372          tagBuf.append(" accesskey=\"");
373          tagBuf.append(getAccessKey());
374          tagBuf.append("\"");
375       }
376
377       if (getTabIndex() != null) {
378          tagBuf.append(" tabindex=\"");
379          tagBuf.append(getTabIndex());
380          tagBuf.append("\"");
381       }
382
383       tagBuf.append(prepareStyles());
384       tagBuf.append(prepareEventHandlers());
385       tagBuf.append(">");
386       tagBuf.append(description);
387       tagBuf.append("</input>");
388
389       return tagBuf.toString();
390    }
391 }
392
Popular Tags