KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > lazydom > html > HTMLOptionElementImpl


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.xml.lazydom.html;
58 import org.enhydra.xml.lazydom.LazyElement;
59 import org.enhydra.xml.lazydom.LazyElementNoNS;
60 import org.w3c.dom.Node JavaDoc;
61 import org.w3c.dom.NodeList JavaDoc;
62 import org.w3c.dom.Text JavaDoc;
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.2 $ $Date: 2005/01/26 08:29:24 $
70  * @author <a HREF="mailto:arkin@openxml.org">Assaf Arkin</a>
71  * @see org.w3c.dom.html.HTMLOptionElement
72  * @see LazyElementNoNS
73  */

74 public class HTMLOptionElementImpl
75     extends LazyHTMLElement
76     implements HTMLOptionElement
77 {
78
79     
80
81     public boolean getDefaultSelected()
82     {
83         // ! NOT FULLY IMPLEMENTED !
84
return getBinary( "default-selected" );
85     }
86     
87     
88     public void setDefaultSelected( boolean defaultSelected )
89     {
90         // ! NOT FULLY IMPLEMENTED !
91
setAttribute( "default-selected", defaultSelected );
92     }
93
94   
95     public String JavaDoc getText()
96     {
97         Node JavaDoc child;
98         String JavaDoc text;
99         
100         // Find the Text nodes contained within this element and return their
101
// concatenated value. Required to go around comments, entities, etc.
102
child = getFirstChild();
103         text = "";
104         while ( child != null )
105         {
106             if ( child instanceof Text JavaDoc )
107                 text = text + ( (Text JavaDoc) child ).getData();
108             child = child.getNextSibling();
109         }
110         return text;
111     }
112     
113     
114     public void setText( String JavaDoc text )
115     {
116         Node JavaDoc child;
117         Node JavaDoc next;
118         
119         // Delete all the nodes and replace them with a single Text node.
120
// This is the only approach that can handle comments and other nodes.
121
child = getFirstChild();
122         while ( child != null )
123         {
124             next = child.getNextSibling();
125             removeChild( child );
126             child = next;
127         }
128         insertBefore( getOwnerDocument().createTextNode( text ), getFirstChild() );
129     }
130     
131     
132     public int getIndex()
133     {
134         Node JavaDoc parent;
135         NodeList JavaDoc options;
136         int i;
137         
138         // Locate the parent SELECT. Note that this OPTION might be inside a
139
// OPTGROUP inside the SELECT. Or it might not have a parent SELECT.
140
// Everything is possible. If no parent is found, return -1.
141
parent = getParentNode();
142         while ( parent != null && ! ( parent instanceof HTMLSelectElement ) )
143             parent = parent.getParentNode();
144         if ( parent != null )
145         {
146             // Use getElementsByTagName() which creates a snapshot of all the
147
// OPTION elements under the SELECT. Access to the returned NodeList
148
// is very fast and the snapshot solves many synchronization problems.
149
options = ( (HTMLElement) parent ).getElementsByTagName( "OPTION" );
150             for ( i = 0 ; i < options.getLength() ; ++i )
151                 if ( options.item( i ) == this )
152                     return i;
153         }
154         return -1;
155     }
156     
157     
158     public void setIndex( int index )
159     {
160         Node JavaDoc parent;
161         NodeList JavaDoc options;
162         Node JavaDoc item;
163         
164         // Locate the parent SELECT. Note that this OPTION might be inside a
165
// OPTGROUP inside the SELECT. Or it might not have a parent SELECT.
166
// Everything is possible. If no parent is found, just return.
167
parent = getParentNode();
168         while ( parent != null && ! ( parent instanceof HTMLSelectElement ) )
169             parent = parent.getParentNode();
170         if ( parent != null )
171         {
172             // Use getElementsByTagName() which creates a snapshot of all the
173
// OPTION elements under the SELECT. Access to the returned NodeList
174
// is very fast and the snapshot solves many synchronization problems.
175
// Make sure this OPTION is not replacing itself.
176
options = ( (HTMLElement) parent ).getElementsByTagName( "OPTION" );
177             if ( options.item( index ) != this )
178             {
179                 // Remove this OPTION from its parent. Place this OPTION right
180
// before indexed OPTION underneath it's direct parent (might
181
// be an OPTGROUP).
182
getParentNode().removeChild( this );
183                 item = options.item( index );
184                 item.getParentNode().insertBefore( this, item );
185             }
186         }
187     }
188   
189   
190     public boolean getDisabled()
191     {
192         return getBinary( "disabled" );
193     }
194     
195     
196     public void setDisabled( boolean disabled )
197     {
198         setAttribute( "disabled", disabled );
199     }
200
201     
202       public String JavaDoc getLabel()
203     {
204         return capitalize( getAttribute( "label" ) );
205     }
206     
207     
208     public void setLabel( String JavaDoc label )
209     {
210         setAttribute( "label", label );
211     }
212
213     
214     public boolean getSelected()
215     {
216         return getBinary( "selected" );
217     }
218   
219   
220     public void setSelected( boolean selected )
221     {
222         setAttribute( "selected", selected );
223     }
224     
225         
226     public String JavaDoc getValue()
227     {
228         return getAttribute( "value" );
229     }
230     
231     
232     public void setValue( String JavaDoc value )
233     {
234         setAttribute( "value", value );
235     }
236
237     
238     /**
239      * Constructor requires owner document.
240      *
241      * @param owner The owner HTML document
242      */

243     public HTMLOptionElementImpl( LazyHTMLDocument owner, LazyElement template, String JavaDoc name )
244     {
245         super( owner, template, name );
246     }
247
248
249 }
250
251
Popular Tags