View Javadoc

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