KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jaxen.JaxenException;
41 import org.mozilla.javascript.Context;
42 import org.mozilla.javascript.Function;
43 import org.mozilla.javascript.Scriptable;
44
45 import com.gargoylesoftware.htmlunit.html.HtmlElement;
46 import com.gargoylesoftware.htmlunit.html.xpath.HtmlUnitXPath;
47 import com.gargoylesoftware.htmlunit.javascript.ElementArray;
48 import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable;
49
50 /**
51  * Superclass for all row-containing JavaScript host classes, including tables,
52  * table headers, table bodies and table footers.
53  *
54  * @version $Revision: 100 $
55  * @author Daniel Gredler
56  * @author Chris Erskine
57  */

58 public class RowContainer extends HTMLElement {
59
60     private static final long serialVersionUID = 3258129146093056308L;
61     private ElementArray rows_; // has to be a member to have equality (==) working
62

63     /**
64      * Create an instance.
65      */

66     public RowContainer() {
67     }
68
69     /**
70      * Javascript constructor. This must be declared in every JavaScript file because
71      * the Rhino engine won't walk up the hierarchy looking for constructors.
72      */

73     public void jsConstructor() {
74     }
75
76     /**
77      * Returns the rows in the element.
78      * @return The rows in the element.
79      */

80     public Object JavaDoc jsxGet_rows() {
81         if (rows_ == null) {
82             rows_ = (ElementArray) makeJavaScriptObject(ElementArray.JS_OBJECT_NAME);
83             try {
84                 rows_.init(getDomNodeOrDie(), new HtmlUnitXPath(".//tr"));
85             }
86             catch (final JaxenException e) {
87                 throw Context.reportRuntimeError("Failed to initialize rowContainer.rows: " + e.getMessage());
88             }
89         }
90         return rows_;
91     }
92
93     /**
94      * Deletes the row at the specified index.
95      * @see <a HREF="http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/deleterow.asp">
96      * MSDN Documentation</a>
97      * @param rowIndex the zero-based index of the row to delete.
98      */

99     public void jsxFunction_deleteRow(final int rowIndex) {
100         final ElementArray rows = (ElementArray) jsxGet_rows();
101         final boolean rowIndexValid = (rowIndex >= 0 && rowIndex < rows.jsGet_length());
102         if (rowIndexValid) {
103             final SimpleScriptable row = (SimpleScriptable) rows.jsFunction_item(new Integer JavaDoc(rowIndex));
104             row.getDomNodeOrDie().remove();
105         }
106     }
107
108     /**
109      * Inserts a new row at the specified index in the element's row collection. If the index
110      * is -1 or there is no index specified, then the row is appended at the end of the
111      * element's row collection.
112      * @see <a HREF="http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/insertrow.asp">
113      * MSDN Documentation</a>
114      * @param cx the current JavaScript context.
115      * @param s this scriptable object.
116      * @param args the arguments for the function call.
117      * @param f the function object that invoked this function.
118      * @return the newly-created row.
119      */

120     public static Object JavaDoc jsxFunction_insertRow(
121             final Context cx, final Scriptable s, final Object JavaDoc[] args,
122             final Function f) {
123         final RowContainer rowContainer = (RowContainer) s;
124         final ElementArray rows = (ElementArray) rowContainer.jsxGet_rows();
125         final Number JavaDoc rowIndex;
126         if (args.length > 0) {
127             rowIndex = (Number JavaDoc) args[0];
128         }
129         else {
130             rowIndex = null;
131         }
132         final int r;
133         if (rowIndex == null || rowIndex.intValue() == -1) {
134             r = rows.jsGet_length() - 1;
135         }
136         else {
137             r = rowIndex.intValue();
138         }
139         final boolean rowIndexValid = (r >= 0 && r <= rows.jsGet_length());
140         if (rowIndexValid) {
141             final HtmlElement newRow = rowContainer.getDomNodeOrDie().getPage().createElement("tr");
142             if (rows.jsGet_length() == 0 || (r == rows.jsGet_length())) {
143                 rowContainer.getDomNodeOrDie().appendChild(newRow);
144             }
145             else {
146                 final SimpleScriptable row = (SimpleScriptable) rows.jsFunction_item(new Integer JavaDoc(r));
147                 // if at the end, then in the same "sub-container" as the last existing row
148
if (r == rows.jsGet_length() - 1) {
149                     row.getDomNodeOrDie().getParentNode().appendChild(newRow);
150                 }
151                 else {
152                     row.getDomNodeOrDie().insertBefore(newRow);
153                 }
154             }
155             return rowContainer.getScriptableFor(newRow);
156         }
157         else {
158             throw Context.reportRuntimeError("Index or size is negative or greater than the allowed amount");
159         }
160     }
161
162     /**
163      * Moves the row at the specified source index to the specified target index, returning
164      * the row that was moved.
165      * @param sourceIndex the index of the row to move.
166      * @param targetIndex the index to move the row to.
167      * @return the row that was moved.
168      */

169     public Object JavaDoc jsxFunction_moveRow(final int sourceIndex, final int targetIndex) {
170         final ElementArray rows = (ElementArray) jsxGet_rows();
171         final boolean sourceIndexValid = (sourceIndex >= 0 && sourceIndex < rows.jsGet_length());
172         final boolean targetIndexValid = (targetIndex >= 0 && targetIndex < rows.jsGet_length());
173         if (sourceIndexValid && targetIndexValid) {
174             final SimpleScriptable sourceRow = (SimpleScriptable) rows.jsFunction_item(new Integer JavaDoc(sourceIndex));
175             final SimpleScriptable targetRow = (SimpleScriptable) rows.jsFunction_item(new Integer JavaDoc(targetIndex));
176             targetRow.getDomNodeOrDie().insertBefore(sourceRow.getDomNodeOrDie());
177             return sourceRow;
178         }
179         else {
180             return null;
181         }
182     }
183
184 }
185
Popular Tags