KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > javascript > OptionsArray


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;
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 /**
47  * This is the array returned by the "options" property of Select.
48  *
49  * @version $Revision: 100 $
50  * @author David K. Taylor
51  * @author <a HREF="mailto:cse@dynabean.de">Christian Sell</a>
52  */

53 public class OptionsArray extends SimpleScriptable {
54     private static final long serialVersionUID = -4790255174217201235L;
55     private HtmlSelect htmlSelect_;
56
57     /**
58      * Create an instance. Javascript objects must have a default constructor.
59      */

60     public OptionsArray() {
61     }
62
63
64     /**
65      * Javascript constructor. This must be declared in every javascript file because
66      * the rhino engine won't walk up the hierarchy looking for constructors.
67      */

68     public final void jsConstructor() {
69     }
70
71
72     /**
73      * Initialize this object
74      * @param select The HtmlSelect that this object will retrive elements from.
75      */

76     public void initialize( final HtmlSelect select ) {
77         Assert.notNull("select", select);
78         htmlSelect_ = select;
79     }
80
81
82     /**
83      * <p>Return the object at the specified index.</p>
84      *
85      * @param index The index
86      * @param start The object that get is being called on.
87      * @return The object or NOT_FOUND
88      */

89     public Object JavaDoc get( final int index, final Scriptable start ) {
90         Object JavaDoc object = null;
91         try {
92             object = getScriptableFor( htmlSelect_.getOption( index ) );
93         }
94         catch( final IndexOutOfBoundsException JavaDoc e ) {
95             object = NOT_FOUND;
96         }
97         return object;
98     }
99
100     /**
101      * <p>Return the object at the specified index.</p>
102      *
103      * @param index The index
104      * @return The object or NOT_FOUND
105      */

106     public Object JavaDoc jsFunction_item(final int index) {
107         return get(index, null);
108     }
109
110
111     /**
112      * Set the index property
113      * @param index The index
114      * @param start The scriptable object that was originally invoked for this property
115      * @param newValue The new value
116      */

117     public void put(
118             final int index, final Scriptable start, final Object JavaDoc newValue) {
119         if ( newValue == null ) {
120             // Remove the indexed option.
121
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                 // BUG: Set the text and value.
131
}
132             if ( index >= jsGet_length() ) {
133                 // Add a new option at the end.
134
htmlSelect_.appendOption( htmlOption );
135             }
136             else {
137                 // Replace the indexed option.
138
htmlSelect_.replaceOption( index, htmlOption );
139             }
140         }
141     }
142
143
144    /**
145     * <p>Return the number of elements in this array</p>
146     *
147     * @return The number of elements in the array
148     */

149     public int jsGet_length() {
150         return htmlSelect_.getOptionSize();
151     }
152
153
154     /**
155      * Remove options by reducing the "length" property
156      * @param newLength The new length property value
157      */

158     public void jsSet_length( final int newLength ) {
159         htmlSelect_.setOptionSize( newLength );
160     }
161 }
162
Popular Tags