View Javadoc

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