View Javadoc

1   /*
2    * $Id: UpDownSelect.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 Select component with buttons to move the elements in the select component
41   * up and down. When the containing form is submited, its elements will be submitted in
42   * the order they are arranged (top to bottom).
43   *
44   * <!-- END SNIPPET: javadoc -->
45   *
46   * <p/>
47   *
48   * <pre>
49   * <!-- START SNIPPET: example -->
50   *
51   * &lt;!-- Example 1: simple example --&gt;
52   * &lt;s:updownselect
53   * list="#{'england':'England', 'america':'America', 'germany':'Germany'}"
54   * name="prioritisedFavouriteCountries"
55   * headerKey="-1"
56   * headerValue="--- Please Order Them Accordingly ---"
57   * emptyOption="true" /&gt;
58   *
59   * &lt;!-- Example 2: more complex example --&gt;
60   * &lt;s:updownselect
61   * list="defaultFavouriteCartoonCharacters"
62   * name="prioritisedFavouriteCartoonCharacters"
63   * headerKey="-1"
64   * headerValue="--- Please Order ---"
65   * emptyOption="true"
66   * allowMoveUp="true"
67   * allowMoveDown="true"
68   * allowSelectAll="true"
69   * moveUpLabel="Move Up"
70   * moveDownLabel="Move Down"
71   * selectAllLabel="Select All" /&gt;
72   *
73   * <!-- END SNIPPET: example -->
74   * </pre>
75   *
76   * @version $Date: 2008-04-27 13:41:38 +0000 (Sun, 27 Apr 2008) $ $Id: UpDownSelect.java 651946 2008-04-27 13:41:38Z apetrelli $
77   *
78   * @s.tag name="updownselect" tld-body-content="JSP" tld-tag-class="org.apache.struts2.views.jsp.ui.UpDownSelectTag"
79   * description="Render a up down select element"
80   */
81  @StrutsTag(name="updownselect", tldTagClass="org.apache.struts2.views.jsp.ui.UpDownSelectTag", 
82          description="Create a Select component with buttons to move the elements in the select component up and down")
83  public class UpDownSelect extends Select {
84  
85      private static final Logger LOG = LoggerFactory.getLogger(UpDownSelect.class);
86  
87  
88      final public static String TEMPLATE = "updownselect";
89  
90      protected String allowMoveUp;
91      protected String allowMoveDown;
92      protected String allowSelectAll;
93  
94      protected String moveUpLabel;
95      protected String moveDownLabel;
96      protected String selectAllLabel;
97  
98  
99      public String getDefaultTemplate() {
100         return TEMPLATE;
101     }
102 
103     public UpDownSelect(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
104         super(stack, request, response);
105     }
106 
107     public void evaluateParams() {
108         super.evaluateParams();
109 
110 
111         // override Select's default
112         if (size == null || size.trim().length() <= 0) {
113             addParameter("size", "5");
114         }
115         if (multiple == null || multiple.trim().length() <= 0) {
116             addParameter("multiple", Boolean.TRUE);
117         }
118 
119 
120 
121         if (allowMoveUp != null) {
122             addParameter("allowMoveUp", findValue(allowMoveUp, Boolean.class));
123         }
124         if (allowMoveDown != null) {
125             addParameter("allowMoveDown", findValue(allowMoveDown, Boolean.class));
126         }
127         if (allowSelectAll != null) {
128             addParameter("allowSelectAll", findValue(allowSelectAll, Boolean.class));
129         }
130 
131         if (moveUpLabel != null) {
132             addParameter("moveUpLabel", findString(moveUpLabel));
133         }
134         if (moveDownLabel != null) {
135             addParameter("moveDownLabel", findString(moveDownLabel));
136         }
137         if (selectAllLabel != null) {
138             addParameter("selectAllLabel", findString(selectAllLabel));
139         }
140 
141 
142         // inform our form ancestor about this UpDownSelect so the form knows how to
143         // auto select all options upon it submission
144         Form ancestorForm = (Form) findAncestor(Form.class);
145         if (ancestorForm != null) {
146 
147             // inform form ancestor that we are using a custom onsubmit
148             enableAncestorFormCustomOnsubmit();
149 
150             Map m = (Map) ancestorForm.getParameters().get("updownselectIds");
151             if (m == null) {
152                 // map with key -> id ,  value -> headerKey
153                 m = new LinkedHashMap();
154             }
155             m.put(getParameters().get("id"), getParameters().get("headerKey"));
156             ancestorForm.getParameters().put("updownselectIds", m);
157         }
158         else {
159             LOG.warn("no ancestor form found for updownselect "+this+", therefore autoselect of all elements upon form submission will not work ");
160         }
161     }
162 
163 
164     public String getAllowMoveUp() {
165         return allowMoveUp;
166     }
167 
168     @StrutsTagAttribute(description="Whether move up button should be displayed", type="Boolean", defaultValue="true")
169     public void setAllowMoveUp(String allowMoveUp) {
170         this.allowMoveUp = allowMoveUp;
171     }
172 
173 
174 
175     public String getAllowMoveDown() {
176         return allowMoveDown;
177     }
178 
179     @StrutsTagAttribute(description="Whether move down button should be displayed", type="Boolean", defaultValue="true")
180     public void setAllowMoveDown(String allowMoveDown) {
181         this.allowMoveDown = allowMoveDown;
182     }
183 
184 
185 
186     public String getAllowSelectAll() {
187         return allowSelectAll;
188     }
189 
190     @StrutsTagAttribute(description="Whether or not select all button should be displayed", type="Boolean", defaultValue="true")
191     public void setAllowSelectAll(String allowSelectAll) {
192         this.allowSelectAll = allowSelectAll;
193     }
194 
195 
196     public String getMoveUpLabel() {
197         return moveUpLabel;
198     }
199 
200     @StrutsTagAttribute(description="Text to display on the move up button", defaultValue="^")
201     public void setMoveUpLabel(String moveUpLabel) {
202         this.moveUpLabel = moveUpLabel;
203     }
204 
205 
206 
207     public String getMoveDownLabel() {
208         return moveDownLabel;
209     }
210 
211     @StrutsTagAttribute(description="Text to display on the move down button", defaultValue="v")
212     public void setMoveDownLabel(String moveDownLabel) {
213         this.moveDownLabel = moveDownLabel;
214     }
215 
216 
217 
218     public String getSelectAllLabel() {
219         return selectAllLabel;
220     }
221 
222     @StrutsTagAttribute(description="Text to display on the select all button", defaultValue="*")
223     public void setSelectAllLabel(String selectAllLabel) {
224         this.selectAllLabel = selectAllLabel;
225     }
226 }