1 38 package com.gargoylesoftware.htmlunit.html; 39 40 import java.util.ArrayList ; 41 import java.util.Collections ; 42 import java.util.Iterator ; 43 import java.util.List ; 44 import java.util.Map ; 45 46 import com.gargoylesoftware.htmlunit.Assert; 47 import com.gargoylesoftware.htmlunit.ElementNotFoundException; 48 import com.gargoylesoftware.htmlunit.KeyValuePair; 49 import com.gargoylesoftware.htmlunit.Page; 50 51 52 63 public class HtmlSelect extends FocusableElement implements DisabledElement, SubmittableElement { 64 65 66 public static final String TAG_NAME = "select"; 67 68 private String [] fakeSelectedValues_; 69 70 71 77 public HtmlSelect( final HtmlPage page, final Map attributes ) { 78 super( page, attributes ); 79 } 80 81 84 public String getTagName() { 85 return TAG_NAME; 86 } 87 88 98 public List getSelectedOptions() { 99 List result; 100 101 if(isMultipleSelectEnabled()) { 102 result = new ArrayList (); 103 104 final DescendantElementsIterator iterator = new DescendantElementsIterator(); 105 while(iterator.hasNext()) { 106 final HtmlElement element = iterator.nextElement(); 107 if(element instanceof HtmlOption && ((HtmlOption)element).isSelected()) { 108 result.add(element); 109 } 110 } 111 } 112 else { 113 result = new ArrayList (1); 114 115 HtmlOption firstOption = null; 116 HtmlOption lastOption = null; 117 118 final DescendantElementsIterator iterator = new DescendantElementsIterator(); 119 while(iterator.hasNext()) { 120 final HtmlElement element = iterator.nextElement(); 121 if(element instanceof HtmlOption) { 122 HtmlOption option = (HtmlOption)element; 123 if(firstOption == null) { 124 firstOption = option; } 126 if(option.isSelected()) { 127 lastOption = option; 128 } 129 } 130 } 131 if(lastOption != null) { 132 result.add(lastOption); 133 } 134 else { 135 int theSize; 136 try { 137 theSize = Integer.parseInt(getSizeAttribute()); 138 } 139 catch (final NumberFormatException e) { 140 theSize = 1; 143 } 144 if (theSize <= 1) { 145 result.add(firstOption); 146 } 147 } 148 return result; 149 } 150 151 return Collections.unmodifiableList( result ); 152 } 153 154 155 162 public List getAllOptions() { 163 return getOptions(); 164 } 165 166 171 public List getOptions() { 172 final List elementList = getHtmlElementsByTagName( "option" ); 173 return Collections.unmodifiableList( elementList ); 174 } 175 176 177 183 public HtmlOption getOption( final int index ) { 184 185 final List elementList = getHtmlElementsByTagName( "option" ); 186 return (HtmlOption) elementList.get( index ); 187 } 188 189 190 194 public int getOptionSize() { 195 196 final List elementList = getHtmlElementsByTagName( "option" ); 197 return elementList.size(); 198 } 199 200 201 206 public void setOptionSize( final int newLength ) { 207 final List elementList = getHtmlElementsByTagName( "option" ); 208 209 for(int i=elementList.size()-1; i >= newLength; i--) { 210 ((HtmlElement)elementList.get(i)).remove(); 211 } 212 } 213 214 215 219 public void removeOption( final int index ) { 220 final ChildElementsIterator iterator = new ChildElementsIterator(); 221 for(int i=0; iterator.hasNext(); i++) { 222 final HtmlElement element = iterator.nextElement(); 223 if(i == index) { 224 element.remove(); 225 return; 226 } 227 } 228 } 229 230 231 236 public void replaceOption( final int index, final HtmlOption newOption ) { 237 238 final ChildElementsIterator iterator = new ChildElementsIterator(); 239 for(int i=0; iterator.hasNext(); i++) { 240 final HtmlElement element = iterator.nextElement(); 241 if(i == index) { 242 element.replace(newOption); 243 return; 244 } 245 } 246 } 247 248 249 253 public void appendOption( final HtmlOption newOption ) { 254 appendChild( newOption ) ; 255 } 256 257 258 272 public Page setSelectedAttribute( final String optionValue, final boolean isSelected ) { 273 try { 274 return setSelectedAttribute( getOptionByValue(optionValue), isSelected ); 275 } 276 catch( final ElementNotFoundException e ) { 277 throw new IllegalArgumentException ("No option found with value: " + optionValue); 278 } 279 } 280 281 295 public Page setSelectedAttribute( final HtmlOption selectedOption, final boolean isSelected ) { 296 if( isMultipleSelectEnabled() ) { 297 setSelected( selectedOption, isSelected ); 298 } 299 else { 300 final Iterator iterator = getAllOptions().iterator(); 301 while( iterator.hasNext() ) { 302 final HtmlOption option = ( HtmlOption )iterator.next(); 303 setSelected(option, option == selectedOption && isSelected ); 304 } 305 } 306 307 return getPage().executeOnChangeHandlerIfAppropriate(this); 308 } 309 310 311 317 public void fakeSelectedAttribute( final String optionValue ) { 318 Assert.notNull( "optionValue", optionValue ); 319 fakeSelectedAttribute( new String []{optionValue} ); 320 } 321 322 323 329 public void fakeSelectedAttribute( final String optionValues[] ) { 330 Assert.notNull( "optionValues", optionValues ); 331 fakeSelectedValues_ = optionValues; 332 } 333 334 335 private void setSelected(final HtmlOption option, final boolean isSelected ) { 336 if( isSelected ) { 337 option.setAttributeValue( "selected", "selected" ); 338 } 339 else { 340 option.removeAttribute( "selected" ); 341 } 342 } 343 344 345 354 public KeyValuePair[] getSubmitKeyValuePairs() { 355 final String name = getNameAttribute(); 356 final KeyValuePair[] pairs; 357 358 if( fakeSelectedValues_ == null ) { 359 final List selectedOptions = getSelectedOptions(); 360 final int optionCount = selectedOptions.size(); 361 362 pairs = new KeyValuePair[optionCount]; 363 364 for( int i = 0; i < optionCount; i++ ) { 365 final HtmlOption option = ( HtmlOption )selectedOptions.get( i ); 366 pairs[i] = new KeyValuePair( name, option.getValue() ); 367 } 368 } 369 else { 370 final List pairsList = new ArrayList (); 371 for( int i = 0; i < fakeSelectedValues_.length; i++ ) { 372 if (fakeSelectedValues_[i].length() > 0) { 373 pairsList.add(new KeyValuePair( name, fakeSelectedValues_[i] )); 374 } 375 } 376 pairs = (KeyValuePair[]) pairsList.toArray(new KeyValuePair[pairsList.size()]); 377 } 378 return pairs; 379 } 380 381 382 385 public void reset() { 386 final Iterator iterator = getAllOptions().iterator(); 387 while( iterator.hasNext() ) { 388 final HtmlOption option = (HtmlOption)iterator.next(); 389 option.reset(); 390 } 391 } 392 393 394 399 public boolean isMultipleSelectEnabled() { 400 return getAttributeValue( "multiple" ) != ATTRIBUTE_NOT_DEFINED; 401 } 402 403 404 412 public HtmlOption getOptionByValue( final String value ) 413 throws ElementNotFoundException { 414 Assert.notNull("value", value); 415 416 return ( HtmlOption )getOneHtmlElementByAttribute( "option", "value", value ); 417 } 418 419 420 428 public String asText() { 429 430 final List options; 431 if( isMultipleSelectEnabled() ) { 432 options = getAllOptions(); 433 } 434 else { 435 options = getSelectedOptions(); 436 } 437 438 boolean isFirstTimeThrough = true; 439 final StringBuffer buffer = new StringBuffer (); 440 441 final Iterator iterator = options.iterator(); 442 while( iterator.hasNext() ) { 443 if( isFirstTimeThrough == true ) { 444 isFirstTimeThrough = false; 445 } 446 else { 447 buffer.append( "\n" ); 448 } 449 450 final HtmlOption currentOption = ( HtmlOption )iterator.next(); 451 if( currentOption != null) { 452 buffer.append( currentOption.asText() ); 453 } 454 } 455 456 return buffer.toString(); 457 } 458 459 460 468 public final String getNameAttribute() { 469 return getAttributeValue( "name" ); 470 } 471 472 473 481 public final String getSizeAttribute() { 482 return getAttributeValue( "size" ); 483 } 484 485 486 494 public final String getMultipleAttribute() { 495 return getAttributeValue( "multiple" ); 496 } 497 498 499 507 public final String getDisabledAttribute() { 508 return getAttributeValue( "disabled" ); 509 } 510 511 516 public final boolean isDisabled() { 517 return isAttributeDefined( "disabled" ); 518 } 519 520 528 public final String getTabIndexAttribute() { 529 return getAttributeValue( "tabindex" ); 530 } 531 532 533 541 public final String getOnFocusAttribute() { 542 return getAttributeValue( "onfocus" ); 543 } 544 545 546 554 public final String getOnBlurAttribute() { 555 return getAttributeValue( "onblur" ); 556 } 557 558 559 567 public final String getOnChangeAttribute() { 568 return getAttributeValue( "onchange" ); 569 } 570 } 571 | Popular Tags |