KickJava   Java API By Example, From Geeks To Geeks.

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


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.Text JavaDoc;
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.10 $ $Date: 2005/04/18 01:12:37 $
28  * @author <a HREF="mailto:arkin@openxml.org">Assaf Arkin</a>
29  * @see org.w3c.dom.html.HTMLOptionElement
30  * @see org.apache.xerces.dom.ElementImpl
31  */

32 public class HTMLOptionElementImpl
33     extends HTMLElementImpl
34     implements HTMLOptionElement
35 {
36
37     private static final long serialVersionUID = 3257285846528112436L;
38
39     public boolean getDefaultSelected()
40     {
41         // ! NOT FULLY IMPLEMENTED !
42
return getBinary( "default-selected" );
43     }
44     
45     
46     public void setDefaultSelected( boolean defaultSelected )
47     {
48         // ! NOT FULLY IMPLEMENTED !
49
setAttribute( "default-selected", defaultSelected );
50     }
51
52   
53     public String JavaDoc getText()
54     {
55         Node JavaDoc child;
56         StringBuffer JavaDoc text = new StringBuffer JavaDoc();
57         
58         // Find the Text nodes contained within this element and return their
59
// concatenated value. Required to go around comments, entities, etc.
60
child = getFirstChild();
61         while ( child != null )
62         {
63             if ( child instanceof Text JavaDoc ) {
64                 text.append(( (Text JavaDoc) child ).getData());
65             }
66             child = child.getNextSibling();
67         }
68         return text.toString();
69     }
70     
71     
72     public void setText( String JavaDoc text )
73     {
74         Node JavaDoc child;
75         Node JavaDoc next;
76         
77         // Delete all the nodes and replace them with a single Text node.
78
// This is the only approach that can handle comments and other nodes.
79
child = getFirstChild();
80         while ( child != null )
81         {
82             next = child.getNextSibling();
83             removeChild( child );
84             child = next;
85         }
86         insertBefore( getOwnerDocument().createTextNode( text ), getFirstChild() );
87     }
88     
89     
90     public int getIndex()
91     {
92         Node JavaDoc parent;
93         NodeList JavaDoc options;
94         int i;
95         
96         // Locate the parent SELECT. Note that this OPTION might be inside a
97
// OPTGROUP inside the SELECT. Or it might not have a parent SELECT.
98
// Everything is possible. If no parent is found, return -1.
99
parent = getParentNode();
100         while ( parent != null && ! ( parent instanceof HTMLSelectElement ) )
101             parent = parent.getParentNode();
102         if ( parent != null )
103         {
104             // Use getElementsByTagName() which creates a snapshot of all the
105
// OPTION elements under the SELECT. Access to the returned NodeList
106
// is very fast and the snapshot solves many synchronization problems.
107
options = ( (HTMLElement) parent ).getElementsByTagName( "OPTION" );
108             for ( i = 0 ; i < options.getLength() ; ++i )
109                 if ( options.item( i ) == this )
110                     return i;
111         }
112         return -1;
113     }
114     
115     
116     public void setIndex( int index )
117     {
118         Node JavaDoc parent;
119         NodeList JavaDoc options;
120         Node JavaDoc item;
121         
122         // Locate the parent SELECT. Note that this OPTION might be inside a
123
// OPTGROUP inside the SELECT. Or it might not have a parent SELECT.
124
// Everything is possible. If no parent is found, just return.
125
parent = getParentNode();
126         while ( parent != null && ! ( parent instanceof HTMLSelectElement ) )
127             parent = parent.getParentNode();
128         if ( parent != null )
129         {
130             // Use getElementsByTagName() which creates a snapshot of all the
131
// OPTION elements under the SELECT. Access to the returned NodeList
132
// is very fast and the snapshot solves many synchronization problems.
133
// Make sure this OPTION is not replacing itself.
134
options = ( (HTMLElement) parent ).getElementsByTagName( "OPTION" );
135             if ( options.item( index ) != this )
136             {
137                 // Remove this OPTION from its parent. Place this OPTION right
138
// before indexed OPTION underneath it's direct parent (might
139
// be an OPTGROUP).
140
getParentNode().removeChild( this );
141                 item = options.item( index );
142                 item.getParentNode().insertBefore( this, item );
143             }
144         }
145     }
146   
147   
148     public boolean getDisabled()
149     {
150         return getBinary( "disabled" );
151     }
152     
153     
154     public void setDisabled( boolean disabled )
155     {
156         setAttribute( "disabled", disabled );
157     }
158
159     
160       public String JavaDoc getLabel()
161     {
162         return capitalize( getAttribute( "label" ) );
163     }
164     
165     
166     public void setLabel( String JavaDoc label )
167     {
168         setAttribute( "label", label );
169     }
170
171     
172     public boolean getSelected()
173     {
174         return getBinary( "selected" );
175     }
176   
177   
178     public void setSelected( boolean selected )
179     {
180         setAttribute( "selected", selected );
181     }
182     
183         
184     public String JavaDoc getValue()
185     {
186         return getAttribute( "value" );
187     }
188     
189     
190     public void setValue( String JavaDoc value )
191     {
192         setAttribute( "value", value );
193     }
194
195     
196     /**
197      * Constructor requires owner document.
198      *
199      * @param owner The owner HTML document
200      */

201     public HTMLOptionElementImpl( HTMLDocumentImpl owner, String JavaDoc name )
202     {
203         super( owner, name );
204     }
205
206
207 }
208
209
Popular Tags