001: /*
002: * $Header: /cvs/j3dfly/J3dEditor/src/org/jdesktop/j3dedit/treelayout/TreePanel.java,v 1.1 2005/04/20 22:21:33 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 javax.swing.JPanel;
022:
023: /**
024: * @author Paul Byrne
025: * @version $Id: TreePanel.java,v 1.1 2005/04/20 22:21:33 paulby Exp $
026: */
027: public class TreePanel extends JPanel {
028: private TreeNode root;
029:
030: private int width = 300;
031:
032: private int height = 300;
033:
034: private int xOrigin = 0; // Origin of canvas, used when subtreeSize
035: private int yOrigin = 0; // origin is not 0,0
036:
037: private static int MARGIN = 15;
038:
039: private double scale = 1.0;
040:
041: private TreeLayout treeLayout;
042:
043: /** Construct a tree panel using SimpleTreeLayout
044: */
045: public TreePanel() {
046: this (new SimpleTreeLayout());
047: }
048:
049: /** Create a panel with the specified layout manager
050: * @param treeLayout The treeLayout manager to use
051: * @see TreeLayout.CircularTreeLayout
052: * @see TreeLayout.SimpleTreeLayout
053: */
054: public TreePanel(TreeLayout treeLayout) {
055: super ();
056: this .treeLayout = treeLayout;
057: }
058:
059: public void setTree(TreeNode root) {
060: this .root = root;
061: root.setContainer(this );
062: }
063:
064: public TreeNode getTree() {
065: return root;
066: }
067:
068: /** Set the treeLayout strategy
069: * @see SimpleTreeLayout, CircularTreeLayout
070: */
071: public void setTreeLayout(TreeLayout treeLayout) {
072: this .treeLayout = treeLayout;
073: }
074:
075: public TreeLayout getTreeLayout() {
076: return treeLayout;
077: }
078:
079: public void layoutTree() {
080: treeLayout.doLayout(root);
081: width = (int) (root.getSubtreeSize().width * scale);
082: height = (int) (root.getSubtreeSize().height * scale);
083: xOrigin = root.getSubtreeSize().x;
084: yOrigin = root.getSubtreeSize().y;
085: }
086:
087: /** Search the tree and return the node that contains the point
088: * x,y. Returns null if no node is found
089: */
090: public TreeNode pickNode(int x, int y) {
091: if (root == null)
092: return null;
093:
094: x = (int) (x / scale) + xOrigin;
095: y = (int) (y / scale) + yOrigin;
096:
097: return treeLayout.doPick(root, x, y);
098: }
099:
100: public double getScale() {
101: return scale;
102: }
103:
104: /**
105: * Scale must be <= 1.0
106: */
107: public void setScale(double scale) {
108: this .scale = scale;
109:
110: if (scale > 1.0)
111: throw new RuntimeException("Illegal scale " + scale);
112:
113: width = (int) (root.getSubtreeSize().width * scale);
114: height = (int) (root.getSubtreeSize().height * scale);
115:
116: setSize(width, height);
117:
118: repaint();
119: }
120:
121: public void scrollRectToVisible(Rectangle rect) {
122: System.out.println("ScrollRectToVisible " + rect);
123:
124: // TODO fix this to correctly handle scale
125:
126: rect.x -= xOrigin;
127: rect.y -= yOrigin;
128: super .scrollRectToVisible(rect);
129: }
130:
131: public void paintComponent(Graphics g) {
132: super .paintComponent(g);
133: // Should check and ensure we don't paint over the
134: // borders of the component
135: Graphics2D g2 = (Graphics2D) g.create();
136:
137: g2.scale(scale, scale);
138: g2.translate(-xOrigin, -yOrigin);
139:
140: //System.out.println("Paint scale "+scale+" "+(-xOrigin)+", "+(-yOrigin)+" w "+width+" h "+height);
141:
142: if (root != null)
143: root.paint(g2);
144:
145: g2.dispose();
146: }
147:
148: public Dimension getPreferredSize() {
149: return new Dimension(width + MARGIN, height + MARGIN);
150: }
151:
152: }
|