1 38 package com.gargoylesoftware.htmlunit.javascript.host; 39 40 import java.util.Iterator ; 41 import java.util.List ; 42 43 import org.mozilla.javascript.Scriptable; 44 45 import com.gargoylesoftware.htmlunit.html.DomNode; 46 import com.gargoylesoftware.htmlunit.html.HtmlOption; 47 import com.gargoylesoftware.htmlunit.html.HtmlSelect; 48 import com.gargoylesoftware.htmlunit.javascript.OptionsArray; 49 50 59 public class Select extends FormField { 60 61 private static final long serialVersionUID = 4332789476842114628L; 62 private OptionsArray optionsArray_; 63 64 67 public Select() { 68 } 69 70 71 75 public void jsConstructor() { 76 } 77 78 79 83 public void initialize() { 84 85 final HtmlSelect htmlSelect = getHtmlSelect(); 86 htmlSelect.setScriptObject(this); 87 if( optionsArray_ == null ) { 88 optionsArray_ = (OptionsArray)makeJavaScriptObject("OptionsArray"); 89 optionsArray_.initialize( htmlSelect ); 90 } 91 } 92 93 97 public void jsxFunction_remove(final int index) { 98 put(index, null, null); 99 } 100 101 106 public void jsxFunction_add(final Object newOptionObject, final Object beforeOptionObject) { 107 108 final HtmlSelect select = getHtmlSelect(); 109 110 final Option option = (Option) newOptionObject; 111 final Option beforeOption = (Option) beforeOptionObject; 112 113 HtmlOption htmlOption = (HtmlOption) option.getHtmlElementOrNull(); 114 if ( htmlOption == null ) { 115 initJavaScriptObject( option ); 116 htmlOption = new HtmlOption(select.getPage(), null); 117 option.setDomNode( htmlOption ); 118 } 119 120 if (beforeOption == null) { 121 select.appendChild(htmlOption); 122 } 123 else { 124 final DomNode before = beforeOption.getDomNodeOrDie(); 125 before.insertBefore(htmlOption); 126 } 127 } 128 129 133 public String jsxGet_type() { 134 final String type; 135 if (getHtmlSelect().isMultipleSelectEnabled()) { 136 type = "select-multiple"; 137 } 138 else { 139 type = "select-one"; 140 } 141 return type; 142 } 143 144 145 149 public OptionsArray jsxGet_options() { 150 151 if( optionsArray_ == null ) { 152 initialize(); 153 } 154 return optionsArray_; 155 } 156 157 158 162 public int jsxGet_selectedIndex() { 163 final HtmlSelect htmlSelect = getHtmlSelect(); 164 final List selectedOptions = htmlSelect.getSelectedOptions(); 165 if( selectedOptions.isEmpty() ) { 166 return -1; 167 } 168 else { 169 final List allOptions = htmlSelect.getOptions(); 170 return allOptions.indexOf(selectedOptions.get(0)); 171 } 172 } 173 174 175 179 public void jsxSet_selectedIndex( final int index ) { 180 final HtmlSelect htmlSelect = getHtmlSelect(); 181 182 final Iterator iter = htmlSelect.getSelectedOptions().iterator(); 183 while (iter.hasNext()){ 184 final HtmlOption itemToUnSelect = (HtmlOption) iter.next(); 185 htmlSelect.setSelectedAttribute(itemToUnSelect, false); 186 } 187 if (index < 0){ 188 htmlSelect.fakeSelectedAttribute(""); 189 return; 190 } 191 192 final List allOptions = htmlSelect.getOptions(); 193 194 final HtmlOption itemToSelect = (HtmlOption) allOptions.get(index); 195 htmlSelect.setSelectedAttribute(itemToSelect, true); 196 } 197 198 202 public String jsxGet_value() { 203 final int selectedIndex = jsxGet_selectedIndex(); 204 final Option selectedOption = (Option) jsxGet_options().jsFunction_item(selectedIndex); 205 return selectedOption.jsxGet_value(); 206 } 207 208 212 public int jsxGet_length() { 213 if( optionsArray_ == null ) { 214 initialize(); 215 } 216 return optionsArray_.jsGet_length(); 217 } 218 219 220 224 public void jsxSet_length( final int newLength ) { 225 if( optionsArray_ == null ) { 226 initialize(); 227 } 228 optionsArray_.jsSet_length( newLength ); 229 } 230 231 237 public Object get( final int index, final Scriptable start ) { 238 if( optionsArray_ == null ) { 239 initialize(); 240 } 241 return optionsArray_.get( index, start ); 242 } 243 244 245 251 public void put( final int index, final Scriptable start, final Object newValue ) { 252 if( optionsArray_ == null ) { 253 initialize(); 254 } 255 optionsArray_.put( index, start, newValue ); 256 } 257 258 259 263 private HtmlSelect getHtmlSelect() { 264 return (HtmlSelect)getHtmlElementOrDie(); 265 } 266 267 268 272 public void jsxSet_value(final String newValue) { 273 getHtmlSelect().setSelectedAttribute(newValue, true); 274 } 275 } 276 277 | Popular Tags |