View Javadoc

1   /*
2    * $Id: UpDownSelect.java 451544 2006-09-30 05:38:02Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.components;
19  
20  import java.util.LinkedHashMap;
21  import java.util.Map;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  import com.opensymphony.xwork2.util.ValueStack;
30  
31  /***
32   * <!-- START SNIPPET: javadoc -->
33   * 
34   * Create a Select component with buttons to move the elements in the select component
35   * up and down. When the containing form is submited, its elements will be submitted in 
36   * the order they are arranged (top to bottom).
37   * 
38   * <!-- END SNIPPET: javadoc -->
39   * 
40   * <p/>
41   * 
42   * <pre>
43   * <!-- START SNIPPET: example -->
44   * 
45   * &lt;!-- Example 1: simple example --&gt;
46   * &lt;s:updownselect
47   * list="#{'england':'England', 'america':'America', 'germany':'Germany'}" 
48   * name="prioritisedFavouriteCountries" 
49   * headerKey="-1" 
50   * headerValue="--- Please Order Them Accordingly ---" 
51   * emptyOption="true" /&gt;
52   *
53   * &lt;!-- Example 2: more complex example --&gt;
54   * &lt;s:updownselect
55   * list="defaultFavouriteCartoonCharacters" 
56   * name="prioritisedFavouriteCartoonCharacters" 
57   * headerKey="-1" 
58   * headerValue="--- Please Order ---" 
59   * emptyOption="true" 
60   * allowMoveUp="true" 
61   * allowMoveDown="true" 
62   * allowSelectAll="true" 
63   * moveUpLabel="Move Up"
64   * moveDownLabel="Move Down" 
65   * selectAllLabel="Select All" /&gt;
66   * 
67   * <!-- END SNIPPET: example -->
68   * </pre>
69   * 
70   * @version $Date: 2006-09-30 01:38:02 -0400 (Sat, 30 Sep 2006) $ $Id: UpDownSelect.java 451544 2006-09-30 05:38:02Z mrdon $
71   * 
72   * @s.tag name="updownselect" tld-body-content="JSP" tld-tag-class="org.apache.struts2.views.jsp.ui.UpDownSelectTag"
73   * description="Render a up down select element"
74   */
75  public class UpDownSelect extends Select {
76  	
77  	private static final Log _log = LogFactory.getLog(UpDownSelect.class);
78  	
79  
80  	final public static String TEMPLATE = "updownselect";
81  	
82  	protected String allowMoveUp;
83  	protected String allowMoveDown;
84  	protected String allowSelectAll;
85  	
86  	protected String moveUpLabel;
87  	protected String moveDownLabel;
88  	protected String selectAllLabel;
89  	
90  	
91  	public String getDefaultTemplate() {
92  		return TEMPLATE;
93  	}
94  	
95  	public UpDownSelect(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
96  		super(stack, request, response);
97  	}
98  	
99  	public void evaluateParams() {
100 		super.evaluateParams();
101 		
102 		
103 		// override Select's default
104 		if (size == null || size.trim().length() <= 0) {
105 			addParameter("size", "5");
106 		}
107 		if (multiple == null || multiple.trim().length() <= 0) {
108 			addParameter("multiple", Boolean.TRUE);
109 		}
110 		
111 		
112 		
113 		if (allowMoveUp != null) {
114 			addParameter("allowMoveUp", findValue(allowMoveUp, Boolean.class));
115 		}
116 		if (allowMoveDown != null) {
117 			addParameter("allowMoveDown", findValue(allowMoveDown, Boolean.class));
118 		}
119 		if (allowSelectAll != null) {
120 			addParameter("allowSelectAll", findValue(allowSelectAll, Boolean.class));
121 		}
122 		
123 		if (moveUpLabel != null) {
124 			addParameter("moveUpLabel", findString(moveUpLabel));
125 		}
126 		if (moveDownLabel != null) {
127 			addParameter("moveDownLabel", findString(moveDownLabel));
128 		}
129 		if (selectAllLabel != null) {
130 			addParameter("selectAllLabel", findString(selectAllLabel));
131 		}
132 		
133 		
134 		// inform our form ancestor about this UpDownSelect so the form knows how to 
135 		// auto select all options upon it submission
136 		Form ancestorForm = (Form) findAncestor(Form.class);
137 		if (ancestorForm != null) {
138 			
139 			// inform form ancestor that we are using a custom onsubmit
140 			enableAncestorFormCustomOnsubmit();
141 			
142 			Map m = (Map) ancestorForm.getParameters().get("updownselectIds");
143 			if (m == null) {
144 				// map with key -> id ,  value -> headerKey
145 				m = new LinkedHashMap();
146 			}
147 			m.put(getParameters().get("id"), getParameters().get("headerKey"));
148 			ancestorForm.getParameters().put("updownselectIds", m);
149 		}
150 		else {
151 			_log.warn("no ancestor form found for updownselect "+this+", therefore autoselect of all elements upon form submission will not work ");
152 		}
153 	}
154 
155 	
156 	public String getAllowMoveUp() { 
157 		return allowMoveUp;
158 	}
159 	/***
160      * Whether move up button should be displayed
161 	 * @s.tagattribute required="false" type="Boolean" default="true"
162 	 */
163 	public void setAllowMoveUp(String allowMoveUp) {
164 		this.allowMoveUp = allowMoveUp;
165 	}
166 	
167 	
168 	
169 	public String getAllowMoveDown() {
170 		return allowMoveDown;
171 	}
172 	/***
173      * Whether move down button should be displayed
174 	 * @s.tagattribute required="false" type="Boolean" default="true"
175 	 */
176 	public void setAllowMoveDown(String allowMoveDown) {
177 		this.allowMoveDown = allowMoveDown;
178 	}
179 	
180 	
181 	
182 	public String getAllowSelectAll() {
183 		return allowSelectAll;
184 	}
185 	/***
186      * Whether or not select all button should be displayed
187 	 * @s.tagattribute required="false" type="Boolean" default="true"
188 	 */
189 	public void setAllowSelectAll(String allowSelectAll) {
190 		this.allowSelectAll = allowSelectAll;
191 	}
192 	
193 	
194 	public String getMoveUpLabel() {
195 		return moveUpLabel;
196 	}
197 	/***
198      * Text to display on the move up button
199 	 * @s.tagattribute required="false" type="String" default="^"
200 	 */
201 	public void setMoveUpLabel(String moveUpLabel) {
202 		this.moveUpLabel = moveUpLabel;
203 	}
204 	
205 	
206 	
207 	public String getMoveDownLabel() {
208 		return moveDownLabel;
209 	}
210 	/***
211      * Text to display on the move down button
212 	 * @s.tagattribute required="false" type="String" default="v"
213 	 */
214 	public void setMoveDownLabel(String moveDownLabel) {
215 		this.moveDownLabel = moveDownLabel;
216 	}
217 	
218 
219 	
220 	public String getSelectAllLabel() {
221 		return selectAllLabel;
222 	}
223 	/***
224      * Text to display on the select all button
225 	 * @s.tagattribute required="false" type="String" default="*"
226 	 */
227 	public void setSelectAllLabel(String selectAllLabel) {
228 		this.selectAllLabel = selectAllLabel;
229 	}
230 }