001: /*
002: * $Header: /cvs/j3dfly/J3dEditor/src/org/jdesktop/j3dedit/treelayout/DefaultTreeNode.java,v 1.1 2005/04/20 22:21:32 paulby Exp $
003: *
004: * Sun Public License Notice
005: *
006: * The contents of this file are subject to the Sun Public License Version
007: * 1.0 (the "License"). You may not use this file except in compliance with
008: * the License. A copy of the License is available at http://www.sun.com/
009: *
010: * The Original Code is the Java 3D(tm) Scene Graph Editor.
011: * The Initial Developer of the Original Code is Paul Byrne.
012: * Portions created by Paul Byrne are Copyright (C) 2002.
013: * All Rights Reserved.
014: *
015: * Contributor(s): Paul Byrne.
016: *
017: **/
018: package org.jdesktop.j3dedit.treelayout;
019:
020: import java.awt.*;
021: import java.util.Vector;
022:
023: /**
024: * A simple Default TreeNode, just draws the node as a rectangle
025: *
026: * @author Paul Byrne
027: * @version $Id: DefaultTreeNode.java,v 1.1 2005/04/20 22:21:32 paulby Exp $
028: */
029: public class DefaultTreeNode implements TreeNode {
030:
031: Vector children = new Vector();
032:
033: protected TreePanel container;
034: TreeNode parent;
035: Rectangle subtreeSize = new Rectangle();
036: Link link;
037: protected Dimension nodeSize;
038: protected Point computedPosition = new Point();
039: Dimension preferredSize;
040:
041: private Rectangle pickArea;
042: protected Point linkExitPoint = new Point();
043: protected Point linkEntryPoint = new Point();
044:
045: protected LayoutData layoutData;
046:
047: public DefaultTreeNode() {
048: nodeSize = new Dimension(10, 10);
049: preferredSize = new Dimension(nodeSize.width + 6,
050: nodeSize.height);
051: pickArea = new Rectangle(nodeSize);
052: link = null;
053: }
054:
055: public DefaultTreeNode(int width, int height, int spacing) {
056: nodeSize = new Dimension(width, height);
057: preferredSize = new Dimension(nodeSize.width * 10 / 6,
058: nodeSize.height);
059: pickArea = new Rectangle(nodeSize);
060: link = null;
061: }
062:
063: public void setLink(Link link) {
064: this .link = link;
065: }
066:
067: public Link getLink() {
068: return link;
069: }
070:
071: public void setContainer(TreePanel panel) {
072: container = panel;
073:
074: for (int i = 0; i < numChildren(); i++)
075: getChild(i).setContainer(panel);
076: }
077:
078: public TreePanel getContainer() {
079: return container;
080: }
081:
082: public int numChildren() {
083: return children.size();
084: }
085:
086: public TreeNode getChild(int index) {
087: return (TreeNode) children.get(index);
088: }
089:
090: public void addChild(TreeNode child) {
091: children.addElement(child);
092: child.setParent(this );
093: }
094:
095: public void removeChild(TreeNode child) {
096: if (!children.remove(child))
097: throw (new RuntimeException(
098: "Attempt to Remove non-existant child"));
099: child.setParent(null);
100: }
101:
102: public int getNodeCount() {
103: int count = 1; // 1 for this node
104:
105: if (numChildren() > 0) {
106: for (int i = 0; i < numChildren(); i++)
107: count += getChild(i).getNodeCount();
108: }
109:
110: return count;
111: }
112:
113: public TreeNode getParent() {
114: return parent;
115: }
116:
117: public void setParent(TreeNode parent) {
118: this .parent = parent;
119: }
120:
121: public Point getLinkExit() {
122: return linkExitPoint;
123: }
124:
125: public Point getLinkEntry() {
126: return linkEntryPoint;
127: }
128:
129: public Point getComputedPosition() {
130: return computedPosition;
131: }
132:
133: public void setComputedPosition(Point pos) {
134: computedPosition = pos;
135: pickArea.x = pos.x;
136: pickArea.y = pos.y;
137:
138: computeLinkPoints(pos);
139: }
140:
141: public void computeLinkPoints(Point pos) {
142: linkExitPoint.x = pos.x + nodeSize.width / 2;
143: linkExitPoint.y = pos.y + nodeSize.height;
144:
145: linkEntryPoint.x = pos.x + nodeSize.width / 2;
146: linkEntryPoint.y = pos.y;
147: }
148:
149: public Rectangle getSubtreeSize() {
150: return subtreeSize;
151: }
152:
153: public void setSubtreeSize(Rectangle size) {
154: subtreeSize = size;
155: }
156:
157: public Dimension getPreferredSize() {
158: return preferredSize;
159: }
160:
161: public Dimension getMinimumSize() {
162: return nodeSize;
163: }
164:
165: public Dimension getNodeSize() {
166: return nodeSize;
167: }
168:
169: public void paint(Graphics g) {
170: drawNode(g);
171:
172: if (numChildren() > 0) {
173: link.paint(g);
174: g.setColor(Color.red);
175: g.drawRect(subtreeSize.x, subtreeSize.y, subtreeSize.width,
176: subtreeSize.height);
177: g.setColor(Color.black);
178: }
179:
180: for (int i = 0; i < numChildren(); i++)
181: getChild(i).paint(g);
182: }
183:
184: /**
185: * Subclasses should override this method to draw different shapes
186: * to represent this node
187: */
188: public void drawNode(Graphics g) {
189: g.drawRect(computedPosition.x, computedPosition.y,
190: nodeSize.width, nodeSize.height);
191: }
192:
193: public boolean picked(int x, int y) {
194: if (pickArea.contains(x, y))
195: return true;
196: else
197: return false;
198: }
199:
200: public void setLayoutData(LayoutData layoutData) {
201: this .layoutData = layoutData;
202: }
203:
204: public LayoutData getLayoutData() {
205: return layoutData;
206: }
207:
208: }
|