001 
002 /*
003  *  JScripter Standard 1.0 - To Script In Java
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package js.user;
021 
022 import js.*;
023 import js.core.*;
024 
025 /**
026  * <p>An <b>opaque</b> class representing JavaScript client-side objects of the global 
027  * {@link JsClient#HTMLCollection} class.</p>
028  * <p>An object of this class is a collection of HTML elements with methods that allow 
029  * you to retrieve an element by its position in the collection or by its {@link JsHTMLElement#id} 
030  * or <tt>name</tt> attribute. In JavaScript, a {@link JsHTMLCollection} object behaves 
031  * like a read-only array, and you can use JavaScript square-bracket notation to index 
032  * the array by number or by <tt>name</tt> instead of calling the {@link JsHTMLCollection#item(Number)} 
033  * and {@link JsHTMLCollection#namedItem(String)} methods.</p>
034  * <p>A number of the properties of the {@link JsHTMLDocument} object are {@link JsHTMLCollection} 
035  * objects and provide convenient access to document elements such as forms, images, and 
036  * links. The {@link JsHTMLFormElement#elements} and {@link JsHTMLSelectElement#options} 
037  * are also objects of this class. This class provides a convenient way to traverse the 
038  * rows of a {@link JsHTMLTableElement} and the cells of a {@link JsHTMLTableRowElement}.</p>
039  * <p>Objects of this class are read-only. You cannot assign new elements to them, even 
040  * when using JavaScript array notation. They are live, meaning that if the underlying 
041  * document changes, those changes are immediately visible through all {@link JsHTMLCollection} 
042  * objects.</p>
043  * <p>This class is similar to {@link JsNodeList} but its objects may be indexed by name 
044  * as well as by number.</p>
045  *
046  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
047  * @see JsHTMLDocument
048  * @see JsHTMLFormElement
049  * @see JsHTMLSelectElement
050  * @see JsHTMLTableElement
051  * @see JsHTMLTableRowElement
052  *
053  * @javascript <b>Opaque</b> types can be resolved but no class objects for them can be
054  * generated into the target codes. Re-compilers must exit with error on the operations of
055  * accessing that kind of class objects.
056  * The <tt>checkcast</tt> operation to the class literal of this interface must be ignored
057  * and <tt>instanceof</tt> to it always <tt>true</tt>.
058  */
059 public class JsHTMLCollection<T extends JsHTMLElement> extends JsClient.HTMLCollection.Prototype<T> implements ArrayObject<T>
060 {
061     /**
062      * <p>An <b>internal</b> class containing membership data for its enclosing
063      * opaque class.</p>
064      * <p>This class is only used inside of <b>opaque</b> or <b>internal</b> classes or
065      * class members.</p>
066      *
067      * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
068      * 
069      * @javascript Re-compilers must report error on resolving an <b>internal</b> class.
070      */
071     protected static abstract class Members extends JsClient.HTMLCollection.Prototype.Members
072     {
073         /**
074          * <p>An <b>internal</b> static field defining a member ID of the field name and
075          * used internally to define a member of the same name.</p>
076          * @since 1.0
077          * @see JsHTMLCollection#length
078          * @see JsHTMLCollection.Member#length
079          * @javascript Re-compilers must report error on accessing an <b>internal</b> field.
080          */
081         protected static final Mid length = id("length");
082     }
083 
084     /**
085      * <p>An <b>opaque</b> class representing members of its enclosing <b>opaque</b> type.</p>
086      * <p>Note that, this class is <b>opaque</b> but its constructors are all <b>internal</b>. 
087      * This class and the subclasses of this class are used to declare either <b>opaque</b> 
088      * <tt>public</tt> instance fields of the opaque type {@link js.Var.Member} or the 
089      * <b>opaque</b> <tt>public</tt> static fields of other <b>opaque</b> types while their 
090      * constructors are used to define the fields inside <b>opaque</b> classes. Under 
091      * either circumstance, the field names must be exactly same as the member names, as 
092      * the <b>opaque</b> fields of <b>opaque</b> types are resolved by re-compilers directly 
093      * based on the field names.</p>
094      *
095      * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
096      * 
097      * @javascript <b>Opaque</b> types can be resolved but no class objects for them can be created
098      * in the target codes. Re-compilers must exit with error on operations accessing that kind 
099      * of class objects.
100      * Re-compilers must resolve an <b>opaque</b> instance field declared by this class in
101      * {@link js.Var.Member} or its subclasses to the JavaScript identifier: 
102      * <pre>q.m</pre>
103      * where <tt>m</tt> is the identifier of the field name and <tt>q</tt> is the identifier
104      * resolved from the instance of the enclosing member. Re-compilers must resolve an 
105      * <b>opaque</b> static field declared by this class in <b>opaque</b> types other than 
106      * {@link js.Var.Member} and its subclasses to the JavaScript identifier: 
107      * <pre>m</pre>
108      * where <tt>m</tt> is the identifier of the field name. And re-compilers must report
109      * error on the access to <b>opaque</b> fields declared by this class under any other 
110      * circumstances.
111      */
112     public final static class Member<T extends JsHTMLElement> extends JsClient.HTMLCollection.Prototype.Member<T>
113     {
114         /**
115          * <p>Internally constructs a member based on a qualifying member with a generic 
116          * wrapper.</p>
117          * <p>This constructor is <b>internal</b> and only called inside of <b>opaque</b> 
118          * or <b>internal</b> classes or class members.</p>
119          * <p>Note that, this constructor is <b>internal</b> but its declaring class is 
120          * <b>opaque</b>. This constructor is used to define <b>opaque</b> instance fields 
121          * declared in the declaring class of this constructor itself or its subclasses. 
122          * Under this circumstance, the field names must be exactly same as the member 
123          * names, as the <b>opaque</b> instance fields of the <b>opaque</b> type 
124          * {@link js.Var.Member} or its subclasses are resolved by re-compilers directly 
125          * to their names appending to the name resolved from the specified qualifying 
126          * member with a dot in between.</p>
127          * @param q A qualifying member
128          * @param mid The ID of the member to construct
129          * @param rWrapper The generic wrapper
130          * @since 1.0
131          * @javascript Re-compilers must report error on the invocation to an <b>internal</b> constructor. 
132          */
133         public Member(JsObject.Member q, Mid mid, Generic<?> rWrapper) {
134             super(q, mid, rWrapper);
135         }
136         /**
137          * <p>Internally constructs a member without a qualifying member but a generic 
138          * wrapper.</p>
139          * <p>This constructor is <b>internal</b> and only called inside of <b>opaque</b> 
140          * or <b>internal</b> classes or class members.</p>
141          * <p>Note that, this constructor is <b>internal</b> but its declaring class is 
142          * <b>opaque</b>. This constructor is used to define <b>opaque</b> static fields, 
143          * declared in <b>opaque</b> types other than the declaring class of this constructor 
144          * itself and its subclasses. Under this circumstance, the field names must be 
145          * exactly same as the member names, as the <b>opaque</b> static fields of <b>opaque</b> 
146          * types are generally resolved by re-compilers directly to identifiers of their names.</p>
147          * @param mid The ID of the member to construct
148          * @param rWrapper The generic wrapper
149          * @since 1.0
150          * @javascript Re-compilers must report error on the invocation to an <b>internal</b> constructor. 
151          */
152         public Member(Mid mid, Generic<?> rWrapper) {
153             super(mid, rWrapper);
154         }
155         /**
156          * <p>Internally constructs a member based on a qualifying member.</p>
157          * <p>This constructor is <b>internal</b> and only called inside of <b>opaque</b>
158          * or <b>internal</b> classes or class members.</p>
159          * <p>Note that, this constructor is <b>internal</b> but its declaring class is
160          * <b>opaque</b>. This constructor is used to define <b>opaque</b> instance fields 
161          * declared in the declaring class of this constructor itself or its subclasses. 
162          * Under this circumstance, the field names must be exactly same as the member 
163          * names, as the <b>opaque</b> instance fields of the <b>opaque</b> type 
164          * {@link js.Var.Member} or its subclasses are resolved by re-compilers directly
165          * to their names appending to the name resolved from the specified qualifying 
166          * member with a dot in between.</p>
167          * @param q A qualifying member
168          * @param mid The ID of the member to construct
169          * @since 1.0
170          * @javascript Re-compilers must report error on the invocation to an <b>internal</b> constructor.
171          */
172         public Member(JsObject.Member q, Mid mid) {
173             super(q, mid);
174         }
175         /**
176          * <p>Internally constructs a member without a qualifying member.</p>
177          * <p>This constructor is <b>internal</b> and only called inside of <b>opaque</b>
178          * or <b>internal</b> classes or class members.</p>
179          * <p>Note that, this constructor is <b>internal</b> but its declaring class is
180          * <b>opaque</b>. This constructor is used to define <b>opaque</b> static fields, 
181          * declared in <b>opaque</b> types other than the declaring class of this constructor 
182          * itself and its subclasses. Under this circumstance, the field names must be
183          * exactly same as the member names, as the <b>opaque</b> static fields of <b>opaque</b>
184          * types are generally resolved by re-compilers directly to identifiers of their names.</p>
185          * @param mid The ID of the member to construct
186          * @since 1.0
187          * @javascript Re-compilers must report error on the invocation to an <b>internal</b> constructor.
188          */
189         public Member(Mid mid) {
190             super(mid);
191         }
192         @Override
193         /**
194          * <p>Evaluates the property, represented by the current member instance, of the
195          * argument object.</p>
196          * @param o The argument object
197          * @return The value of the current member based on the object argument.
198          * @since 1.0
199          * @javascript Re-compilers must convert the instance invocation of this method into
200          * the JavaScript expression: 
201          * <pre>o.m</pre>
202          * where <tt>m</tt> is the identifier name resolved from the current member
203          * instance of the invocation.
204          */
205         public JsHTMLCollection<T> with(ObjectLike o) {
206             return new JsHTMLCollection<T>(super.with(o), getElementWrapper());
207         }
208 
209         /**
210          * <p>An <b>opaque</b> instance field defining a sub-member that is named by the
211          * name of this field, qualified by the current member instance of the field, and 
212          * to access the property of the name on an object.</p>
213          * @since 1.0
214          * @see JsHTMLCollection#length()
215          * @javascript Re-compilers must resolve the member of this instance field to the
216          * identifier of the field name appending to the identifier resolved from its 
217          * qualifying member with a dot in between.
218          */
219         public final Value.Number.Member length = new Value.Number.Member(this, Members.length);
220     }
221 
222     /**
223      * <p>Casts an <b>opaque</b> object to the current <b>opaque</b> type by wrapping it
224      * with the wrapping constructor.</p>
225      * @param var The argument of an <b>opaque</b> object.
226      * @since 1.0
227      * @javascript Re-compilers must ignore the construction operation of this constructor,
228      * that is, replacing it with its only argument.
229      */
230     public JsHTMLCollection(JsObject var, Generic<?> eWrapper) {
231         super(var, eWrapper);
232     }
233     /**
234      * <p>Casts an <b>opaque</b> object to the current <b>opaque</b> type by wrapping it
235      * with the wrapping constructor.</p>
236      * @param var The argument of an <b>opaque</b> object.
237      * @since 1.0
238      * @javascript Re-compilers must ignore the construction operation of this constructor,
239      * that is, replacing it with its only argument.
240      */
241     public JsHTMLCollection(JsObject var) {
242         super(var);
243     }
244 
245     /**
246      * <p>An <b>opaque</b> static field defining a member that is named by the field name
247      * without a qualifying member and to access the property of the name on an object.</p>
248      * @since 1.0
249      * @see #length()
250      * @javascript Re-compilers must resolve the member of this static field to the
251      * identifier of the field name.
252      */
253     public static final Value.Number.Member length = new Value.Number.Member(Members.length);
254 
255     @Override
256     /**
257      * <p>Returns the primitive value associated with the current instance, if there is one.
258      * This invocation simply returns the instance itself for the current instance is an 
259      * object and there is no primitive value for it.</p>
260      * @return The current object itself.
261      * @since 1.0
262      * @javascript Re-compilers must convert the instance invocation of this method directly
263      * into a JavaScript invocation on its current object instance without changing the 
264      * method name, but expanding variable arguments, if any, into comma-separated values. 
265      */
266     public JsHTMLCollection<T> valueOf() {
267         return new JsHTMLCollection<T>((JsObject)var().valueOf());
268     }
269 
270     /**
271      * <p>Gets the size of the current array instance.</p>
272      * @return size of the array
273      * @see jsx.core.ArrayLikes#length(ArrayObject)
274      * @since 1.0
275      * @javascript Re-compilers must convert the instance invocation of this method into the 
276      * JavaScript expression: 
277      * <pre>a.length</pre>
278      * where <tt>a</tt> is the current array instance of the invocation.
279      */
280     public final int length() {
281         return length.with(this).intValue();
282     }
283 
284     /**
285      * <p>Reads an element of the current array instance.</p>
286      * @param i Array index of the element
287      * @return value of the element
288      * @since 1.0
289      * @see #item(Number)
290      * @see #item(NumberLike)
291      * @see #namedItem(String)
292      * @see #namedItem(StringLike)
293      * @javascript Re-compilers must convert the instance invocation of this method into the 
294      * JavaScript expression: 
295      * <pre>a[i]</pre>
296      * where <tt>a</tt> is the current array instance of the invocation.
297      */
298     public final T get(int i) {
299         return item(i);
300     }
301 
302     @SuppressWarnings("unchecked")
303     /**
304      * <p>Returns the element at the specified position in the collection.</p>
305      * @param i The position of the element to be returned. Elements appear in a {@link JsHTMLCollection} 
306      * object in the same order in which they appear in the document source.
307      * @return The element at the specified index <tt>i</tt>, or <tt>null</tt> if the 
308      * index is less than zero or greater than or equal to the {@link #length} 
309      * property.
310      * @since 1.0
311      * @see #get(int)
312      * @see #item(NumberLike)
313      * @see #namedItem(String)
314      * @see #namedItem(StringLike)
315      * @javascript Re-compilers must convert the instance invocation of this method directly
316      * into a JavaScript invocation on its current object instance without changing the 
317      * method name, but expanding variable arguments, if any, into comma-separated values. 
318      */
319     public final T item(Number i) {
320         return (T)getElementWrapper().wrap(call(item, i));
321     }
322     /**
323      * <p>Returns the element at the specified position in the collection.</p>
324      * @param i The position of the element to be returned. Elements appear in a {@link JsHTMLCollection} 
325      * object in the same order in which they appear in the document source.
326      * @return The element at the specified index <tt>i</tt>, or <tt>null</tt> if the 
327      * index is less than zero or greater than or equal to the {@link #length} 
328      * property.
329      * @since 1.0
330      * @see #get(int)
331      * @see #item(Number)
332      * @see #namedItem(String)
333      * @see #namedItem(StringLike)
334      * @javascript Re-compilers must convert the instance invocation of this method directly
335      * into a JavaScript invocation on its current object instance without changing the 
336      * method name, but expanding variable arguments, if any, into comma-separated values. 
337      */
338     public final T item(NumberLike<?> i) {
339         return item(Js.valueOf(i));
340     }
341     @SuppressWarnings("unchecked")
342     /**
343      * <p>Returns the element from the collection that has the specified value for its 
344      * {@link JsHTMLElement#id} or <tt>name</tt> attribute, or <tt>null</tt> if there is no such element.</p>
345      * @param name The name of the element to be returned.
346      * @return The element in the collection that has the specified value for its {@link JsHTMLElement#id} 
347      * or <tt>name</tt> attribute, or <tt>null</tt> if no elements in the collection 
348      * have that name.
349      * @since 1.0
350      * @see #namedItem(StringLike)
351      * @see #get(int)
352      * @see #item(Number)
353      * @see #item(NumberLike)
354      * @javascript Re-compilers must convert the instance invocation of this method directly
355      * into a JavaScript invocation on its current object instance without changing the 
356      * method name, but expanding variable arguments, if any, into comma-separated values. 
357      */
358     public final T namedItem(String name) {
359         return (T)getElementWrapper().wrap(call(namedItem, name));
360     }
361     /**
362      * <p>Returns the element from the collection that has the specified value for its 
363      * {@link JsHTMLElement#id} or <tt>name</tt> attribute, or <tt>null</tt> if there is no such element.</p>
364      * @param name The name of the element to be returned.
365      * @return The element in the collection that has the specified value for its {@link JsHTMLElement#id} 
366      * or <tt>name</tt> attribute, or <tt>null</tt> if no elements in the collection 
367      * have that name.
368      * @since 1.0
369      * @see #namedItem(String)
370      * @see #get(int)
371      * @see #item(Number)
372      * @see #item(NumberLike)
373      * @javascript Re-compilers must convert the instance invocation of this method directly
374      * into a JavaScript invocation on its current object instance without changing the 
375      * method name, but expanding variable arguments, if any, into comma-separated values. 
376      */
377     public final T namedItem(StringLike name) {
378         return namedItem(Js.valueOf(name));
379     }
380 }