KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/DbSearchComboTag.java,v 1.23 2004/10/17 07:02:29 hkollmann Exp $
3  * $Revision: 1.23 $
4  * $Date: 2004/10/17 07:02:29 $
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.config.*;
27
28 import org.dbforms.util.*;
29
30 import java.util.List JavaDoc;
31
32 import javax.servlet.http.*;
33 import javax.servlet.jsp.*;
34
35
36
37 /**
38  * <p>
39  * renders a select field for searching with special default search modes.
40  * </p>
41  *
42  * <p>
43  * example:
44  * </p>
45  * &lt;input type="hidden" name="searchalgo_0_1" value="weakEnd"/&gt; &lt;input
46  * type="hidden" name="searchmode_0_1" value="AND"/&gt; &lt;select
47  * name="search_0_1"/&gt; &lt;/select&gt; searchalgo and searchmode are set by
48  * parameter.
49  *
50  * @author Henner Kollmann
51  */

52 public class DbSearchComboTag extends DbSearchTag implements DataContainer,
53                                                              javax.servlet.jsp.tagext.TryCatchFinally JavaDoc {
54    private List JavaDoc embeddedData = null;
55    private String JavaDoc compareWith = "key";
56    private String JavaDoc customEntry;
57    private String JavaDoc selectedIndex;
58    private String JavaDoc size = "1";
59
60    /**
61     * Creates a new DbSearchComboTag object.
62     */

63    public DbSearchComboTag() {
64       setSearchAlgo("sharp");
65    }
66
67    /**
68     * DOCUMENT ME!
69     *
70     * @param string
71     */

72    public void setCompareWith(String JavaDoc string) {
73       compareWith = string;
74    }
75
76
77    /**
78     * DOCUMENT ME!
79     *
80     * @return
81     */

82    public String JavaDoc getCompareWith() {
83       return compareWith;
84    }
85
86
87    /**
88     * DOCUMENT ME!
89     *
90     * @param customEntry DOCUMENT ME!
91     */

92    public void setCustomEntry(String JavaDoc customEntry) {
93       this.customEntry = customEntry;
94    }
95
96
97    /**
98     * DOCUMENT ME!
99     *
100     * @return DOCUMENT ME!
101     */

102    public String JavaDoc getCustomEntry() {
103       return customEntry;
104    }
105
106
107    /**
108     * This method is a "hookup" for EmbeddedData - Tags which can assign the
109     * lines of data they loaded (by querying a database, or by rendering
110     * data-subelements, etc. etc.) and make the data available to this tag.
111     * [this method is defined in Interface DataContainer]
112     *
113     * @param embeddedData DOCUMENT ME!
114     */

115    public void setEmbeddedData(List JavaDoc embeddedData) {
116       this.embeddedData = embeddedData;
117    }
118
119
120    /**
121     * DOCUMENT ME!
122     *
123     * @param selectedIndex DOCUMENT ME!
124     */

125    public void setSelectedIndex(String JavaDoc selectedIndex) {
126       this.selectedIndex = selectedIndex;
127    }
128
129
130    /**
131     * DOCUMENT ME!
132     *
133     * @return DOCUMENT ME!
134     */

135    public String JavaDoc getSelectedIndex() {
136       return selectedIndex;
137    }
138
139
140    /**
141     * DOCUMENT ME!
142     *
143     * @param string
144     */

145    public void setSize(String JavaDoc string) {
146       size = string;
147    }
148
149
150    // ------------------------------------------------------ Protected Methods
151

152    /**
153     * DOCUMENT ME!
154     *
155     * @return
156     */

157    public String JavaDoc getSize() {
158       return size;
159    }
160
161
162    /**
163     * @see javax.servlet.jsp.tagext.TryCatchFinally#doCatch(java.lang.Throwable)
164     */

165    public void doCatch(Throwable JavaDoc t) throws Throwable JavaDoc {
166       throw t;
167    }
168
169
170    /**
171     * DOCUMENT ME!
172     *
173     * @return DOCUMENT ME!
174     *
175     * @throws javax.servlet.jsp.JspException DOCUMENT ME!
176     * @throws JspException DOCUMENT ME!
177     */

178    public int doEndTag() throws javax.servlet.jsp.JspException JavaDoc {
179       HttpServletRequest request = (HttpServletRequest) this.pageContext
180                                    .getRequest();
181
182       Field field = getField();
183
184       StringBuffer JavaDoc tagBuf = new StringBuffer JavaDoc();
185
186       String JavaDoc oldValue = ParseUtil.getParameter(request,
187                                                            field
188                                                            .getSearchFieldName());
189
190       if (!Util.isNull(oldValue)) {
191          selectedIndex = oldValue;
192       }
193
194       boolean isSelected = false;
195
196       if (embeddedData != null) { // no embedded data is nested in this tag
197

198          if (!Util.isNull(customEntry)) {
199             String JavaDoc aKey = org.dbforms.util.StringUtil
200                           .getEmbeddedStringWithoutDots(customEntry, 0, ',');
201             String JavaDoc aValue = org.dbforms.util.StringUtil
202                             .getEmbeddedStringWithoutDots(customEntry, 1, ',');
203
204             if (Util.isNull(selectedIndex)) {
205                isSelected = Util.getTrue(org.dbforms.util.StringUtil.getEmbeddedStringWithoutDots(customEntry,
206                                                                                                  2,
207                                                                                                  ',').trim());
208             }
209
210             tagBuf.append(generateTagString(aKey, aValue, isSelected));
211          }
212
213          int embeddedDataSize = embeddedData.size();
214          int selectedIndexList = 0;
215
216          try {
217             selectedIndexList = Integer.parseInt(selectedIndex);
218          } catch (Exception JavaDoc e) {
219             selectedIndexList = 0;
220          }
221
222          for (int i = 0; i < embeddedDataSize; i++) {
223             KeyValuePair aKeyValuePair = (KeyValuePair) embeddedData.get(i);
224             String JavaDoc aKey = aKeyValuePair.getKey();
225             String JavaDoc aValue = aKeyValuePair.getValue();
226
227             // select, if datadriven and data matches with current value OR if explicitly set by user
228
if (Util.isNull(selectedIndex) && !isSelected) {
229                isSelected = i == 0;
230             } else {
231                if ("value".equalsIgnoreCase(getCompareWith())) {
232                   isSelected = aValue.equals(selectedIndex);
233                } else if ("list".equalsIgnoreCase(getCompareWith())) {
234                   isSelected = selectedIndexList == i;
235                } else {
236                   isSelected = aKey.equals(selectedIndex);
237                }
238             }
239
240             tagBuf.append(generateTagString(aKey, aValue, isSelected));
241          }
242       }
243
244       tagBuf.append("</select>");
245
246       try {
247          pageContext.getOut()
248                     .write(renderPatternHtmlInputField());
249          pageContext.getOut()
250                     .write(RenderHiddenFields(field));
251          pageContext.getOut()
252                     .write(generateSelectHeader(field));
253          pageContext.getOut()
254                     .write(tagBuf.toString());
255       } catch (java.io.IOException JavaDoc ioe) {
256          throw new JspException("IO Error: " + ioe.getMessage());
257       }
258
259       return EVAL_PAGE;
260    }
261
262
263    /**
264     * DOCUMENT ME!
265     */

266    public void doFinally() {
267       embeddedData = null;
268       selectedIndex = null;
269       customEntry = null;
270       size = "1";
271       compareWith = "key";
272       super.doFinally();
273    }
274
275
276    /**
277     * DOCUMENT ME!
278     *
279     * @return DOCUMENT ME!
280     *
281     * @throws javax.servlet.jsp.JspException DOCUMENT ME!
282     */

283    public int doStartTag() throws javax.servlet.jsp.JspException JavaDoc {
284       return EVAL_BODY_INCLUDE;
285    }
286
287
288    private String JavaDoc generateSelectHeader(Field f)
289                                 throws javax.servlet.jsp.JspException JavaDoc {
290       // This method have been
291
StringBuffer JavaDoc tagBuf = new StringBuffer JavaDoc();
292       tagBuf.append("<select name=\"");
293       tagBuf.append(f.getSearchFieldName());
294       tagBuf.append("\"");
295
296       if (size != null) {
297          tagBuf.append(" size=\"");
298          tagBuf.append(size);
299          tagBuf.append("\"");
300       }
301
302       tagBuf.append(prepareStyles());
303       tagBuf.append(prepareEventHandlers());
304       tagBuf.append(">");
305
306       return tagBuf.toString();
307    }
308
309
310    private String JavaDoc generateTagString(String JavaDoc value,
311                                     String JavaDoc description,
312                                     boolean selected) {
313       StringBuffer JavaDoc tagBuf = new StringBuffer JavaDoc();
314       tagBuf.append("<option value=\"");
315       tagBuf.append(value);
316       tagBuf.append("\"");
317
318       if (selected) {
319          tagBuf.append(" selected=\"selected\"");
320       }
321
322       tagBuf.append(">");
323       tagBuf.append(description.trim());
324       tagBuf.append("</option>");
325
326       return tagBuf.toString();
327    }
328 }
329
Popular Tags