View Javadoc

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