View Javadoc

    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;
    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, TreeSelectionListener {
    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());
    47          tree = new JTree((TreeNode) null);
    48          tree.addTreeSelectionListener(this);
    49          tree.addMouseListener(new MouseAdapter() {
    50              public void mouseReleased(MouseEvent e) {
    51                  if (e.isPopupTrigger()) {
    52                      TreePath 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<Node> list = new LinkedList<Node>();
    74                      for (Node n = (Node) e.getParameter(); n != null; n = n.jjtGetParent()) {
    75                          list.addFirst(n);
    76                      }
    77                      TreePath path = new TreePath(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 e) {
    89          model.selectNode((SimpleNode) e.getNewLeadSelectionPath().getLastPathComponent(), this);
    90      }
    91  }