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