001: /*
002: * $Id: ComboBox.java 497654 2007-01-19 00:21:57Z rgielen $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.components;
022:
023: import java.util.Collection;
024: import java.util.Iterator;
025: import java.util.Map;
026:
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.apache.struts2.views.annotations.StrutsTag;
031: import org.apache.struts2.views.annotations.StrutsTagAttribute;
032: import org.apache.struts2.util.MakeIterator;
033:
034: import com.opensymphony.xwork2.util.ValueStack;
035:
036: /**
037: * <!-- START SNIPPET: javadoc -->
038: * The combo box is basically an HTML INPUT of type text and HTML SELECT grouped together to give you a combo box
039: * functionality. You can place text in the INPUT control by using the SELECT control or type it in directly in
040: * the text field.<p/>
041: *
042: * In this example, the SELECT will be populated from id=year attribute. Counter is itself an Iterator. It will
043: * span from first to last. The population is done via javascript, and requires that this tag be surrounded by a
044: * <form>.<p/>
045: *
046: * Note that unlike the <s:select/> tag, there is no ability to define the individual <option> tags' id attribute
047: * or content separately. Each of these is simply populated from the toString() method of the list item. Presumably
048: * this is because the select box isn't intended to actually submit useful data, but to assist the user in filling
049: * out the text field.<p/>
050: * <!-- END SNIPPET: javadoc -->
051: *
052: * <p/> <b>Examples</b>
053: *
054: * <pre>
055: * <!-- START SNIPPET: example -->
056: * JSP:
057: * <-- Example One -->
058: * <s:bean name="struts.util.Counter" id="year">
059: * <s:param name="first" value="text('firstBirthYear')"/>
060: * <s:param name="last" value="2000"/>
061: *
062: * <s:combobox label="Birth year" size="6" maxlength="4" name="birthYear" list="#year"/>
063: * </s:bean>
064: *
065: * <-- Example Two -->
066: * <s:combobox
067: * label="My Favourite Fruit"
068: * name="myFavouriteFruit"
069: * list="{'apple','banana','grape','pear'}"
070: * headerKey="-1"
071: * headerValue="--- Please Select ---"
072: * emptyOption="true"
073: * value="banana" />
074: *
075: * <-- Example Two -->
076: * <s:combobox
077: * label="My Favourite Color"
078: * name="myFavouriteColor"
079: * list="#{'red':'red','green':'green','blue':'blue'}"
080: * headerKey="-1"
081: * headerValue="--- Please Select ---"
082: * emptyOption="true"
083: * value="green" />
084: *
085: * Velocity:
086: * #tag( ComboBox "label=Birth year" "size=6" "maxlength=4" "name=birthYear" "list=#year" )
087: * <!-- END SNIPPET: example -->
088: * </pre>
089: *
090: */
091: @StrutsTag(name="combobox",tldTagClass="org.apache.struts2.views.jsp.ui.ComboBoxTag",description="Widget that fills a text box from a select")
092: public class ComboBox extends TextField {
093: final public static String TEMPLATE = "combobox";
094:
095: protected String list;
096: protected String listKey;
097: protected String listValue;
098: protected String headerKey;
099: protected String headerValue;
100: protected String emptyOption;
101:
102: public ComboBox(ValueStack stack, HttpServletRequest request,
103: 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,
124: Boolean.class));
125: }
126:
127: if (value != null) {
128: if (value instanceof Collection) {
129: Collection tmp = (Collection) value;
130: addParameter("list", tmp);
131: if (listKey != null) {
132: addParameter("listKey", listKey);
133: }
134: if (listValue != null) {
135: addParameter("listValue", listValue);
136: }
137: } else if (value instanceof Map) {
138: Map tmp = (Map) value;
139: addParameter("list", MakeIterator.convert(tmp));
140: addParameter("listKey", "key");
141: addParameter("listValue", "value");
142: } else if (value.getClass().isArray()) {
143: Iterator i = MakeIterator.convert(value);
144: addParameter("list", i);
145: if (listKey != null) {
146: addParameter("listKey", listKey);
147: }
148: if (listValue != null) {
149: addParameter("listValue", listValue);
150: }
151: } else {
152: Iterator i = MakeIterator.convert(value);
153: addParameter("list", i);
154: if (listKey != null) {
155: addParameter("listKey", listKey);
156: }
157: if (listValue != null) {
158: addParameter("listValue", listValue);
159: }
160: }
161: }
162: }
163:
164: protected Object findListValue() {
165: return findValue(list, "list",
166: "You must specify a collection/array/map/enumeration/iterator. "
167: + "Example: people or people.{name}");
168: }
169:
170: @StrutsTagAttribute(description="Iteratable source to populate from. " + "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: }
|