KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > javascript > host > Select


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.javascript.host;
39
40 import java.util.Iterator JavaDoc;
41 import java.util.List JavaDoc;
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 /**
51  * The javascript object that represents a select
52  *
53  * @version $Revision: 100 $
54  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
55  * @author David K. Taylor
56  * @author Marc Guillemot
57  * @author Chris Erskine
58  */

59 public class Select extends FormField {
60
61     private static final long serialVersionUID = 4332789476842114628L;
62     private OptionsArray optionsArray_;
63
64     /**
65      * Create an instance.
66      */

67     public Select() {
68     }
69
70
71     /**
72      * Javascript constructor. This must be declared in every javascript file because
73      * the rhino engine won't walk up the hierarchy looking for constructors.
74      */

75     public void jsConstructor() {
76     }
77
78
79     /**
80      * Initialize the object.
81      *
82      */

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     /**
94      * Remove option at the specified index
95      * @param index The index of the item to remove
96      */

97     public void jsxFunction_remove(final int index) {
98         put(index, null, null);
99     }
100
101     /**
102      * Add a new item to the list (optionally) before the specified item
103      * @param newOptionObject The DomNode to insert
104      * @param beforeOptionObject The DomNode to insert the previous element before (null if at end)
105      */

106     public void jsxFunction_add(final Object JavaDoc newOptionObject, final Object JavaDoc 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     /**
130      * Return the type of this input.
131      * @return The type
132      */

133     public String JavaDoc jsxGet_type() {
134         final String JavaDoc type;
135         if (getHtmlSelect().isMultipleSelectEnabled()) {
136             type = "select-multiple";
137         }
138         else {
139             type = "select-one";
140         }
141         return type;
142     }
143
144
145     /**
146      * Return the value of the "options" property
147      * @return The options property
148      */

149     public OptionsArray jsxGet_options() {
150
151         if( optionsArray_ == null ) {
152             initialize();
153         }
154         return optionsArray_;
155     }
156
157
158     /**
159      * Return the value of the "selectedIndex" property
160      * @return The selectedIndex property
161      */

162     public int jsxGet_selectedIndex() {
163         final HtmlSelect htmlSelect = getHtmlSelect();
164         final List JavaDoc selectedOptions = htmlSelect.getSelectedOptions();
165         if( selectedOptions.isEmpty() ) {
166             return -1;
167         }
168         else {
169             final List JavaDoc allOptions = htmlSelect.getOptions();
170             return allOptions.indexOf(selectedOptions.get(0));
171         }
172     }
173
174
175     /**
176      * Set the value of the "selectedIndex" property
177      * @param index The new value
178      */

179     public void jsxSet_selectedIndex( final int index ) {
180         final HtmlSelect htmlSelect = getHtmlSelect();
181         
182         final Iterator JavaDoc 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 JavaDoc allOptions = htmlSelect.getOptions();
193          
194         final HtmlOption itemToSelect = (HtmlOption) allOptions.get(index);
195         htmlSelect.setSelectedAttribute(itemToSelect, true);
196     }
197     
198     /**
199      * Return the actual value of the selected Option
200      * @return The value
201      */

202     public String JavaDoc 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     /**
209      * Return the value of the "length" property
210      * @return The length property
211      */

212     public int jsxGet_length() {
213         if( optionsArray_ == null ) {
214             initialize();
215         }
216         return optionsArray_.jsGet_length();
217     }
218
219
220     /**
221      * Remove options by reducing the "length" property
222      * @param newLength The new length property value
223      */

224     public void jsxSet_length( final int newLength ) {
225         if( optionsArray_ == null ) {
226             initialize();
227         }
228         optionsArray_.jsSet_length( newLength );
229     }
230
231     /**
232      * Return the specified indexed property
233      * @param index The index of the property
234      * @param start The scriptable object that was originally queried for this property
235      * @return The property.
236      */

237     public Object JavaDoc get( final int index, final Scriptable start ) {
238         if( optionsArray_ == null ) {
239             initialize();
240         }
241         return optionsArray_.get( index, start );
242     }
243
244
245     /**
246      * Set the index property
247      * @param index The index
248      * @param start The scriptable object that was originally invoked for this property
249      * @param newValue The new value
250      */

251     public void put( final int index, final Scriptable start, final Object JavaDoc newValue ) {
252         if( optionsArray_ == null ) {
253             initialize();
254         }
255         optionsArray_.put( index, start, newValue );
256     }
257
258
259     /**
260      * Return the HTML select object.
261      * @return The HTML select object.
262      */

263     private HtmlSelect getHtmlSelect() {
264         return (HtmlSelect)getHtmlElementOrDie();
265     }
266
267     
268     /**
269      * Selects the option with the specified value
270      * @param newValue The value of the option to select
271      */

272     public void jsxSet_value(final String JavaDoc newValue) {
273         getHtmlSelect().setSelectedAttribute(newValue, true);
274     }
275 }
276
277
Popular Tags