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 4711 2006-10-20 02:40:49Z hooperbloob $
    22   */
    23
    24  public class SourceCodePanel extends JPanel implements ViewerModelListener {
    25
    26      private ViewerModel model;
    27      private JTextArea sourceCodeArea;
    28
    29      private static final Color highlightColor = new Color(79, 237, 111);
    30
    31      public SourceCodePanel(ViewerModel model) {
    32          this.model = model;
    33          init();
    34      }
    35
    36      private void init() {
    37          model.addViewerModelListener(this);
    38          setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), NLS.nls("SOURCE.PANEL.TITLE")));
    39          setLayout(new BorderLayout());
    40          sourceCodeArea = new JTextArea();
    41          add(new JScrollPane(sourceCodeArea), BorderLayout.CENTER);
    42      }
    43
    44      /**
    45       * retrieves the string representation of the source code
    46       *
    47       * @return source code's string representation
    48       */
    49      public String getSourceCode() {
    50          return sourceCodeArea.getText();
    51      }
    52
    53      /**
    54       * @see ViewerModelListener#viewerModelChanged(ViewerModelEvent)
    55       */
    56      public void viewerModelChanged(ViewerModelEvent e) {
    57          if (e.getReason() == ViewerModelEvent.NODE_SELECTED) {
    58              final SimpleNode node = (SimpleNode) e.getParameter();
    59              SwingUtilities.invokeLater(new Runnable() {
    60                  public void run() {
    61                      try {
    62                          sourceCodeArea.getHighlighter().removeAllHighlights();
    63                          if (node == null) {
    64                              return;
    65                          }
    66                          int startOffset =
    67                                  (sourceCodeArea.getLineStartOffset(node.getBeginLine() - 1) +
    68                                  node.getBeginColumn()) - 1;
    69                          int end =
    70                                  (sourceCodeArea.getLineStartOffset(node.getEndLine() - 1) +
    71                                  node.getEndColumn());
    72                          sourceCodeArea.getHighlighter().addHighlight(startOffset, end,
    73                                  new DefaultHighlighter.DefaultHighlightPainter(highlightColor));
    74                          sourceCodeArea.moveCaretPosition(startOffset);
    75                      } catch (BadLocationException exc) {
    76                          throw new IllegalStateException(exc.getMessage());
    77                      }
    78                  }
    79              });
    80          }
    81      }
    82  }
    83