View Javadoc

1   /*
2    * $Id: OptionTransferSelect.java 497654 2007-01-19 00:21:57Z rgielen $
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 
127     public OptionTransferSelect(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
128         super(stack, request, response);
129     }
130 
131     protected String getDefaultTemplate() {
132         return TEMPLATE;
133     }
134 
135 
136     public void evaluateExtraParams() {
137         super.evaluateExtraParams();
138 
139         Object doubleValue = null;
140 
141         // override DoubleListUIBean's
142         if (doubleList != null) {
143             doubleValue = findValue(doubleList);
144             addParameter("doubleList", doubleValue);
145         }
146         if (size == null || size.trim().length() <= 0) {
147             addParameter("size", "15");
148         }
149         if (doubleSize == null || doubleSize.trim().length() <= 0) {
150             addParameter("doubleSize", "15");
151         }
152         if (multiple == null || multiple.trim().length() <= 0) {
153             addParameter("multiple", Boolean.TRUE);
154         }
155         if (doubleMultiple == null || doubleMultiple.trim().length() <= 0) {
156             addParameter("doubleMultiple", Boolean.TRUE);
157         }
158 
159 
160 
161 
162 
163         // buttonCssClass
164         if (buttonCssClass != null && buttonCssClass.trim().length() > 0) {
165             addParameter("buttonCssClass", buttonCssClass);
166         }
167 
168         // buttonCssStyle
169         if (buttonCssStyle != null && buttonCssStyle.trim().length() > 0) {
170             addParameter("buttonCssStyle", buttonCssStyle);
171         }
172 
173 
174 
175         // allowSelectAll
176         addParameter("allowSelectAll",
177                 allowSelectAll != null ? findValue(allowSelectAll, Boolean.class) : Boolean.TRUE);
178 
179         // allowAddToLeft
180         addParameter("allowAddToLeft",
181                 allowAddToLeft != null ? findValue(allowAddToLeft, Boolean.class) : Boolean.TRUE);
182 
183         // allowAddToRight
184         addParameter("allowAddToRight",
185                 allowAddToRight != null ? findValue(allowAddToRight, Boolean.class) : Boolean.TRUE);
186 
187         // allowAddAllToLeft
188         addParameter("allowAddAllToLeft",
189                 allowAddAllToLeft != null ? findValue(allowAddAllToLeft, Boolean.class) : Boolean.TRUE);
190 
191         // allowAddAllToRight
192         addParameter("allowAddAllToRight",
193                 allowAddAllToRight != null ? findValue(allowAddAllToRight, Boolean.class) : Boolean.TRUE);
194 
195         // allowUpDownOnLeft
196         addParameter("allowUpDownOnLeft",
197                 allowUpDownOnLeft != null ? findValue(allowUpDownOnLeft, Boolean.class) : Boolean.TRUE);
198 
199         // allowUpDownOnRight
200         addParameter("allowUpDownOnRight",
201                 allowUpDownOnRight != null ? findValue(allowUpDownOnRight, Boolean.class) : Boolean.TRUE);
202 
203 
204         // leftTitle
205         if (leftTitle != null) {
206             addParameter("leftTitle", findValue(leftTitle, String.class));
207         }
208 
209         // rightTitle
210         if (rightTitle != null) {
211             addParameter("rightTitle", findValue(rightTitle, String.class));
212         }
213 
214 
215         // addToLeftLabel
216         addParameter("addToLeftLabel",
217                 addToLeftLabel != null ? findValue(addToLeftLabel, String.class) : "<-" );
218 
219         // addToRightLabel
220         addParameter("addToRightLabel",
221                 addToRightLabel != null ? findValue(addToRightLabel, String.class) : "->");
222 
223         // addAllToLeftLabel
224         addParameter("addAllToLeftLabel",
225                 addAllToLeftLabel != null ? findValue(addAllToLeftLabel, String.class) : "<<--");
226 
227         // addAllToRightLabel
228         addParameter("addAllToRightLabel",
229                 addAllToRightLabel != null ? findValue(addAllToRightLabel, String.class) : "-->>");
230 
231         // selectAllLabel
232         addParameter("selectAllLabel",
233                 selectAllLabel != null ? findValue(selectAllLabel, String.class) : "<*>");
234 
235         // leftUpLabel
236         addParameter("leftUpLabel",
237                 leftUpLabel != null ? findValue(leftUpLabel, String.class) : "^");
238 
239 
240         // leftDownLabel
241         addParameter("leftDownLabel",
242                 leftDownlabel != null ? findValue(leftDownlabel, String.class) : "v");
243 
244 
245         // rightUpLabel
246         addParameter("rightUpLabel",
247                 rightUpLabel != null ? findValue(rightUpLabel, String.class) : "^");
248 
249 
250         // rightDownlabel
251         addParameter("rightDownLabel",
252                 rightDownLabel != null ? findValue(rightDownLabel, String.class) : "v");
253 
254 
255 
256         // inform the form component our select tag infos, so they know how to select
257         // its elements upon onsubmit
258         Form formAncestor = (Form) findAncestor(Form.class);
259         if (formAncestor != null) {
260 
261             // inform ancestor form that we are having a customOnsubmit (see form-close.ftl [simple theme])
262             enableAncestorFormCustomOnsubmit();
263 
264 
265             // key -> select tag id, value -> headerKey (if exists)
266             Map formOptiontransferselectIds = (Map) formAncestor.getParameters().get("optiontransferselectIds");
267             Map formOptiontransferselectDoubleIds = (Map) formAncestor.getParameters().get("optiontransferselectDoubleIds");
268 
269             // init lists
270             if (formOptiontransferselectIds == null) {
271                 formOptiontransferselectIds = new LinkedHashMap();
272             }
273             if (formOptiontransferselectDoubleIds == null) {
274                 formOptiontransferselectDoubleIds = new LinkedHashMap();
275             }
276 
277 
278             // id
279             String tmpId = (String) getParameters().get("id");
280             String tmpHeaderKey = (String) getParameters().get("headerKey");
281             if (tmpId != null && (! formOptiontransferselectIds.containsKey(tmpId))) {
282                 formOptiontransferselectIds.put(tmpId, tmpHeaderKey);
283             }
284 
285             // doubleId
286             String tmpDoubleId = (String) getParameters().get("doubleId");
287             String tmpDoubleHeaderKey = (String) getParameters().get("doubleHeaderKey");
288             if (tmpDoubleId != null && (! formOptiontransferselectDoubleIds.containsKey(tmpDoubleId))) {
289                 formOptiontransferselectDoubleIds.put(tmpDoubleId, tmpDoubleHeaderKey);
290             }
291 
292             formAncestor.getParameters().put("optiontransferselectIds", formOptiontransferselectIds);
293             formAncestor.getParameters().put("optiontransferselectDoubleIds", formOptiontransferselectDoubleIds);
294 
295         }
296         else {
297             _log.warn("form enclosing optiontransferselect "+this+" not found, auto select upon form submit of optiontransferselect will not work");
298         }
299     }
300 
301 
302 
303     public String getAddAllToLeftLabel() {
304         return addAllToLeftLabel;
305     }
306 
307     @StrutsTagAttribute(description="Set Add To Left button label")
308     public void setAddAllToLeftLabel(String addAllToLeftLabel) {
309         this.addAllToLeftLabel = addAllToLeftLabel;
310     }
311 
312     public String getAddAllToRightLabel() {
313         return addAllToRightLabel;
314     }
315 
316     @StrutsTagAttribute(description="Set Add All To Right button label")
317     public void setAddAllToRightLabel(String addAllToRightLabel) {
318         this.addAllToRightLabel = addAllToRightLabel;
319     }
320 
321     public String getAddToLeftLabel() {
322         return addToLeftLabel;
323     }
324 
325     @StrutsTagAttribute(description="Set Add To Left button label")
326     public void setAddToLeftLabel(String addToLeftLabel) {
327         this.addToLeftLabel = addToLeftLabel;
328     }
329 
330     public String getAddToRightLabel() {
331         return addToRightLabel;
332     }
333 
334     @StrutsTagAttribute(description="Set Add To Right button label")
335     public void setAddToRightLabel(String addToRightLabel) {
336         this.addToRightLabel = addToRightLabel;
337     }
338 
339     public String getAllowAddAllToLeft() {
340         return allowAddAllToLeft;
341     }
342 
343     @StrutsTagAttribute(description="Enable Add All To Left button")
344     public void setAllowAddAllToLeft(String allowAddAllToLeft) {
345         this.allowAddAllToLeft = allowAddAllToLeft;
346     }
347 
348     public String getAllowAddAllToRight() {
349         return allowAddAllToRight;
350     }
351 
352     @StrutsTagAttribute(description="Enable Add All To Right button")
353     public void setAllowAddAllToRight(String allowAddAllToRight) {
354         this.allowAddAllToRight = allowAddAllToRight;
355     }
356 
357     public String getAllowAddToLeft() {
358         return allowAddToLeft;
359     }
360 
361     @StrutsTagAttribute(description="Enable Add To Left button")
362     public void setAllowAddToLeft(String allowAddToLeft) {
363         this.allowAddToLeft = allowAddToLeft;
364     }
365 
366     public String getAllowAddToRight() {
367         return allowAddToRight;
368     }
369 
370     @StrutsTagAttribute(description="Enable Add To Right button")
371     public void setAllowAddToRight(String allowAddToRight) {
372         this.allowAddToRight = allowAddToRight;
373     }
374 
375     public String getLeftTitle() {
376         return leftTitle;
377     }
378 
379     @StrutsTagAttribute(description="Enable up / down on the left side")
380     public void setAllowUpDownOnLeft(String allowUpDownOnLeft) {
381         this.allowUpDownOnLeft = allowUpDownOnLeft;
382     }
383 
384     public String getAllowUpDownOnLeft() {
385         return this.allowUpDownOnLeft;
386     }
387 
388     @StrutsTagAttribute(description="Enable up / down on the right side")
389     public void setAllowUpDownOnRight(String allowUpDownOnRight) {
390         this.allowUpDownOnRight = allowUpDownOnRight;
391     }
392 
393     public String getAllowUpDownOnRight() {
394         return this.allowUpDownOnRight;
395     }
396 
397     @StrutsTagAttribute(description="Set Left title")
398     public void setLeftTitle(String leftTitle) {
399         this.leftTitle = leftTitle;
400     }
401 
402     public String getRightTitle() {
403         return rightTitle;
404     }
405 
406     @StrutsTagAttribute(description="Set Right title")
407     public void setRightTitle(String rightTitle) {
408         this.rightTitle = rightTitle;
409     }
410 
411     @StrutsTagAttribute(description="Enable Select All button")
412     public void setAllowSelectAll(String allowSelectAll) {
413         this.allowSelectAll = allowSelectAll;
414     }
415 
416     public String getAllowSelectAll() {
417         return this.allowSelectAll;
418     }
419 
420     @StrutsTagAttribute(description="Set Select All button label")
421     public void setSelectAllLabel(String selectAllLabel) {
422         this.selectAllLabel = selectAllLabel;
423     }
424 
425     public String getSelectAllLabel() {
426         return this.selectAllLabel;
427     }
428 
429     @StrutsTagAttribute(description="Set buttons css class")
430     public void setButtonCssClass(String buttonCssClass) {
431         this.buttonCssClass = buttonCssClass;
432     }
433 
434     public String getButtonCssClass() {
435         return buttonCssClass;
436     }
437 
438     @StrutsTagAttribute(description="Set button css style")
439     public void setButtonCssStyle(String buttonCssStyle) {
440         this.buttonCssStyle = buttonCssStyle;
441     }
442 
443     public String getButtonCssStyle() {
444         return this.buttonCssStyle;
445     }
446 
447     @StrutsTagAttribute(description="Up label for the left side")
448     public void setLeftUpLabel(String leftUpLabel) {
449         this.leftUpLabel = leftUpLabel;
450     }
451     public String getLeftUpLabel() {
452         return this.leftUpLabel;
453     }
454 
455     @StrutsTagAttribute(description="Down label for the left side.")
456     public void setLeftDownLabel(String leftDownLabel) {
457         this.leftDownlabel = leftDownLabel;
458     }
459     public String getLeftDownLabel() {
460         return this.leftDownlabel;
461     }
462 
463     @StrutsTagAttribute(description="Up label for the right side.")
464     public void setRightUpLabel(String rightUpLabel) {
465         this.rightUpLabel = rightUpLabel;
466     }
467     public String getRightUpLabel() {
468         return this.rightUpLabel;
469     }
470 
471     @StrutsTagAttribute(description="Down label for the left side.")
472     public void setRightDownLabel(String rightDownlabel) {
473         this.rightDownLabel = rightDownlabel;
474     }
475     public String getRightDownLabel() {
476         return rightDownLabel;
477     }
478 
479 
480 }