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
// ========================================================================
16
package org.mortbay.html;
17
import java.util.Enumeration;
21
/* -------------------------------------------------------------------- */
22
/** HTML select Block.
23
* @see org.mortbay.html.Block
25
public class Select extends Block
27
/* ----------------------------------------------------------------- */
29
* @param name Name of the form element
30
* @param multiple Whether multiple selections can be made
32
public Select(String name,boolean multiple)
35
attribute("name",name);
38
attribute("multiple");
41
/* ----------------------------------------------------------------- */
43
* @param name Name of the form element
44
* @param multiple Whether multiple selections can be made
46
public Select(String name,boolean multiple, String[] options)
50
for (int i=0; i<options.length; i++)
54
/* ----------------------------------------------------------------- */
55
/** Set the number of options to display at once */
56
public Select setSize(int size)
62
/* ----------------------------------------------------------------- */
63
public Select add(Enumeration e)
65
while (e.hasMoreElements())
66
add(e.nextElement().toString());
70
/* ----------------------------------------------------------------- */
71
/** Add option and specify if selected.
73
public Composite add(Object o)
75
if (o instanceof Enumeration)
76
this.add((Enumeration)o);
79
super.add("<option>");
85
/* ----------------------------------------------------------------- */
86
/** Add option and specify if selected.
88
public Select add(Object o, boolean selected)
91
super.add("<option selected>");
93
super.add("<option>");
98
/* ----------------------------------------------------------------- */
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)
104
public Select add(Object o, boolean selected, String value)
107
super.add("<option selected value=\""+value+"\">");
109
super.add("<option value=\""+value+"\">");
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
123
public Select add(String arr[], int selected)
125
for (int i = 0; i < arr.length; i++){
126
this.add(arr[i], i == selected, Integer.toString(i));
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
138
public Select add(String arr[], String selected)
140
for (int i = 0; i < arr.length; i++){
141
this.add(arr[i], arr[i].equals(selected));
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.
152
public static int bitsetFormResult(String result)
158
while ((i = result.indexOf(' ', from)) != -1){
159
sres = result.substring(from, i);
160
res = res | Integer.parseInt(sres);
163
sres = result.substring(from);
164
res = res | Integer.parseInt(sres);