1 16 package org.apache.html.dom; 17 18 import org.w3c.dom.Node ; 19 import org.w3c.dom.NodeList ; 20 import org.w3c.dom.Text ; 21 import org.w3c.dom.html.HTMLElement; 22 import org.w3c.dom.html.HTMLOptionElement; 23 import org.w3c.dom.html.HTMLSelectElement; 24 25 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 return getBinary( "default-selected" ); 43 } 44 45 46 public void setDefaultSelected( boolean defaultSelected ) 47 { 48 setAttribute( "default-selected", defaultSelected ); 50 } 51 52 53 public String getText() 54 { 55 Node child; 56 StringBuffer text = new StringBuffer (); 57 58 child = getFirstChild(); 61 while ( child != null ) 62 { 63 if ( child instanceof Text ) { 64 text.append(( (Text ) child ).getData()); 65 } 66 child = child.getNextSibling(); 67 } 68 return text.toString(); 69 } 70 71 72 public void setText( String text ) 73 { 74 Node child; 75 Node next; 76 77 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 parent; 93 NodeList options; 94 int i; 95 96 parent = getParentNode(); 100 while ( parent != null && ! ( parent instanceof HTMLSelectElement ) ) 101 parent = parent.getParentNode(); 102 if ( parent != null ) 103 { 104 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 parent; 119 NodeList options; 120 Node item; 121 122 parent = getParentNode(); 126 while ( parent != null && ! ( parent instanceof HTMLSelectElement ) ) 127 parent = parent.getParentNode(); 128 if ( parent != null ) 129 { 130 options = ( (HTMLElement) parent ).getElementsByTagName( "OPTION" ); 135 if ( options.item( index ) != this ) 136 { 137 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 getLabel() 161 { 162 return capitalize( getAttribute( "label" ) ); 163 } 164 165 166 public void setLabel( String 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 getValue() 185 { 186 return getAttribute( "value" ); 187 } 188 189 190 public void setValue( String value ) 191 { 192 setAttribute( "value", value ); 193 } 194 195 196 201 public HTMLOptionElementImpl( HTMLDocumentImpl owner, String name ) 202 { 203 super( owner, name ); 204 } 205 206 207 } 208 209 | Popular Tags |