View Javadoc

1   /*
2    * $Id: OptionTransferSelect.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 option transfer select component which is basically two &lt;select ...&gt;
35   * tag with buttons in the middle of them allowing options in each of the 
36   * &lt;select ...&gt; to be moved between themselves. Will auto-select all its 
37   * elements upon its containing form submision.
38   * 
39   * <!-- END SNIPPET: javadoc -->
40   * 
41   * <p/>
42   * 
43   * 
44   * <!-- START SNIPPET: notice -->
45   * 
46   * NOTE: The id and doubleId need not be supplied as they will generated provided
47   * that the optiontransferselect tag is being used in a form tag. The generated id 
48   * and doubleId will be &lt;form_id&gt;_&lt;optiontransferselect_doubleName&gt; and 
49   * &lt;form_id&gt;_&lt;optiontransferselect_doubleName&gt; respectively.
50   * 
51   * <!-- END SNIPPET: notice -->
52   * 
53   * <p/>
54   * 
55   * <pre>
56   * <!-- START SNIPPET: example -->
57   * 
58   * &lt;-- minimum configuration --&gt;
59   * &lt;s:optiontransferselect
60   *   	label="Favourite Cartoons Characters"
61   *		name="leftSideCartoonCharacters" 
62   *		list="{'Popeye', 'He-Man', 'Spiderman'}" 
63   *		doubleName="rightSideCartoonCharacters"
64   *		doubleList="{'Superman', 'Mickey Mouse', 'Donald Duck'}" 
65   *	/&gt;
66   *
67   *  &lt;-- possible configuration --&gt;
68   *  &lt;s:optiontransferselect
69   *   	label="Favourite Cartoons Characters"
70   *		name="leftSideCartoonCharacters" 
71   *		leftTitle="Left Title"
72   *		rightTitle="Right Title"
73   *		list="{'Popeye', 'He-Man', 'Spiderman'}" 
74   *		multiple="true"
75   *		headerKey="headerKey"
76   *		headerValue="--- Please Select ---"
77   *		emptyOption="true"
78   *		doubleList="{'Superman', 'Mickey Mouse', 'Donald Duck'}" 
79   *		doubleName="rightSideCartoonCharacters"
80   *		doubleHeaderKey="doubleHeaderKey"
81   *		doubleHeaderValue="--- Please Select ---" 
82   *		doubleEmptyOption="true"
83   *		doubleMultiple="true"
84   *	/&gt;
85   * 
86   * <!-- END SNIPPET: example -->
87   * </pre>
88   * 
89   * @s.tag name="optiontransferselect" tld-body-content="JSP" tld-tag-class="org.apache.struts2.views.jsp.ui.OptionTransferSelectTag"
90   * description="Renders an input form"
91   */
92  public class OptionTransferSelect extends DoubleListUIBean {
93  	
94  	private static final Log _log = LogFactory.getLog(OptionTransferSelect.class);
95  
96  	private static final String TEMPLATE = "optiontransferselect";
97  	
98  	protected String allowAddToLeft;
99  	protected String allowAddToRight;
100 	protected String allowAddAllToLeft;
101 	protected String allowAddAllToRight;
102 	protected String allowSelectAll;
103 	protected String allowUpDownOnLeft;
104 	protected String allowUpDownOnRight;
105 	
106 	protected String leftTitle;
107 	protected String rightTitle;
108 	
109 	protected String buttonCssClass;
110 	protected String buttonCssStyle;
111 
112 	protected String addToLeftLabel;
113 	protected String addToRightLabel;
114 	protected String addAllToLeftLabel;
115 	protected String addAllToRightLabel;
116 	protected String selectAllLabel;
117 	protected String leftUpLabel;
118 	protected String leftDownlabel;
119 	protected String rightUpLabel;
120 	protected String rightDownLabel;
121 	
122 	
123 	public OptionTransferSelect(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
124 		super(stack, request, response);
125 	}
126 
127 	protected String getDefaultTemplate() {
128 		return TEMPLATE;
129 	}
130 	
131 	
132 	public void evaluateExtraParams() {
133 		super.evaluateExtraParams();
134 		
135 		Object doubleValue = null;
136 		
137 		// override DoubleListUIBean's 
138         if (doubleList != null) {
139             doubleValue = findValue(doubleList);
140             addParameter("doubleList", doubleValue);
141         }
142         if (size == null || size.trim().length() <= 0) {
143         	addParameter("size", "15");
144         }
145         if (doubleSize == null || doubleSize.trim().length() <= 0) {
146         	addParameter("doubleSize", "15");
147         }
148         if (multiple == null || multiple.trim().length() <= 0) {
149         	addParameter("multiple", Boolean.TRUE);
150         }
151         if (doubleMultiple == null || doubleMultiple.trim().length() <= 0) {
152         	addParameter("doubleMultiple", Boolean.TRUE);
153         }
154         
155         
156         
157         
158         
159         // buttonCssClass 
160         if (buttonCssClass != null && buttonCssClass.trim().length() > 0) {
161         	addParameter("buttonCssClass", buttonCssClass);
162         }
163         
164         // buttonCssStyle
165         if (buttonCssStyle != null && buttonCssStyle.trim().length() > 0) {
166         	addParameter("buttonCssStyle", buttonCssStyle);
167         }
168         
169         
170 
171         // allowSelectAll
172         addParameter("allowSelectAll", 
173         		allowSelectAll != null ? findValue(allowSelectAll, Boolean.class) : Boolean.TRUE);
174 		
175 		// allowAddToLeft
176 		addParameter("allowAddToLeft", 
177 				allowAddToLeft != null ? findValue(allowAddToLeft, Boolean.class) : Boolean.TRUE);
178 		
179 		// allowAddToRight
180 		addParameter("allowAddToRight",
181 				allowAddToRight != null ? findValue(allowAddToRight, Boolean.class) : Boolean.TRUE);
182 		
183 		// allowAddAllToLeft
184 		addParameter("allowAddAllToLeft",
185 				allowAddAllToLeft != null ? findValue(allowAddAllToLeft, Boolean.class) : Boolean.TRUE);
186 		
187 		// allowAddAllToRight
188 		addParameter("allowAddAllToRight", 
189 				allowAddAllToRight != null ? findValue(allowAddAllToRight, Boolean.class) : Boolean.TRUE);
190 		
191 		// allowUpDownOnLeft
192 		addParameter("allowUpDownOnLeft", 
193 				allowUpDownOnLeft != null ? findValue(allowUpDownOnLeft, Boolean.class) : Boolean.TRUE);
194 		
195 		// allowUpDownOnRight
196 		addParameter("allowUpDownOnRight", 
197 				allowUpDownOnRight != null ? findValue(allowUpDownOnRight, Boolean.class) : Boolean.TRUE);
198 		
199 		
200 		// leftTitle
201 		if (leftTitle != null) {
202 			addParameter("leftTitle", findValue(leftTitle, String.class));
203 		}
204 		
205 		// rightTitle
206 		if (rightTitle != null) {
207 			addParameter("rightTitle", findValue(rightTitle, String.class));
208 		}
209 		
210 		
211 		// addToLeftLabel
212 		addParameter("addToLeftLabel", 
213 				addToLeftLabel != null ? findValue(addToLeftLabel, String.class) : "<-" );
214 		
215 		// addToRightLabel
216 		addParameter("addToRightLabel", 
217 				addToRightLabel != null ? findValue(addToRightLabel, String.class) : "->");
218 		
219 		// addAllToLeftLabel
220 		addParameter("addAllToLeftLabel", 
221 				addAllToLeftLabel != null ? findValue(addAllToLeftLabel, String.class) : "<<--");
222 		
223 		// addAllToRightLabel
224 		addParameter("addAllToRightLabel", 
225 				addAllToRightLabel != null ? findValue(addAllToRightLabel, String.class) : "-->>");
226 		
227 		// selectAllLabel
228 		addParameter("selectAllLabel",
229 				selectAllLabel != null ? findValue(selectAllLabel, String.class) : "<*>");
230 		
231 		// leftUpLabel
232 		addParameter("leftUpLabel", 
233 				leftUpLabel != null ? findValue(leftUpLabel, String.class) : "^");
234 		
235 		
236 		// leftDownLabel
237 		addParameter("leftDownLabel", 
238 				leftDownlabel != null ? findValue(leftDownlabel, String.class) : "v");
239 		
240 		
241 		// rightUpLabel
242 		addParameter("rightUpLabel", 
243 				rightUpLabel != null ? findValue(rightUpLabel, String.class) : "^");
244 		
245 		
246 		// rightDownlabel
247 		addParameter("rightDownLabel", 
248 				rightDownLabel != null ? findValue(rightDownLabel, String.class) : "v");
249 		
250 		
251 		
252 		// inform the form component our select tag infos, so they know how to select 
253 		// its elements upon onsubmit
254 		Form formAncestor = (Form) findAncestor(Form.class);
255 		if (formAncestor != null) {
256 			
257 			// inform ancestor form that we are having a customOnsubmit (see form-close.ftl [simple theme])
258 			enableAncestorFormCustomOnsubmit();
259 			
260 			
261 			// key -> select tag id, value -> headerKey (if exists)
262 			Map formOptiontransferselectIds = (Map) formAncestor.getParameters().get("optiontransferselectIds");
263 			Map formOptiontransferselectDoubleIds = (Map) formAncestor.getParameters().get("optiontransferselectDoubleIds");
264 
265 			// init lists
266 			if (formOptiontransferselectIds == null) {
267 				formOptiontransferselectIds = new LinkedHashMap();
268 			}
269 			if (formOptiontransferselectDoubleIds == null) {
270 				formOptiontransferselectDoubleIds = new LinkedHashMap();
271 			}
272 			
273 			
274 			// id
275 			String tmpId = (String) getParameters().get("id");
276 			String tmpHeaderKey = (String) getParameters().get("headerKey");
277 			if (tmpId != null && (! formOptiontransferselectIds.containsKey(tmpId))) {
278 				formOptiontransferselectIds.put(tmpId, tmpHeaderKey);
279 			}
280 			
281 			// doubleId
282 			String tmpDoubleId = (String) getParameters().get("doubleId");
283 			String tmpDoubleHeaderKey = (String) getParameters().get("doubleHeaderKey");
284 			if (tmpDoubleId != null && (! formOptiontransferselectDoubleIds.containsKey(tmpDoubleId))) {
285 				formOptiontransferselectDoubleIds.put(tmpDoubleId, tmpDoubleHeaderKey);
286 			}
287 			
288 			formAncestor.getParameters().put("optiontransferselectIds", formOptiontransferselectIds);
289 			formAncestor.getParameters().put("optiontransferselectDoubleIds", formOptiontransferselectDoubleIds);
290 			
291 		}
292 		else {
293 			_log.warn("form enclosing optiontransferselect "+this+" not found, auto select upon form submit of optiontransferselect will not work");
294 		}
295 	}
296 	
297 	
298 	
299 	public String getAddAllToLeftLabel() {
300 		return addAllToLeftLabel;
301 	}
302 
303 	/***
304      * set Add To Left button label
305 	 * @s.tagattribute required="false"
306 	 */
307 	public void setAddAllToLeftLabel(String addAllToLeftLabel) {
308 		this.addAllToLeftLabel = addAllToLeftLabel;
309 	}
310 
311 	public String getAddAllToRightLabel() {
312 		return addAllToRightLabel;
313 	}
314 
315 	/***
316      * set Add All To Right button label
317 	 * @s.tagattribute required="false"
318 	 */
319 	public void setAddAllToRightLabel(String addAllToRightLabel) {
320 		this.addAllToRightLabel = addAllToRightLabel;
321 	}
322 
323 	public String getAddToLeftLabel() {
324 		return addToLeftLabel;
325 	}
326 
327 	/***
328      * set Add To Left button label
329 	 * @s.tagattribute required="false"
330 	 */
331 	public void setAddToLeftLabel(String addToLeftLabel) {
332 		this.addToLeftLabel = addToLeftLabel;
333 	}
334 
335 	public String getAddToRightLabel() {
336 		return addToRightLabel;
337 	}
338 
339 	/***
340      * set Add To Right button label
341 	 * @s.tagattribute required="false"
342 	 */
343 	public void setAddToRightLabel(String addToRightLabel) {
344 		this.addToRightLabel = addToRightLabel;
345 	}
346 
347 	public String getAllowAddAllToLeft() {
348 		return allowAddAllToLeft;
349 	}
350 
351 	/***
352      * enable Add All To Left button
353 	 * @s.tagattribute required="false"
354 	 */
355 	public void setAllowAddAllToLeft(String allowAddAllToLeft) {
356 		this.allowAddAllToLeft = allowAddAllToLeft;
357 	}
358 
359 	public String getAllowAddAllToRight() {
360 		return allowAddAllToRight;
361 	}
362 
363 	/***
364      * enable Add All To Right button
365 	 * @s.tagattribute required="false"
366 	 */
367 	public void setAllowAddAllToRight(String allowAddAllToRight) {
368 		this.allowAddAllToRight = allowAddAllToRight;
369 	}
370 
371 	public String getAllowAddToLeft() {
372 		return allowAddToLeft;
373 	}
374 
375 	/***
376      * enable Add To Left button
377 	 * @s.tagattribute required="false"
378 	 */
379 	public void setAllowAddToLeft(String allowAddToLeft) {
380 		this.allowAddToLeft = allowAddToLeft;
381 	}
382 
383 	public String getAllowAddToRight() {
384 		return allowAddToRight;
385 	}
386 
387 	/***
388      * enable Add To Right button
389 	 * @s.tagattribute required="false"
390 	 */
391 	public void setAllowAddToRight(String allowAddToRight) {
392 		this.allowAddToRight = allowAddToRight;
393 	}
394 
395 	public String getLeftTitle() {
396 		return leftTitle;
397 	}
398 	
399 	
400 	/***
401 	 * enable up / down on the left side
402 	 * @a2 tagattribute required="false" 
403 	 */
404 	public void setAllowUpDownOnLeft(String allowUpDownOnLeft) {
405 		this.allowUpDownOnLeft = allowUpDownOnLeft;
406 	}
407 	
408 	public String getAllowUpDownOnLeft() {
409 		return this.allowUpDownOnLeft;
410 	}
411 	
412 	
413 	/***
414 	 * enable up / down on the right side
415 	 * @a2 tagattribute required="false"
416 	 */
417 	public void setAllowUpDownOnRight(String allowUpDownOnRight) {
418 		this.allowUpDownOnRight = allowUpDownOnRight;
419 	}
420 	
421 	public String getAllowUpDownOnRight() {
422 		return this.allowUpDownOnRight;
423 	}
424 	
425 
426 	/***
427      * set Left title
428 	 * @s.tagattribute required="false"
429 	 */
430 	public void setLeftTitle(String leftTitle) {
431 		this.leftTitle = leftTitle;
432 	}
433 
434 	public String getRightTitle() {
435 		return rightTitle;
436 	}
437 
438 	/***
439      * set Right title
440 	 * @s.tagattribute required="false"
441 	 */
442 	public void setRightTitle(String rightTitle) {
443 		this.rightTitle = rightTitle;
444 	}
445 	
446 	
447 	/***
448      * enable Select All button
449 	 * @s.tagattribute required="false"
450 	 */
451 	public void setAllowSelectAll(String allowSelectAll) {
452 		this.allowSelectAll = allowSelectAll;
453 	}
454 
455     public String getAllowSelectAll() {
456 		return this.allowSelectAll;
457 	}
458 	
459 	
460 	/***
461      * set Select All button label
462 	 * @s.tagattribute required="false"
463 	 */
464 	public void setSelectAllLabel(String selectAllLabel) {
465 		this.selectAllLabel = selectAllLabel;
466 	}
467 
468     public String getSelectAllLabel() {
469 		return this.selectAllLabel;
470 	}
471 
472 
473     /***
474      * set buttons css class
475 	 * @s.tagattribute required="false"
476 	 */
477 	public void setButtonCssClass(String buttonCssClass) {
478 		this.buttonCssClass = buttonCssClass;
479 	}
480 
481     public String getButtonCssClass() {
482 		return buttonCssClass;
483 	}
484 	
485 	
486 	/***
487      * set button css style
488 	 * @s.tagattribute required="false"
489 	 */
490 	public void setButtonCssStyle(String buttonCssStyle) {
491 		this.buttonCssStyle = buttonCssStyle;
492 	}
493 
494     public String getButtonCssStyle() {
495 		return this.buttonCssStyle;
496 	}
497     
498     
499     /***
500      * Up label for the left side
501      * @a2 tagattribute required="false"
502      */
503     public void setLeftUpLabel(String leftUpLabel) {
504     	this.leftUpLabel = leftUpLabel;
505     }
506     public String getLeftUpLabel() {
507     	return this.leftUpLabel;
508     }
509     
510     /***
511      * Down label for the left side.
512      * @a2 tagattribute required="false"
513      */
514     public void setLeftDownLabel(String leftDownLabel) {
515     	this.leftDownlabel = leftDownLabel;
516     }
517     public String getLeftDownLabel() {
518     	return this.leftDownlabel;
519     }
520     
521     /***
522      * Up label for the right side.
523      * @a2 tagattribute required="false"
524      */
525     public void setRightUpLabel(String rightUpLabel) {
526     	this.rightUpLabel = rightUpLabel;
527     }
528     public String getRightUpLabel() {
529     	return this.rightUpLabel;
530     }
531     
532     
533     /***
534      * Down label for the left side.
535      * @a2 tagattribute required="false"
536      */
537     public void setRightDownLabel(String rightDownlabel) {
538     	this.rightDownLabel = rightDownlabel;
539     }
540     public String getRightDownLabel() {
541     	return rightDownLabel;
542     }
543     
544     
545 }