001: // ========================================================================
002: // $Id: Select.java,v 1.7 2005/08/13 00:01:23 gregwilkins Exp $
003: // Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
004: // ------------------------------------------------------------------------
005: // Licensed under the Apache License, Version 2.0 (the "License");
006: // you may not use this file except in compliance with the License.
007: // You may obtain a copy of the License at
008: // http://www.apache.org/licenses/LICENSE-2.0
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014: // ========================================================================
015:
016: package org.mortbay.html;
017:
018: import java.util.Enumeration;
019:
020: /* -------------------------------------------------------------------- */
021: /** HTML select Block.
022: * @see org.mortbay.html.Block
023: */
024: public class Select extends Block {
025: /* ----------------------------------------------------------------- */
026: /**
027: * @param name Name of the form element
028: * @param multiple Whether multiple selections can be made
029: */
030: public Select(String name, boolean multiple) {
031: super ("select");
032: attribute("name", name);
033:
034: if (multiple)
035: attribute("multiple");
036: }
037:
038: /* ----------------------------------------------------------------- */
039: /**
040: * @param name Name of the form element
041: * @param multiple Whether multiple selections can be made
042: */
043: public Select(String name, boolean multiple, String[] options) {
044: this (name, multiple);
045:
046: for (int i = 0; i < options.length; i++)
047: add(options[i]);
048: }
049:
050: /* ----------------------------------------------------------------- */
051: /** Set the number of options to display at once */
052: public Select setSize(int size) {
053: size(size);
054: return this ;
055: }
056:
057: /* ----------------------------------------------------------------- */
058: public Select add(Enumeration e) {
059: while (e.hasMoreElements())
060: add(e.nextElement().toString());
061: return this ;
062: }
063:
064: /* ----------------------------------------------------------------- */
065: /** Add option and specify if selected.
066: */
067: public Composite add(Object o) {
068: if (o instanceof Enumeration)
069: this .add((Enumeration) o);
070: else {
071: super .add("<option>");
072: super .add(o);
073: }
074: return this ;
075: }
076:
077: /* ----------------------------------------------------------------- */
078: /** Add option and specify if selected.
079: */
080: public Select add(Object o, boolean selected) {
081: if (selected)
082: super .add("<option selected>");
083: else
084: super .add("<option>");
085: super .add(o);
086: return this ;
087: }
088:
089: /* ----------------------------------------------------------------- */
090: /** Add an option.
091: * @param o The name of the option (displayed in the form)
092: * @param selected Whether the option is selected
093: * @param value The value of this option (returned in the form content)
094: */
095: public Select add(Object o, boolean selected, String value) {
096: if (selected)
097: super .add("<option selected value=\"" + value + "\">");
098: else
099: super .add("<option value=\"" + value + "\">");
100:
101: super .add(o);
102:
103: return this ;
104: }
105:
106: /* ----------------------------------------------------------------- */
107: /** Build a select from the given array of Strings. The values of the
108: * select are the indexes into the array of the strings, which are used
109: * as the labels on the selector.
110: * @param arr The array of strings for labels
111: * @param selected The index of the selected label, -1 for default
112: */
113: public Select add(String arr[], int selected) {
114: for (int i = 0; i < arr.length; i++) {
115: this .add(arr[i], i == selected, Integer.toString(i));
116: }
117: return this ;
118: }
119:
120: /* ----------------------------------------------------------------- */
121: /** Build a select from the given array of Strings. The values of the
122: * select are the indexes into the array of the strings, which are used
123: * as the labels on the selector.
124: * @param arr The array of strings for labels
125: * @param selected The index of the selected label, -1 for default
126: */
127: public Select add(String arr[], String selected) {
128: for (int i = 0; i < arr.length; i++) {
129: this .add(arr[i], arr[i].equals(selected));
130: }
131: return this ;
132: }
133:
134: /* ----------------------------------------------------------------- */
135: /** Utility function for multi-selectors.
136: * <p> This function takes the result returned by a multi-select input
137: * and produces an integer bit-set result of the selections made. It
138: * assumes the values of the multi-select are all different powers of 2.
139: */
140: public static int bitsetFormResult(String result) {
141: int i;
142: int from = 0;
143: int res = 0;
144: String sres = null;
145: while ((i = result.indexOf(' ', from)) != -1) {
146: sres = result.substring(from, i);
147: res = res | Integer.parseInt(sres);
148: from = i + 1;
149: }
150: sres = result.substring(from);
151: res = res | Integer.parseInt(sres);
152: return res;
153: }
154: }
|