View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/DbSearchComboTag.java,v 1.27 2006/03/30 02:08:07 nicparise Exp $
3    * $Revision: 1.27 $
4    * $Date: 2006/03/30 02:08:07 $
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  import org.dbforms.interfaces.IDataContainer;
28  import org.dbforms.interfaces.StaticData;
29  
30  import org.dbforms.util.*;
31  
32  import java.util.List;
33  
34  import javax.servlet.http.*;
35  import javax.servlet.jsp.*;
36  
37  
38  
39  /***
40   * <p>
41   * renders a select field for searching with special default search modes.
42   * </p>
43   *
44   * <p>
45   * example:
46   * </p>
47   * &lt;input type="hidden" name="searchalgo_0_1" value="weakEnd"/&gt; &lt;input
48   * type="hidden" name="searchmode_0_1" value="AND"/&gt; &lt;select
49   * name="search_0_1"/&gt; &lt;/select&gt; searchalgo and searchmode are set by
50   * parameter.
51   *
52   * @author Henner Kollmann
53   */
54  public class DbSearchComboTag extends DbSearchTag implements IDataContainer,
55                                                               javax.servlet.jsp.tagext.TryCatchFinally {
56     private List   embeddedData  = null;
57     private String compareWith   = "key";
58     private String customEntry;
59     private String selectedIndex;
60     private String size          = "1";
61  
62     /***
63      * Creates a new DbSearchComboTag object.
64      */
65     public DbSearchComboTag() {
66        setSearchAlgo("sharp");
67     }
68  
69     /***
70      * DOCUMENT ME!
71      *
72      * @param string
73      */
74     public void setCompareWith(String string) {
75        compareWith = string;
76     }
77  
78  
79     /***
80      * DOCUMENT ME!
81      *
82      * @return
83      */
84     public String getCompareWith() {
85        return compareWith;
86     }
87  
88  
89     /***
90      * DOCUMENT ME!
91      *
92      * @param customEntry DOCUMENT ME!
93      */
94     public void setCustomEntry(String customEntry) {
95        this.customEntry = customEntry;
96     }
97  
98  
99     /***
100     * DOCUMENT ME!
101     *
102     * @return DOCUMENT ME!
103     */
104    public String getCustomEntry() {
105       return customEntry;
106    }
107 
108 
109    /***
110     * This method is a "hookup" for EmbeddedData - Tags which can assign the
111     * lines of data they loaded (by querying a database, or by rendering
112     * data-subelements, etc. etc.) and make the data available to this tag.
113     * [this method is defined in Interface DataContainer]
114     *
115     * @param embeddedData DOCUMENT ME!
116     */
117    public void setEmbeddedData(List embeddedData) {
118       this.embeddedData = embeddedData;
119    }
120 
121 
122    /***
123     * DOCUMENT ME!
124     *
125     * @param selectedIndex DOCUMENT ME!
126     */
127    public void setSelectedIndex(String selectedIndex) {
128       this.selectedIndex = selectedIndex;
129    }
130 
131 
132    /***
133     * DOCUMENT ME!
134     *
135     * @return DOCUMENT ME!
136     */
137    public String getSelectedIndex() {
138       return selectedIndex;
139    }
140 
141 
142    /***
143     * DOCUMENT ME!
144     *
145     * @param string
146     */
147    public void setSize(String string) {
148       size = string;
149    }
150 
151 
152    // ------------------------------------------------------ Protected Methods
153 
154    /***
155     * DOCUMENT ME!
156     *
157     * @return
158     */
159    public String getSize() {
160       return size;
161    }
162 
163 
164    /***
165     * DOCUMENT ME!
166     *
167     * @return DOCUMENT ME!
168     *
169     * @throws javax.servlet.jsp.JspException DOCUMENT ME!
170     * @throws JspException DOCUMENT ME!
171     */
172    public int doEndTag() throws javax.servlet.jsp.JspException {
173       HttpServletRequest request = (HttpServletRequest) this.pageContext
174                                    .getRequest();
175 
176       Field              field = getField();
177 
178       StringBuffer       tagBuf = new StringBuffer();
179 
180       String             oldValue = ParseUtil.getParameter(request,
181                                                            field
182                                                            .getSearchFieldName());
183 
184       if (!Util.isNull(oldValue)) {
185          selectedIndex = oldValue;
186       }
187 
188       boolean isSelected = false;
189 
190       if (embeddedData != null) { // no embedded data is nested in this tag
191 
192          if (!Util.isNull(customEntry)) {
193             String aKey = org.dbforms.util.StringUtil
194                           .getEmbeddedStringWithoutDots(customEntry, 0, ',');
195             String aValue = org.dbforms.util.StringUtil
196                             .getEmbeddedStringWithoutDots(customEntry, 1, ',');
197 
198          	// is captionResource is activated, retrieve the value from the MessageResources bundle
199             if (getParentForm().hasCaptionResourceSet()) {
200               aValue = MessageResources.getMessage(request,aValue);
201             }
202 
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 e) {
219             selectedIndexList = 0;
220          }
221 
222          for (int i = 0; i < embeddedDataSize; i++) {
223             StaticData aKeyValuePair = (StaticData) embeddedData.get(i);
224             String       aKey   = aKeyValuePair.getKey();
225             String       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 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 {
284       return EVAL_BODY_INCLUDE;
285    }
286 
287 
288    private String generateSelectHeader(Field f)
289                                 throws javax.servlet.jsp.JspException {
290       // This method have been 
291       StringBuffer tagBuf = new StringBuffer();
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 generateTagString(String  value,
311                                     String  description,
312                                     boolean selected) {
313       StringBuffer tagBuf = new StringBuffer();
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 }