KickJava   Java API By Example, From Geeks To Geeks.

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


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
62 /**
63 <p>
64     This class creates a &lt;Option&gt; tag.
65 </p><p>
66     The Option tag now defaults to having a closing &lt;/Option&gt;
67     (as is now required). This can be
68     overridden by setNeedClosingTag(false).
69 </p><p>
70     This change means that you should construct select options element in the following manner:
71 <code>
72 <pre>
73     new org.apache.ecs.html.Select
74         .addElement(new option("value1").addElement("text1"))
75         .addElement(new option("value2").addElement("text2"))
76 </pre>
77 </code>
78 rather than
79 <code>
80 <pre>
81     new org.apache.ecs.html.Select
82         .addElement(new option("value1").addElement("text1")
83         .addElement(new option("value2").addElement("text2")))
84 </pre>
85 </code>
86 (this change should not break existing code too badly since browsers generally display
87 the output from the above correctly).
88 </p><p>
89 Alternatively, you could use the new option constructor and do something like
90 <code>
91 <pre>
92     new org.apache.ecs.html.Select
93         .addElement(new option("text1","value1","text1"))
94         .addElement(new option("text2","value2","text2"))
95 </pre>
96 </code>
97 or even
98 <code>
99 <pre>
100     new org.apache.ecs.html.Select
101         .appendOption("text1","value1","text1")
102         .appendOption("text2","value2","text2")
103 </pre>
104 </code>
105 </p><p>
106     @version $Id: Option.java,v 1.6 2003/04/27 09:22:12 rdonkin Exp $
107     @author <a HREF="mailto:snagy@servletapi.com">Stephan Nagy</a>
108     @author <a HREF="mailto:jon@clearink.com">Jon S. Stevens</a>
109 */

110 public class Option extends MultiPartElement implements Printable, FocusEvents, FormEvents, MouseEvents, KeyEvents
111
112 {
113
114     /**
115         Private initialization routine.
116     */

117     {
118         setElementType("option");
119         setNeedClosingTag(true);
120     }
121
122     /**
123         Basic constructor.
124         Use the set* methods to set the values
125         of the attributes.
126     */

127     public Option()
128     {
129     }
130
131     /**
132         Constructor sets the value attribute.
133         Use the set* methods to set the values
134         of the other attributes.
135
136         @param value sets the attribute VALUE=""
137     */

138     public Option(String JavaDoc value)
139     {
140         setValue(value);
141     }
142
143     /**
144         Constructor sets the value and label attributes.
145         Use the set* methods to set the values
146         of the other attributes.
147
148         @param label sets the attribute LABEL=""
149         @param value sets the attribute VALUE=""
150     */

151     public Option(String JavaDoc label, String JavaDoc value)
152     {
153         setLabel(label);
154         setValue(value);
155     }
156
157     /**
158         Constructor sets the value and label attributes.
159         Use the set* methods to set the values
160         of the other attributes.
161
162         @param label sets the attribute LABEL=""
163         @param value sets the attribute VALUE=""
164     */

165     public Option(String JavaDoc label, int value)
166     {
167         setLabel(label);
168         setValue(value);
169     }
170
171     /**
172         Constructor sets the value and label attributes.
173         Use the set* methods to set the values
174         of the other attributes.
175
176         @param label sets the attribute LABEL=""
177         @param value sets the attribute VALUE=""
178     */

179     public Option(String JavaDoc label, double value)
180     {
181         setLabel(label);
182         setValue(value);
183     }
184
185     /**
186         Same as Option(label,value).addElement(text).
187         Use the set* methods to set the values
188         of the other attributes.
189
190         @param label sets the attribute LABEL=""
191         @param value sets the attribute VALUE=""
192         @param text is added as an element
193     */

194     public Option(String JavaDoc label, String JavaDoc value, String JavaDoc text)
195     {
196         this(label,value);
197         addElement(text);
198     }
199
200     /**
201         Same as Option(label,value).addElement(text).
202         Use the set* methods to set the values
203         of the other attributes.
204
205         @param label sets the attribute LABEL=""
206         @param value sets the attribute VALUE=""
207         @param text is added as an element
208     */

209     public Option(String JavaDoc label, int value, String JavaDoc text)
210     {
211         this(label,value);
212         addElement(text);
213     }
214
215     /**
216         Same as Option(label,value).addElement(text).
217         Use the set* methods to set the values
218         of the other attributes.
219
220         @param label sets the attribute LABEL=""
221         @param value sets the attribute VALUE=""
222         @param text is added as an element
223     */

224     public Option(String JavaDoc label, double value, String JavaDoc text)
225     {
226         this(label,value);
227         addElement(text);
228     }
229
230     /**
231         Sets the LABEL="" attribute
232         @param label the LABEL="" attribute
233     */

234     public Option setLabel(String JavaDoc label)
235     {
236         addAttribute("label",label);
237         return this;
238     }
239
240     /**
241         Gets the LABEL attribute.
242     */

243     public String JavaDoc getLabel()
244     {
245         return getAttribute("label");
246     }
247
248     /**
249         Sets the VALUE="" attribute
250         @param value the VALUE="" attribute
251     */

252     public Option setValue(String JavaDoc value)
253     {
254         addAttribute("value",value);
255         return this;
256     }
257
258     /**
259         Sets the VALUE="" attribute
260         @param value the VALUE="" attribute
261     */

262     public Option setValue(int value)
263     {
264         addAttribute("value",Integer.toString(value));
265         return this;
266     }
267
268     /**
269         Sets the VALUE="" attribute
270         @param value the VALUE="" attribute
271     */

272     public Option setValue(double value)
273     {
274         addAttribute("value",Double.toString(value));
275         return this;
276     }
277
278     /**
279         Gets the VALUE attribute.
280     */

281     public String JavaDoc getValue()
282     {
283         return getAttribute("value");
284     }
285
286     /**
287         Sets the selected value
288         @param selected true or false
289     */

290     public Option setSelected(boolean selected)
291     {
292         if ( selected == true )
293             addAttribute("selected", NO_ATTRIBUTE_VALUE);
294         else
295             removeAttribute("selected");
296
297         return(this);
298     }
299
300     /**
301         Gets the SELECTED attribute.
302         Of course, this return true is the attribute exists and false otherwise.
303     */

304     public boolean getSelected()
305     {
306         if ( hasAttribute("selected"))
307         {
308             return true;
309         } else {
310             return false;
311         }
312     }
313
314     /**
315         Sets the disabled value
316         @param disabled true or false
317     */

318     public Option setDisabled(boolean disabled)
319     {
320         if ( disabled == true )
321             addAttribute("disabled", NO_ATTRIBUTE_VALUE);
322         else
323             removeAttribute("disabled");
324
325         return(this);
326     }
327
328     /**
329         Gets the value of the disabled attribute.
330         Of course, this return true is the attribute exists and false otherwise.
331     */

332     public boolean getDisabled()
333     {
334         if ( hasAttribute("disabled"))
335         {
336             return true;
337         } else {
338             return false;
339         }
340     }
341
342     /**
343         Adds an Element to the element.
344         @param hashcode name of element for hash table
345         @param element Adds an Element to the element.
346      */

347     public Option addElement(String JavaDoc hashcode,Element element)
348     {
349         addElementToRegistry(hashcode,element);
350         return(this);
351     }
352
353     /**
354         Adds an Element to the element.
355         @param hashcode name of element for hash table
356         @param element Adds an Element to the element.
357      */

358     public Option addElement(String JavaDoc hashcode,String JavaDoc element)
359     {
360         addElementToRegistry(hashcode,element);
361         return(this);
362     }
363
364     /**
365         Adds an Element to the element.
366         @param element Adds an Element to the element.
367      */

368     public Option addElement(Element element)
369     {
370         addElementToRegistry(element);
371         return(this);
372     }
373
374     /**
375         Adds an Element to the element.
376         @param element Adds an Element to the element.
377      */

378     public Option addElement(String JavaDoc element)
379     {
380         addElementToRegistry(element);
381         return(this);
382     }
383
384     /**
385         Creates a group of options.
386         @param Creates a group of options.
387      */

388     public Option[] addElement(String JavaDoc[] element)
389     {
390         Option[] option = new Option[element.length];
391         for(int x = 0; x < element.length; x++)
392         {
393             option[x]= new Option().addElement(element[x]);
394         }
395         return(option);
396     }
397
398     /**
399         Removes an Element from the element.
400         @param hashcode the name of the element to be removed.
401     */

402     public Option removeElement(String JavaDoc hashcode)
403     {
404         removeElementFromRegistry(hashcode);
405         return(this);
406     }
407
408     /**
409         The onfocus event occurs when an element receives focus either by the
410         pointing device or by tabbing navigation. This attribute may be used
411         with the following elements: LABEL, INPUT, SELECT, TEXTAREA, and
412         BUTTON.
413
414         @param The script
415     */

416     public void setOnFocus(String JavaDoc script)
417     {
418         addAttribute ( "onFocus", script );
419     }
420
421     /**
422         The onblur event occurs when an element loses focus either by the
423         pointing device or by tabbing navigation. It may be used with the same
424         elements as onfocus.
425
426         @param The script
427     */

428     public void setOnBlur(String JavaDoc script)
429     {
430         addAttribute ( "onBlur", script );
431     }
432
433     /**
434         The onsubmit event occurs when a form is submitted. It only applies to
435         the FORM element.
436
437         @param The script
438     */

439     public void setOnSubmit(String JavaDoc script)
440     {
441         addAttribute ( "onSubmit", script );
442     }
443
444     /**
445         The onreset event occurs when a form is reset. It only applies to the
446         FORM element.
447
448         @param The script
449     */

450     public void setOnReset(String JavaDoc script)
451     {
452         addAttribute ( "onReset", script );
453     }
454
455     /**
456         The onselect event occurs when a user selects some text in a text
457         field. This attribute may be used with the INPUT and TEXTAREA elements.
458
459         @param The script
460     */

461     public void setOnSelect(String JavaDoc script)
462     {
463         addAttribute ( "onSelect", script );
464     }
465
466     /**
467         The onchange event occurs when a control loses the input focus and its
468         value has been modified since gaining focus. This attribute applies to
469         the following elements: INPUT, SELECT, and TEXTAREA.
470
471         @param The script
472     */

473     public void setOnChange(String JavaDoc script)
474     {
475         addAttribute ( "onChange", script );
476     }
477
478     /**
479         The onclick event occurs when the pointing device button is clicked
480         over an element. This attribute may be used with most elements.
481
482         @param The script
483     */

484     public void setOnClick(String JavaDoc script)
485     {
486         addAttribute ( "onClick", script );
487     }
488     /**
489         The ondblclick event occurs when the pointing device button is double
490         clicked over an element. This attribute may be used with most elements.
491
492         @param The script
493     */

494     public void setOnDblClick(String JavaDoc script)
495     {
496         addAttribute ( "onDblClick", script );
497     }
498     /**
499         The onmousedown event occurs when the pointing device button is pressed
500         over an element. This attribute may be used with most elements.
501
502         @param The script
503     */

504     public void setOnMouseDown(String JavaDoc script)
505     {
506         addAttribute ( "onMouseDown", script );
507     }
508     /**
509         The onmouseup event occurs when the pointing device button is released
510         over an element. This attribute may be used with most elements.
511
512         @param The script
513     */

514     public void setOnMouseUp(String JavaDoc script)
515     {
516         addAttribute ( "onMouseUp", script );
517     }
518     /**
519         The onmouseover event occurs when the pointing device is moved onto an
520         element. This attribute may be used with most elements.
521
522         @param The script
523     */

524     public void setOnMouseOver(String JavaDoc script)
525     {
526         addAttribute ( "onMouseOver", script );
527     }
528     /**
529         The onmousemove event occurs when the pointing device is moved while it
530         is over an element. This attribute may be used with most elements.
531
532         @param The script
533     */

534     public void setOnMouseMove(String JavaDoc script)
535     {
536         addAttribute ( "onMouseMove", script );
537     }
538     /**
539         The onmouseout event occurs when the pointing device is moved away from
540         an element. This attribute may be used with most elements.
541
542         @param The script
543     */

544     public void setOnMouseOut(String JavaDoc script)
545     {
546         addAttribute ( "onMouseOut", script );
547     }
548
549     /**
550         The onkeypress event occurs when a key is pressed and released over an
551         element. This attribute may be used with most elements.
552
553         @param The script
554     */

555     public void setOnKeyPress(String JavaDoc script)
556     {
557         addAttribute ( "onKeyPress", script );
558     }
559
560     /**
561         The onkeydown event occurs when a key is pressed down over an element.
562         This attribute may be used with most elements.
563
564         @param The script
565     */

566     public void setOnKeyDown(String JavaDoc script)
567     {
568         addAttribute ( "onKeyDown", script );
569     }
570
571     /**
572         The onkeyup event occurs when a key is released over an element. This
573         attribute may be used with most elements.
574
575         @param The script
576     */

577     public void setOnKeyUp(String JavaDoc script)
578     {
579         addAttribute ( "onKeyUp", script );
580     }
581 }
582
Popular Tags