KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > html > internal > 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 com.sun.org.apache.html.internal.dom;
58
59
60 import org.w3c.dom.Node JavaDoc;
61 import org.w3c.dom.NodeList JavaDoc;
62 import org.w3c.dom.html.HTMLCollection;
63 import org.w3c.dom.html.HTMLElement;
64 import org.w3c.dom.html.HTMLOptionElement;
65 import org.w3c.dom.html.HTMLSelectElement;
66
67
68 /**
69  * @version $Revision: 1.7 $ $Date: 2003/05/08 20:13:09 $
70  * @author <a HREF="mailto:arkin@exoffice.com">Assaf Arkin</a>
71  * @see org.w3c.dom.html.HTMLSelectElement
72  * @see com.sun.org.apache.xerces.internal.dom.ElementImpl
73  */

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

244     public NodeList JavaDoc getChildNodes() {
245         return getChildNodesUnoptimized();
246     }
247   
248     /**
249      * Constructor requires owner document.
250      *
251      * @param owner The owner HTML document
252      */

253     public HTMLSelectElementImpl( HTMLDocumentImpl owner, String JavaDoc name )
254     {
255         super( owner, name );
256     }
257
258
259     private HTMLCollection _options;
260   
261   
262 }
263
264
Popular Tags