View Javadoc

    1   package net.sourceforge.pmd.util.viewer.gui;
    2
    3   import java.awt.BorderLayout;
    4   import java.util.Vector;
    5
    6   import javax.swing.JList;
    7   import javax.swing.JPanel;
    8   import javax.swing.JScrollPane;
    9   import javax.swing.ListSelectionModel;
    10  import javax.swing.event.ListSelectionEvent;
    11  import javax.swing.event.ListSelectionListener;
    12
    13  import net.sourceforge.pmd.lang.ast.Node;
    14  import net.sourceforge.pmd.util.viewer.model.ViewerModel;
    15  import net.sourceforge.pmd.util.viewer.model.ViewerModelEvent;
    16  import net.sourceforge.pmd.util.viewer.model.ViewerModelListener;
    17
    18  /**
    19   * A panel showing XPath expression evaluation results
    20   *
    21   * @author Boris Gruschko ( boris at gruschko.org )
    22   * @version $Id$
    23   */
    24  public class EvaluationResultsPanel extends JPanel implements ViewerModelListener {
    25      private ViewerModel model;
    26      private JList list;
    27
    28      /**
    29       * constructs the panel
    30       *
    31       * @param model model to refer to
    32       */
    33      public EvaluationResultsPanel(ViewerModel model) {
    34          super(new BorderLayout());
    35
    36          this.model = model;
    37
    38          init();
    39      }
    40
    41      private void init() {
    42          model.addViewerModelListener(this);
    43
    44          list = new JList();
    45          list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    46
    47          list.addListSelectionListener(new ListSelectionListener() {
    48              public void valueChanged(ListSelectionEvent e) {
    49                  if (list.getSelectedValue() != null) {
    50                      model.selectNode((Node) list.getSelectedValue(), EvaluationResultsPanel.this);
    51                  }
    52              }
    53          });
    54
    55          add(new JScrollPane(list), BorderLayout.CENTER);
    56      }
    57
    58      /**
    59       * @see ViewerModelListener#viewerModelChanged(ViewerModelEvent)
    60       */
    61      @SuppressWarnings("PMD.UseArrayListInsteadOfVector")
    62      public void viewerModelChanged(ViewerModelEvent e) {
    63          switch (e.getReason()) {
    64              case ViewerModelEvent.PATH_EXPRESSION_EVALUATED:
    65
    66                  if (e.getSource() != this) {
    67                      list.setListData(new Vector(model.getLastEvaluationResults()));
    68                  }
    69
    70                  break;
    71
    72              case ViewerModelEvent.CODE_RECOMPILED:
    73                  list.setListData(new Vector(0));
    74
    75                  break;
    76              default:
    77          	// Do nothing
    78                  break;
    79          }
    80      }
    81  }