001: /*
002: ItsNat Java Web Application Framework
003: Copyright (C) 2007 Innowhere Software Services S.L., Spanish Company
004: Author: Jose Maria Arranz Santamaria
005:
006: This program is free software: you can redistribute it and/or modify
007: it under the terms of the GNU Affero General Public License as published by
008: the Free Software Foundation, either version 3 of the License, or
009: (at your option) any later version. See the GNU Affero General Public
010: License for more details. See the copy of the GNU Affero General Public License
011: included in this program. If not, see <http://www.gnu.org/licenses/>.
012: */
013:
014: package org.itsnat.impl.comp;
015:
016: import org.itsnat.comp.ItsNatTable;
017: import org.itsnat.comp.ItsNatTableHeader;
018: import org.itsnat.comp.ItsNatTableStructure;
019: import org.itsnat.core.ItsNatDOMException;
020: import org.itsnat.core.domutil.ItsNatTreeWalker;
021: import org.itsnat.impl.core.http.ItsNatImpl;
022: import org.itsnat.impl.core.domutil.ElementListStructureDefaultImpl;
023: import org.itsnat.impl.core.domutil.ElementTableStructureDefaultImpl;
024: import org.w3c.dom.Element;
025: import org.w3c.dom.html.HTMLTableElement;
026: import org.w3c.dom.html.HTMLTableSectionElement;
027:
028: /**
029: *
030: * @author jmarranz
031: */
032: public class ItsNatTableStructureDefaultImpl implements
033: ItsNatTableStructure {
034: protected final static ItsNatTableStructureDefaultImpl SINGLETON = new ItsNatTableStructureDefaultImpl();
035:
036: /** Creates a new instance of ItsNatTableStructureDefaultImpl */
037: private ItsNatTableStructureDefaultImpl() {
038: }
039:
040: public static ItsNatTableStructureDefaultImpl newItsNatTableStructureDefault() {
041: // No se guarda estado, usamos el SINGLETON en este falso método factoría
042: return SINGLETON;
043: }
044:
045: public Element getHeadElement(ItsNatTable table, Element tableElem) {
046: if (tableElem == null)
047: tableElem = table.getElement();
048: if (tableElem instanceof HTMLTableElement) {
049: HTMLTableElement htmlTableElem = (HTMLTableElement) tableElem;
050: return htmlTableElem.getTHead(); // Si no tiene <thead> devolverá null
051: } else {
052: Element firstChild = ItsNatTreeWalker
053: .getFirstChildElement(tableElem);
054: Element secondChild = ItsNatTreeWalker
055: .getNextSiblingElement(firstChild); // Si no es null es el body
056: if (secondChild == null) // No hay header, sólo body, firstChild es la fila patrón
057: return null;
058: else
059: return firstChild; // secondChild es el body
060: }
061: }
062:
063: public Element getBodyElement(ItsNatTable table, Element tableElem) {
064: if (tableElem == null)
065: tableElem = table.getElement();
066: if (tableElem instanceof HTMLTableElement) {
067: HTMLTableElement htmlTableElem = (HTMLTableElement) tableElem;
068:
069: // Buscamos el tbody, debe existir
070: HTMLTableSectionElement tBody;
071: if (ItsNatImpl.isOldXerces()) // En el Xerces 2.6.2 (el de la JVM 1.5) no funciona bien el getTBodies
072: tBody = (HTMLTableSectionElement) ItsNatTreeWalker
073: .getFirstChildElementWithTag(htmlTableElem,
074: "TBODY");
075: else
076: tBody = (HTMLTableSectionElement) htmlTableElem
077: .getTBodies().item(0);
078: if (tBody == null)
079: throw new ItsNatDOMException("Missing <tbody>",
080: htmlTableElem);
081:
082: return tBody;
083: } else {
084: // En el caso de tabla sólo con body, éste sólo debería tener una sóla fila de patrón
085: Element firstChild = ItsNatTreeWalker
086: .getFirstChildElement(tableElem);
087: Element secondChild = ItsNatTreeWalker
088: .getNextSiblingElement(firstChild);
089: if (secondChild == null) // No hay header, sólo body, firstChild es la fila patrón
090: return tableElem;
091: else
092: return secondChild; // firstChild es el header
093: }
094: }
095:
096: public Element getHeaderColumnContentElement(
097: ItsNatTableHeader tableHeader, int index, Element parentElem) {
098: if (parentElem == null)
099: parentElem = tableHeader.getItsNatTableHeaderUI()
100: .getElementAt(index);
101: return ElementListStructureDefaultImpl.getContentElement(index,
102: parentElem);
103: }
104:
105: public Element getRowContentElement(ItsNatTable table, int row,
106: Element rowElem) {
107: if (rowElem == null)
108: rowElem = table.getItsNatTableUI().getRowElementAt(row);
109: return ElementTableStructureDefaultImpl.getRowContentElement(
110: row, rowElem);
111: }
112:
113: public Element getCellContentElement(ItsNatTable table, int row,
114: int col, Element cellElem) {
115: if (cellElem == null)
116: cellElem = table.getItsNatTableUI().getCellElementAt(row,
117: col);
118: return ElementTableStructureDefaultImpl.getCellContentElement(
119: row, col, cellElem);
120: }
121:
122: }
|