KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > html > Select


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 JavaDoc;
18
19 import org.apache.commons.logging.Log;
20 import org.mortbay.log.LogFactory;
21
22 /* -------------------------------------------------------------------- */
23 /** HTML select Block.
24  * @see org.mortbay.html.Block
25  */

26 public class Select extends Block
27 {
28     private static Log log = LogFactory.getLog(Select.class);
29
30     /* ----------------------------------------------------------------- */
31     /**
32      * @param name Name of the form element
33      * @param multiple Whether multiple selections can be made
34      */

35     public Select(String JavaDoc name,boolean multiple)
36     {
37         super("select");
38         attribute("name",name);
39         
40         if (multiple)
41             attribute("multiple");
42     }
43
44     /* ----------------------------------------------------------------- */
45     /**
46      * @param name Name of the form element
47      * @param multiple Whether multiple selections can be made
48      */

49     public Select(String JavaDoc name,boolean multiple, String JavaDoc[] options)
50     {
51         this(name,multiple);
52         
53         for (int i=0; i<options.length; i++)
54             add(options[i]);
55     }
56
57     /* ----------------------------------------------------------------- */
58     /** Set the number of options to display at once */
59     public Select setSize(int size)
60     {
61         size(size);
62         return this;
63     }
64
65     /* ----------------------------------------------------------------- */
66     public Select add(Enumeration JavaDoc e)
67     {
68         while (e.hasMoreElements())
69             add(e.nextElement().toString());
70         return this;
71     }
72
73     /* ----------------------------------------------------------------- */
74     /** Add option and specify if selected.
75      */

76     public Composite add(Object JavaDoc o)
77     {
78         if (o instanceof Enumeration JavaDoc)
79             this.add((Enumeration JavaDoc)o);
80         else
81         {
82             super.add("<option>");
83             super.add(o);
84         }
85         return this;
86     }
87
88     /* ----------------------------------------------------------------- */
89     /** Add option and specify if selected.
90      */

91     public Select add(Object JavaDoc o, boolean selected)
92     {
93         if (selected)
94             super.add("<option selected>");
95         else
96             super.add("<option>");
97         super.add(o);
98         return this;
99     }
100
101     /* ----------------------------------------------------------------- */
102     /** Add an option.
103      * @param o The name of the option (displayed in the form)
104      * @param selected Whether the option is selected
105      * @param value The value of this option (returned in the form content)
106      */

107     public Select add(Object JavaDoc o, boolean selected, String JavaDoc value)
108     {
109         if (selected)
110             super.add("<option selected value=\""+value+"\">");
111         else
112             super.add("<option value=\""+value+"\">");
113         
114         super.add(o);
115         
116         return this;
117     }
118     
119     /* ----------------------------------------------------------------- */
120     /** Build a select from the given array of Strings. The values of the
121       * select are the indexes into the array of the strings, which are used
122       * as the labels on the selector.
123       * @param arr The array of strings for labels
124       * @param selected The index of the selected label, -1 for default
125       */

126     public Select add(String JavaDoc arr[], int selected)
127     {
128         for (int i = 0; i < arr.length; i++){
129             this.add(arr[i], i == selected, Integer.toString(i));
130         }
131         return this;
132     }
133     
134     /* ----------------------------------------------------------------- */
135     /** Build a select from the given array of Strings. The values of the
136       * select are the indexes into the array of the strings, which are used
137       * as the labels on the selector.
138       * @param arr The array of strings for labels
139       * @param selected The index of the selected label, -1 for default
140       */

141     public Select add(String JavaDoc arr[], String JavaDoc selected)
142     {
143         for (int i = 0; i < arr.length; i++){
144             this.add(arr[i], arr[i].equals(selected));
145         }
146         return this;
147     }
148
149     /* ----------------------------------------------------------------- */
150     /** Utility function for multi-selectors.
151      * <p> This function takes the result returned by a multi-select input
152      * and produces an integer bit-set result of the selections made. It
153      * assumes the values of the multi-select are all different powers of 2.
154      */

155     public static int bitsetFormResult(String JavaDoc result)
156     {
157         int i;
158         int from = 0;
159         int res = 0;
160         if(log.isDebugEnabled())log.debug("Result:"+result);
161         String JavaDoc sres = null;
162         while ((i = result.indexOf(' ', from)) != -1){
163             sres = result.substring(from, i);
164             res = res | Integer.parseInt(sres);
165             if(log.isDebugEnabled())log.debug("Match:"+sres+"+ res="+res);
166             from = i+1;
167         }
168         sres = result.substring(from);
169         res = res | Integer.parseInt(sres);
170         if(log.isDebugEnabled())log.debug("Match:"+sres+", res="+res);
171         return res;
172     }
173 }
174
175
176
177
178
Popular Tags