001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.xslt.mapper.model.nodes;
021:
022: import java.awt.Color;
023: import java.awt.Image;
024: import java.util.List;
025: import javax.swing.JPopupMenu;
026: import javax.swing.tree.TreePath;
027: import org.netbeans.modules.soa.mapper.common.IMapperNode;
028: import org.netbeans.modules.soa.mapper.common.basicmapper.tree.IMapperTreeNode;
029: import org.netbeans.modules.xml.axi.AXIComponent;
030: import org.netbeans.modules.xslt.mapper.view.XsltMapper;
031:
032: /**
033: *
034: * @author radval
035: *
036: */
037: public abstract class TreeNode extends Node {
038:
039: private TreeNode parent;
040: private List<TreeNode> children;
041:
042: private List<TreeNode> oldChildren;
043:
044: protected abstract List<TreeNode> loadChildren();
045:
046: public abstract AXIComponent getType();
047:
048: protected TreeNode(Object dataObject, XsltMapper mapper) {
049: super (dataObject, mapper);
050: }
051:
052: /**
053: * Gets the parent node for this node.
054: *
055: * @return the parent node, or null if none
056: */
057: public TreeNode getParent() {
058: return this .parent;
059: }
060:
061: public void setParent(TreeNode parent) {
062: this .parent = parent;
063: }
064:
065: public List<TreeNode> getChildren() {
066: if (children == null) {
067: children = loadChildren();
068:
069: /**
070: * The most important part in whole project
071: * when tree is being reloaded, try to reuse all nodes as much as possible
072: */
073: if (oldChildren != null) {
074: for (int pos1 = 0; pos1 < children.size(); pos1++) {
075: TreeNode new_child = children.get(pos1);
076:
077: for (int pos2 = 0; pos2 < oldChildren.size(); pos2++) {
078: TreeNode old_child = oldChildren.get(pos2);
079: if (old_child == null) {
080: continue;
081: }
082:
083: if (new_child.getDataObject() == old_child
084: .getDataObject()) {
085: children.set(pos1, old_child);
086: oldChildren.set(pos2, null);
087: break;
088: }
089: }
090:
091: }
092: for (TreeNode oldChild : oldChildren) {
093:
094: if (oldChild != null) {
095: oldChild.removeFromTree();
096: }
097: }
098: oldChildren = null;
099: }
100:
101: }
102: return children;
103: }
104:
105: public void reload() {
106: this .oldChildren = this .children;
107: this .children = null;
108: if (this .oldChildren != null) {
109: for (TreeNode n : this .oldChildren) {
110: n.reload();
111: }
112: }
113:
114: }
115:
116: /**
117: * Whether this tree node is mappable.
118: * A tree node that is not mappable cannot be linked to or from.
119: */
120: public abstract boolean isMappable();
121:
122: /**
123: * The current highlight color, i.e. from a search.
124: */
125: public Color getHighlightColor() {
126: return new Color(0);
127: }
128:
129: /**
130: * This Icon can be used to represent the node in a tree view.
131: */
132: public Image getIcon() {
133: return null;
134: }
135:
136: public String getName() {
137: return toString();
138: }
139:
140: /**
141: * Does quite the same like the getName() method does.
142: * It is intended to provide different HTML text depend on the parameter value.
143: */
144: public String getName(boolean selected) {
145: return getName();
146: }
147:
148: public static TreeNode getNode(IMapperTreeNode node) {
149: return (TreeNode) node.getPath().getLastPathComponent();
150: }
151:
152: public IMapperNode getMapperNode() {
153:
154: return getMapper().getMapperNode(this );
155: }
156:
157: public void setMapperNode(IMapperNode node) {
158:
159: assert false : "Linking with tree nodes is not allowed. Tree nodes created/removed dynamically";
160: }
161:
162: public static TreePath getTreePath(TreeNode node) {
163: if (node.getParent() != null) {
164: return getTreePath(node.getParent())
165: .pathByAddingChild(node);
166: }
167: return new TreePath(node);
168: }
169:
170: public IMapperNode getOutputNode() {
171: return this .getMapperNode();
172: }
173:
174: public IMapperNode getInputNode(Node node) {
175: return this .getMapperNode();
176: }
177:
178: public JPopupMenu constructPopupMenu() {
179: return null;
180: }
181:
182: public boolean isSourceViewNode() {
183: return (((IMapperTreeNode) getMapperNode()) != null)
184: && ((IMapperTreeNode) getMapperNode())
185: .isSourceTreeNode();
186: }
187:
188: public void removeFromTree() {
189:
190: getMapper().getBuilder().destroyDiagramRecursive(this );
191:
192: if (oldChildren != null) {
193: for (TreeNode child : oldChildren) {
194: child.removeFromTree();
195: }
196: }
197:
198: if (children != null) {
199: for (TreeNode child : children) {
200: child.removeFromTree();
201: }
202: }
203: parent = null;
204: children = null;
205: oldChildren = null;
206: }
207: }
|