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   
7   import java.awt.BorderLayout;
8   import java.awt.Color;
9   
10  import javax.swing.BorderFactory;
11  import javax.swing.JPanel;
12  import javax.swing.JScrollPane;
13  import javax.swing.JTextArea;
14  import javax.swing.SwingUtilities;
15  import javax.swing.text.BadLocationException;
16  import javax.swing.text.DefaultHighlighter;
17  
18  import net.sourceforge.pmd.lang.ast.Node;
19  import net.sourceforge.pmd.util.viewer.model.ViewerModel;
20  import net.sourceforge.pmd.util.viewer.model.ViewerModelEvent;
21  import net.sourceforge.pmd.util.viewer.model.ViewerModelListener;
22  import net.sourceforge.pmd.util.viewer.util.NLS;
23  
24  
25  /**
26   * source code panel
27   *
28   * @author Boris Gruschko ( boris at gruschko.org )
29   * @version $Id$
30   */
31  
32  public class SourceCodePanel extends JPanel implements ViewerModelListener {
33  	
34      private ViewerModel model;
35      private JTextArea sourceCodeArea;
36  
37      private static final Color HIGHLIGHT_COLOR = new Color(79, 237, 111);
38      
39      public SourceCodePanel(ViewerModel model) {
40          this.model = model;
41          init();
42      }
43  
44      private void init() {
45          model.addViewerModelListener(this);
46          setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), NLS.nls("SOURCE.PANEL.TITLE")));
47          setLayout(new BorderLayout());
48          sourceCodeArea = new JTextArea();
49          add(new JScrollPane(sourceCodeArea), BorderLayout.CENTER);
50      }
51  
52      /**
53       * retrieves the string representation of the source code
54       *
55       * @return source code's string representation
56       */
57      public String getSourceCode() {
58          return sourceCodeArea.getText();
59      }
60  
61      /**
62       * @see ViewerModelListener#viewerModelChanged(ViewerModelEvent)
63       */
64      public void viewerModelChanged(ViewerModelEvent e) {
65          if (e.getReason() == ViewerModelEvent.NODE_SELECTED) {
66              final Node node = (Node) e.getParameter();
67              SwingUtilities.invokeLater(new Runnable() {
68                  public void run() {
69                      try {
70                          sourceCodeArea.getHighlighter().removeAllHighlights();
71                          if (node == null) {
72                              return;
73                          }
74                          int startOffset =
75                                  sourceCodeArea.getLineStartOffset(node.getBeginLine() - 1) +
76                                  node.getBeginColumn() - 1;
77                          int end =
78                                  sourceCodeArea.getLineStartOffset(node.getEndLine() - 1) +
79                                  node.getEndColumn();
80                          sourceCodeArea.getHighlighter().addHighlight(startOffset, end,
81                                  new DefaultHighlighter.DefaultHighlightPainter(HIGHLIGHT_COLOR));
82                          sourceCodeArea.moveCaretPosition(startOffset);
83                      } catch (BadLocationException exc) {
84                          throw new IllegalStateException(exc.getMessage());
85                      }
86                  }
87              });
88          }
89      }
90  }
91