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.core.domutil;
015:
016: import org.itsnat.core.ItsNatDocument;
017: import org.itsnat.core.ItsNatException;
018: import org.itsnat.core.domutil.ElementTreeNodeRenderer;
019: import org.itsnat.core.domutil.ElementTree;
020: import org.itsnat.core.domutil.ElementTreeNode;
021: import org.itsnat.core.domutil.ElementTreeNodeStructure;
022: import org.itsnat.impl.core.ItsNatDocumentImpl;
023: import org.w3c.dom.Element;
024: import org.w3c.dom.Node;
025:
026: /**
027: *
028: * @author jmarranz
029: */
030: public class ElementTreeImpl implements ElementTree {
031: protected ElementTreeNodeListImpl rootContainerList; // Sirve para crear y borrar el único posible nodo: el root
032: protected boolean usePatternMarkupToRender;
033:
034: /**
035: * Creates a new instance of ElementTreeImpl
036: */
037: public ElementTreeImpl(ItsNatDocumentImpl itsNatDoc,
038: boolean treeTable, Element parentElement,
039: boolean removePattern, ElementTreeNodeStructure structure,
040: ElementTreeNodeRenderer renderer) {
041: this .rootContainerList = itsNatDoc
042: .createElementTreeNodeListInternal(treeTable,
043: parentElement, removePattern, structure,
044: renderer);
045:
046: if (!removePattern && (rootContainerList.getLength() > 1)) // Si es 1 es el nodo patrón que pasa a ser el root pero no debería haber nada más
047: throw new ItsNatException(
048: "A tree only can have a root node");
049:
050: this .usePatternMarkupToRender = itsNatDoc
051: .isUsePatternMarkupToRender();
052: }
053:
054: public boolean isUsePatternMarkupToRender() {
055: return usePatternMarkupToRender;
056: }
057:
058: public void setUsePatternMarkupToRender(
059: boolean usePatternMarkupToRender) {
060: this .usePatternMarkupToRender = usePatternMarkupToRender;
061: }
062:
063: public Element getTreeContainerElement() {
064: return rootContainerList.getTreeContainerElement();
065: }
066:
067: public ItsNatDocument getItsNatDocument() {
068: return rootContainerList.getItsNatDocument();
069: }
070:
071: public Element getParentElement() {
072: return rootContainerList.getParentElement();
073: }
074:
075: public boolean hasTreeNodeRoot() {
076: return !rootContainerList.isEmpty();
077: }
078:
079: public ElementTreeNode getRootNode() {
080: if (hasTreeNodeRoot())
081: return rootContainerList.getFirstTreeNode();
082: else
083: return null;
084: }
085:
086: public ElementTreeNode addRootNode() {
087: if (hasTreeNodeRoot())
088: throw new ItsNatException("Already has a root node");
089:
090: return rootContainerList.addTreeNode();
091: }
092:
093: public ElementTreeNode addRootNode(Object value) {
094: if (hasTreeNodeRoot())
095: throw new ItsNatException("Already has a root node");
096:
097: return rootContainerList.addTreeNode(value);
098: }
099:
100: public void removeRootNode() {
101: if (!hasTreeNodeRoot()) // Ya está quitado
102: return;
103:
104: rootContainerList.removeAllTreeNodes();
105: }
106:
107: public Element getRootPatternElement() {
108: return rootContainerList.getChildPatternElement();
109: }
110:
111: public ElementTreeNode getElementTreeNodeFromNode(Node node) {
112: return getElementTreeNodeFromNode(node,
113: getTreeContainerElement());
114: }
115:
116: public ElementTreeNode getElementTreeNodeFromNode(Node node,
117: Element treeContainerElem) {
118: ElementTreeNodeImpl rootNode = (ElementTreeNodeImpl) getRootNode();
119: if (rootNode == null)
120: return null;
121:
122: return rootNode.getElementTreeNodeFromNode(node,
123: treeContainerElem);
124: }
125:
126: public ElementTreeNode getElementTreeNodeFromPath(int[] path) {
127: // No se usa, para un posible uso recuperar.
128: ElementTreeNodeImpl rootNode = (ElementTreeNodeImpl) getRootNode();
129: if (rootNode == null)
130: return null;
131:
132: return rootNode.getElementTreeNodeFromPath(path);
133: }
134:
135: public ElementTreeNode getElementTreeNodeFromPath(int[] path,
136: int fromIndex) {
137: // No se usa, para un posible uso recuperar.
138: ElementTreeNodeImpl rootNode = (ElementTreeNodeImpl) getRootNode();
139: if (rootNode == null)
140: return null;
141:
142: return rootNode.getElementTreeNodeFromPath(path, fromIndex);
143: }
144:
145: public ElementTreeNode getElementTreeNodeFromRow(int row) {
146: ElementTreeNodeImpl rootNode = (ElementTreeNodeImpl) getRootNode();
147: if (rootNode == null)
148: return null; // No tiene root (el root es row = 0)
149:
150: if (row == 0)
151: return rootNode;
152:
153: return rootNode.getElementTreeNodeFromRow(row);
154: }
155:
156: public ElementTreeNodeRenderer getElementTreeNodeRenderer() {
157: return rootContainerList.getElementTreeNodeRenderer();
158: }
159:
160: public void setElementTreeNodeRenderer(
161: ElementTreeNodeRenderer renderer) {
162: rootContainerList.setElementTreeNodeRenderer(renderer);
163: }
164:
165: public ElementTreeNodeStructure getElementTreeNodeStructure() {
166: return rootContainerList.getElementTreeNodeStructure();
167: }
168:
169: public void setElementTreeNodeStructure(
170: ElementTreeNodeStructure structure) {
171: rootContainerList.setElementTreeNodeStructure(structure);
172: }
173:
174: public boolean containsUserValueName(String name) {
175: return rootContainerList.containsUserValueName(name);
176: }
177:
178: public Object removeUserValue(String name) {
179: return rootContainerList.removeUserValue(name);
180: }
181:
182: public Object getUserValue(String name) {
183: return rootContainerList.getUserValue(name);
184: }
185:
186: public Object setUserValue(String name, Object value) {
187: return rootContainerList.setUserValue(name, value);
188: }
189:
190: public String[] getUserValueNames() {
191: return rootContainerList.getUserValueNames();
192: }
193:
194: }
|