KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * org/ozone-db/xml/dom/html/HTMLOptionElementImpl.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.Text JavaDoc;
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.HTMLOptionElement
38  * @see ElementImpl
39  */

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

191     public HTMLOptionElementImpl( HTMLDocumentImpl owner, String JavaDoc name ) {
192         super( owner, "OPTION" );
193     }
194
195
196 }
197
Popular Tags