KickJava   Java API By Example, From Geeks To Geeks.

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


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.HtmlTable;
47 import com.gargoylesoftware.htmlunit.html.HtmlTableRow;
48 import com.gargoylesoftware.htmlunit.html.xpath.HtmlUnitXPath;
49 import com.gargoylesoftware.htmlunit.javascript.ElementArray;
50
51 /**
52  * A JavaScript object representing a TR.
53  *
54  * @version $Revision: 100 $
55  * @author Marc Guillemot
56  * @author Chris Erskine
57  */

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

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

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

72     public void jsConstructor() {
73     }
74
75     /**
76      * Returns the index of the row within parent's table
77      * @return the index.
78      * @see <a HREF="http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/rowindex.asp">
79      * MSDN Documentation</a>
80      */

81     public int jsxGet_rowIndex() {
82         final HtmlTableRow row = (HtmlTableRow) getHtmlElementOrDie();
83         final HtmlTable table = row.getEnclosingTable();
84         return table.getRows().indexOf(row);
85     }
86
87     /**
88      * Returns the cells in the row.
89      * @return The cells in the row.
90      */

91     public Object JavaDoc jsxGet_cells() {
92         if (cells_ == null) {
93             cells_ = (ElementArray) makeJavaScriptObject(ElementArray.JS_OBJECT_NAME);
94             try {
95                 cells_.init(getDomNodeOrDie(), new HtmlUnitXPath(".//td"));
96             }
97             catch (final JaxenException e) {
98                 throw Context.reportRuntimeError("Failed to initialize row.cells: " + e.getMessage());
99             }
100         }
101         return cells_;
102     }
103
104     /**
105      * Inserts a new cell at the specified index in the element's cells collection. If the index
106      * is -1 or there is no index specified, then the cell is appended at the end of the
107      * element's cells collection.
108      * @see <a HREF="http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/insertcell.asp">
109      * MSDN Documentation</a>
110      * @param cx the current JavaScript context.
111      * @param s this scriptable object.
112      * @param args the arguments for the function call.
113      * @param f the function object that invoked this function.
114      * @return the newly-created cell.
115      */

116     public static Object JavaDoc jsxFunction_insertCell(final Context cx, final Scriptable s,
117             final Object JavaDoc[] args, final Function f) {
118         final TableRow row = (TableRow) s;
119         final HtmlTableRow htmlRow = (HtmlTableRow) row.getDomNodeOrDie();
120         
121         final int position = getIntArg(0, args, -1);
122
123         final boolean indexValid = (position >= -1 && position <= htmlRow.getCells().size());
124         if (indexValid) {
125             final HtmlElement newCell = htmlRow.getPage().createElement("td");
126             if (position == -1 || position == htmlRow.getCells().size()) {
127                 htmlRow.appendChild(newCell);
128             }
129             else {
130                 htmlRow.getCell(position).insertBefore(newCell);
131             }
132             return row.getScriptableFor(newCell);
133         }
134         else {
135             throw Context.reportRuntimeError("Index or size is negative or greater than the allowed amount");
136         }
137     }
138 }
139
Popular Tags