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.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());
49 private List embeddedData = null;
50 private String growDirection;
51 private String growSize = "0";
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
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
118
119
120 if (hasReadOnlySet() || getParentForm()
121 .hasReadOnlySet()) {
122
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 += ";";
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
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
177 getParentForm()
178 .addChildName(getName(), getFormFieldName());
179
180 try {
181 pageContext.getOut()
182 .write(tagBuf.toString());
183
184
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 }