KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > util > viewer > gui > ASTPanel


1 package net.sourceforge.pmd.util.viewer.gui;
2
3 import net.sourceforge.pmd.ast.Node;
4 import net.sourceforge.pmd.ast.SimpleNode;
5 import net.sourceforge.pmd.util.viewer.gui.menu.ASTNodePopupMenu;
6 import net.sourceforge.pmd.util.viewer.model.ASTModel;
7 import net.sourceforge.pmd.util.viewer.model.ViewerModel;
8 import net.sourceforge.pmd.util.viewer.model.ViewerModelEvent;
9 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 JavaDoc;
14 import javax.swing.event.TreeSelectionListener JavaDoc;
15 import javax.swing.tree.TreeNode JavaDoc;
16 import javax.swing.tree.TreePath JavaDoc;
17 import java.awt.BorderLayout JavaDoc;
18 import java.awt.event.MouseAdapter JavaDoc;
19 import java.awt.event.MouseEvent JavaDoc;
20 import java.util.LinkedList JavaDoc;
21
22 /**
23  * tree panel GUI
24  *
25  * @author Boris Gruschko ( boris at gruschko.org )
26  * @version $Id: ASTPanel.java,v 1.11 2006/02/10 14:15:29 tomcopeland Exp $
27  */

28
29 public class ASTPanel extends JPanel implements ViewerModelListener, TreeSelectionListener JavaDoc {
30     private ViewerModel model;
31     private JTree tree;
32
33     /**
34      * constructs the panel
35      *
36      * @param model model to attach the panel to
37      */

38     public ASTPanel(ViewerModel model) {
39         this.model = model;
40         init();
41     }
42
43     private void init() {
44         model.addViewerModelListener(this);
45         setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), NLS.nls("AST.PANEL.TITLE")));
46         setLayout(new BorderLayout JavaDoc());
47         tree = new JTree((TreeNode JavaDoc) null);
48         tree.addTreeSelectionListener(this);
49         tree.addMouseListener(new MouseAdapter JavaDoc() {
50             public void mouseReleased(MouseEvent JavaDoc e) {
51                 if (e.isPopupTrigger()) {
52                     TreePath JavaDoc path = tree.getClosestPathForLocation(e.getX(), e.getY());
53                     tree.setSelectionPath(path);
54                     JPopupMenu menu = new ASTNodePopupMenu(model, (SimpleNode) path.getLastPathComponent());
55                     menu.show(tree, e.getX(), e.getY());
56                 }
57             }
58         });
59
60         add(new JScrollPane(tree), BorderLayout.CENTER);
61     }
62
63     /**
64      * @see ViewerModelListener#viewerModelChanged(ViewerModelEvent)
65      */

66     public void viewerModelChanged(ViewerModelEvent e) {
67         switch (e.getReason()) {
68             case ViewerModelEvent.CODE_RECOMPILED:
69                 tree.setModel(new ASTModel(model.getRootNode()));
70                 break;
71             case ViewerModelEvent.NODE_SELECTED:
72                 if (e.getSource() != this) {
73                     LinkedList JavaDoc list = new LinkedList JavaDoc();
74                     for (Node n = (Node) e.getParameter(); n != null; n = n.jjtGetParent()) {
75                         list.addFirst(n);
76                     }
77                     TreePath JavaDoc path = new TreePath JavaDoc(list.toArray());
78                     tree.setSelectionPath(path);
79                     tree.scrollPathToVisible(path);
80                 }
81                 break;
82         }
83     }
84
85     /**
86      * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent)
87      */

88     public void valueChanged(TreeSelectionEvent JavaDoc e) {
89         model.selectNode((SimpleNode) e.getNewLeadSelectionPath().getLastPathComponent(), this);
90     }
91 }
92
Popular Tags