View Javadoc

    1   // ========================================================================
    2   // $Id: Select.java,v 1.7 2005/08/13 00:01:23 gregwilkins Exp $
    3   // Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
    4   // ------------------------------------------------------------------------
    5   // Licensed under the Apache License, Version 2.0 (the "License");
    6   // you may not use this file except in compliance with the License.
    7   // You may obtain a copy of the License at
    8   // http://www.apache.org/licenses/LICENSE-2.0
    9   // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  // ========================================================================
    15
    16  package org.mortbay.html;
    17  import java.util.Enumeration;
    18
    19
    20
    21  /* -------------------------------------------------------------------- */
    22  /** HTML select Block.
    23   * @see  org.mortbay.html.Block
    24   */
    25  public class Select extends Block
    26  {
    27      /* ----------------------------------------------------------------- */
    28      /**
    29       * @param name Name of the form element
    30       * @param multiple Whether multiple selections can be made
    31       */
    32      public Select(String name,boolean multiple)
    33      {
    34          super("select");
    35          attribute("name",name);
    36
    37          if (multiple)
    38              attribute("multiple");
    39      }
    40
    41      /* ----------------------------------------------------------------- */
    42      /**
    43       * @param name Name of the form element
    44       * @param multiple Whether multiple selections can be made
    45       */
    46      public Select(String name,boolean multiple, String[] options)
    47      {
    48          this(name,multiple);
    49
    50          for (int i=0; i<options.length; i++)
    51              add(options[i]);
    52      }
    53
    54      /* ----------------------------------------------------------------- */
    55      /** Set the number of options to display at once */
    56      public Select setSize(int size)
    57      {
    58          size(size);
    59          return this;
    60      }
    61
    62      /* ----------------------------------------------------------------- */
    63      public Select add(Enumeration e)
    64      {
    65          while (e.hasMoreElements())
    66              add(e.nextElement().toString());
    67          return this;
    68      }
    69
    70      /* ----------------------------------------------------------------- */
    71      /** Add option and specify if selected.
    72       */
    73      public Composite add(Object o)
    74      {
    75          if (o instanceof Enumeration)
    76              this.add((Enumeration)o);
    77          else
    78          {
    79              super.add("<option>");
    80              super.add(o);
    81          }
    82          return this;
    83      }
    84
    85      /* ----------------------------------------------------------------- */
    86      /** Add option and specify if selected.
    87       */
    88      public Select add(Object o, boolean selected)
    89      {
    90          if (selected)
    91              super.add("<option selected>");
    92          else
    93              super.add("<option>");
    94          super.add(o);
    95          return this;
    96      }
    97
    98      /* ----------------------------------------------------------------- */
    99      /** Add an option.
    100      * @param o The name of the option (displayed in the form)
    101      * @param selected Whether the option is selected
    102      * @param value The value of this option (returned in the form content)
    103      */
    104     public Select add(Object o, boolean selected, String value)
    105     {
    106         if (selected)
    107             super.add("<option selected value=\""+value+"\">");
    108         else
    109             super.add("<option value=\""+value+"\">");
    110
    111         super.add(o);
    112
    113         return this;
    114     }
    115
    116     /* ----------------------------------------------------------------- */
    117     /** Build a select from the given array of Strings. The values of the
    118       * select are the indexes into the array of the strings, which are used
    119       * as the labels on the selector.
    120       * @param arr The array of strings for labels
    121       * @param selected The index of the selected label, -1 for default
    122       */
    123     public Select add(String arr[], int selected)
    124     {
    125         for (int i = 0; i < arr.length; i++){
    126             this.add(arr[i], i == selected, Integer.toString(i));
    127         }
    128         return this;
    129     }
    130
    131     /* ----------------------------------------------------------------- */
    132     /** Build a select from the given array of Strings. The values of the
    133       * select are the indexes into the array of the strings, which are used
    134       * as the labels on the selector.
    135       * @param arr The array of strings for labels
    136       * @param selected The index of the selected label, -1 for default
    137       */
    138     public Select add(String arr[], String selected)
    139     {
    140         for (int i = 0; i < arr.length; i++){
    141             this.add(arr[i], arr[i].equals(selected));
    142         }
    143         return this;
    144     }
    145
    146     /* ----------------------------------------------------------------- */
    147     /** Utility function for multi-selectors.
    148      * <p> This function takes the result returned by a multi-select input
    149      * and produces an integer bit-set result of the selections made. It
    150      * assumes the values of the multi-select are all different powers of 2.
    151      */
    152     public static int bitsetFormResult(String result)
    153     {
    154         int i;
    155         int from = 0;
    156         int res = 0;
    157         String sres = null;
    158         while ((i = result.indexOf(' ', from)) != -1){
    159             sres = result.substring(from, i);
    160             res = res | Integer.parseInt(sres);
    161             from = i+1;
    162         }
    163         sres = result.substring(from);
    164         res = res | Integer.parseInt(sres);
    165         return res;
    166     }
    167 }
    168
    169
    170
    171