KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > xml > dom > html > HTMLSelectElementImpl


1 /**
2  * org/ozone-db/xml/dom/html/HTMLSelectElementImpl.java
3  *
4  * The contents of this file are subject to the OpenXML Public
5  * License Version 1.0; you may not use this file except in compliance
6  * with the License. You may obtain a copy of the License at
7  * http://www.openxml.org/license.html
8  *
9  * THIS SOFTWARE IS DISTRIBUTED ON AN "AS IS" BASIS WITHOUT WARRANTY
10  * OF ANY KIND, EITHER EXPRESSED OR IMPLIED. THE INITIAL DEVELOPER
11  * AND ALL CONTRIBUTORS SHALL NOT BE LIABLE FOR ANY DAMAGES AS A
12  * RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
13  * DERIVATIVES. SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING
14  * RIGHTS AND LIMITATIONS UNDER THE LICENSE.
15  *
16  * The Initial Developer of this code under the License is Assaf Arkin.
17  * Portions created by Assaf Arkin are Copyright (C) 1998, 1999.
18  * All Rights Reserved.
19  */

20
21
22 package org.ozoneDB.xml.dom.html;
23
24
25 import org.ozoneDB.xml.dom.ElementImpl;
26 import org.w3c.dom.Node JavaDoc;
27 import org.w3c.dom.NodeList JavaDoc;
28 import org.w3c.dom.html.HTMLCollection;
29 import org.w3c.dom.html.HTMLElement;
30 import org.w3c.dom.html.HTMLOptionElement;
31 import org.w3c.dom.html.HTMLSelectElement;
32
33
34 /**
35  * @version $Revision: 1.2 $ $Date: 2003/11/20 23:18:42 $
36  * @author <a HREF="mailto:arkin@trendline.co.il">Assaf Arkin</a>
37  * @see org.w3c.dom.html.HTMLSelectElement
38  * @see ElementImpl
39  */

40 public final class HTMLSelectElementImpl extends HTMLElementImpl implements HTMLSelectElement, HTMLFormControl {
41
42
43     public String JavaDoc getType() {
44         return getAttribute( "type" );
45     }
46
47
48     public String JavaDoc getValue() {
49         return getAttribute( "value" );
50     }
51
52
53     public void setValue( String JavaDoc value ) {
54         setAttribute( "value", value );
55     }
56
57
58     public int getSelectedIndex() {
59         NodeList JavaDoc options;
60         int i;
61
62         // Use getElementsByTagName() which creates a snapshot of all the
63
// OPTION elements under this SELECT. Access to the returned NodeList
64
// is very fast and the snapshot solves many synchronization problems.
65
// Locate the first selected OPTION and return its index. Note that
66
// the OPTION might be under an OPTGROUP.
67
options = getElementsByTagName( "OPTION" );
68         for (i = 0; i < options.getLength(); ++i) {
69             if (((HTMLOptionElement)options.item( i )).getSelected()) {
70                 return i;
71             }
72         }
73         return -1;
74     }
75
76
77     public void setSelectedIndex( int selectedIndex ) {
78         NodeList JavaDoc options;
79         int i;
80
81         // Use getElementsByTagName() which creates a snapshot of all the
82
// OPTION elements under this SELECT. Access to the returned NodeList
83
// is very fast and the snapshot solves many synchronization problems.
84
// Change the select so all OPTIONs are off, except for the
85
// selectIndex-th one.
86
options = getElementsByTagName( "OPTION" );
87         for (i = 0; i < options.getLength(); ++i) {
88             ((HTMLOptionElementImpl)options.item( i )).setSelected( i == selectedIndex );
89         }
90     }
91
92
93     public HTMLCollection getOptions() {
94         if (_options == null) {
95             _options = new HTMLCollectionImpl( this, HTMLCollectionImpl.OPTION );
96         }
97         return _options;
98     }
99
100
101     public int getLength() {
102         return getOptions().getLength();
103     }
104
105
106     public boolean getDisabled() {
107         return getAttribute( "disabled" ) != null;
108     }
109
110
111     public void setDisabled( boolean disabled ) {
112         setAttribute( "disabled", disabled ? "" : null );
113     }
114
115
116     public boolean getMultiple() {
117         return getAttribute( "multiple" ) != null;
118     }
119
120
121     public void setMultiple( boolean multiple ) {
122         setAttribute( "multiple", multiple ? "" : null );
123     }
124
125
126     public String JavaDoc getName() {
127         return getAttribute( "name" );
128     }
129
130
131     public void setName( String JavaDoc name ) {
132         setAttribute( "name", name );
133     }
134
135
136     public int getSize() {
137         return toInteger( getAttribute( "size" ) );
138     }
139
140
141     public void setSize( int size ) {
142         setAttribute( "size", String.valueOf( size ) );
143     }
144
145
146     public int getTabIndex() {
147         return toInteger( getAttribute( "tabindex" ) );
148     }
149
150
151     public void setTabIndex( int tabIndex ) {
152         setAttribute( "tabindex", String.valueOf( tabIndex ) );
153     }
154
155
156     public void add( HTMLElement element, HTMLElement before ) {
157         insertBefore( element, before );
158     }
159
160
161     public void remove( int index ) {
162         NodeList JavaDoc options;
163         Node JavaDoc removed;
164
165         // Use getElementsByTagName() which creates a snapshot of all the
166
// OPTION elements under this SELECT. Access to the returned NodeList
167
// is very fast and the snapshot solves many synchronization problems.
168
// Remove the indexed OPTION from it's parent, this might be this
169
// SELECT or an OPTGROUP.
170
options = getElementsByTagName( "OPTION" );
171         removed = options.item( index );
172         if (removed != null) {
173             removed.getParentNode().removeChild( removed );
174         }
175     }
176
177
178     public void blur() {
179     // No scripting in server-side DOM. This method is moot.
180
}
181
182
183     public void focus() {
184     // No scripting in server-side DOM. This method is moot.
185
}
186
187
188     /**
189      * Constructor requires owner document.
190      *
191      * @param owner The owner HTML document
192      */

193     public HTMLSelectElementImpl( HTMLDocumentImpl owner, String JavaDoc name ) {
194         super( owner, "SELECT" );
195     }
196
197     private HTMLCollection _options;
198
199
200 }
201
Popular Tags