KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ecs > html > Select


1 /*
2  * ====================================================================
3  *
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "The Jakarta Project", "Jakarta Element Construction Set",
29  * "Jakarta ECS" , and "Apache Software Foundation" must not be used
30  * to endorse or promote products derived
31  * from this software without prior written permission. For written
32  * permission, please contact apache@apache.org.
33  *
34  * 5. Products derived from this software may not be called "Apache",
35  * "Jakarta Element Construction Set" nor "Jakarta ECS" nor may "Apache"
36  * appear in their names without prior written permission of the Apache Group.
37  *
38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This software consists of voluntary contributions made by many
53  * individuals on behalf of the Apache Software Foundation. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  *
57  */

58 package org.apache.ecs.html;
59
60 import org.apache.ecs.*;
61 import java.util.Enumeration;
62
63 /**
64     <p>
65     This class creates a &lt;SELECT&gt; tag.
66     </p><p>
67     <strong>Please note</strong> that the {@link Option} element now defaults
68     to add a closing tag (as is now required by the specification).
69     </p>
70     @version $Id: Select.java,v 1.7 2003/04/27 09:03:51 rdonkin Exp $
71     @author <a HREF="mailto:snagy@servletapi.com">Stephan Nagy</a>
72     @author <a HREF="mailto:jon@clearink.com">Jon S. Stevens</a>
73 */

74 public class Select extends MultiPartElement implements
75     Printable, PageEvents, FormEvents, MouseEvents, KeyEvents, FocusEvents
76 {
77     /**
78         Private initializer
79     */

80     {
81         setElementType("select");
82     }
83     /**
84         Basic constructor.
85         Use the set* methods to set attributes.
86     */

87     public Select()
88     {
89     }
90
91     /**
92         Constructor sets the name attribute.
93         Use the set* methods to set the other attributes.
94
95         @param name set the NAME="" attribute
96     */

97     public Select(String name)
98     {
99         setName(name);
100     }
101
102     /**
103         Constructor sets the name and size attribute.
104         Use the set* methods to set the other attributes.
105
106         @param name set the NAME="" attribute
107         @param name set the SIZE="" attribute
108     */

109     public Select(String name, String size)
110     {
111         setName(name);
112         setSize(size);
113     }
114
115     /**
116         Constructor sets the name and size attribute.
117         Use the set* methods to set the other attributes.
118
119         @param name set the NAME="" attribute
120         @param name set the SIZE="" attribute
121     */

122     public Select(String name, int size)
123     {
124         setName(name);
125         setSize(size);
126     }
127
128     /**
129         Constructor sets the name attribute and adds all the elements in the array.
130         Use the set* methods to set the other attributes.
131
132         @param name set the NAME="" attribute
133         @param element provide a group of strings to be converted to options elements.
134     */

135     public Select(String name, String[] element)
136     {
137         setName(name);
138         addElement(element);
139     }
140
141     /**
142         Constructor sets the name attribute and adds all the option elements in the array.
143         Use the set* methods to set the other attributes.
144
145         @param name set the NAME="" attribute
146         @param element provide a group of strings to be converted to options elements.
147     */

148     public Select(String name, Option[] element)
149     {
150         setName(name);
151         addElement(element);
152     }
153
154     /**
155         Adds an Element to the element.
156         @param hashcode name of element for hash table
157         @param element Adds an Element to the element.
158      */

159     public Select addElement(String hashcode,Element element)
160     {
161         addElementToRegistry(hashcode,element);
162         return(this);
163     }
164
165     /**
166         Adds an Element to the element.
167         @param hashcode name of element for hash table
168         @param element Adds an Element to the element.
169      */

170     public Select addElement(String hashcode,String element)
171     {
172         addElementToRegistry(hashcode,element);
173         return(this);
174     }
175
176     /**
177         Adds an Element to the Element.
178         @param element adds and Element to the Element.
179     */

180     public Select addElement(Element element)
181     {
182         addElementToRegistry(element);
183         return(this);
184     }
185
186     /**
187         Adds a group of elements to the select element.
188         @param element adds a group of elements to the select element.
189     */

190     public Select addElement(Option[] element)
191     {
192         for(int x = 0 ; x < element.length; x++)
193         {
194             addElementToRegistry(element[x]);
195         }
196         return(this);
197     }
198
199     /**
200         Adds an Element to the Element.
201         @param element adds and Element to the Element.
202     */

203     public Select addElement(String element)
204     {
205         addElementToRegistry(element);
206         return(this);
207     }
208
209     /**
210         Creates a group of option elements and adds them to this select.
211         @param element adds a group of option elements to this select.
212     */

213     public Select addElement(String[] element)
214     {
215         Option[] option = new Option().addElement(element);
216         addElement(option);
217         return(this);
218     }
219
220     public Select selectOption(int option)
221     {
222         Enumeration enum = keys();
223         for(int x = 0; enum.hasMoreElements(); x++)
224         {
225             ConcreteElement element = (ConcreteElement)getElement((String)enum.nextElement());
226             if(x == option)
227             {
228                 ((Option)element).setSelected(true);
229                 break;
230             }
231         }
232         return this;
233     }
234
235     /**
236         <p>
237         Creates and appends an option element.
238         </p><p>
239         Same as addElement(new org.apache.ecs.html.Option(value)).
240         </p>
241         @param value creates an option with value attribute set.
242     */

243     public Select appendOption(String value)
244     {
245         return addElement(new org.apache.ecs.html.Option(value));
246     }
247
248     /**
249         <p>
250         Creates and appends an option element.
251         </p><p>
252         Same as addElement(new org.apache.ecs.html.Option(label,value)).
253         </p>
254         @param label creates an option with label attribute set.
255         @param value creates an option with value attribute set.
256     */

257     public Select appendOption(String label,String value)
258     {
259         return addElement(new org.apache.ecs.html.Option(label,value));
260     }
261
262     /**
263         <p>
264         Creates and appends an option element.
265         </p><p>
266         Same as addElement(new org.apache.ecs.html.Option(label,value)).
267         </p>
268         @param label creates an option with label attribute set.
269         @param value creates an option with value attribute set.
270     */

271     public Select appendOption(String label,int value)
272     {
273         return addElement(new org.apache.ecs.html.Option(label,value));
274     }
275
276     /**
277         <p>
278         Creates and appends an option element.
279         </p><p>
280         Same as addElement(new org.apache.ecs.html.Option(label,value)).
281         </p>
282         @param label creates an option with label attribute set.
283         @param value creates an option with value attribute set.
284     */

285     public Select appendOption(String label,double value)
286     {
287         return addElement(new org.apache.ecs.html.Option(label,value));
288     }
289
290     /**
291         <p>
292         Creates and appends an option element.
293         </p><p>
294         Same as addElement(new org.apache.ecs.html.Option(label,value,text)).
295         </p>
296         @param label creates an option with label attribute set.
297         @param value creates an option with value attribute set.
298         @param text added to the option as a text element.
299     */

300     public Select appendOption(String label,String value,String text)
301     {
302         return addElement(new org.apache.ecs.html.Option(label,value,text));
303     }
304
305     /**
306         <p>
307         Creates and appends an option element.
308         </p><p>
309         Same as addElement(new org.apache.ecs.html.Option(label,value,text)).
310         </p>
311         @param label creates an option with label attribute set.
312         @param value creates an option with value attribute set.
313         @param text added to the option as a text element.
314     */

315     public Select appendOption(String label,int value,String text)
316     {
317         return addElement(new org.apache.ecs.html.Option(label,value,text));
318     }
319
320     /**
321         <p>
322         Creates and appends an option element.
323         </p><p>
324         Same as addElement(new org.apache.ecs.html.Option(label,value,text)).
325         </p>
326         @param label creates an option with label attribute set.
327         @param value creates an option with value attribute set.
328         @param text added to the option as a text element.
329     */

330     public Select appendOption(String label,double value,String text)
331     {
332         return addElement(new org.apache.ecs.html.Option(label,value,text));
333     }
334
335     /**
336         Sets the NAME="" attribute
337         @param name the NAME="" attribute
338     */

339     public Select setName(String name)
340     {
341         addAttribute("name",name);
342         return this;
343     }
344
345     /**
346         Sets the SIZE="" attribute
347         @param size the SIZE="" attribute
348     */

349     public Select setSize(String size)
350     {
351         addAttribute("size",size);
352         return this;
353     }
354
355     /**
356         Sets the SIZE="" attribute
357         @param size the SIZE="" attribute
358     */

359     public Select setSize(int size)
360     {
361         setSize(Integer.toString(size));
362         return this;
363     }
364
365     /**
366         Sets the multiple value
367         @param multiple true or false
368     */

369     public Select setMultiple(boolean multiple)
370     {
371         if ( multiple == true )
372             addAttribute("multiple", NO_ATTRIBUTE_VALUE);
373         else
374             removeAttribute("multiple");
375
376         return(this);
377     }
378
379     /**
380         Sets the TABINDEX="" attribute
381         @param alt the TABINDEX="" attribute
382     */

383     public Select setTabindex(String index)
384     {
385         addAttribute("tabindex",index);
386         return this;
387     }
388
389     /**
390         Sets the TABINDEX="" attribute
391         @param alt the TABINDEX="" attribute
392     */

393     public Select setTabindex(int index)
394     {
395         setTabindex(Integer.toString(index));
396         return this;
397     }
398
399     /**
400         Sets the disabled value
401         @param disabled true or false
402     */

403     public Select setDisabled(boolean disabled)
404     {
405         if ( disabled == true )
406             addAttribute("disabled", NO_ATTRIBUTE_VALUE);
407         else
408             removeAttribute("disabled");
409
410         return(this);
411     }
412     /**
413         Removes an Element from the element.
414         @param hashcode the name of the element to be removed.
415     */

416     public Select removeElement(String hashcode)
417     {
418         removeElementFromRegistry(hashcode);
419         return(this);
420     }
421
422     /**
423         The onload event occurs when the user agent finishes loading a window
424         or all frames within a FRAMESET. This attribute may be used with BODY
425         and FRAMESET elements.
426
427         @param The script
428     */

429     public void setOnLoad(String script)
430     {
431         addAttribute ( "onLoad", script );
432     }
433
434     /**
435         The onunload event occurs when the user agent removes a document from a
436         window or frame. This attribute may be used with BODY and FRAMESET
437         elements.
438
439         @param The script
440     */

441     public void setOnUnload(String script)
442     {
443         addAttribute ( "onUnload", script );
444     }
445
446     /**
447         The onsubmit event occurs when a form is submitted. It only applies to
448         the FORM element.
449
450         @param The script
451     */

452     public void setOnSubmit(String script)
453     {
454         addAttribute ( "onSubmit", script );
455     }
456
457     /**
458         The onreset event occurs when a form is reset. It only applies to the
459         FORM element.
460
461         @param The script
462     */

463     public void setOnReset(String script)
464     {
465         addAttribute ( "onReset", script );
466     }
467
468     /**
469         The onselect event occurs when a user selects some text in a text
470         field. This attribute may be used with the INPUT and TEXTAREA elements.
471
472         @param The script
473     */

474     public void setOnSelect(String script)
475     {
476         addAttribute ( "onSelect", script );
477     }
478
479     /**
480         The onchange event occurs when a control loses the input focus and its
481         value has been modified since gaining focus. This attribute applies to
482         the following elements: INPUT, SELECT, and TEXTAREA.
483
484         @param The script
485     */

486     public void setOnChange(String script)
487     {
488         addAttribute ( "onChange", script );
489     }
490
491     /**
492         The onclick event occurs when the pointing device button is clicked
493         over an element. This attribute may be used with most elements.
494
495         @param The script
496     */

497     public void setOnClick(String script)
498     {
499         addAttribute ( "onClick", script );
500     }
501     /**
502         The ondblclick event occurs when the pointing device button is double
503         clicked over an element. This attribute may be used with most elements.
504
505         @param The script
506     */

507     public void setOnDblClick(String script)
508     {
509         addAttribute ( "onDblClick", script );
510     }
511     /**
512         The onmousedown event occurs when the pointing device button is pressed
513         over an element. This attribute may be used with most elements.
514
515         @param The script
516     */

517     public void setOnMouseDown(String script)
518     {
519         addAttribute ( "onMouseDown", script );
520     }
521     /**
522         The onmouseup event occurs when the pointing device button is released
523         over an element. This attribute may be used with most elements.
524
525         @param The script
526     */

527     public void setOnMouseUp(String script)
528     {
529         addAttribute ( "onMouseUp", script );
530     }
531     /**
532         The onmouseover event occurs when the pointing device is moved onto an
533         element. This attribute may be used with most elements.
534
535         @param The script
536     */

537     public void setOnMouseOver(String script)
538     {
539         addAttribute ( "onMouseOver", script );
540     }
541     /**
542         The onmousemove event occurs when the pointing device is moved while it
543         is over an element. This attribute may be used with most elements.
544
545         @param The script
546     */

547     public void setOnMouseMove(String script)
548     {
549         addAttribute ( "onMouseMove", script );
550     }
551     /**
552         The onmouseout event occurs when the pointing device is moved away from
553         an element. This attribute may be used with most elements.
554
555         @param The script
556     */

557     public void setOnMouseOut(String script)
558     {
559         addAttribute ( "onMouseOut", script );
560     }
561
562     /**
563         The onkeypress event occurs when a key is pressed and released over an
564         element. This attribute may be used with most elements.
565
566         @param The script
567     */

568     public void setOnKeyPress(String script)
569     {
570         addAttribute ( "onKeyPress", script );
571     }
572
573     /**
574         The onkeydown event occurs when a key is pressed down over an element.
575         This attribute may be used with most elements.
576
577         @param The script
578     */

579     public void setOnKeyDown(String script)
580     {
581         addAttribute ( "onKeyDown", script );
582     }
583
584     /**
585         The onkeyup event occurs when a key is released over an element. This
586         attribute may be used with most elements.
587
588         @param The script
589     */

590     public void setOnKeyUp(String script)
591     {
592         addAttribute ( "onKeyUp", script );
593     }
594
595     /**
596         The onfocus event occurs when an element receives focus either by the
597         pointing device or by tabbing navigation. This attribute may be used
598         with the following elements: LABEL, INPUT, SELECT, TEXTAREA, and
599         BUTTON.
600
601         @param The script
602     */

603     public void setOnFocus(String script)
604     {
605         addAttribute ( "onFocus", script );
606     }
607
608     /**
609         The onblur event occurs when an element loses focus either by the
610         pointing device or by tabbing navigation. It may be used with the same
611         elements as onfocus.
612
613         @param The script
614     */

615     public void setOnBlur(String script)
616     {
617         addAttribute ( "onBlur", script );
618     }
619
620 }
621
Popular Tags