View Javadoc

1   /*
2    * $Id: InputTransferSelect.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
22  package org.apache.struts2.components;
23  
24  import java.util.LinkedHashMap;
25  import java.util.Map;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.apache.struts2.views.annotations.StrutsTag;
31  import org.apache.struts2.views.annotations.StrutsTagAttribute;
32  
33  import com.opensymphony.xwork2.util.ValueStack;
34  import com.opensymphony.xwork2.util.logging.Logger;
35  import com.opensymphony.xwork2.util.logging.LoggerFactory;
36  
37  /***
38   * <!-- START SNIPPET: javadoc -->
39   *
40   * Create a input transfer select component which is basically an text input
41   * and  &lt;select ...&gt; tag with buttons in the middle of them allowing text
42   * to be added to the transfer select. Will auto-select all its
43   * elements upon its containing form submision.
44   *
45   * <!-- END SNIPPET: javadoc -->
46   *
47   * <p/>
48   *
49   *
50   * <!-- START SNIPPET: notice -->
51   *
52   * NOTE: The id and doubleId need not be supplied as they will generated provided
53   * that the inputtransferselect tag is being used in a form tag. The generated id
54   * and doubleId will be &lt;form_id&gt;_&lt;inputtransferselect_doubleName&gt; and
55   * &lt;form_id&gt;_&lt;inputtransferselect_doubleName&gt; respectively.
56   *
57   * <!-- END SNIPPET: notice -->
58   *
59   * <p/>
60   *
61   * <pre>
62   * <!-- START SNIPPET: example -->
63   *
64   * &lt;-- minimum configuration --&gt;
65   * &lt;s:inputtransferselect
66   *      label="Favourite Cartoons Characters"
67   *      name="cartoons"
68   *      list="{'Popeye', 'He-Man', 'Spiderman'}"
69   *  /&gt;
70   *
71   * <!-- END SNIPPET: example -->
72   * </pre>
73   *
74   */
75  @StrutsTag(name="inputtransferselect", tldTagClass="org.apache.struts2.views.jsp.ui.InputTransferSelectTag", description="Renders an input form")
76  public class InputTransferSelect extends ListUIBean {
77  
78      private static final Logger LOG = LoggerFactory.getLogger(InputTransferSelect.class);
79  
80      private static final String TEMPLATE = "inputtransferselect";
81  
82      protected String size;
83      protected String multiple;
84  
85      protected String allowRemoveAll;
86      protected String allowUpDown;
87  
88      protected String leftTitle;
89      protected String rightTitle;
90  
91      protected String buttonCssClass;
92      protected String buttonCssStyle;
93  
94      protected String addLabel;
95      protected String removeLabel;
96      protected String removeAllLabel;
97      protected String upLabel;
98      protected String downLabel;
99  
100     protected String headerKey;
101     protected String headerValue;
102 
103 
104     public InputTransferSelect(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
105         super(stack, request, response);
106     }
107 
108     protected String getDefaultTemplate() {
109         return TEMPLATE;
110     }
111 
112 
113     public void evaluateExtraParams() {
114         super.evaluateExtraParams();
115 
116         if (size == null || size.trim().length() <= 0) {
117             addParameter("size", "5");
118         }
119 
120         if (multiple == null || multiple.trim().length() <= 0) {
121             addParameter("multiple", Boolean.TRUE);
122         }
123 
124         // allowUpDown
125         addParameter("allowUpDown", allowUpDown != null ? findValue(allowUpDown, Boolean.class) : Boolean.TRUE);
126 
127         // allowRemoveAll
128         addParameter("allowRemoveAll", allowRemoveAll != null ? findValue(allowRemoveAll, Boolean.class) : Boolean.TRUE);
129 
130 
131         // leftTitle
132         if (leftTitle != null) {
133             addParameter("leftTitle", findValue(leftTitle, String.class));
134         }
135 
136         // rightTitle
137         if (rightTitle != null) {
138             addParameter("rightTitle", findValue(rightTitle, String.class));
139         }
140 
141 
142         // buttonCssClass
143         if (buttonCssClass != null && buttonCssClass.trim().length() > 0) {
144             addParameter("buttonCssClass", buttonCssClass);
145         }
146 
147         // buttonCssStyle
148         if (buttonCssStyle != null && buttonCssStyle.trim().length() > 0) {
149             addParameter("buttonCssStyle", buttonCssStyle);
150         }
151 
152         // addLabel
153         addParameter("addLabel", addLabel != null ? findValue(addLabel, String.class) : "->" );
154 
155         // removeLabel
156         addParameter("removeLabel", removeLabel != null ? findValue(removeLabel, String.class) : "<-");
157 
158         // removeAllLabel
159         addParameter("removeAllLabel", removeAllLabel != null ? findValue(removeAllLabel, String.class) : "<<--");
160 
161 
162         // upLabel
163         addParameter("upLabel", upLabel != null ? findValue(upLabel, String.class) : "^");
164 
165 
166         // leftDownLabel
167         addParameter("downLabel", downLabel != null ? findValue(downLabel, String.class) : "v");
168 
169         if ((headerKey != null) && (headerValue != null)) {
170             addParameter("headerKey", findString(headerKey));
171             addParameter("headerValue", findString(headerValue));
172         }
173 
174 
175 
176         // inform the form component our select tag infos, so they know how to select
177         // its elements upon onsubmit
178         Form formAncestor = (Form) findAncestor(Form.class);
179         if (formAncestor != null) {
180 
181             // inform ancestor form that we are having a customOnsubmit (see form-close.ftl [simple theme])
182             enableAncestorFormCustomOnsubmit();
183 
184 
185             // key -> select tag id, value -> headerKey (if exists)
186             Map formInputtransferselectIds = (Map) formAncestor.getParameters().get("inputtransferselectIds");
187 
188             // init lists
189             if (formInputtransferselectIds == null) {
190                 formInputtransferselectIds = new LinkedHashMap();
191             }
192 
193             // id
194             String tmpId = (String) getParameters().get("id");
195             String tmpHeaderKey = (String) getParameters().get("headerKey");
196             if (tmpId != null && (! formInputtransferselectIds.containsKey(tmpId))) {
197                 formInputtransferselectIds.put(tmpId, tmpHeaderKey);
198             }
199 
200             formAncestor.getParameters().put("inputtransferselectIds", formInputtransferselectIds);
201 
202         }
203         else {
204             LOG.warn("form enclosing inputtransferselect "+this+" not found, auto select upon form submit of inputtransferselect will not work");
205         }
206     }
207 
208     public String getSize() {
209         return size;
210     }
211 
212     @StrutsTagAttribute(description="the size of the select box")
213     public void setSize(String size) {
214         this.size = size;
215     }
216 
217     public String getMultiple() {
218         return multiple;
219     }
220 
221     @StrutsTagAttribute(description="Determine whether or not multiple entries are shown")
222     public void setMultiple(String multiple) {
223         this.multiple = multiple;
224     }
225 
226     public String getAllowRemoveAll() {
227         return allowRemoveAll;
228     }
229 
230     @StrutsTagAttribute(description="Determine whether the remove all button will display")
231     public void setAllowRemoveAll(String allowRemoveAll) {
232         this.allowRemoveAll = allowRemoveAll;
233     }
234 
235     public String getAllowUpDown() {
236         return allowUpDown;
237     }
238 
239     @StrutsTagAttribute(description="Determine whether items in the list can be reordered")
240     public void setAllowUpDown(String allowUpDown) {
241         this.allowUpDown = allowUpDown;
242     }
243 
244     public String getLeftTitle() {
245         return leftTitle;
246     }
247 
248     @StrutsTagAttribute(description="the left hand title")
249     public void setLeftTitle(String leftTitle) {
250         this.leftTitle = leftTitle;
251     }
252 
253     public String getRightTitle() {
254         return rightTitle;
255     }
256 
257     @StrutsTagAttribute(description="the right hand title")
258     public void setRightTitle(String rightTitle) {
259         this.rightTitle = rightTitle;
260     }
261 
262     public String getButtonCssClass() {
263         return buttonCssClass;
264     }
265 
266     @StrutsTagAttribute(description="the css class used for rendering buttons")
267     public void setButtonCssClass(String buttonCssClass) {
268         this.buttonCssClass = buttonCssClass;
269     }
270 
271     public String getButtonCssStyle() {
272         return buttonCssStyle;
273     }
274 
275     @StrutsTagAttribute(description="the css style used for rendering buttons")
276     public void setButtonCssStyle(String buttonCssStyle) {
277         this.buttonCssStyle = buttonCssStyle;
278     }
279 
280     public String getAddLabel() {
281         return addLabel;
282     }
283 
284     @StrutsTagAttribute(description="the label used for the add button")
285     public void setAddLabel(String addLabel) {
286         this.addLabel = addLabel;
287     }
288 
289     public String getRemoveLabel() {
290         return removeLabel;
291     }
292 
293     @StrutsTagAttribute(description="the label used for the remove button")
294     public void setRemoveLabel(String removeLabel) {
295         this.removeLabel = removeLabel;
296     }
297 
298     public String getRemoveAllLabel() {
299         return removeAllLabel;
300     }
301 
302     @StrutsTagAttribute(description="the label used for the remove all button")
303     public void setRemoveAllLabel(String removeAllLabel) {
304         this.removeAllLabel = removeAllLabel;
305     }
306 
307     public String getUpLabel() {
308         return upLabel;
309     }
310 
311     @StrutsTagAttribute(description="the label used for the up button")
312     public void setUpLabel(String upLabel) {
313         this.upLabel = upLabel;
314     }
315 
316     public String getDownLabel() {
317         return downLabel;
318     }
319 
320     @StrutsTagAttribute(description="the label used for the down button")
321     public void setDownLabel(String downLabel) {
322         this.downLabel = downLabel;
323     }
324 
325     public String getHeaderKey() {
326         return headerKey;
327     }
328 
329     @StrutsTagAttribute(description="the header key of the select box")
330     public void setHeaderKey(String headerKey) {
331         this.headerKey = headerKey;
332     }
333 
334     public String getHeaderValue() {
335         return headerValue;
336     }
337 
338     @StrutsTagAttribute(description="the header value of the select box")
339     public void setHeaderValue(String headerValue) {
340         this.headerValue = headerValue;
341     }
342 }