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