View Javadoc

    1   package org.apache.turbine.util.template;
    2
    3
    4   /*
    5    * Licensed to the Apache Software Foundation (ASF) under one
    6    * or more contributor license agreements.  See the NOTICE file
    7    * distributed with this work for additional information
    8    * regarding copyright ownership.  The ASF licenses this file
    9    * to you under the Apache License, Version 2.0 (the
    10   * "License"); you may not use this file except in compliance
    11   * with the License.  You may obtain a copy of the License at
    12   *
    13   *   http://www.apache.org/licenses/LICENSE-2.0
    14   *
    15   * Unless required by applicable law or agreed to in writing,
    16   * software distributed under the License is distributed on an
    17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    18   * KIND, either express or implied.  See the License for the
    19   * specific language governing permissions and limitations
    20   * under the License.
    21   */
    22
    23
    24  import org.apache.ecs.html.Option;
    25  import org.apache.ecs.html.Select;
    26
    27  /**
    28   * This class is for generating a SelectorBox. It is good when used
    29   * with WM because you can stuff it into the context and then just
    30   * call it to generate the HTML.  It can be used in other cases as
    31   * well, but WM is the best case for it right now.
    32   *
    33   * <p>For example code showing the usage for this module, please see
    34   * the toString() method below to see how it would be refered to from
    35   * WM.
    36   *
    37   * <pre>
    38   * // get the roles for a user
    39   * RoleSet userRoles = new DefaultAccessControl().getRoles(loginid, null);
    40   * if ( userRoles != null )
    41   * {
    42   *     context.put("hasRoleSet", Boolean.TRUE);
    43   *
    44   *     // get an array of the users roles
    45   *     Role[] usersRoles = userRoles.getRolesArray();
    46   *     // get an array of all the roles in the system
    47   *     Role[] allRoles = ((RoleSet)RolePeer.retrieveSet()).getRolesArray();
    48   *
    49   *     Object[] names = new Object[allRoles.length];
    50   *     Object[] values = new Object[allRoles.length];
    51   *     for ( int i=0;i&lt;allRoles.length; i++ )
    52   *     {
    53   *         names[i] = new Integer(allRoles[i].getPrimaryKey()).toString();
    54   *         values[i] = allRoles[i].getName();
    55   *     }
    56   *
    57   *     SelectorBox sb = new SelectorBox("roleSetBox", names, values);
    58   *     sb.buildBooleans(usersRoles, allRoles);
    59   *     context.put("roleSetBox", sb);
    60   * }
    61   * else
    62   * {
    63   *     context.put("hasRoleSet", Boolean.FALSE);
    64   * }
    65   * </pre>
    66   *
    67   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
    68   * @version $Id: SelectorBox.java 615328 2008-01-25 20:25:05Z tv $
    69   */
    70  public class SelectorBox
    71  {
    72      /** This is the Select ECS element. */
    73      private Select sel = null;
    74
    75      /** This is the size of the Select statement. */
    76      private int size = 1;
    77
    78      /** This is the name= value. */
    79      private String name = null;
    80
    81      /** This is the value= portion of the option element. */
    82      private Object[] names = null;
    83
    84      /** This is the data after the option element. */
    85      private Object[] values = null;
    86
    87      /** This is an array of which items are selected. */
    88      private boolean[] selected = null;
    89
    90      /**
    91       * Generic constructor, builds a select box with a default size of
    92       * 1 and no selected items.
    93       *
    94       * @param name A String with the name for the select box.
    95       * @param names An Object[] with the names.
    96       * @param values An Object[] with the values.
    97       */
    98      public SelectorBox(String name, Object[] names, Object[] values)
    99      {
    100         this(name, names, values, 1, null);
    101     }
    102
    103     /**
    104      * Generic constructor builds a select box.
    105      *
    106      * @param name A String with the name for the select box.
    107      * @param names An Object[] with the names.
    108      * @param values An Object[] with the values.
    109      * @param size An int specifying the size.
    110      */
    111     public SelectorBox(String name, Object[] names, Object[] values, int size)
    112     {
    113         this(name, names, values, size, null);
    114     }
    115
    116     /**
    117      * Generic constructor builds a select box.
    118      *
    119      * @param name A String with the name for the select box.
    120      * @param names An Object[] with the names.
    121      * @param values An Object[] with the values.
    122      * @param selected A boolean[] with the selected items.
    123      */
    124     public SelectorBox(String name, Object[] names, Object[] values,
    125                        boolean[] selected)
    126     {
    127         this(name, names, values, 1, selected);
    128     }
    129
    130     /**
    131      * Primary constructor for everything.
    132      *
    133      * @param name A String with the name for the select box.
    134      * @param names An Object[] with the names.
    135      * @param values An Object[] with the values.
    136      * @param size An int specifying the size.
    137      * @param selected A boolean[] with the selected items.
    138      */
    139     public SelectorBox(String name, Object[] names, Object[] values, int size,
    140                        boolean[] selected)
    141     {
    142         this.name = name;
    143         this.names = names;
    144         this.values = values;
    145         this.size = size;
    146         this.selected = selected;
    147
    148         sel = new Select(name, size);
    149         sel.setName(name);
    150         sel.setSize(size);
    151     }
    152
    153     /**
    154      * Pass in an array of selected items and the entire set of items
    155      * and it will determine which items in the selected set are also
    156      * in the entireset and then build a boolean[] up that is the same
    157      * size as the entireSet with markings to tell whether or not the
    158      * items are marked or not.  It uses toString().equalsIgnoreCase()
    159      * on the Object in the Object[] to determine if the items are
    160      * equal.
    161      *
    162      * @param selectedSet An Object[].
    163      * @param entireSet An Object[].
    164      */
    165     public void buildBooleans(Object[] selectedSet, Object[] entireSet)
    166     {
    167         selected = new boolean[entireSet.length];
    168         for (int j = 0; j < entireSet.length; j++)
    169         {
    170             Object r2 = entireSet[j];
    171             for (int i = 0; i < selectedSet.length; i++)
    172             {
    173                 Object r1 = selectedSet[i];
    174                 if (r1 != null && r2 != null &&
    175                         r1.toString().equalsIgnoreCase(r2.toString()))
    176                 {
    177                     selected[j] = true;
    178                 }
    179             }
    180         }
    181     }
    182
    183     /**
    184      * This builds out the select box at a certain size.  To use this
    185      * element in WM, you simply build this object in your java code,
    186      * put it into the context and then call $selectBox.toString(5).
    187      *
    188      * @param size An int with the size.
    189      * @return A String with the HTML code.
    190      */
    191     public String toString(int size)
    192     {
    193         sel.setSize(size);
    194         sel.setName(name);
    195         for (int f = 0; f < values.length; f++)
    196         {
    197             Option opt = new Option((String) values[f]);
    198             opt.addElement((String) names[f]);
    199             if (selected != null && selected[f] == true)
    200             {
    201                 opt.setSelected(true);
    202             }
    203             sel.addElement(opt);
    204         }
    205         String output = sel.toString();
    206         reset();
    207         return output;
    208     }
    209
    210     /**
    211      * Resets the internal state of the SelectorBox.
    212      */
    213     public void reset()
    214     {
    215         sel = new Select(name, size);
    216     }
    217
    218     /**
    219      * This builds out the select box at a certain size.  To use this
    220      * element in WM, you simply build this object in your java code,
    221      * put it into the context and then call $selectBox and it will
    222      * build it with the default size of 1.
    223      *
    224      * @return A String with the HTML code.
    225      */
    226     public String toString()
    227     {
    228         return this.toString(size);
    229     }
    230
    231     /**
    232      * This allows you to set the multiple attribute to the select
    233      * element.  Example usage from within WM is like this:
    234      *
    235      * <p>
    236      * $selectBox.setMultiple(true).toString(4)
    237      *
    238      * @param val True if multiple selection should be allowed.
    239      * @return A SelectorBox (self).
    240      */
    241     public SelectorBox setMultiple(boolean val)
    242     {
    243         sel.setMultiple(val);
    244         return this;
    245     }
    246
    247     /**
    248      * This allows one to set the name= attribute to the select
    249      * element.
    250      *
    251      * @param name A String with the name.
    252      * @return A SelectorBox (self).
    253      */
    254     public SelectorBox setName(String name)
    255     {
    256         this.name = name;
    257         sel.setName(name);
    258         return this;
    259     }
    260
    261     /**
    262      * This allows one to set the size of the select element.
    263      *
    264      * @param size An int with the size.
    265      * @return A SelectorBox (self).
    266      */
    267     public SelectorBox setSize(int size)
    268     {
    269         this.size = size;
    270         sel.setSize(size);
    271         return this;
    272     }
    273
    274     /**
    275      * This allows one to set an onChange attribute on the select tag
    276      *
    277      * @param script A string with the script to put in onChange
    278      * @return A SelectorBox (self).
    279      */
    280     public SelectorBox setOnChange(String script)
    281     {
    282         sel.setOnChange(script);
    283         return this;
    284     }
    285
    286     /**
    287      * This allows one to set the array of selected booleans.
    288      *
    289      * @param an array of booleans
    290      * @return A SelectorBox (self).
    291      */
    292     public SelectorBox setSelected(boolean[] bools)
    293     {
    294         this.selected = bools;
    295         return this;
    296     }
    297
    298     /**
    299      * This will set all elements as unselected, except for the
    300      * element(s) with the given name.
    301      *
    302      * @param name The name to appear as selected.
    303      * @return A SelectorBox (self).
    304      */
    305     public SelectorBox setSelected(Object name)
    306     {
    307         if (name != null)
    308         {
    309             selected = new boolean[names.length];
    310             for (int i = 0; i < names.length; i++)
    311             {
    312                 Object o = names[i];
    313                 if (o != null && o.toString().equalsIgnoreCase(name.toString()))
    314                 {
    315                     selected[i] = true;
    316                 }
    317             }
    318         }
    319         return this;
    320     }
    321 }