View Javadoc

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