KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.List JavaDoc;
41
42 import org.jaxen.JaxenException;
43 import org.mozilla.javascript.Context;
44
45 import com.gargoylesoftware.htmlunit.html.xpath.HtmlUnitXPath;
46 import com.gargoylesoftware.htmlunit.javascript.ElementArray;
47
48 /**
49  * A JavaScript object representing a Table.
50  *
51  * @version $Revision: 100 $
52  * @author David D. Kilzer
53  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
54  * @author Daniel Gredler
55  * @author Chris Erskine
56  */

57 public class Table extends RowContainer {
58
59     private static final long serialVersionUID = 2779888994049521608L;
60     private ElementArray tBodies_; // has to be a member to have equality (==) working
61

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

65     public Table() {
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 table's caption element, or <tt>null</tt> if none exists. If more than one
77      * caption is declared in the table, this method returns the first one.
78      * @return the table's caption element.
79      */

80     public Object JavaDoc jsxGet_caption() {
81         final List JavaDoc captions = getHtmlElementOrDie().getHtmlElementsByTagName("caption");
82         if(captions.isEmpty()) {
83             return null;
84         }
85         else {
86             return getScriptableFor(captions.get(0));
87         }
88     }
89
90     /**
91      * Returns the table's tfoot element, or <tt>null</tt> if none exists. If more than one
92      * tfoot is declared in the table, this method returns the first one.
93      * @return the table's tfoot element.
94      */

95     public Object JavaDoc jsxGet_tFoot() {
96         final List JavaDoc tfoots = getHtmlElementOrDie().getHtmlElementsByTagName("tfoot");
97         if(tfoots.isEmpty()) {
98             return null;
99         }
100         else {
101             return getScriptableFor(tfoots.get(0));
102         }
103     }
104
105     /**
106      * Returns the table's thead element, or <tt>null</tt> if none exists. If more than one
107      * thead is declared in the table, this method returns the first one.
108      * @return the table's thead element.
109      */

110     public Object JavaDoc jsxGet_tHead() {
111         final List JavaDoc theads = getHtmlElementOrDie().getHtmlElementsByTagName("thead");
112         if(theads.isEmpty()) {
113             return null;
114         }
115         else {
116             return getScriptableFor(theads.get(0));
117         }
118     }
119
120     /**
121      * Returns the tbody's in the table.
122      * @return The tbody's in the table.
123      */

124     public Object JavaDoc jsxGet_tBodies() {
125         if (tBodies_ == null) {
126             tBodies_ = (ElementArray) makeJavaScriptObject(ElementArray.JS_OBJECT_NAME);
127             try {
128                 tBodies_.init(getDomNodeOrDie(), new HtmlUnitXPath("//tbody"));
129             }
130             catch (final JaxenException e) {
131                 throw Context.reportRuntimeError("Failed to initialize collection table.tBodies: " + e.getMessage());
132             }
133         }
134         return tBodies_;
135     }
136
137     /**
138      * If this table does not have a caption, this method creates an empty table caption,
139      * adds it to the table and then returns it. If one or more captions already exist,
140      * this method returns the first existing caption.
141      * @see <a HREF="http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/createcaption.asp">
142      * MSDN Documentation</a>
143      * @return a newly added caption if no caption exists, or the first existing caption.
144      */

145     public Object JavaDoc jsxFunction_createCaption() {
146         return getScriptableFor( getHtmlElementOrDie().appendChildIfNoneExists("caption") );
147     }
148
149     /**
150      * If this table does not have a tfoot element, this method creates an empty tfoot
151      * element, adds it to the table and then returns it. If this table already has a
152      * tfoot element, this method returns the existing tfoot element.
153      * @see <a HREF="http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/createtfoot.asp">
154      * MSDN Documentation</a>
155      * @return a newly added caption if no caption exists, or the first existing caption.
156      */

157     public Object JavaDoc jsxFunction_createTFoot() {
158         return getScriptableFor( getHtmlElementOrDie().appendChildIfNoneExists("tfoot") );
159     }
160
161     /**
162      * If this table does not have a thead element, this method creates an empty
163      * thead element, adds it to the table and then returns it. If this table
164      * already has a thead element, this method returns the existing thead element.
165      * @see <a HREF="http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/createthead.asp">
166      * MSDN Documentation</a>
167      * @return a newly added caption if no caption exists, or the first existing caption.
168      */

169     public Object JavaDoc jsxFunction_createTHead() {
170         return getScriptableFor( getHtmlElementOrDie().appendChildIfNoneExists("thead") );
171     }
172
173     /**
174      * Deletes this table's caption. If the table has multiple captions, this method
175      * deletes only the first caption. If this table does not have any captions, this
176      * method does nothing.
177      * @see <a HREF="http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/deletecaption.asp">
178      * MSDN Documentation</a>
179      */

180     public void jsxFunction_deleteCaption() {
181         getHtmlElementOrDie().removeChild("caption", 0);
182     }
183
184     /**
185      * Deletes this table's tfoot element. If the table has multiple tfoot elements, this
186      * method deletes only the first tfoot element. If this table does not have any tfoot
187      * elements, this method does nothing.
188      * @see <a HREF="http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/deletecaption.asp">
189      * MSDN Documentation</a>
190      */

191     public void jsxFunction_deleteTFoot() {
192         getHtmlElementOrDie().removeChild("tfoot", 0);
193     }
194
195     /**
196      * Deletes this table's thead element. If the table has multiple thead elements, this
197      * method deletes only the first thead element. If this table does not have any thead
198      * elements, this method does nothing.
199      * @see <a HREF="http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/deletethead.asp">
200      * MSDN Documentation</a>
201      */

202     public void jsxFunction_deleteTHead() {
203         getHtmlElementOrDie().removeChild("thead", 0);
204     }
205
206 }
207
Popular Tags