KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/DbSelectTag.java,v 1.36 2004/10/20 10:52:03 hkollmann Exp $
3  * $Revision: 1.36 $
4  * $Date: 2004/10/20 10:52:03 $
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.dbforms.event.WebEvent;
27 import org.dbforms.event.eventtype.EventType;
28
29 import org.dbforms.util.*;
30
31 import java.util.List JavaDoc;
32
33 import javax.servlet.http.*;
34 import javax.servlet.jsp.*;
35
36
37
38 /**
39  * <p>
40  * This tag renders a html SELECT element including embedding OPTION elements.
41  * </p>
42  *
43  * @author Joachim Peer
44  * @author Philip Grunikiewicz
45  */

46 public class DbSelectTag extends DbBaseHandlerTag implements DataContainer,
47                                                              javax.servlet.jsp.tagext.TryCatchFinally JavaDoc {
48    private List JavaDoc embeddedData = null;
49    private String JavaDoc customEntry;
50    private String JavaDoc ifEmptyDontDraw = "false";
51    private String JavaDoc ifEmptyItem = null;
52    private String JavaDoc overrideReadOnly = "false";
53    private String JavaDoc selectedIndex;
54    private String JavaDoc size;
55
56    /**
57     * Sets the customEntry
58     *
59     * @param customEntry The customEntry to set
60     */

61    public void setCustomEntry(String JavaDoc customEntry) {
62       this.customEntry = customEntry;
63    }
64
65
66    /**
67     * Gets the customEntry
68     *
69     * @return Returns a String
70     */

71    public String JavaDoc getCustomEntry() {
72       return customEntry;
73    }
74
75
76    /**
77     * This method is a "hookup" for EmbeddedData - Tags which can assign the
78     * lines of data they loaded (by querying a database, or by rendering
79     * data-subelements, etc. etc.) and make the data available to this tag.
80     * [this method is defined in Interface DataContainer]
81     *
82     * @param embeddedData DOCUMENT ME!
83     */

84    public void setEmbeddedData(List JavaDoc embeddedData) {
85       this.embeddedData = embeddedData;
86    }
87
88
89    /**
90     * DOCUMENT ME!
91     *
92     * @return DOCUMENT ME!
93     */

94    public String JavaDoc getFormattedFieldValue() {
95       String JavaDoc res = getFieldValue();
96
97       if (res == null) {
98          res = ParseUtil.getParameter((HttpServletRequest) this.pageContext
99                                       .getRequest(), getFormFieldName());
100       }
101
102       return res;
103    }
104
105
106    /**
107     * DOCUMENT ME!
108     *
109     * @param b
110     */

111    public void setIfEmptyDontDraw(String JavaDoc b) {
112       ifEmptyDontDraw = b;
113    }
114
115
116    /**
117     * DOCUMENT ME!
118     *
119     * @return
120     */

121    public boolean hasIfEmptyDontDrawSet() {
122       return Util.getTrue(ifEmptyDontDraw);
123    }
124
125
126    /**
127     * DOCUMENT ME!
128     *
129     * @param string
130     */

131    public void setIfEmptyItem(String JavaDoc string) {
132       ifEmptyItem = string;
133    }
134
135
136    /**
137     * DOCUMENT ME!
138     *
139     * @return
140     */

141    public String JavaDoc getIfEmptyItem() {
142       return ifEmptyItem;
143    }
144
145
146    /**
147     * DOCUMENT ME!
148     *
149     * @param b
150     */

151    public void setOverrideReadOnly(String JavaDoc b) {
152       overrideReadOnly = b;
153    }
154
155
156    /**
157     * DOCUMENT ME!
158     *
159     * @param selectedIndex DOCUMENT ME!
160     */

161    public void setSelectedIndex(String JavaDoc selectedIndex) {
162       this.selectedIndex = selectedIndex;
163    }
164
165
166    /**
167     * DOCUMENT ME!
168     *
169     * @return DOCUMENT ME!
170     */

171    public String JavaDoc getSelectedIndex() {
172       return selectedIndex;
173    }
174
175
176    /**
177     * Set the size of this field (synonym for <code>setCols()</code>).
178     *
179     * @param size The new size
180     */

181    public void setSize(String JavaDoc size) {
182       this.size = size;
183    }
184
185
186    /**
187     * Return the size of this field (synonym for <code>getCols()</code>).
188     *
189     * @return DOCUMENT ME!
190     */

191    public String JavaDoc getSize() {
192       return size;
193    }
194
195
196    /**
197     * @see javax.servlet.jsp.tagext.TryCatchFinally#doCatch(java.lang.Throwable)
198     */

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

212    public int doEndTag() throws javax.servlet.jsp.JspException JavaDoc {
213       HttpServletRequest request = (HttpServletRequest) this.pageContext
214                                    .getRequest();
215       List JavaDoc errors = (List JavaDoc) request.getAttribute("errors");
216       WebEvent we = getParentForm()
217                                      .getWebEvent();
218
219       StringBuffer JavaDoc tagBuf = new StringBuffer JavaDoc();
220       StringBuffer JavaDoc selectedOptions = new StringBuffer JavaDoc();
221       int embeddedDataSize = 0;
222
223       String JavaDoc currentValue = getFormFieldValue();
224
225       if (embeddedData != null) {
226          // PG, 2001-12-14
227
// Is their a custom entry? Display first...
228
String JavaDoc ce = null;
229
230          if (((ce = this.getCustomEntry()) != null) && (ce.trim()
231                                                                 .length() > 0)) {
232             boolean isSelected = false;
233             String JavaDoc aKey = org.dbforms.util.StringUtil
234                            .getEmbeddedStringWithoutDots(ce, 0, ',');
235             String JavaDoc aValue = org.dbforms.util.StringUtil
236                              .getEmbeddedStringWithoutDots(ce, 1, ',');
237
238             // Check if we are in redisplayFieldsOnError mode and errors have occured
239
// If so, only set to selected if currentRow is equal to custom row.
240
if ((getParentForm()
241                           .hasRedisplayFieldsOnErrorSet() && (errors != null)
242                       && (errors.size() > 0))
243                       || ((we != null)
244                       && (we.getType().equals(EventType.EVENT_NAVIGATION_RELOAD)))) {
245                isSelected = (currentValue.equals(aKey));
246             } else {
247                isSelected = "true".equals(org.dbforms.util.StringUtil
248                                           .getEmbeddedStringWithoutDots(ce, 2,
249                                                                         ','));
250             }
251
252             if (isSelected) {
253                selectedOptions.append("-")
254                               .append(aKey);
255             }
256
257             tagBuf.append(generateTagString(aKey, aValue, isSelected));
258          }
259
260          embeddedDataSize = embeddedData.size();
261
262          // check for special 'IfEmpty' processing. if used skip the datadriven loop
263
boolean drawIt = true;
264          String JavaDoc me = null;
265
266          if (embeddedDataSize == 0) {
267             if (hasIfEmptyDontDrawSet()) {
268                drawIt = false;
269             } else if ((me = getIfEmptyItem()) != null) {
270                drawIt = false;
271
272                String JavaDoc aKey = org.dbforms.util.StringUtil
273                              .getEmbeddedStringWithoutDots(me, 0, ',');
274                String JavaDoc aValue = org.dbforms.util.StringUtil
275                                .getEmbeddedStringWithoutDots(me, 1, ',');
276
277                // always selected, since no other items
278
selectedOptions.append("-")
279                               .append(aKey);
280                tagBuf.append(generateTagString(aKey, aValue, true));
281             }
282          }
283
284          if (drawIt) {
285             for (int i = 0; i < embeddedDataSize; i++) {
286                KeyValuePair aKeyValuePair = (KeyValuePair) embeddedData.get(i);
287                String JavaDoc aKey = aKeyValuePair.getKey();
288                String JavaDoc aValue = aKeyValuePair.getValue();
289
290                // select, if datadriven and data matches with current value OR if explicitly set by user
291
boolean isSelected = aKey.equals(currentValue);
292
293                if (isSelected) {
294                   selectedOptions.append("-")
295                                  .append(aKey);
296                }
297
298                tagBuf.append(generateTagString(aKey, aValue, isSelected));
299             }
300          }
301       }
302
303       tagBuf.append("</select>");
304
305       //
306
// For read-only mode. Add unique generic javascript function.
307
// Reset to the default selected values (in case of multiselection)
308
// using javascript function on client side.
309
//
310
if (((!hasOverrideReadOnlySet()) && hasReadOnlySet())
311                 || getParentForm()
312                             .hasReadOnlySet()) {
313          selectedOptions.append("-");
314
315          String JavaDoc onChange = "resetSelect(this,'" + selectedOptions.toString()
316                            + "');";
317          setOnChange(onChange + ((getOnChange() != null) ? getOnChange()
318                                                          : ""));
319
320          if (!getParentForm()
321                        .existJavascriptFunction("resetSelect")) {
322             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
323             buf.append("\nfunction resetSelect(sel,x){\n");
324             buf.append(" for(i=0;i<sel.length;i++){\n");
325             buf.append(" var tmp = sel.options[i].value;\n");
326             buf.append(" if(x.indexOf(('-'+tmp+'-'))!=-1){\n");
327             buf.append(" sel.options[i].selected=true;\n");
328             buf.append(" }else{;\n");
329             buf.append(" sel.options[i].selected=false;\n");
330             buf.append(" }\n");
331             buf.append(" }\n");
332             buf.append("}\n");
333
334             getParentForm()
335                .addJavascriptFunction("resetSelect", buf);
336          }
337       }
338
339       // For generation Javascript Validation. Need original and modified fields name
340
getParentForm()
341          .addChildName(getName(), getFormFieldName());
342
343       try {
344          if ((embeddedDataSize > 0) || !hasIfEmptyDontDrawSet()) {
345             pageContext.getOut()
346                        .write(generateSelectHeader());
347             pageContext.getOut()
348                        .write(tagBuf.toString());
349             writeOutSpecialValues();
350          }
351       } catch (java.io.IOException JavaDoc ioe) {
352          throw new JspException("IO Error: " + ioe.getMessage());
353       }
354
355       return EVAL_PAGE;
356    }
357
358
359    /**
360     * DOCUMENT ME!
361     */

362    public void doFinally() {
363       embeddedData = null;
364       selectedIndex = null;
365       customEntry = null;
366       size = null;
367       ifEmptyDontDraw = "false";
368       ifEmptyItem = null;
369       overrideReadOnly = "false";
370       super.doFinally();
371    }
372
373
374    /**
375     * DOCUMENT ME!
376     *
377     * @return DOCUMENT ME!
378     *
379     * @throws javax.servlet.jsp.JspException DOCUMENT ME!
380     */

381    public int doStartTag() throws javax.servlet.jsp.JspException JavaDoc {
382       return EVAL_BODY_BUFFERED;
383    }
384
385
386    /**
387     * DOCUMENT ME!
388     *
389     * @return
390     */

391    public boolean hasOverrideReadOnlySet() {
392       return overrideReadOnly.equalsIgnoreCase("true");
393    }
394
395
396    /**
397     * DOCUMENT ME!
398     *
399     * @return DOCUMENT ME!
400     */

401    protected String JavaDoc typicalDefaultValue() {
402       String JavaDoc val;
403
404       // Lets check if the selectedIndex parameter has been input
405
if (((val = this.getSelectedIndex()) != null)
406                 && (val.trim()
407                              .length() != 0)) {
408          return val;
409       }
410
411       // No selectedIndex - business as usual...
412
return (super.typicalDefaultValue());
413    }
414
415
416    private String JavaDoc generateSelectHeader() throws javax.servlet.jsp.JspException JavaDoc {
417       // This method have been
418
StringBuffer JavaDoc tagBuf = new StringBuffer JavaDoc();
419
420       tagBuf.append("<select name=\"");
421
422       tagBuf.append(getFormFieldName());
423       tagBuf.append("\"");
424
425       if (size != null) {
426          tagBuf.append(" size=\"");
427          tagBuf.append(size);
428          tagBuf.append("\"");
429       }
430
431       if (getAccessKey() != null) {
432          tagBuf.append(" accesskey=\"");
433          tagBuf.append(getAccessKey());
434          tagBuf.append("\"");
435       }
436
437       if (getTabIndex() != null) {
438          tagBuf.append(" tabindex=\"");
439          tagBuf.append(getTabIndex());
440          tagBuf.append("\"");
441       }
442
443       tagBuf.append(prepareStyles());
444       tagBuf.append(prepareEventHandlers());
445       tagBuf.append(">");
446
447       return tagBuf.toString();
448    }
449
450
451    private String JavaDoc generateTagString(String JavaDoc value,
452                                     String JavaDoc description,
453                                     boolean selected) {
454       StringBuffer JavaDoc tagBuf = new StringBuffer JavaDoc();
455       tagBuf.append("<option value=\"");
456       tagBuf.append(escapeHTML(value));
457       tagBuf.append("\"");
458
459       if (selected) {
460          tagBuf.append(" selected=\"selected\" ");
461       }
462
463       //20021203-HKK: Removed unneeded blank
464
tagBuf.append(">");
465       tagBuf.append(escapeHTML(description));
466
467       // 20021025-HKK: Appended </option>
468
tagBuf.append("</option>");
469
470       return tagBuf.toString();
471    }
472 }
473
Popular Tags