001: /*
002: * $Header: /cvs/j3dfly/J3dEditor/src/org/jdesktop/j3dedit/treelayout/SimpleTreeLink.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.*;
022:
023: /**
024: *
025: * @author Paul Byrne
026: * @version $Id: SimpleTreeLink.java,v 1.1 2005/04/20 22:21:32 paulby Exp $
027: */
028: public class SimpleTreeLink extends Link {
029:
030: private Rectangle linkSize;
031:
032: Line centerLine;
033: Line rootLine;
034: Vector childLines;
035:
036: /**
037: * Create the link with the specified parent
038: */
039: public SimpleTreeLink(TreeNode parent) {
040: this (parent, 30);
041: }
042:
043: /**
044: * Create the link with the specfied parent and
045: * set the vertical space occupied by the link to be <param>spacing</param>
046: */
047: public SimpleTreeLink(TreeNode parent, int spacing) {
048: super (parent, spacing);
049: childLines = new Vector();
050: centerLine = new Line(0, 0, 0, 0);
051: rootLine = new Line(0, 0, 0, 0);
052: linkSize = new Rectangle(0, 0, parent.getMinimumSize().width,
053: spacing);
054: }
055:
056: public void paint(Graphics g) {
057: centerLine.paint(g);
058: rootLine.paint(g);
059:
060: Enumeration e = childLines.elements();
061: while (e.hasMoreElements())
062: ((Line) e.nextElement()).paint(g);
063: }
064:
065: /**
066: * Called when the tree nodes have moved.
067: * Calculates and caches the new link geometry for this link only
068: */
069: public void updateLink() {
070: int cmin = Integer.MAX_VALUE;
071: int cmax = 0;
072: //int cy = parent.getLinkExit().y+spacing/2; // y of center line
073: int cy = parent.getComputedPosition().y
074: + parent.getMinimumSize().height + spacing / 2;
075: Point entry;
076: Line line;
077:
078: rootLine.set(parent.getLinkExit().x, parent.getLinkExit().y,
079: parent.getLinkExit().x, cy);
080:
081: // Remove any unused child links
082: if (childLines.size() > parent.numChildren())
083: childLines.setSize(parent.numChildren());
084:
085: for (int i = 0; i < parent.numChildren(); i++) {
086: entry = parent.getChild(i).getLinkEntry();
087: if (i >= childLines.size())
088: childLines.addElement(new Line(entry.x, cy, entry.x,
089: entry.y));
090: else {
091: line = (Line) childLines.elementAt(i);
092: line.set(entry.x, cy, entry.x, entry.y);
093: }
094:
095: cmin = Math.min(cmin, entry.x);
096: cmax = Math.max(cmax, entry.x);
097: }
098:
099: centerLine.set(cmin, cy, cmax, cy);
100: }
101:
102: /**
103: * Updates this link and all links in the subtree
104: */
105: public void updateAllLinks() {
106: updateLink();
107: for (int i = 0; i < parent.numChildren(); i++)
108: ((SimpleTreeLink) parent.getChild(i).getLink())
109: .updateAllLinks();
110: }
111:
112: /**
113: * Get the size of the link
114: */
115: public Rectangle getMinimumSize() {
116: return linkSize;
117: }
118:
119: public Rectangle getPreferredSize() {
120: return getMinimumSize();
121: }
122: }
123:
124: class Line {
125:
126: int x1, x2;
127: int y1, y2;
128:
129: public Line(int startX, int startY, int endX, int endY) {
130: x1 = startX;
131: y1 = startY;
132: x2 = endX;
133: y2 = endY;
134: }
135:
136: public void set(int startX, int startY, int endX, int endY) {
137: x1 = startX;
138: y1 = startY;
139: x2 = endX;
140: y2 = endY;
141: }
142:
143: public void paint(Graphics g) {
144: g.drawLine(x1, y1, x2, y2);
145: }
146: }
|