001: /*
002: * $Header: /cvs/j3dfly/J3dEditor/src/org/jdesktop/j3dedit/treelayout/CircularTreeLink.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.awt.geom.Arc2D;
022: import java.awt.geom.Line2D;
023: import java.util.*;
024:
025: /**
026: *
027: * @author Paul Byrne
028: * @version $Id%
029: */
030: public class CircularTreeLink extends Link {
031:
032: Arc2D.Double centerLine;
033: Line2D.Double rootLine;
034: Vector childLines;
035:
036: /**
037: * Create the link with the specified parent
038: */
039: public CircularTreeLink(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 CircularTreeLink(TreeNode parent, int spacing) {
048: super (parent, spacing);
049: childLines = new Vector();
050: centerLine = null;
051: rootLine = new Line2D.Double(0, 0, 0, 0);
052: }
053:
054: public void paint(Graphics g) {
055: Graphics2D g2d = (Graphics2D) g;
056:
057: if (centerLine != null)
058: g2d.draw(centerLine);
059:
060: g2d.draw(rootLine);
061:
062: Enumeration e = childLines.elements();
063: while (e.hasMoreElements()) {
064: g2d.draw((Line2D) e.nextElement());
065: }
066:
067: }
068:
069: /**
070: * Called when the tree nodes have moved.
071: * Calculates and caches the new link geometry for this link only
072: */
073: public void updateLink(double center, double angle, int level,
074: double levelRadius, double parentAngle) {
075: double centerRadius = levelRadius * level + levelRadius / 2; // Radius of center line
076: double firstChildAngle = ((CircularTreeLayoutData) parent
077: .getChild(0).getLayoutData()).angle;
078: double lastChildAngle = ((CircularTreeLayoutData) parent
079: .getChild(parent.numChildren() - 1).getLayoutData()).angle;
080: if (parent.numChildren() > 0) {
081: if (centerLine == null)
082: centerLine = new Arc2D.Double(Arc2D.OPEN);
083:
084: centerLine.setArcByCenter(center, center, centerRadius,
085: Math.toDegrees(firstChildAngle) - 90,
086: Math.toDegrees(lastChildAngle - firstChildAngle),
087: Arc2D.OPEN);
088: } else
089: centerLine = null;
090:
091: Point parentPos = parent.getComputedPosition();
092: rootLine.setLine(parentPos.x + parent.getMinimumSize().width
093: / 2, parentPos.y + parent.getMinimumSize().height / 2,
094:
095: center + Math.sin(parentAngle) * centerRadius, center
096: + Math.cos(parentAngle) * centerRadius);
097:
098: childLines.clear();
099:
100: TreeNode child;
101: double childAngle;
102:
103: for (int i = 0; i < parent.numChildren(); i++) {
104: child = parent.getChild(i);
105: childAngle = ((CircularTreeLayoutData) child
106: .getLayoutData()).angle;
107:
108: childLines.add(new Line2D.Double(center
109: + Math.sin(childAngle) * centerRadius, center
110: + Math.cos(childAngle) * centerRadius, center
111: + Math.sin(childAngle) * levelRadius * (level + 1),
112: center + Math.cos(childAngle) * levelRadius
113: * (level + 1)));
114: }
115:
116: }
117:
118: }
|