KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > apache > html > dom > HTMLTableElementImpl


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999,2000 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57 package org.enhydra.apache.html.dom;
58
59
60 import org.enhydra.apache.xerces.dom.ElementImpl;
61 import org.w3c.dom.Node JavaDoc;
62 import org.w3c.dom.html.HTMLCollection;
63 import org.w3c.dom.html.HTMLElement;
64 import org.w3c.dom.html.HTMLTableCaptionElement;
65 import org.w3c.dom.html.HTMLTableElement;
66 import org.w3c.dom.html.HTMLTableRowElement;
67 import org.w3c.dom.html.HTMLTableSectionElement;
68
69
70 /**
71  * @version $Revision: 1.3 $ $Date: 2005/01/26 08:28:44 $
72  * @author <a HREF="mailto:arkin@exoffice.com">Assaf Arkin</a>
73  * @see org.w3c.dom.html.HTMLAnchorElement
74  * @see ElementImpl
75  */

76 public class HTMLTableElementImpl
77     extends HTMLElementImpl
78     implements HTMLTableElement
79 {
80     
81     
82     public synchronized HTMLTableCaptionElement getCaption()
83     {
84         Node JavaDoc child;
85         
86         child = getFirstChild();
87         while ( child != null )
88         {
89             if ( child instanceof HTMLTableCaptionElement &&
90                  child.getNodeName().equals( "CAPTION" ) )
91                 return (HTMLTableCaptionElement) child;
92             child = child.getNextSibling();
93         }
94         return null;
95     }
96     
97     
98     public synchronized void setCaption( HTMLTableCaptionElement caption )
99     {
100         if ( caption != null && ! caption.getTagName().equals( "CAPTION" ) )
101             throw new IllegalArgumentException JavaDoc( "HTM016 Argument 'caption' is not an element of type <CAPTION>." );
102         deleteCaption();
103         if ( caption != null )
104             appendChild( caption );
105     }
106     
107     
108     public synchronized HTMLElement createCaption()
109     {
110         HTMLElement section;
111         
112         section = getCaption();
113         if ( section != null )
114             return section;
115         section = new HTMLTableCaptionElementImpl( (HTMLDocumentImpl) getOwnerDocument(), "CAPTION" );
116         appendChild( section );
117         return section;
118     }
119
120   
121     public synchronized void deleteCaption()
122     {
123         Node JavaDoc old;
124         
125         old = getCaption();
126         if ( old != null )
127             removeChild ( old );
128     }
129     
130     public synchronized HTMLTableSectionElement getTHead()
131     {
132         Node JavaDoc child;
133         
134         child = getFirstChild();
135         while ( child != null )
136         {
137             if ( child instanceof HTMLTableSectionElement &&
138                  child.getNodeName().equals( "THEAD" ) )
139                 return (HTMLTableSectionElement) child;
140             child = child.getNextSibling();
141         }
142         return null;
143     }
144     
145     
146     public synchronized void setTHead( HTMLTableSectionElement tHead )
147     {
148         if ( tHead != null && ! tHead.getTagName().equals( "THEAD" ) )
149             throw new IllegalArgumentException JavaDoc( "HTM017 Argument 'tHead' is not an element of type <THEAD>." );
150         deleteTHead();
151         if ( tHead != null )
152             appendChild( tHead );
153     }
154     
155     
156     public synchronized HTMLElement createTHead()
157     {
158         HTMLElement section;
159         
160         section = getTHead();
161         if ( section != null )
162             return section;
163         section = new HTMLTableSectionElementImpl( (HTMLDocumentImpl) getOwnerDocument(), "THEAD" );
164         appendChild( section );
165         return section;
166     }
167
168     
169     public synchronized void deleteTHead()
170     {
171         Node JavaDoc old;
172         
173         old = getTHead();
174         if ( old != null )
175             removeChild ( old );
176     }
177     
178     public synchronized HTMLTableSectionElement getTFoot()
179     {
180         Node JavaDoc child;
181         
182         child = getFirstChild();
183         while ( child != null )
184         {
185             if ( child instanceof HTMLTableSectionElement &&
186                  child.getNodeName().equals( "TFOOT" ) )
187                 return (HTMLTableSectionElement) child;
188             child = child.getNextSibling();
189         }
190         return null;
191     }
192     
193     
194     public synchronized void setTFoot( HTMLTableSectionElement tFoot )
195     {
196         if ( tFoot != null && ! tFoot.getTagName().equals( "TFOOT" ) )
197             throw new IllegalArgumentException JavaDoc( "HTM018 Argument 'tFoot' is not an element of type <TFOOT>." );
198         deleteTFoot();
199         if ( tFoot != null )
200             appendChild( tFoot );
201     }
202     
203     
204     public synchronized HTMLElement createTFoot()
205     {
206         HTMLElement section;
207         
208         section = getTFoot();
209         if ( section != null )
210             return section;
211         section = new HTMLTableSectionElementImpl( (HTMLDocumentImpl) getOwnerDocument(), "TFOOT" );
212         appendChild( section );
213         return section;
214     }
215
216     
217     public synchronized void deleteTFoot()
218     {
219         Node JavaDoc old;
220         
221         old = getTFoot();
222         if ( old != null )
223             removeChild ( old );
224     }
225     
226     public HTMLCollection getRows()
227     {
228         if ( _rows == null )
229             _rows = new HTMLCollectionImpl( this, HTMLCollectionImpl.ROW );
230         return _rows;
231     }
232     
233
234     public HTMLCollection getTBodies()
235     {
236         if ( _bodies == null )
237             _bodies = new HTMLCollectionImpl( this, HTMLCollectionImpl.TBODY );
238         return _bodies;
239     }
240   
241   
242     public String JavaDoc getAlign()
243     {
244         return capitalize( getAttribute( "align" ) );
245     }
246     
247     
248     public void setAlign( String JavaDoc align )
249     {
250         setAttribute( "align", align );
251     }
252   
253     
254     public String JavaDoc getBgColor()
255     {
256         return getAttribute( "bgcolor" );
257     }
258     
259     
260     public void setBgColor( String JavaDoc bgColor )
261     {
262         setAttribute( "bgcolor", bgColor );
263     }
264   
265   
266     public String JavaDoc getBorder()
267     {
268         return getAttribute( "border" );
269     }
270     
271     
272     public void setBorder( String JavaDoc border )
273     {
274         setAttribute( "border", border );
275     }
276
277     
278     public String JavaDoc getCellPadding()
279     {
280         return getAttribute( "cellpadding" );
281     }
282     
283     
284     public void setCellPadding( String JavaDoc cellPadding )
285     {
286         setAttribute( "cellpadding", cellPadding );
287     }
288     
289     
290     public String JavaDoc getCellSpacing()
291     {
292         return getAttribute( "cellspacing" );
293     }
294     
295     
296     public void setCellSpacing( String JavaDoc cellSpacing )
297     {
298         setAttribute( "cellspacing", cellSpacing );
299     }
300     
301     
302     public String JavaDoc getFrame()
303     {
304         return capitalize( getAttribute( "frame" ) );
305     }
306     
307     
308     public void setFrame( String JavaDoc frame )
309     {
310         setAttribute( "frame", frame );
311     }
312     
313     
314     public String JavaDoc getRules()
315     {
316         return capitalize( getAttribute( "rules" ) );
317     }
318     
319     
320     public void setRules( String JavaDoc rules )
321     {
322         setAttribute( "rules", rules );
323     }
324     
325     
326     public String JavaDoc getSummary()
327     {
328         return getAttribute( "summary" );
329     }
330     
331     
332     public void setSummary( String JavaDoc summary )
333     {
334         setAttribute( "summary", summary );
335     }
336
337   
338       public String JavaDoc getWidth()
339     {
340         return getAttribute( "width" );
341     }
342     
343     
344     public void setWidth( String JavaDoc width )
345     {
346         setAttribute( "width", width );
347     }
348
349     
350     public HTMLElement insertRow( int index )
351     {
352         HTMLTableRowElementImpl newRow;
353
354         newRow = new HTMLTableRowElementImpl( (HTMLDocumentImpl) getOwnerDocument(), "TR" );
355         //newRow.insertCell( 0 );
356
insertRowX( index, newRow );
357         return newRow;
358     }
359         
360         
361     void insertRowX( int index, HTMLTableRowElementImpl newRow )
362     {
363         Node JavaDoc child;
364         Node JavaDoc lastSection = null;
365                 
366         child = getFirstChild();
367         while ( child != null )
368         {
369             if ( child instanceof HTMLTableRowElement )
370             {
371                 if ( index == 0 )
372                 {
373                     insertBefore( newRow, child );
374                     return;
375                 }
376             }
377             else
378             if ( child instanceof HTMLTableSectionElementImpl )
379             {
380                 lastSection = child;
381                 index = ( (HTMLTableSectionElementImpl) child ).insertRowX( index, newRow );
382                 if ( index < 0 )
383                     return;
384             }
385             child = child.getNextSibling();
386         }
387         if ( lastSection != null )
388             lastSection.appendChild( newRow );
389         else
390             appendChild( newRow );
391     }
392     
393     
394     public synchronized void deleteRow( int index )
395     {
396         Node JavaDoc child;
397         
398         child = getFirstChild();
399         while ( child != null )
400         {
401             if ( child instanceof HTMLTableRowElement )
402             {
403                 if ( index == 0 )
404                 {
405                     removeChild ( child );
406                     return;
407                 }
408                 --index;
409             }
410             else
411             if ( child instanceof HTMLTableSectionElementImpl )
412             {
413                 index = ( (HTMLTableSectionElementImpl) child ).deleteRowX( index );
414                 if ( index < 0 )
415                     return;
416             }
417             child = child.getNextSibling();
418         }
419     }
420
421     /**
422      * Explicit implementation of cloneNode() to ensure that cache used
423      * for getRows() and getTBodies() gets cleared.
424      */

425     public Node JavaDoc cloneNode( boolean deep ) {
426         HTMLTableElementImpl clonedNode = (HTMLTableElementImpl)super.cloneNode( deep );
427         clonedNode._rows = null;
428         clonedNode._bodies = null;
429         return clonedNode;
430     }
431
432     /**
433      * Constructor requires owner document.
434      *
435      * @param owner The owner HTML document
436      */

437     public HTMLTableElementImpl( HTMLDocumentImpl owner, String JavaDoc name )
438     {
439         super( owner, name );
440     }
441     
442   
443     private HTMLCollectionImpl _rows;
444     
445     
446     private HTMLCollectionImpl _bodies;
447   
448     
449 }
450
451
Popular Tags