KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > html > dom > HTMLSelectElementImpl


1 /*
2  * Copyright 1999,2000,2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.html.dom;
17
18 import org.w3c.dom.Node JavaDoc;
19 import org.w3c.dom.NodeList JavaDoc;
20 import org.w3c.dom.html.HTMLCollection;
21 import org.w3c.dom.html.HTMLElement;
22 import org.w3c.dom.html.HTMLOptionElement;
23 import org.w3c.dom.html.HTMLSelectElement;
24
25 /**
26  * @xerces.internal
27  * @version $Revision: 1.11 $ $Date: 2005/04/18 01:12:37 $
28  * @author <a HREF="mailto:arkin@exoffice.com">Assaf Arkin</a>
29  * @see org.w3c.dom.html.HTMLSelectElement
30  * @see org.apache.xerces.dom.ElementImpl
31  */

32 public class HTMLSelectElementImpl
33     extends HTMLElementImpl
34     implements HTMLSelectElement, HTMLFormControl
35 {
36
37     private static final long serialVersionUID = 3256722883588665912L;
38
39     public String JavaDoc getType()
40     {
41         return getAttribute( "type" );
42     }
43
44     
45       public String JavaDoc getValue()
46     {
47         return getAttribute( "value" );
48     }
49     
50     
51     public void setValue( String JavaDoc value )
52     {
53         setAttribute( "value", value );
54     }
55
56     
57     public int getSelectedIndex()
58     {
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         return -1;
72     }
73     
74     
75     public void setSelectedIndex( int selectedIndex )
76     {
77         NodeList JavaDoc options;
78         int i;
79         
80         // Use getElementsByTagName() which creates a snapshot of all the
81
// OPTION elements under this SELECT. Access to the returned NodeList
82
// is very fast and the snapshot solves many synchronization problems.
83
// Change the select so all OPTIONs are off, except for the
84
// selectIndex-th one.
85
options = getElementsByTagName( "OPTION" );
86         for ( i = 0 ; i < options.getLength() ; ++i )
87             ( (HTMLOptionElementImpl) options.item( i ) ).setSelected( i == selectedIndex );
88     }
89
90   
91     public HTMLCollection getOptions()
92     {
93         if ( _options == null )
94             _options = new HTMLCollectionImpl( this, HTMLCollectionImpl.OPTION );
95         return _options;
96     }
97     
98
99     public int getLength()
100     {
101         return getOptions().getLength();
102     }
103     
104     
105     public boolean getDisabled()
106     {
107         return getBinary( "disabled" );
108     }
109     
110     
111     public void setDisabled( boolean disabled )
112     {
113         setAttribute( "disabled", disabled );
114     }
115
116     
117       public boolean getMultiple()
118     {
119         return getBinary( "multiple" );
120     }
121     
122     
123     public void setMultiple( boolean multiple )
124     {
125         setAttribute( "multiple", multiple );
126     }
127
128   
129       public String JavaDoc getName()
130     {
131         return getAttribute( "name" );
132     }
133     
134     
135     public void setName( String JavaDoc name )
136     {
137         setAttribute( "name", name );
138     }
139
140     
141     public int getSize()
142     {
143         return getInteger( getAttribute( "size" ) );
144     }
145     
146     
147     public void setSize( int size )
148     {
149         setAttribute( "size", String.valueOf( size ) );
150     }
151
152   
153     public int getTabIndex()
154     {
155         return getInteger( getAttribute( "tabindex" ) );
156     }
157     
158     
159     public void setTabIndex( int tabIndex )
160     {
161         setAttribute( "tabindex", String.valueOf( tabIndex ) );
162     }
163
164     
165     public void add( HTMLElement element, HTMLElement before )
166     {
167         insertBefore( element, before );
168     }
169   
170   
171     public void remove( int index )
172     {
173         NodeList JavaDoc options;
174         Node JavaDoc removed;
175         
176         // Use getElementsByTagName() which creates a snapshot of all the
177
// OPTION elements under this SELECT. Access to the returned NodeList
178
// is very fast and the snapshot solves many synchronization problems.
179
// Remove the indexed OPTION from it's parent, this might be this
180
// SELECT or an OPTGROUP.
181
options = getElementsByTagName( "OPTION" );
182         removed = options.item( index );
183         if ( removed != null )
184             removed.getParentNode().removeChild ( removed );
185     }
186
187   
188     public void blur()
189     {
190         // No scripting in server-side DOM. This method is moot.
191
}
192       
193       
194     public void focus()
195     {
196         // No scripting in server-side DOM. This method is moot.
197
}
198
199     /**
200      * Explicit implementation of getChildNodes() to avoid problems with
201      * overriding the getLength() method hidden in the super class.
202      */

203     public NodeList JavaDoc getChildNodes() {
204         return getChildNodesUnoptimized();
205     }
206     
207     /**
208      * Explicit implementation of cloneNode() to ensure that cache used
209      * for getOptions() gets cleared.
210      */

211     public Node JavaDoc cloneNode(boolean deep) {
212         HTMLSelectElementImpl clonedNode = (HTMLSelectElementImpl)super.cloneNode( deep );
213         clonedNode._options = null;
214         return clonedNode;
215     }
216   
217     /**
218      * Constructor requires owner document.
219      *
220      * @param owner The owner HTML document
221      */

222     public HTMLSelectElementImpl( HTMLDocumentImpl owner, String JavaDoc name )
223     {
224         super( owner, name );
225     }
226
227
228     private HTMLCollection _options;
229   
230   
231 }
232
233
Popular Tags