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#NodeList} class.</p>
028  * <p>The <b>opaque</b> class defines a read-only, ordered list or an array of {@link JsNode} 
029  * objects. The {@link JsNodeList#length} property specifies how many nodes are in the 
030  * list, and the {@link #item(Number)} method allows you to obtain the node at a specified 
031  * position in the list. The elements of the list are always valid {@link JsNode} objects and 
032  * they never contain <tt>null</tt> elements.</p>
033  * <p>Objects of {@link JsNodeList} behave like JavaScript arrays, and you can query an 
034  * element from the list using square-bracket array notation in JavaScript, or using {@link #get(int)} 
035  * method in JS applications, instead of calling the {@link #item(Number)} method. 
036  * However, you cannot assign new nodes to the array (even if using square brackets in JavaScript).</p>
037  * <p>Note that objects of {@link JsNodeList} are live, that is, they are not static 
038  * snapshots but immediately reflect changes to the document tree.</p>
039  *
040  * @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>
041  *
042  * @javascript <b>Opaque</b> types can be resolved but no class objects for them can be
043  * generated into the target codes. Re-compilers must exit with error on the operations of
044  * accessing that kind of class objects.
045  * The <tt>checkcast</tt> operation to the class literal of this interface must be ignored
046  * and <tt>instanceof</tt> to it always <tt>true</tt>.
047  */
048 public final class JsNodeList<T extends JsNode> extends JsClient.NodeList.Prototype<T> implements ArrayObject<JsNode>
049 {
050     /**
051      * <p>An <b>internal</b> class containing membership data for its enclosing
052      * opaque class.</p>
053      * <p>This class is only used inside of <b>opaque</b> or <b>internal</b> classes or
054      * class members.</p>
055      *
056      * @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>
057      * 
058      * @javascript Re-compilers must report error on resolving an <b>internal</b> class.
059      */
060     protected static abstract class Members extends JsClient.NodeList.Prototype.Members
061     {
062         /**
063          * <p>An <b>internal</b> static field defining a member ID of the field name and
064          * used internally to define a member of the same name.</p>
065          * @since 1.0
066          * @see JsNodeList#length
067          * @see JsNodeList.Member#length
068          * @javascript Re-compilers must report error on accessing an <b>internal</b> field.
069          */
070         protected static final Mid length = id("length");
071     }
072     /**
073      * <p>An <b>opaque</b> class representing members of its enclosing <b>opaque</b> type.</p>
074      * <p>Note that, this class is <b>opaque</b> but its constructors are all <b>internal</b>. 
075      * This class and the subclasses of this class are used to declare either <b>opaque</b> 
076      * <tt>public</tt> instance fields of the opaque type {@link js.Var.Member} or the 
077      * <b>opaque</b> <tt>public</tt> static fields of other <b>opaque</b> types while their 
078      * constructors are used to define the fields inside <b>opaque</b> classes. Under 
079      * either circumstance, the field names must be exactly same as the member names, as 
080      * the <b>opaque</b> fields of <b>opaque</b> types are resolved by re-compilers directly 
081      * based on the field names.</p>
082      *
083      * @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>
084      * 
085      * @javascript <b>Opaque</b> types can be resolved but no class objects for them can be created
086      * in the target codes. Re-compilers must exit with error on operations accessing that kind 
087      * of class objects.
088      * Re-compilers must resolve an <b>opaque</b> instance field declared by this class in
089      * {@link js.Var.Member} or its subclasses to the JavaScript identifier: 
090      * <pre>q.m</pre>
091      * where <tt>m</tt> is the identifier of the field name and <tt>q</tt> is the identifier
092      * resolved from the instance of the enclosing member. Re-compilers must resolve an 
093      * <b>opaque</b> static field declared by this class in <b>opaque</b> types other than 
094      * {@link js.Var.Member} and its subclasses to the JavaScript identifier: 
095      * <pre>m</pre>
096      * where <tt>m</tt> is the identifier of the field name. And re-compilers must report
097      * error on the access to <b>opaque</b> fields declared by this class under any other 
098      * circumstances.
099      */
100     public final static class Member<T extends JsNode> extends JsClient.NodeList.Prototype.Member<T>
101     {
102         /**
103          * <p>Internally constructs a member based on a qualifying member with a generic 
104          * wrapper.</p>
105          * <p>This constructor is <b>internal</b> and only called inside of <b>opaque</b> 
106          * or <b>internal</b> classes or class members.</p>
107          * <p>Note that, this constructor is <b>internal</b> but its declaring class is 
108          * <b>opaque</b>. This constructor is used to define <b>opaque</b> instance fields 
109          * declared in the declaring class of this constructor itself or its subclasses. 
110          * Under this circumstance, the field names must be exactly same as the member 
111          * names, as the <b>opaque</b> instance fields of the <b>opaque</b> type 
112          * {@link js.Var.Member} or its subclasses are resolved by re-compilers directly 
113          * to their names appending to the name resolved from the specified qualifying 
114          * member with a dot in between.</p>
115          * @param q A qualifying member
116          * @param mid The ID of the member to construct
117          * @param rWrapper The generic wrapper
118          * @since 1.0
119          * @javascript Re-compilers must report error on the invocation to an <b>internal</b> constructor. 
120          */
121         public Member(JsObject.Member q, Mid mid, Generic<?> rWrapper) {
122             super(q, mid, rWrapper);
123         }
124         /**
125          * <p>Internally constructs a member without a qualifying member but a generic 
126          * wrapper.</p>
127          * <p>This constructor is <b>internal</b> and only called inside of <b>opaque</b> 
128          * or <b>internal</b> classes or class members.</p>
129          * <p>Note that, this constructor is <b>internal</b> but its declaring class is 
130          * <b>opaque</b>. This constructor is used to define <b>opaque</b> static fields, 
131          * declared in <b>opaque</b> types other than the declaring class of this constructor 
132          * itself and its subclasses. Under this circumstance, the field names must be 
133          * exactly same as the member names, as the <b>opaque</b> static fields of <b>opaque</b> 
134          * types are generally resolved by re-compilers directly to identifiers of their names.</p>
135          * @param mid The ID of the member to construct
136          * @param rWrapper The generic wrapper
137          * @since 1.0
138          * @javascript Re-compilers must report error on the invocation to an <b>internal</b> constructor. 
139          */
140         public Member(Mid mid, Generic<?> rWrapper) {
141             super(mid, rWrapper);
142         }
143         /**
144          * <p>Internally constructs a member based on a qualifying member.</p>
145          * <p>This constructor is <b>internal</b> and only called inside of <b>opaque</b>
146          * or <b>internal</b> classes or class members.</p>
147          * <p>Note that, this constructor is <b>internal</b> but its declaring class is
148          * <b>opaque</b>. This constructor is used to define <b>opaque</b> instance fields 
149          * declared in the declaring class of this constructor itself or its subclasses. 
150          * Under this circumstance, the field names must be exactly same as the member 
151          * names, as the <b>opaque</b> instance fields of the <b>opaque</b> type 
152          * {@link js.Var.Member} or its subclasses are resolved by re-compilers directly
153          * to their names appending to the name resolved from the specified qualifying 
154          * member with a dot in between.</p>
155          * @param q A qualifying member
156          * @param mid The ID of the member to construct
157          * @since 1.0
158          * @javascript Re-compilers must report error on the invocation to an <b>internal</b> constructor.
159          */
160         public Member(JsObject.Member q, Mid mid) {
161             super(q, mid);
162         }
163         /**
164          * <p>Internally constructs a member without a qualifying member.</p>
165          * <p>This constructor is <b>internal</b> and only called inside of <b>opaque</b>
166          * or <b>internal</b> classes or class members.</p>
167          * <p>Note that, this constructor is <b>internal</b> but its declaring class is
168          * <b>opaque</b>. This constructor is used to define <b>opaque</b> static fields, 
169          * declared in <b>opaque</b> types other than the declaring class of this constructor 
170          * itself and its subclasses. Under this circumstance, the field names must be
171          * exactly same as the member names, as the <b>opaque</b> static fields of <b>opaque</b>
172          * types are generally resolved by re-compilers directly to identifiers of their names.</p>
173          * @param mid The ID of the member to construct
174          * @since 1.0
175          * @javascript Re-compilers must report error on the invocation to an <b>internal</b> constructor.
176          */
177         public Member(Mid mid) {
178             super(mid);
179         }
180         @Override
181         /**
182          * <p>Evaluates the property, represented by the current member instance, of the
183          * argument object.</p>
184          * @param o The argument object
185          * @return The value of the current member based on the object argument.
186          * @since 1.0
187          * @javascript Re-compilers must convert the instance invocation of this method into
188          * the JavaScript expression: 
189          * <pre>o.m</pre>
190          * where <tt>m</tt> is the identifier name resolved from the current member
191          * instance of the invocation.
192          */
193         public JsNodeList<T> with(ObjectLike o) {
194             return new JsNodeList<T>(super.with(o));
195         }
196 
197         /**
198          * <p>An <b>opaque</b> instance field defining a sub-member that is named by the
199          * name of this field, qualified by the current member instance of the field, and 
200          * to access the property of the name on an object.</p>
201          * <p>The property, identified by this member, of a {@link JsNodeList} object 
202          * refers to the length of the array specifying the number of nodes in it.</p>
203          * @since 1.0
204          * @see JsNodeList#length()
205          * @javascript Re-compilers must resolve the member of this instance field to the
206          * identifier of the field name appending to the identifier resolved from its 
207          * qualifying member with a dot in between.
208          */
209         public final Value.Number.Member length = new Value.Number.Member(this, Members.length);
210     }
211 
212     public JsNodeList(JsObject var, Generic<?> eWrapper) {
213         super(var, eWrapper);
214     }
215     /**
216      * <p>Casts an <b>opaque</b> object to the current <b>opaque</b> type by wrapping it
217      * with the wrapping constructor.</p>
218      * @param var The argument of an <b>opaque</b> object.
219      * @since 1.0
220      * @javascript Re-compilers must ignore the construction operation of this constructor,
221      * that is, replacing it with its only argument.
222      */
223     public JsNodeList(JsObject var) {
224         super(var);
225     }
226 
227     /**
228      * <p>An <b>opaque</b> static field defining a member that is named by the field name
229      * without a qualifying member and to access the property of the name on an object.</p>
230      * <p>The property, identified by this member, of a {@link JsNodeList} object 
231      * refers to the length of the array specifying the number of nodes in it.</p>
232      * @since 1.0
233      * @see #length()
234      * @javascript Re-compilers must resolve the member of this static field to the
235      * identifier of the field name.
236      */
237     public static final Value.Number.Member length = new Value.Number.Member(Members.length);
238 
239     @Override
240     /**
241      * <p>Returns the primitive value associated with the current instance, if there is one.
242      * This invocation simply returns the instance itself for the current instance is an 
243      * object and there is no primitive value for it.</p>
244      * @return The current object itself.
245      * @since 1.0
246      * @javascript Re-compilers must convert the instance invocation of this method directly
247      * into a JavaScript invocation on its current object instance without changing the 
248      * method name, but expanding variable arguments, if any, into comma-separated values. 
249      */
250     public JsNodeList<T> valueOf() {
251         return new JsNodeList<T>((JsObject)var().valueOf());
252     }
253 
254     /**
255      * <p>Reads an element of the current array instance.</p>
256      * @param i Array index of the element
257      * @return value of the element
258      * @since 1.0
259      * @see #item(Number)
260      * @see #item(NumberLike)
261      * @see #length()
262      * @see #length
263      * @javascript Re-compilers must convert the instance invocation of this method into the 
264      * JavaScript expression: 
265      * <pre>a[i]</pre>
266      * where <tt>a</tt> is the current array instance of the invocation.
267      */
268     public final T get(int i) {
269         return item(i);
270     }
271 
272     /**
273      * <p>Gets the size of the current array instance.</p>
274      * @return size of the array
275      * @see jsx.core.ArrayLikes#length(ArrayObject)
276      * @since 1.0
277      * @see #length
278      * @javascript Re-compilers must convert the instance invocation of this method into the 
279      * JavaScript expression: 
280      * <pre>a.length</pre>
281      * where <tt>a</tt> is the current array instance of the invocation.
282      */
283     public final int length() {
284         return length.with(this).intValue();
285     }
286 
287     /**
288      * <p>Returns the specified element of the array object.</p>
289      * <p>This method is equivalent to {@link #get(int)}. In JavaScript, you can use the 
290      * square-bracket array notation instead of calling this method.</p>
291      * @param index The position (or index) of the desired node in the array object. The 
292      * index of the first node in the array is 0, and the index of the last node is 
293      * {@link #length} minus 1.
294      * @return The node at the specified position in the {@link JsNodeList} array 
295      * object, or <tt>null</tt> if <tt>index</tt> is less than zero or greater than or 
296      * equal to the {@link #length} of the {@link JsNodeList} array object.
297      * @since 1.0
298      * @see #item(NumberLike)
299      * @see #get(int)
300      * @see #length()
301      * @javascript Re-compilers must convert the instance invocation of this method directly
302      * into a JavaScript invocation on its current object instance without changing the 
303      * method name, but expanding variable arguments, if any, into comma-separated values. 
304      */
305     @SuppressWarnings("unchecked")
306     public final T item(Number index) {
307         return (T)getElementWrapper().wrap(call(item, index));
308     }
309     /**
310      * <p>Returns the specified element of the array object.</p>
311      * <p>This method is equivalent to {@link #get(int)}. In JavaScript, you can use the 
312      * square-bracket array notation instead of calling this method.</p>
313      * @param index The position (or index) of the desired node in the array object. The 
314      * index of the first node in the array is 0, and the index of the last node is 
315      * {@link #length} minus 1.
316      * @return The node at the specified position in the {@link JsNodeList} array 
317      * object, or <tt>null</tt> if <tt>index</tt> is less than zero or greater than or 
318      * equal to the {@link #length} of the {@link JsNodeList} array object.
319      * @since 1.0
320      * @see #item(Number)
321      * @see #get(int)
322      * @see #length()
323      * @javascript Re-compilers must convert the instance invocation of this method directly
324      * into a JavaScript invocation on its current object instance without changing the 
325      * method name, but expanding variable arguments, if any, into comma-separated values. 
326      */
327     public final T item(NumberLike<?> index) {
328         return item(Js.valueOf(index));
329     }
330 }