View Javadoc

1   /*
2    * $Id: ComboBox.java 497654 2007-01-19 00:21:57Z rgielen $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.components;
22  
23  import java.util.Collection;
24  import java.util.Iterator;
25  import java.util.Map;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.apache.struts2.views.annotations.StrutsTag;
31  import org.apache.struts2.views.annotations.StrutsTagAttribute;
32  import org.apache.struts2.util.MakeIterator;
33  
34  import com.opensymphony.xwork2.util.ValueStack;
35  
36  /***
37   * <!-- START SNIPPET: javadoc -->
38   * The combo box is basically an HTML INPUT of type text and HTML SELECT grouped together to give you a combo box
39   * functionality. You can place text in the INPUT control by using the SELECT control or type it in directly in
40   * the text field.<p/>
41   *
42   * In this example, the SELECT will be populated from id=year attribute. Counter is itself an Iterator. It will
43   * span from first to last. The population is done via javascript, and requires that this tag be surrounded by a
44   * &lt;form&gt;.<p/>
45   *
46   * Note that unlike the &lt;s:select/&gt; tag, there is no ability to define the individual &lt;option&gt; tags' id attribute
47   * or content separately. Each of these is simply populated from the toString() method of the list item. Presumably
48   * this is because the select box isn't intended to actually submit useful data, but to assist the user in filling
49   * out the text field.<p/>
50   * <!-- END SNIPPET: javadoc -->
51   *
52   * <p/> <b>Examples</b>
53   *
54   * <pre>
55   * <!-- START SNIPPET: example -->
56   * JSP:
57   * &lt;-- Example One --&gt;
58   * &lt;s:bean name="struts.util.Counter" id="year"&gt;
59   *   &lt;s:param name="first" value="text('firstBirthYear')"/&gt;
60   *   &lt;s:param name="last" value="2000"/&gt;
61   *
62   *   &lt;s:combobox label="Birth year" size="6" maxlength="4" name="birthYear" list="#year"/&gt;
63   * &lt;/s:bean&gt;
64   *
65   * &lt;-- Example Two --&gt;
66   * <s:combobox
67   *     label="My Favourite Fruit"
68   *     name="myFavouriteFruit"
69   *     list="{'apple','banana','grape','pear'}"
70   *     headerKey="-1"
71   *     headerValue="--- Please Select ---"
72   *     emptyOption="true"
73   *     value="banana" />
74   *
75   * &lt;-- Example Two --&gt;
76   * <s:combobox
77   *    label="My Favourite Color"
78   *    name="myFavouriteColor"
79   *    list="#{'red':'red','green':'green','blue':'blue'}"
80   *    headerKey="-1"
81   *    headerValue="--- Please Select ---"
82   *    emptyOption="true"
83   *    value="green" />
84   *
85   * Velocity:
86   * #tag( ComboBox "label=Birth year" "size=6" "maxlength=4" "name=birthYear" "list=#year" )
87   * <!-- END SNIPPET: example -->
88   * </pre>
89   *
90   */
91  @StrutsTag(name="combobox", tldTagClass="org.apache.struts2.views.jsp.ui.ComboBoxTag", description="Widget that fills a text box from a select")
92  public class ComboBox extends TextField {
93      final public static String TEMPLATE = "combobox";
94  
95      protected String list;
96      protected String listKey;
97      protected String listValue;
98      protected String headerKey;
99      protected String headerValue;
100     protected String emptyOption;
101 
102 
103     public ComboBox(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
104         super(stack, request, response);
105     }
106 
107     protected String getDefaultTemplate() {
108         return TEMPLATE;
109     }
110 
111     public void evaluateExtraParams() {
112         super.evaluateExtraParams();
113 
114         Object value = findListValue();
115 
116         if (headerKey != null) {
117             addParameter("headerKey", findString(headerKey));
118         }
119         if (headerValue != null) {
120             addParameter("headerValue", findString(headerValue));
121         }
122         if (emptyOption != null) {
123             addParameter("emptyOption", findValue(emptyOption, Boolean.class));
124         }
125 
126         if (value != null) {
127             if (value instanceof Collection) {
128                 Collection tmp = (Collection) value;
129                 addParameter("list", tmp);
130                 if (listKey != null) {
131                     addParameter("listKey", listKey);
132                 }
133                 if (listValue != null) {
134                     addParameter("listValue", listValue);
135                 }
136             } else if (value instanceof Map) {
137                 Map tmp = (Map) value;
138                 addParameter("list", MakeIterator.convert(tmp));
139                 addParameter("listKey", "key");
140                 addParameter("listValue", "value");
141             } else if (value.getClass().isArray()) {
142                 Iterator i = MakeIterator.convert(value);
143                 addParameter("list", i);
144                 if (listKey != null) {
145                     addParameter("listKey", listKey);
146                 }
147                 if (listValue != null) {
148                     addParameter("listValue", listValue);
149                 }
150             } else {
151                 Iterator i = MakeIterator.convert(value);
152                 addParameter("list", i);
153                 if (listKey != null) {
154                     addParameter("listKey", listKey);
155                 }
156                 if (listValue != null) {
157                     addParameter("listValue", listValue);
158                 }
159             }
160         }
161     }
162 
163     protected Object findListValue() {
164         return findValue(list, "list",
165                 "You must specify a collection/array/map/enumeration/iterator. " +
166                 "Example: people or people.{name}");
167     }
168 
169     @StrutsTagAttribute(description="Iteratable source to populate from. " +
170                 "If this is missing, the select widget is simply not displayed.", required=true)
171     public void setList(String list) {
172         this.list = list;
173     }
174 
175     @StrutsTagAttribute(description="Decide if an empty option is to be inserted. Default false.")
176     public void setEmptyOption(String emptyOption) {
177         this.emptyOption = emptyOption;
178     }
179 
180     @StrutsTagAttribute(description="Set the header key for the header option.")
181     public void setHeaderKey(String headerKey) {
182         this.headerKey = headerKey;
183     }
184 
185     @StrutsTagAttribute(description="Set the header value for the header option.")
186     public void setHeaderValue(String headerValue) {
187         this.headerValue = headerValue;
188     }
189 
190     @StrutsTagAttribute(description="Set the key used to retrive the option key.")
191     public void setListKey(String listKey) {
192         this.listKey = listKey;
193     }
194 
195     @StrutsTagAttribute(description="Set the value used to retrive the option value.")
196     public void setListValue(String listValue) {
197         this.listValue = listValue;
198     }
199 
200 
201 }