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.ui;
015:
016: import javax.swing.tree.TreeModel;
017: import javax.swing.tree.TreePath;
018: import org.itsnat.comp.ui.ItsNatTreeCellUI;
019: import org.itsnat.comp.ui.ItsNatTreeUI;
020: import org.itsnat.impl.core.ItsNatUserDataImpl;
021: import org.itsnat.impl.core.domutil.ElementTreeNodeImpl;
022: import org.itsnat.impl.core.util.*;
023: import org.w3c.dom.Element;
024:
025: /**
026: * En cierto modo el par ItsNatTreeCellUI/Impl es redundante pues se podría
027: * usar ElementTreeNode directanemte sin embargo nos sirve
028: * para sacar información del ElementTreeNode sin exponer
029: * métodos que puedan modificar el árbol, esto en los ItsNatTree es
030: * fundamental pues ha de modificarse el árbol (nuevos items etc)
031: * a través de los métodos normales de ItsNatTree, TreeModel etc,
032: * en ese contexto ItsNatTreeCellUI nos ofrece info del nodo a modo de "sólo lectura"
033: * NO debe usarse el ItsNatTreeCellUI obtenido antes de un cambio en la estructura
034: *
035: *
036: *
037: *
038: *
039: *
040: * @author jmarranz
041: */
042: public class ItsNatTreeCellUIImpl extends ItsNatUserDataImpl implements
043: ItsNatTreeCellUI {
044: protected ElementTreeNodeImpl treeNode;
045: protected ItsNatTreeUIImpl treeUI;
046: protected boolean expandState = true;
047:
048: /**
049: * Creates a new instance of ItsNatTreeCellUIImpl
050: */
051: private ItsNatTreeCellUIImpl(ItsNatTreeUIImpl treeUI,
052: ElementTreeNodeImpl treeNode) {
053: super (false);
054:
055: this .treeUI = treeUI;
056: this .treeNode = treeNode;
057: }
058:
059: public static ItsNatTreeCellUIImpl getItsNatTreeCellUI(
060: ItsNatTreeUIImpl treeUI, ElementTreeNodeImpl treeNode) {
061: if (treeNode == null)
062: return null; // puede ser el root y modo rootless
063: ItsNatTreeCellUIImpl treeNodeUI = (ItsNatTreeCellUIImpl) treeNode
064: .getAuxObject();
065: if (treeNodeUI == null) {
066: treeNodeUI = new ItsNatTreeCellUIImpl(treeUI, treeNode);
067: treeNode.setAuxObject(treeNodeUI);
068: }
069: return treeNodeUI;
070: }
071:
072: public ItsNatTreeUI getItsNatTreeUI() {
073: return treeUI;
074: }
075:
076: public ElementTreeNodeImpl getElementTreeNode() {
077: // No publicar
078: return treeNode;
079: }
080:
081: public ItsNatTreeCellUI getTreeNodeUIParent() {
082: ElementTreeNodeImpl treeNodeParent = (ElementTreeNodeImpl) treeNode
083: .getElementTreeNodeParent();
084: return getItsNatTreeCellUI(treeUI, treeNodeParent);
085: }
086:
087: public Element getParentElement() {
088: return treeNode.getParentElement();
089: }
090:
091: public Element getContentElement() {
092: return treeUI.getContentElement(treeNode);
093: }
094:
095: public Element getHandleElement() {
096: return treeUI.getHandleElement(treeNode);
097: }
098:
099: public Element getIconElement() {
100: return treeUI.getIconElement(treeNode);
101: }
102:
103: public Element getLabelElement() {
104: return treeUI.getLabelElement(treeNode);
105: }
106:
107: public int getIndex() {
108: return treeNode.getIndex();
109: }
110:
111: public int getChildCount() {
112: return treeUI.getChildCount(treeNode);
113: }
114:
115: public ItsNatTreeCellUI getChildItsNatTreeCellUIAt(int index) {
116: return treeUI.getChildItsNatTreeCellUIAt(index, treeNode);
117: }
118:
119: public int getRow() {
120: return treeUI.getRow(treeNode);
121: }
122:
123: public ItsNatTreeCellUI[] getTreeNodeUIPath() {
124: ItsNatTreeCellUI nodeInfoCurr;
125:
126: int count = 1;
127: nodeInfoCurr = this ;
128: while (nodeInfoCurr.getTreeNodeUIParent() != null) {
129: count++;
130: nodeInfoCurr = nodeInfoCurr.getTreeNodeUIParent();
131: }
132:
133: ItsNatTreeCellUI[] path = new ItsNatTreeCellUI[count];
134: nodeInfoCurr = this ;
135: for (int i = path.length - 1; i >= 0; i--) {
136: path[i] = nodeInfoCurr;
137: nodeInfoCurr = nodeInfoCurr.getTreeNodeUIParent();
138: }
139: return path;
140: }
141:
142: public int getDeepLevel() {
143: return treeNode.getDeepLevel();
144: }
145:
146: public TreePath getTreePath() {
147: // No memorizamos el path calculado porque puede cambiar en cualquier momento al cambiar el árbol
148: return calcTreePath();
149: }
150:
151: public TreePath calcTreePath() {
152: TreeModel dataModel = treeUI.getItsNatTree().getTreeModel();
153: ItsNatTreeCellUI[] infoPath = getTreeNodeUIPath();
154: int pathLen = infoPath.length;
155: if (treeUI.isRootless())
156: pathLen++;
157: Object[] path = new Object[pathLen];
158: Object parentNode = dataModel.getRoot();
159: path[0] = parentNode;
160: int first, delta;
161: if (treeUI.isRootless()) {
162: first = 0;
163: delta = 1;
164: } else {
165: first = 1;
166: delta = 0;
167: }
168: for (int i = first; i < infoPath.length; i++) {
169: int index = infoPath[i].getIndex();
170: parentNode = dataModel.getChild(parentNode, index);
171: path[i + delta] = parentNode;
172: }
173: return new TreePath(path);
174: }
175:
176: public void expand(boolean expandState) {
177: this .expandState = expandState;
178: }
179:
180: public boolean isExpanded() {
181: return expandState;
182: }
183:
184: }
|