KickJava   Java API By Example, From Geeks To Geeks.

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


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

57 package org.enhydra.apache.html.dom;
58
59
60 import org.enhydra.apache.xerces.dom.ElementImpl;
61 import org.w3c.dom.Node JavaDoc;
62 import org.w3c.dom.NodeList JavaDoc;
63 import org.w3c.dom.html.HTMLCollection;
64 import org.w3c.dom.html.HTMLElement;
65 import org.w3c.dom.html.HTMLOptionElement;
66 import org.w3c.dom.html.HTMLSelectElement;
67
68
69 /**
70  * @version $Revision: 1.3 $ $Date: 2005/01/26 08:28:44 $
71  * @author <a HREF="mailto:arkin@exoffice.com">Assaf Arkin</a>
72  * @see org.w3c.dom.html.HTMLSelectElement
73  * @see ElementImpl
74  */

75 public class HTMLSelectElementImpl
76     extends HTMLElementImpl
77     implements HTMLSelectElement, HTMLFormControl
78 {
79     
80     
81     public String JavaDoc getType()
82     {
83         return getAttribute( "type" );
84     }
85
86     
87       public String JavaDoc getValue()
88     {
89         return getAttribute( "value" );
90     }
91     
92     
93     public void setValue( String JavaDoc value )
94     {
95         setAttribute( "value", value );
96     }
97
98     
99     public int getSelectedIndex()
100     {
101         NodeList JavaDoc options;
102         int i;
103         
104         // Use getElementsByTagName() which creates a snapshot of all the
105
// OPTION elements under this SELECT. Access to the returned NodeList
106
// is very fast and the snapshot solves many synchronization problems.
107
// Locate the first selected OPTION and return its index. Note that
108
// the OPTION might be under an OPTGROUP.
109
options = getElementsByTagName( "OPTION" );
110         for ( i = 0 ; i < options.getLength() ; ++i )
111             if ( ( (HTMLOptionElement) options.item( i ) ).getSelected() )
112                 return i;
113         return -1;
114     }
115     
116     
117     public void setSelectedIndex( int selectedIndex )
118     {
119         NodeList JavaDoc options;
120         int i;
121         
122         // Use getElementsByTagName() which creates a snapshot of all the
123
// OPTION elements under this SELECT. Access to the returned NodeList
124
// is very fast and the snapshot solves many synchronization problems.
125
// Change the select so all OPTIONs are off, except for the
126
// selectIndex-th one.
127
options = getElementsByTagName( "OPTION" );
128         for ( i = 0 ; i < options.getLength() ; ++i )
129             ( (HTMLOptionElementImpl) options.item( i ) ).setSelected( i == selectedIndex );
130     }
131
132   
133     public HTMLCollection getOptions()
134     {
135         if ( _options == null )
136             _options = new HTMLCollectionImpl( this, HTMLCollectionImpl.OPTION );
137         return _options;
138     }
139     
140
141     public int getLength()
142     {
143         return getOptions().getLength();
144     }
145     
146     
147     public boolean getDisabled()
148     {
149         return getBinary( "disabled" );
150     }
151     
152     
153     public void setDisabled( boolean disabled )
154     {
155         setAttribute( "disabled", disabled );
156     }
157
158     
159       public boolean getMultiple()
160     {
161         return getBinary( "multiple" );
162     }
163     
164     
165     public void setMultiple( boolean multiple )
166     {
167         setAttribute( "multiple", multiple );
168     }
169
170   
171       public String JavaDoc getName()
172     {
173         return getAttribute( "name" );
174     }
175     
176     
177     public void setName( String JavaDoc name )
178     {
179         setAttribute( "name", name );
180     }
181
182     
183     public int getSize()
184     {
185         return getInteger( getAttribute( "size" ) );
186     }
187     
188     
189     public void setSize( int size )
190     {
191         setAttribute( "size", String.valueOf( size ) );
192     }
193
194   
195     public int getTabIndex()
196     {
197         return getInteger( getAttribute( "tabindex" ) );
198     }
199     
200     
201     public void setTabIndex( int tabIndex )
202     {
203         setAttribute( "tabindex", String.valueOf( tabIndex ) );
204     }
205
206     
207     public void add( HTMLElement element, HTMLElement before )
208     {
209         insertBefore( element, before );
210     }
211   
212   
213     public void remove( int index )
214     {
215         NodeList JavaDoc options;
216         Node JavaDoc removed;
217         
218         // Use getElementsByTagName() which creates a snapshot of all the
219
// OPTION elements under this SELECT. Access to the returned NodeList
220
// is very fast and the snapshot solves many synchronization problems.
221
// Remove the indexed OPTION from it's parent, this might be this
222
// SELECT or an OPTGROUP.
223
options = getElementsByTagName( "OPTION" );
224         removed = options.item( index );
225         if ( removed != null )
226             removed.getParentNode().removeChild ( removed );
227     }
228
229   
230     public void blur()
231     {
232         // No scripting in server-side DOM. This method is moot.
233
}
234       
235       
236     public void focus()
237     {
238         // No scripting in server-side DOM. This method is moot.
239
}
240
241     /**
242      * Explicit implementation of getChildNodes() to avoid problems with
243      * overriding the getLength() method hidden in the super class.
244      */

245     public NodeList JavaDoc getChildNodes() {
246         return getChildNodesUnoptimized();
247     }
248
249     /**
250      * Explicit implementation of cloneNode() to ensure that cache used
251      * for getOptions() gets cleared.
252      */

253     public Node JavaDoc cloneNode(boolean deep) {
254         HTMLSelectElementImpl clonedNode = (HTMLSelectElementImpl)super.cloneNode( deep );
255         clonedNode._options = null;
256         return clonedNode;
257     }
258
259     /**
260      * Constructor requires owner document.
261      *
262      * @param owner The owner HTML document
263      */

264     public HTMLSelectElementImpl( HTMLDocumentImpl owner, String JavaDoc name )
265     {
266         super( owner, name );
267     }
268
269
270     private HTMLCollection _options;
271   
272   
273 }
274
275
Popular Tags