001: /*
002: * $Id: Select.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 javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletResponse;
025:
026: import org.apache.struts2.views.annotations.StrutsTag;
027: import org.apache.struts2.views.annotations.StrutsTagAttribute;
028:
029: import com.opensymphony.xwork2.util.ValueStack;
030:
031: /**
032: * <!-- START SNIPPET: javadoc -->
033: *
034: * Render an HTML input tag of type select.
035: *
036: * <!-- END SNIPPET: javadoc -->
037: *
038: * <p/> <b>Examples</b>
039: * <pre>
040: * <!-- START SNIPPET: example -->
041: *
042: * <s:select label="Pets"
043: * name="petIds"
044: * list="petDao.pets"
045: * listKey="id"
046: * listValue="name"
047: * multiple="true"
048: * size="3"
049: * required="true"
050: * />
051: *
052: * <s:select label="Months"
053: * name="months"
054: * headerKey="-1" headerValue="Select Month"
055: * list="#{'01':'Jan', '02':'Feb', [...]}"
056: * value="selectedMonth"
057: * required="true"
058: * />
059: *
060: * // The month id (01, 02, ...) returned by the getSelectedMonth() call
061: * // against the stack will be auto-selected
062: *
063: * <!-- END SNIPPET: example -->
064: * </pre>
065: *
066: * <p/>
067: *
068: * <!-- START SNIPPET: exnote -->
069: *
070: * Note: For any of the tags that use lists (select probably being the most ubiquitous), which uses the OGNL list
071: * notation (see the "months" example above), it should be noted that the map key created (in the months example,
072: * the '01', '02', etc.) is typed. '1' is a char, '01' is a String, "1" is a String. This is important since if
073: * the value returned by your "value" attribute is NOT the same type as the key in the "list" attribute, they
074: * WILL NOT MATCH, even though their String values may be equivalent. If they don't match, nothing in your list
075: * will be auto-selected.<p/>
076: *
077: * <!-- END SNIPPET: exnote -->
078: *
079: */
080: @StrutsTag(name="select",tldTagClass="org.apache.struts2.views.jsp.ui.SelectTag",description="Render a select element")
081: public class Select extends ListUIBean {
082: final public static String TEMPLATE = "select";
083:
084: protected String emptyOption;
085: protected String headerKey;
086: protected String headerValue;
087: protected String multiple;
088: protected String size;
089:
090: public Select(ValueStack stack, HttpServletRequest request,
091: HttpServletResponse response) {
092: super (stack, request, response);
093: }
094:
095: protected String getDefaultTemplate() {
096: return TEMPLATE;
097: }
098:
099: public void evaluateExtraParams() {
100: super .evaluateExtraParams();
101:
102: if (emptyOption != null) {
103: addParameter("emptyOption", findValue(emptyOption,
104: Boolean.class));
105: }
106:
107: if (multiple != null) {
108: addParameter("multiple", findValue(multiple, Boolean.class));
109: }
110:
111: if (size != null) {
112: addParameter("size", findString(size));
113: }
114:
115: if ((headerKey != null) && (headerValue != null)) {
116: addParameter("headerKey", findString(headerKey));
117: addParameter("headerValue", findString(headerValue));
118: }
119: }
120:
121: @StrutsTagAttribute(description="Whether or not to add an empty (--) option after the header option",type="Boolean",defaultValue="false")
122: public void setEmptyOption(String emptyOption) {
123: this .emptyOption = emptyOption;
124: }
125:
126: @StrutsTagAttribute(description=" Key for first item in list. Must not be empty! '-1' and '' is correct, '' is bad.")
127: public void setHeaderKey(String headerKey) {
128: this .headerKey = headerKey;
129: }
130:
131: @StrutsTagAttribute(description="Value expression for first item in list")
132: public void setHeaderValue(String headerValue) {
133: this .headerValue = headerValue;
134: }
135:
136: @StrutsTagAttribute(description=" Creates a multiple select. The tag will pre-select multiple values" + " if the values are passed as an Array (of appropriate types) via the value attribute. Passing " + "a Collection may work too? Haven't tested this.",type="Boolean",defaultValue="false")
137: public void setMultiple(String multiple) {
138: this .multiple = multiple;
139: }
140:
141: @StrutsTagAttribute(description="Size of the element box (# of elements to show)",type="Integer")
142: public void setSize(String size) {
143: this.size = size;
144: }
145: }
|