KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/DbRadioTag.java,v 1.30 2004/08/18 12:26:08 hkollmann Exp $
3  * $Revision: 1.30 $
4  * $Date: 2004/08/18 12:26:08 $
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.util.*;
30
31 import java.util.List JavaDoc;
32
33 import javax.servlet.jsp.*;
34
35
36
37 /**
38  * <p>
39  * This tag renders a html RADIO element or a whole group of them
40  * </p>
41  *
42  * @author Joachim Peer
43  */

44 public class DbRadioTag extends DbBaseHandlerTag implements DataContainer,
45                                                             javax.servlet.jsp.tagext.TryCatchFinally JavaDoc {
46    private static Log logCat = LogFactory.getLog(DbRadioTag.class.getName()); // logging category for this class
47
private List JavaDoc embeddedData = null;
48    private String JavaDoc growDirection; // only needed if we have a whole "group" of DbRadioTags; default = null == horizontal
49
private String JavaDoc growSize = "0"; // limit the number of elements per row (growDirection="horizontal")
50

51    /**
52     * This method is a "hookup" for EmbeddedData - Tags which can assign the
53     * lines of data they loaded (by querying a database, or by rendering
54     * data-subelements, etc. etc.) and make the data available to this tag.
55     * [this method is defined in Interface DataContainer]
56     *
57     * @param embeddedData DOCUMENT ME!
58     */

59    public void setEmbeddedData(List JavaDoc embeddedData) {
60       this.embeddedData = embeddedData;
61    }
62
63
64    /**
65     * DOCUMENT ME!
66     *
67     * @param growDirection DOCUMENT ME!
68     */

69    public void setGrowDirection(String JavaDoc growDirection) {
70       this.growDirection = growDirection;
71    }
72
73
74    /**
75     * DOCUMENT ME!
76     *
77     * @return DOCUMENT ME!
78     */

79    public String JavaDoc getGrowDirection() {
80       return growDirection;
81    }
82
83
84    /**
85     * DOCUMENT ME!
86     *
87     * @param growSize DOCUMENT ME!
88     */

89    public void setGrowSize(String JavaDoc growSize) {
90       this.growSize = growSize;
91    }
92
93
94    /**
95     * @see javax.servlet.jsp.tagext.TryCatchFinally#doCatch(java.lang.Throwable)
96     */

97    public void doCatch(Throwable JavaDoc t) throws Throwable JavaDoc {
98       throw t;
99    }
100
101
102    /**
103     * DOCUMENT ME!
104     *
105     * @return DOCUMENT ME!
106     *
107     * @throws javax.servlet.jsp.JspException DOCUMENT ME!
108     * @throws JspException DOCUMENT ME!
109     */

110    public int doEndTag() throws javax.servlet.jsp.JspException JavaDoc {
111       StringBuffer JavaDoc tagBuf = new StringBuffer JavaDoc();
112
113       // current Value from Database; or if no data: explicitly set by user; or ""
114
String JavaDoc currentValue = getFormFieldValue();
115
116       if (Util.isNull(currentValue)) {
117          currentValue = getDefaultValue();
118       }
119
120       if (embeddedData != null) {
121          int embeddedDataSize = embeddedData.size();
122
123          // If radio is in read-only, retrieve selectedIndex and set the onclick of all radio with
124
// "document.formName['radioName'][selectedIndex].checked=true"
125
//
126
if (hasReadOnlySet() || getParentForm()
127                                           .hasReadOnlySet()) {
128             // First pass to retreive radio selectedIndex, because in Javascript it use only this index (Netscape 4.x)
129
for (int i = 0; i < embeddedDataSize; i++) {
130                KeyValuePair aKeyValuePair = (KeyValuePair) embeddedData.get(i);
131                String JavaDoc aKey = aKeyValuePair.getKey();
132
133                if (aKey.equals(currentValue)) {
134                   String JavaDoc onclick = (getOnClick() != null) ? getOnClick()
135                                                           : "";
136
137                   if (onclick.lastIndexOf(";") != (onclick.length() - 1)) {
138                      onclick += ";"; // be sure javascript end with ";"
139
}
140
141                   setOnClick("document.dbform['" + getFormFieldName() + "']["
142                              + i + "].checked=true;" + onclick);
143
144                   break;
145                }
146             }
147          }
148
149          int maxSize = growSize();
150
151          tagBuf.append("<table BORDER=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr valign=\"top\">");
152
153          for (int i = 0; i < embeddedDataSize; i++) {
154             KeyValuePair aKeyValuePair = (KeyValuePair) embeddedData.get(i);
155             String JavaDoc aKey = aKeyValuePair.getKey();
156             String JavaDoc aValue = aKeyValuePair.getValue();
157
158             // select, if datadriven and data matches with current value OR if explicitly set by user
159
boolean isSelected = aKey.equals(currentValue);
160
161             if ("horizontal".equals(getGrowDirection())
162                       && (maxSize != 0)
163                       && ((i % maxSize) == 0)
164                       && (i != 0)) {
165                tagBuf.append("</tr><tr valign=\"top\">");
166             }
167
168             if ("vertical".equals(getGrowDirection()) && (i != 0)) {
169                tagBuf.append("</tr><tr valign=\"top\">");
170             }
171
172             tagBuf.append("<td ");
173             tagBuf.append(prepareStyles());
174             tagBuf.append(">")
175                   .append(generateTagString(aKey, aValue, isSelected))
176                   .append("</td>");
177          }
178
179          tagBuf.append("</tr></table>");
180       }
181
182       // For generation Javascript Validation. Need all original and modified fields name
183
getParentForm()
184          .addChildName(getName(), getFormFieldName());
185
186       try {
187          pageContext.getOut()
188                     .write(tagBuf.toString());
189
190          // Writes out the old field value
191
writeOutSpecialValues();
192       } catch (java.io.IOException JavaDoc ioe) {
193          throw new JspException("IO Error: " + ioe.getMessage());
194       }
195
196       return EVAL_PAGE;
197    }
198
199
200    /**
201     * DOCUMENT ME!
202     */

203    public void doFinally() {
204       embeddedData = null;
205       growDirection = null;
206       growSize = "0";
207       super.doFinally();
208    }
209
210
211    /**
212     * DOCUMENT ME!
213     *
214     * @return DOCUMENT ME!
215     *
216     * @throws javax.servlet.jsp.JspException DOCUMENT ME!
217     */

218    public int doStartTag() throws javax.servlet.jsp.JspException JavaDoc {
219       return EVAL_BODY_BUFFERED;
220    }
221
222
223    private String JavaDoc generateTagString(String JavaDoc value,
224                                     String JavaDoc description,
225                                     boolean selected) {
226       StringBuffer JavaDoc tagBuf = new StringBuffer JavaDoc();
227       tagBuf.append("<input type=\"radio\" name=\"");
228       tagBuf.append(getFormFieldName());
229       tagBuf.append("\" value=\"");
230       tagBuf.append(value);
231       tagBuf.append("\" ");
232
233       if (selected) {
234          tagBuf.append(" checked=\"checked\" ");
235       }
236
237       if (getAccessKey() != null) {
238          tagBuf.append(" accesskey=\"");
239          tagBuf.append(getAccessKey());
240          tagBuf.append("\"");
241       }
242
243       if (getTabIndex() != null) {
244          tagBuf.append(" tabindex=\"");
245          tagBuf.append(getTabIndex());
246          tagBuf.append("\"");
247       }
248
249       tagBuf.append(prepareStyles());
250       tagBuf.append(prepareEventHandlers());
251       tagBuf.append(">\n");
252       tagBuf.append(description);
253       tagBuf.append("</input>");
254
255       return tagBuf.toString();
256    }
257
258
259    /**
260     * DOCUMENT ME!
261     *
262     * @return DOCUMENT ME!
263     */

264    private int growSize() {
265       int res = 0;
266
267       try {
268          res = Integer.parseInt(growSize);
269       } catch (NumberFormatException JavaDoc nfe) {
270          logCat.warn(" setGrowSize(" + growSize + ") NumberFormatException : "
271                      + nfe.getMessage());
272       }
273
274       return res;
275    }
276 }
277
Popular Tags