001: package jdepend.swingui;
002:
003: import java.awt.*;
004: import javax.swing.*;
005: import javax.swing.tree.*;
006: import javax.swing.event.*;
007:
008: import jdepend.framework.JavaPackage;
009:
010: /**
011: * The <code>DependTree</code> class defines the graphical tree for displaying
012: * the packages and their hierarchical dependencies.
013: *
014: * @author <b>Mike Clark</b>
015: * @author Clarkware Consulting, Inc.
016: */
017:
018: public class DependTree extends JPanel implements TreeSelectionListener {
019:
020: private JTree tree;
021:
022: private DependTreeModel model;
023:
024: /**
025: * Constructs a <code>DependTree</code> with an empty tree model.
026: */
027: public DependTree() {
028: this (new DependTreeModel(new AfferentNode(null,
029: new JavaPackage(""))));
030: }
031:
032: /**
033: * Constructs a <code>DependTree</code> with the specified tree model.
034: *
035: * @param model Depend tree model.
036: */
037: public DependTree(DependTreeModel model) {
038:
039: setBorder(BorderFactory.createTitledBorder(model.getRoot()
040: .toString()));
041:
042: setModel(model);
043:
044: setLayout(new BorderLayout());
045:
046: JScrollPane pane = createScrollPane();
047:
048: add(pane, "Center");
049: }
050:
051: /**
052: * Sets the tree model.
053: *
054: * @param model Tree model.
055: */
056: public void setModel(DependTreeModel model) {
057: this .model = model;
058: setBorder(BorderFactory.createTitledBorder(model.getRoot()
059: .toString()));
060: getTree().setModel(this .model);
061:
062: }
063:
064: /**
065: * Returns the tree model.
066: *
067: * @return Tree model.
068: */
069: public DependTreeModel getModel() {
070: return (DependTreeModel) getTree().getModel();
071: }
072:
073: /**
074: * Registers the specified listener with this tree.
075: *
076: * @param l Tree selection listener.
077: */
078: public void addTreeSelectionListener(TreeSelectionListener l) {
079: getTree().addTreeSelectionListener(l);
080: }
081:
082: /**
083: * Callback method triggered whenever the value of the tree selection
084: * changes.
085: *
086: * @param te Event that characterizes the change.
087: */
088: public void valueChanged(TreeSelectionEvent te) {
089:
090: TreePath path = te.getNewLeadSelectionPath();
091:
092: if (path != null) {
093: Object o = path.getLastPathComponent();
094: }
095: }
096:
097: /**
098: * Creates and returns a scroll pane.
099: *
100: * @return Scroll pane.
101: */
102: private JScrollPane createScrollPane() {
103: JScrollPane pane = new JScrollPane(getTree());
104: return pane;
105: }
106:
107: /**
108: * Creates and returns a peered tree.
109: *
110: * @return Tree.
111: */
112: private JTree createTree() {
113:
114: JTree tree = new JTree();
115: tree.setShowsRootHandles(false);
116: tree.setFont(new Font("Dialog", Font.PLAIN, 12));
117: tree.addTreeSelectionListener(this );
118: tree.setRootVisible(false);
119: tree.setLargeModel(true);
120:
121: return tree;
122: }
123:
124: /*
125: * Returns the peered tree. @return A non-null tree.
126: */
127: private JTree getTree() {
128: if (tree == null) {
129: tree = createTree();
130: }
131:
132: return tree;
133: }
134: }
|