View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/DbRadioTag.java,v 1.34 2006/01/13 13:38:51 hkollmann Exp $
3    * $Revision: 1.34 $
4    * $Date: 2006/01/13 13:38:51 $
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.interfaces.IDataContainer;
30  import org.dbforms.interfaces.StaticData;
31  import org.dbforms.util.*;
32  
33  import java.util.List;
34  
35  import javax.servlet.jsp.*;
36  
37  
38  
39  /***
40   * <p>
41   * This tag renders a html RADIO element or a whole group of them
42   * </p>
43   *
44   * @author Joachim Peer
45   */
46  public class DbRadioTag extends AbstractDbBaseHandlerTag implements IDataContainer,
47                                                              javax.servlet.jsp.tagext.TryCatchFinally {
48     private static Log logCat = LogFactory.getLog(DbRadioTag.class.getName()); // logging category for this class
49     private List       embeddedData  = null;
50     private String     growDirection; // only needed if we have a whole "group" of DbRadioTags; default = null == horizontal
51     private String     growSize      = "0"; // limit the number of elements per row (growDirection="horizontal")
52  
53     /***
54      * This method is a "hookup" for EmbeddedData - Tags which can assign the
55      * lines of data they loaded (by querying a database, or by rendering
56      * data-subelements, etc. etc.) and make the data available to this tag.
57      * [this method is defined in Interface DataContainer]
58      *
59      * @param embeddedData DOCUMENT ME!
60      */
61     public void setEmbeddedData(List embeddedData) {
62        this.embeddedData = embeddedData;
63     }
64  
65  
66     /***
67      * DOCUMENT ME!
68      *
69      * @param growDirection DOCUMENT ME!
70      */
71     public void setGrowDirection(String growDirection) {
72        this.growDirection = growDirection;
73     }
74  
75  
76     /***
77      * DOCUMENT ME!
78      *
79      * @return DOCUMENT ME!
80      */
81     public String getGrowDirection() {
82        return growDirection;
83     }
84  
85  
86     /***
87      * DOCUMENT ME!
88      *
89      * @param growSize DOCUMENT ME!
90      */
91     public void setGrowSize(String growSize) {
92        this.growSize = growSize;
93     }
94  
95  
96     /***
97      * DOCUMENT ME!
98      *
99      * @return DOCUMENT ME!
100     *
101     * @throws javax.servlet.jsp.JspException DOCUMENT ME!
102     * @throws JspException DOCUMENT ME!
103     */
104    public int doEndTag() throws javax.servlet.jsp.JspException {
105       StringBuffer tagBuf = new StringBuffer();
106 
107       // current Value from Database; or if no data: explicitly set by user; or ""
108       String currentValue = getFormFieldValue();
109 
110       if (Util.isNull(currentValue)) {
111          currentValue = getDefaultValue();
112       }
113 
114       if (embeddedData != null) {
115          int embeddedDataSize = embeddedData.size();
116 
117          // If radio is in read-only, retrieve selectedIndex and set the onclick of all radio with
118          // "document.formName['radioName'][selectedIndex].checked=true"
119          //
120          if (hasReadOnlySet() || getParentForm()
121                                           .hasReadOnlySet()) {
122             // First pass to retreive radio selectedIndex, because in Javascript it use only this index (Netscape 4.x)
123             for (int i = 0; i < embeddedDataSize; i++) {
124                StaticData aKeyValuePair = (StaticData) embeddedData.get(i);
125                String       aKey = aKeyValuePair.getKey();
126 
127                if (aKey.equals(currentValue)) {
128                   String onclick = (getOnClick() != null) ? getOnClick()
129                                                           : "";
130 
131                   if (onclick.lastIndexOf(";") != (onclick.length() - 1)) {
132                      onclick += ";"; // be sure javascript end with ";"
133                   }
134 
135                   setOnClick("document.dbform['" + getFormFieldName() + "']["
136                              + i + "].checked=true;" + onclick);
137 
138                   break;
139                }
140             }
141          }
142 
143          int maxSize = growSize();
144 
145          tagBuf.append("<table  BORDER=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr valign=\"top\">");
146 
147          for (int i = 0; i < embeddedDataSize; i++) {
148             StaticData aKeyValuePair = (StaticData) embeddedData.get(i);
149             String       aKey   = aKeyValuePair.getKey();
150             String       aValue = aKeyValuePair.getValue();
151 
152             // select, if datadriven and data matches with current value OR if explicitly set by user
153             boolean isSelected = aKey.equals(currentValue);
154 
155             if ("horizontal".equals(getGrowDirection())
156                       && (maxSize != 0)
157                       && ((i % maxSize) == 0)
158                       && (i != 0)) {
159                tagBuf.append("</tr><tr valign=\"top\">");
160             }
161 
162             if ("vertical".equals(getGrowDirection()) && (i != 0)) {
163                tagBuf.append("</tr><tr valign=\"top\">");
164             }
165 
166             tagBuf.append("<td ");
167             tagBuf.append(prepareStyles());
168             tagBuf.append(">")
169                   .append(generateTagString(aKey, aValue, isSelected))
170                   .append("</td>");
171          }
172 
173          tagBuf.append("</tr></table>");
174       }
175 
176       // For generation Javascript Validation.  Need all original and modified fields name
177       getParentForm()
178          .addChildName(getName(), getFormFieldName());
179 
180       try {
181          pageContext.getOut()
182                     .write(tagBuf.toString());
183 
184          // Writes out the old field value
185          writeOutSpecialValues();
186       } catch (java.io.IOException ioe) {
187          throw new JspException("IO Error: " + ioe.getMessage());
188       }
189 
190       return EVAL_PAGE;
191    }
192 
193 
194    /***
195     * DOCUMENT ME!
196     */
197    public void doFinally() {
198       embeddedData  = null;
199       growDirection = null;
200       growSize      = "0";
201       super.doFinally();
202    }
203 
204 
205    /***
206     * DOCUMENT ME!
207     *
208     * @return DOCUMENT ME!
209     *
210     * @throws javax.servlet.jsp.JspException DOCUMENT ME!
211     */
212    public int doStartTag() throws javax.servlet.jsp.JspException {
213       return EVAL_BODY_BUFFERED;
214    }
215 
216 
217    private String generateTagString(String  value,
218                                     String  description,
219                                     boolean selected) {
220       StringBuffer tagBuf = new StringBuffer();
221       tagBuf.append("<input type=\"radio\" name=\"");
222       tagBuf.append(getFormFieldName());
223       tagBuf.append("\" value=\"");
224       tagBuf.append(value);
225       tagBuf.append("\" ");
226 
227       if (selected) {
228          tagBuf.append(" checked=\"checked\" ");
229       }
230 
231       if (getAccessKey() != null) {
232          tagBuf.append(" accesskey=\"");
233          tagBuf.append(getAccessKey());
234          tagBuf.append("\"");
235       }
236 
237       if (getTabIndex() != null) {
238          tagBuf.append(" tabindex=\"");
239          tagBuf.append(getTabIndex());
240          tagBuf.append("\"");
241       }
242 
243       tagBuf.append(prepareStyles());
244       tagBuf.append(prepareEventHandlers());
245       tagBuf.append(">\n");
246       tagBuf.append(description);
247       tagBuf.append("</input>");
248 
249       return tagBuf.toString();
250    }
251 
252 
253    /***
254     * DOCUMENT ME!
255     *
256     * @return DOCUMENT ME!
257     */
258    private int growSize() {
259       int res = 0;
260 
261       try {
262          res = Integer.parseInt(growSize);
263       } catch (NumberFormatException nfe) {
264          logCat.warn(" setGrowSize(" + growSize + ") NumberFormatException : "
265                      + nfe.getMessage());
266       }
267 
268       return res;
269    }
270 }