001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.html.dom;
018:
019: import org.w3c.dom.Node;
020: import org.w3c.dom.NodeList;
021: import org.w3c.dom.html.HTMLCollection;
022: import org.w3c.dom.html.HTMLElement;
023: import org.w3c.dom.html.HTMLTableCellElement;
024: import org.w3c.dom.html.HTMLTableElement;
025: import org.w3c.dom.html.HTMLTableRowElement;
026: import org.w3c.dom.html.HTMLTableSectionElement;
027:
028: /**
029: * @xerces.internal
030: * @version $Revision: 449313 $ $Date: 2006-09-23 18:01:43 -0400 (Sat, 23 Sep 2006) $
031: * @author <a href="mailto:arkin@exoffice.com">Assaf Arkin</a>
032: * @see org.w3c.dom.html.HTMLTableRowElement
033: * @see org.apache.xerces.dom.ElementImpl
034: */
035: public class HTMLTableRowElementImpl extends HTMLElementImpl implements
036: HTMLTableRowElement {
037:
038: private static final long serialVersionUID = 5409562635656244263L;
039:
040: public int getRowIndex() {
041: Node parent;
042:
043: parent = getParentNode();
044: if (parent instanceof HTMLTableSectionElement) {
045: parent = parent.getParentNode();
046: }
047: if (parent instanceof HTMLTableElement) {
048: return getRowIndex(parent);
049: }
050: return -1;
051: }
052:
053: public void setRowIndex(int rowIndex) {
054: Node parent;
055:
056: parent = getParentNode();
057: if (parent instanceof HTMLTableSectionElement) {
058: parent = parent.getParentNode();
059: }
060: if (parent instanceof HTMLTableElement) {
061: ((HTMLTableElementImpl) parent).insertRowX(rowIndex, this );
062: }
063: }
064:
065: public int getSectionRowIndex() {
066: Node parent;
067:
068: parent = getParentNode();
069: if (parent instanceof HTMLTableSectionElement) {
070: return getRowIndex(parent);
071: }
072: return -1;
073: }
074:
075: public void setSectionRowIndex(int sectionRowIndex) {
076: Node parent;
077:
078: parent = getParentNode();
079: if (parent instanceof HTMLTableSectionElement) {
080: ((HTMLTableSectionElementImpl) parent).insertRowX(
081: sectionRowIndex, this );
082: }
083: }
084:
085: int getRowIndex(Node parent) {
086: NodeList rows;
087: int i;
088:
089: // Use getElementsByTagName() which creates a snapshot of all the
090: // TR elements under the TABLE/section. Access to the returned NodeList
091: // is very fast and the snapshot solves many synchronization problems.
092: rows = ((HTMLElement) parent).getElementsByTagName("TR");
093: for (i = 0; i < rows.getLength(); ++i) {
094: if (rows.item(i) == this ) {
095: return i;
096: }
097: }
098: return -1;
099: }
100:
101: public HTMLCollection getCells() {
102: if (_cells == null) {
103: _cells = new HTMLCollectionImpl(this ,
104: HTMLCollectionImpl.CELL);
105: }
106: return _cells;
107: }
108:
109: public void setCells(HTMLCollection cells) {
110: Node child;
111: int i;
112:
113: child = getFirstChild();
114: while (child != null) {
115: removeChild(child);
116: child = child.getNextSibling();
117: }
118: i = 0;
119: child = cells.item(i);
120: while (child != null) {
121: appendChild(child);
122: ++i;
123: child = cells.item(i);
124: }
125: }
126:
127: public HTMLElement insertCell(int index) {
128: Node child;
129: HTMLElement newCell;
130:
131: newCell = new HTMLTableCellElementImpl(
132: (HTMLDocumentImpl) getOwnerDocument(), "TD");
133: child = getFirstChild();
134: while (child != null) {
135: if (child instanceof HTMLTableCellElement) {
136: if (index == 0) {
137: insertBefore(newCell, child);
138: return newCell;
139: }
140: --index;
141: }
142: child = child.getNextSibling();
143: }
144: appendChild(newCell);
145: return newCell;
146: }
147:
148: public void deleteCell(int index) {
149: Node child;
150:
151: child = getFirstChild();
152: while (child != null) {
153: if (child instanceof HTMLTableCellElement) {
154: if (index == 0) {
155: removeChild(child);
156: return;
157: }
158: --index;
159: }
160: child = child.getNextSibling();
161: }
162: }
163:
164: public String getAlign() {
165: return capitalize(getAttribute("align"));
166: }
167:
168: public void setAlign(String align) {
169: setAttribute("align", align);
170: }
171:
172: public String getBgColor() {
173: return getAttribute("bgcolor");
174: }
175:
176: public void setBgColor(String bgColor) {
177: setAttribute("bgcolor", bgColor);
178: }
179:
180: public String getCh() {
181: String ch;
182:
183: // Make sure that the access key is a single character.
184: ch = getAttribute("char");
185: if (ch != null && ch.length() > 1) {
186: ch = ch.substring(0, 1);
187: }
188: return ch;
189: }
190:
191: public void setCh(String ch) {
192: // Make sure that the access key is a single character.
193: if (ch != null && ch.length() > 1) {
194: ch = ch.substring(0, 1);
195: }
196: setAttribute("char", ch);
197: }
198:
199: public String getChOff() {
200: return getAttribute("charoff");
201: }
202:
203: public void setChOff(String chOff) {
204: setAttribute("charoff", chOff);
205: }
206:
207: public String getVAlign() {
208: return capitalize(getAttribute("valign"));
209: }
210:
211: public void setVAlign(String vAlign) {
212: setAttribute("valign", vAlign);
213: }
214:
215: /**
216: * Explicit implementation of cloneNode() to ensure that cache used
217: * for getCells() gets cleared.
218: */
219: public Node cloneNode(boolean deep) {
220: HTMLTableRowElementImpl clonedNode = (HTMLTableRowElementImpl) super
221: .cloneNode(deep);
222: clonedNode._cells = null;
223: return clonedNode;
224: }
225:
226: /**
227: * Constructor requires owner document.
228: *
229: * @param owner The owner HTML document
230: */
231: public HTMLTableRowElementImpl(HTMLDocumentImpl owner, String name) {
232: super (owner, name);
233: }
234:
235: HTMLCollection _cells;
236:
237: }
|