View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/DbCheckboxTag.java,v 1.36 2006/01/13 13:38:50 hkollmann Exp $
3    * $Revision: 1.36 $
4    * $Date: 2006/01/13 13:38:50 $
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.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); // logging category for this class
52     private List       embeddedData  = null;
53     private String     checked; // only needed if parentForm is in "insert-mode", otherwise the DbForms-Framework determinates whether a checkbox should be selected or not.
54     private String     growDirection; // only needed if we habe a whole "group" of DbRadioTags; default = null == horizontal
55     private String     growSize      = "0"; // only needed if we habe a whole "group" of DbRadioTags; default = 1
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       // current Value from Database; or if no data: explicitly set by user; or ""
209       String currentValue = getFormFieldValue();
210 
211       // Because it can generate more than one checkbox, and to avoid wrong concatenation
212       // ex: onclick="this.checked=true; this.checked=false; this.checked=true ..."
213       String onclick = (getOnClick() != null) ? getOnClick()
214                                               : "";
215 
216       if (onclick.lastIndexOf(";") != (onclick.length() - 1)) {
217          onclick += ";"; // be sure javascript end with ";"
218       }
219 
220       // For generation Javascript Validation.  Need all original and modified fields name
221       getParentForm()
222          .addChildName(getName(), getFormFieldName());
223 
224       if (embeddedData == null) { // no embedded data is nested in this tag
225 
226          // select, if datadriven and data matches with current value OR if explicitly set by user
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          // nk, check if need to force it on. Useful w/ custom controller or javascript
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             // select, if datadriven and data matches with current value OR if explicitly set by user
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("&nbsp;</td>");
282          }
283 
284          tagBuf.append("</tr></table>");
285       }
286 
287       if (!Util.isNull(getNovalue())) {
288          // Write noValue last. During parameter parsing the 
289          // first written value will be returned.
290          // This the setted value!!!
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          // Writes out the old field value
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 }