001: /*
002: * $Id: UpDownSelect.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.LinkedHashMap;
024: import java.util.Map;
025:
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.apache.struts2.views.annotations.StrutsTag;
032: import org.apache.struts2.views.annotations.StrutsTagAttribute;
033:
034: import com.opensymphony.xwork2.util.ValueStack;
035:
036: /**
037: * <!-- START SNIPPET: javadoc -->
038: *
039: * Create a Select component with buttons to move the elements in the select component
040: * up and down. When the containing form is submited, its elements will be submitted in
041: * the order they are arranged (top to bottom).
042: *
043: * <!-- END SNIPPET: javadoc -->
044: *
045: * <p/>
046: *
047: * <pre>
048: * <!-- START SNIPPET: example -->
049: *
050: * <!-- Example 1: simple example -->
051: * <s:updownselect
052: * list="#{'england':'England', 'america':'America', 'germany':'Germany'}"
053: * name="prioritisedFavouriteCountries"
054: * headerKey="-1"
055: * headerValue="--- Please Order Them Accordingly ---"
056: * emptyOption="true" />
057: *
058: * <!-- Example 2: more complex example -->
059: * <s:updownselect
060: * list="defaultFavouriteCartoonCharacters"
061: * name="prioritisedFavouriteCartoonCharacters"
062: * headerKey="-1"
063: * headerValue="--- Please Order ---"
064: * emptyOption="true"
065: * allowMoveUp="true"
066: * allowMoveDown="true"
067: * allowSelectAll="true"
068: * moveUpLabel="Move Up"
069: * moveDownLabel="Move Down"
070: * selectAllLabel="Select All" />
071: *
072: * <!-- END SNIPPET: example -->
073: * </pre>
074: *
075: * @version $Date: 2007-01-18 19:21:57 -0500 (Thu, 18 Jan 2007) $ $Id: UpDownSelect.java 497654 2007-01-19 00:21:57Z rgielen $
076: *
077: * @s.tag name="updownselect" tld-body-content="JSP" tld-tag-class="org.apache.struts2.views.jsp.ui.UpDownSelectTag"
078: * description="Render a up down select element"
079: */
080: @StrutsTag(name="updownselect",tldTagClass="org.apache.struts2.views.jsp.ui.UpDownSelectTag",description="Create a Select component with buttons to move the elements in the select component up and down")
081: public class UpDownSelect extends Select {
082:
083: private static final Log _log = LogFactory
084: .getLog(UpDownSelect.class);
085:
086: final public static String TEMPLATE = "updownselect";
087:
088: protected String allowMoveUp;
089: protected String allowMoveDown;
090: protected String allowSelectAll;
091:
092: protected String moveUpLabel;
093: protected String moveDownLabel;
094: protected String selectAllLabel;
095:
096: public String getDefaultTemplate() {
097: return TEMPLATE;
098: }
099:
100: public UpDownSelect(ValueStack stack, HttpServletRequest request,
101: HttpServletResponse response) {
102: super (stack, request, response);
103: }
104:
105: public void evaluateParams() {
106: super .evaluateParams();
107:
108: // override Select's default
109: if (size == null || size.trim().length() <= 0) {
110: addParameter("size", "5");
111: }
112: if (multiple == null || multiple.trim().length() <= 0) {
113: addParameter("multiple", Boolean.TRUE);
114: }
115:
116: if (allowMoveUp != null) {
117: addParameter("allowMoveUp", findValue(allowMoveUp,
118: Boolean.class));
119: }
120: if (allowMoveDown != null) {
121: addParameter("allowMoveDown", findValue(allowMoveDown,
122: Boolean.class));
123: }
124: if (allowSelectAll != null) {
125: addParameter("allowSelectAll", findValue(allowSelectAll,
126: Boolean.class));
127: }
128:
129: if (moveUpLabel != null) {
130: addParameter("moveUpLabel", findString(moveUpLabel));
131: }
132: if (moveDownLabel != null) {
133: addParameter("moveDownLabel", findString(moveDownLabel));
134: }
135: if (selectAllLabel != null) {
136: addParameter("selectAllLabel", findString(selectAllLabel));
137: }
138:
139: // inform our form ancestor about this UpDownSelect so the form knows how to
140: // auto select all options upon it submission
141: Form ancestorForm = (Form) findAncestor(Form.class);
142: if (ancestorForm != null) {
143:
144: // inform form ancestor that we are using a custom onsubmit
145: enableAncestorFormCustomOnsubmit();
146:
147: Map m = (Map) ancestorForm.getParameters().get(
148: "updownselectIds");
149: if (m == null) {
150: // map with key -> id , value -> headerKey
151: m = new LinkedHashMap();
152: }
153: m.put(getParameters().get("id"), getParameters().get(
154: "headerKey"));
155: ancestorForm.getParameters().put("updownselectIds", m);
156: } else {
157: _log
158: .warn("no ancestor form found for updownselect "
159: + this
160: + ", therefore autoselect of all elements upon form submission will not work ");
161: }
162: }
163:
164: public String getAllowMoveUp() {
165: return allowMoveUp;
166: }
167:
168: @StrutsTagAttribute(description="Whether move up button should be displayed",type="Boolean",defaultValue="true")
169: public void setAllowMoveUp(String allowMoveUp) {
170: this .allowMoveUp = allowMoveUp;
171: }
172:
173: public String getAllowMoveDown() {
174: return allowMoveDown;
175: }
176:
177: @StrutsTagAttribute(description="Whether move down button should be displayed",type="Boolean",defaultValue="true")
178: public void setAllowMoveDown(String allowMoveDown) {
179: this .allowMoveDown = allowMoveDown;
180: }
181:
182: public String getAllowSelectAll() {
183: return allowSelectAll;
184: }
185:
186: @StrutsTagAttribute(description="Whether or not select all button should be displayed",type="Boolean",defaultValue="true")
187: public void setAllowSelectAll(String allowSelectAll) {
188: this .allowSelectAll = allowSelectAll;
189: }
190:
191: public String getMoveUpLabel() {
192: return moveUpLabel;
193: }
194:
195: @StrutsTagAttribute(description="Text to display on the move up button",defaultValue="^")
196: public void setMoveUpLabel(String moveUpLabel) {
197: this .moveUpLabel = moveUpLabel;
198: }
199:
200: public String getMoveDownLabel() {
201: return moveDownLabel;
202: }
203:
204: @StrutsTagAttribute(description="Text to display on the move down button",defaultValue="v")
205: public void setMoveDownLabel(String moveDownLabel) {
206: this .moveDownLabel = moveDownLabel;
207: }
208:
209: public String getSelectAllLabel() {
210: return selectAllLabel;
211: }
212:
213: @StrutsTagAttribute(description="Text to display on the select all button",defaultValue="*")
214: public void setSelectAllLabel(String selectAllLabel) {
215: this.selectAllLabel = selectAllLabel;
216: }
217: }
|