1 38 package com.gargoylesoftware.htmlunit.javascript; 39 40 import com.gargoylesoftware.htmlunit.Assert; 41 import com.gargoylesoftware.htmlunit.html.HtmlOption; 42 import com.gargoylesoftware.htmlunit.html.HtmlSelect; 43 import com.gargoylesoftware.htmlunit.javascript.host.Option; 44 import org.mozilla.javascript.Scriptable; 45 46 53 public class OptionsArray extends SimpleScriptable { 54 private static final long serialVersionUID = -4790255174217201235L; 55 private HtmlSelect htmlSelect_; 56 57 60 public OptionsArray() { 61 } 62 63 64 68 public final void jsConstructor() { 69 } 70 71 72 76 public void initialize( final HtmlSelect select ) { 77 Assert.notNull("select", select); 78 htmlSelect_ = select; 79 } 80 81 82 89 public Object get( final int index, final Scriptable start ) { 90 Object object = null; 91 try { 92 object = getScriptableFor( htmlSelect_.getOption( index ) ); 93 } 94 catch( final IndexOutOfBoundsException e ) { 95 object = NOT_FOUND; 96 } 97 return object; 98 } 99 100 106 public Object jsFunction_item(final int index) { 107 return get(index, null); 108 } 109 110 111 117 public void put( 118 final int index, final Scriptable start, final Object newValue) { 119 if ( newValue == null ) { 120 htmlSelect_.removeOption( index ); 122 } 123 else { 124 final Option option = (Option) newValue; 125 HtmlOption htmlOption = (HtmlOption) option.getHtmlElementOrNull(); 126 if ( htmlOption == null ) { 127 initJavaScriptObject( option ); 128 htmlOption = new HtmlOption(htmlSelect_.getPage(), null); 129 option.setDomNode( htmlOption ); 130 } 132 if ( index >= jsGet_length() ) { 133 htmlSelect_.appendOption( htmlOption ); 135 } 136 else { 137 htmlSelect_.replaceOption( index, htmlOption ); 139 } 140 } 141 } 142 143 144 149 public int jsGet_length() { 150 return htmlSelect_.getOptionSize(); 151 } 152 153 154 158 public void jsSet_length( final int newLength ) { 159 htmlSelect_.setOptionSize( newLength ); 160 } 161 } 162 | Popular Tags |