01: package net.sourceforge.pmd.util.viewer.gui;
02:
03: import net.sourceforge.pmd.ast.Node;
04: import net.sourceforge.pmd.ast.SimpleNode;
05: import net.sourceforge.pmd.util.viewer.gui.menu.ASTNodePopupMenu;
06: import net.sourceforge.pmd.util.viewer.model.ASTModel;
07: import net.sourceforge.pmd.util.viewer.model.ViewerModel;
08: import net.sourceforge.pmd.util.viewer.model.ViewerModelEvent;
09: import net.sourceforge.pmd.util.viewer.model.ViewerModelListener;
10: import net.sourceforge.pmd.util.viewer.util.NLS;
11:
12: import javax.swing.*;
13: import javax.swing.event.TreeSelectionEvent;
14: import javax.swing.event.TreeSelectionListener;
15: import javax.swing.tree.TreeNode;
16: import javax.swing.tree.TreePath;
17: import java.awt.BorderLayout;
18: import java.awt.event.MouseAdapter;
19: import java.awt.event.MouseEvent;
20: import java.util.LinkedList;
21:
22: /**
23: * tree panel GUI
24: *
25: * @author Boris Gruschko ( boris at gruschko.org )
26: * @version $Id: ASTPanel.java 4983 2007-01-27 21:26:11Z xlv $
27: */
28:
29: public class ASTPanel extends JPanel implements ViewerModelListener,
30: TreeSelectionListener {
31: private ViewerModel model;
32: private JTree tree;
33:
34: /**
35: * constructs the panel
36: *
37: * @param model model to attach the panel to
38: */
39: public ASTPanel(ViewerModel model) {
40: this .model = model;
41: init();
42: }
43:
44: private void init() {
45: model.addViewerModelListener(this );
46: setBorder(BorderFactory.createTitledBorder(BorderFactory
47: .createEtchedBorder(), NLS.nls("AST.PANEL.TITLE")));
48: setLayout(new BorderLayout());
49: tree = new JTree((TreeNode) null);
50: tree.addTreeSelectionListener(this );
51: tree.addMouseListener(new MouseAdapter() {
52: public void mouseReleased(MouseEvent e) {
53: if (e.isPopupTrigger()) {
54: TreePath path = tree.getClosestPathForLocation(e
55: .getX(), e.getY());
56: tree.setSelectionPath(path);
57: JPopupMenu menu = new ASTNodePopupMenu(model,
58: (SimpleNode) path.getLastPathComponent());
59: menu.show(tree, e.getX(), e.getY());
60: }
61: }
62: });
63:
64: add(new JScrollPane(tree), BorderLayout.CENTER);
65: }
66:
67: /**
68: * @see ViewerModelListener#viewerModelChanged(ViewerModelEvent)
69: */
70: public void viewerModelChanged(ViewerModelEvent e) {
71: switch (e.getReason()) {
72: case ViewerModelEvent.CODE_RECOMPILED:
73: tree.setModel(new ASTModel(model.getRootNode()));
74: break;
75: case ViewerModelEvent.NODE_SELECTED:
76: if (e.getSource() != this ) {
77: LinkedList<Node> list = new LinkedList<Node>();
78: for (Node n = (Node) e.getParameter(); n != null; n = n
79: .jjtGetParent()) {
80: list.addFirst(n);
81: }
82: TreePath path = new TreePath(list.toArray());
83: tree.setSelectionPath(path);
84: tree.scrollPathToVisible(path);
85: }
86: break;
87: }
88: }
89:
90: /**
91: * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent)
92: */
93: public void valueChanged(TreeSelectionEvent e) {
94: model.selectNode((SimpleNode) e.getNewLeadSelectionPath()
95: .getLastPathComponent(), this);
96: }
97: }
|