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. import org.w3c.dom.Node;
  59. import org.w3c.dom.NodeList;
  60. import org.w3c.dom.html.HTMLCollection;
  61. import org.w3c.dom.html.HTMLElement;
  62. import org.w3c.dom.html.HTMLOptionElement;
  63. import org.w3c.dom.html.HTMLSelectElement;
  64. /**
  65. * @version $Revision: 1.7 $ $Date: 2003/05/08 20:13:09 $
  66. * @author <a href="mailto:arkin@exoffice.com">Assaf Arkin</a>
  67. * @see org.w3c.dom.html.HTMLSelectElement
  68. * @see com.sun.org.apache.xerces.internal.dom.ElementImpl
  69. */
  70. public class HTMLSelectElementImpl
  71. extends HTMLElementImpl
  72. implements HTMLSelectElement, HTMLFormControl
  73. {
  74. public String getType()
  75. {
  76. return getAttribute( "type" );
  77. }
  78. public String getValue()
  79. {
  80. return getAttribute( "value" );
  81. }
  82. public void setValue( String value )
  83. {
  84. setAttribute( "value", value );
  85. }
  86. public int getSelectedIndex()
  87. {
  88. NodeList options;
  89. int i;
  90. // Use getElementsByTagName() which creates a snapshot of all the
  91. // OPTION elements under this SELECT. Access to the returned NodeList
  92. // is very fast and the snapshot solves many synchronization problems.
  93. // Locate the first selected OPTION and return its index. Note that
  94. // the OPTION might be under an OPTGROUP.
  95. options = getElementsByTagName( "OPTION" );
  96. for ( i = 0 ; i < options.getLength() ; ++i )
  97. if ( ( (HTMLOptionElement) options.item( i ) ).getSelected() )
  98. return i;
  99. return -1;
  100. }
  101. public void setSelectedIndex( int selectedIndex )
  102. {
  103. NodeList options;
  104. int i;
  105. // Use getElementsByTagName() which creates a snapshot of all the
  106. // OPTION elements under this SELECT. Access to the returned NodeList
  107. // is very fast and the snapshot solves many synchronization problems.
  108. // Change the select so all OPTIONs are off, except for the
  109. // selectIndex-th one.
  110. options = getElementsByTagName( "OPTION" );
  111. for ( i = 0 ; i < options.getLength() ; ++i )
  112. ( (HTMLOptionElementImpl) options.item( i ) ).setSelected( i == selectedIndex );
  113. }
  114. public HTMLCollection getOptions()
  115. {
  116. if ( _options == null )
  117. _options = new HTMLCollectionImpl( this, HTMLCollectionImpl.OPTION );
  118. return _options;
  119. }
  120. public int getLength()
  121. {
  122. return getOptions().getLength();
  123. }
  124. public boolean getDisabled()
  125. {
  126. return getBinary( "disabled" );
  127. }
  128. public void setDisabled( boolean disabled )
  129. {
  130. setAttribute( "disabled", disabled );
  131. }
  132. public boolean getMultiple()
  133. {
  134. return getBinary( "multiple" );
  135. }
  136. public void setMultiple( boolean multiple )
  137. {
  138. setAttribute( "multiple", multiple );
  139. }
  140. public String getName()
  141. {
  142. return getAttribute( "name" );
  143. }
  144. public void setName( String name )
  145. {
  146. setAttribute( "name", name );
  147. }
  148. public int getSize()
  149. {
  150. return getInteger( getAttribute( "size" ) );
  151. }
  152. public void setSize( int size )
  153. {
  154. setAttribute( "size", String.valueOf( size ) );
  155. }
  156. public int getTabIndex()
  157. {
  158. return getInteger( getAttribute( "tabindex" ) );
  159. }
  160. public void setTabIndex( int tabIndex )
  161. {
  162. setAttribute( "tabindex", String.valueOf( tabIndex ) );
  163. }
  164. public void add( HTMLElement element, HTMLElement before )
  165. {
  166. insertBefore( element, before );
  167. }
  168. public void remove( int index )
  169. {
  170. NodeList options;
  171. Node removed;
  172. // Use getElementsByTagName() which creates a snapshot of all the
  173. // OPTION elements under this SELECT. Access to the returned NodeList
  174. // is very fast and the snapshot solves many synchronization problems.
  175. // Remove the indexed OPTION from it's parent, this might be this
  176. // SELECT or an OPTGROUP.
  177. options = getElementsByTagName( "OPTION" );
  178. removed = options.item( index );
  179. if ( removed != null )
  180. removed.getParentNode().removeChild ( removed );
  181. }
  182. public void blur()
  183. {
  184. // No scripting in server-side DOM. This method is moot.
  185. }
  186. public void focus()
  187. {
  188. // No scripting in server-side DOM. This method is moot.
  189. }
  190. /*
  191. * Explicit implementation of getChildNodes() to avoid problems with
  192. * overriding the getLength() method hidden in the super class.
  193. */
  194. public NodeList getChildNodes() {
  195. return getChildNodesUnoptimized();
  196. }
  197. /**
  198. * Constructor requires owner document.
  199. *
  200. * @param owner The owner HTML document
  201. */
  202. public HTMLSelectElementImpl( HTMLDocumentImpl owner, String name )
  203. {
  204. super( owner, name );
  205. }
  206. private HTMLCollection _options;
  207. }