KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > util > viewer > gui > SourceCodePanel


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 JavaDoc;
12 import javax.swing.text.DefaultHighlighter JavaDoc;
13 import java.awt.BorderLayout JavaDoc;
14 import java.awt.Color JavaDoc;
15
16
17 /**
18  * source code panel
19  *
20  * @author Boris Gruschko ( boris at gruschko.org )
21  * @version $Id: SourceCodePanel.java,v 1.12 2006/10/20 02:40:49 hooperbloob Exp $
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 JavaDoc highlightColor = new Color JavaDoc(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 JavaDoc());
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 JavaDoc 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 JavaDoc() {
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 JavaDoc(highlightColor));
74                         sourceCodeArea.moveCaretPosition(startOffset);
75                     } catch (BadLocationException JavaDoc exc) {
76                         throw new IllegalStateException JavaDoc(exc.getMessage());
77                     }
78                 }
79             });
80         }
81     }
82 }
83
84
Popular Tags