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