View Javadoc

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