KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > gui > LineNumberer


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2005 Dave Brosius <dbrosius@users.sourceforge.net>
4  * Copyright (C) 2005 University of Maryland
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20 package edu.umd.cs.findbugs.gui;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.awt.FontMetrics JavaDoc;
26 import java.awt.Graphics JavaDoc;
27 import java.awt.Rectangle JavaDoc;
28
29 import javax.swing.JComponent JavaDoc;
30 import javax.swing.JTextArea JavaDoc;
31
32 public class LineNumberer extends JComponent JavaDoc
33 {
34     public static final int PAD = 10;
35     public static final String JavaDoc PROTOTYPE = "00000";
36     
37     private JTextArea JavaDoc textArea;
38     private FontMetrics JavaDoc fm;
39     
40     public LineNumberer(JTextArea JavaDoc ta) {
41         setFont( ta.getFont() );
42         textArea = ta;
43         setForeground( Color.BLUE );
44         
45         fm = this.getFontMetrics(ta.getFont());
46         setWidths();
47     }
48     
49     @Override JavaDoc
50     public void setFont(Font JavaDoc font)
51     {
52         //ignore
53
}
54     
55     private void setWidths() {
56         int width = fm.stringWidth( PROTOTYPE );
57         Dimension JavaDoc d = getPreferredSize();
58         d.setSize(PAD + width, Integer.MAX_VALUE);
59         setPreferredSize( d );
60         setSize( d );
61     }
62
63
64     @Override JavaDoc
65     public void paintComponent(Graphics JavaDoc g)
66     {
67         int lineHeight = fm.getHeight();
68         int startOffset = textArea.getInsets().top + fm.getAscent();
69
70         Rectangle JavaDoc clip = g.getClipBounds();
71
72         g.setColor( getBackground() );
73         g.fillRect(clip.x, clip.y, clip.width, clip.height);
74
75         g.setColor( getForeground() );
76         int beginLineNumber = (clip.y / lineHeight) + 1;
77         int endLineNumber = beginLineNumber + (clip.height / lineHeight);
78
79         int y = (clip.y / lineHeight) * lineHeight + startOffset;
80
81         for (int i = beginLineNumber; i <= endLineNumber; i++)
82         {
83             String JavaDoc ln = String.valueOf(i);
84             int width = fm.stringWidth( ln );
85             int rowWidth = getSize().width;
86             g.drawString(ln, rowWidth - width - PAD, y);
87             y += lineHeight;
88         }
89     }
90 }
91
Popular Tags