KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > sourceViewer > NumberedParagraphView


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

19
20 package edu.umd.cs.findbugs.sourceViewer;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.FontMetrics JavaDoc;
24 import java.awt.Graphics JavaDoc;
25 import java.awt.Graphics2D JavaDoc;
26 import java.awt.Rectangle JavaDoc;
27 import java.awt.Shape JavaDoc;
28 import java.util.WeakHashMap JavaDoc;
29
30 import javax.swing.text.AttributeSet JavaDoc;
31 import javax.swing.text.Element JavaDoc;
32 import javax.swing.text.ParagraphView JavaDoc;
33 import javax.swing.text.StyleConstants JavaDoc;
34 import javax.swing.text.View JavaDoc;
35
36 import edu.umd.cs.findbugs.gui2.Driver;
37
38
39 // Code inspired by http://www.developer.com/java/other/article.php/3318421
40

41 class NumberedParagraphView extends ParagraphView JavaDoc {
42     public final static int NUMBERS_WIDTH= (int) Driver.getFontSize()*3+9;
43     HighlightInformation highlight;
44     public NumberedParagraphView(Element JavaDoc e, HighlightInformation highlight) {
45         super(e);
46         this.highlight = highlight;
47     }
48
49 // protected void setInsets(short top, short left, short bottom,
50
// short right) {super.setInsets
51
// (top,(short)(left+NUMBERS_WIDTH),
52
// bottom,right);
53
//}
54
public void paint(Graphics JavaDoc g, Shape JavaDoc allocation) {
55         Rectangle JavaDoc r = (allocation instanceof Rectangle JavaDoc) ?
56                 (Rectangle JavaDoc)allocation : allocation.getBounds();
57
58         Color JavaDoc oldColor = g.getColor();
59         Integer JavaDoc lineNumber = getLineNumber();
60         Color JavaDoc highlightColor = highlight.getHighlight(lineNumber);
61         if (highlightColor != null) {
62             g.setColor(highlightColor);
63             g.fillRect(r.x, r.y, r.width, r.height);
64             g.setColor(oldColor);
65         }
66         // r.x += NUMBERS_WIDTH;
67

68         super.paint(g, r);
69
70
71         FontMetrics JavaDoc metrics = g.getFontMetrics();
72          
73         g.setColor(Color.GRAY);
74         String JavaDoc lineNumberString = lineNumber.toString();
75         int width = metrics.stringWidth(lineNumberString);
76         int numberX = r.x - width-9 + NUMBERS_WIDTH;
77         int numberY = r.y + metrics.getAscent();
78        
79         g.drawString(lineNumberString, numberX, numberY);
80         g.setColor(oldColor);
81         // System.out.println("Drawing line for " + lineNumber + " @ " + numberX +"," + numberY);
82
// r.x -= NUMBERS_WIDTH;
83

84     }
85
86     public int getPreviousLineCount0() {
87         int lineCount = 0;
88         View JavaDoc parent = this.getParent();
89         int count = parent.getViewCount();
90         for (int i = 0; i < count; i++) {
91             if (parent.getView(i) == this) {
92                 break;
93             }
94             else {
95                 lineCount += parent.getView(i).getViewCount();
96             }
97         }
98         return lineCount;
99     }
100     static WeakHashMap JavaDoc<Element JavaDoc, Integer JavaDoc> elementLineNumberCache = new WeakHashMap JavaDoc<Element JavaDoc, Integer JavaDoc>();
101     public Integer JavaDoc getLineNumber() {
102     
103         Element JavaDoc element = this.getElement();
104         Integer JavaDoc result = elementLineNumberCache.get(element);
105         if (result != null) return result;
106         Element JavaDoc parent = element.getParentElement();
107         int count = parent.getElementCount();
108         for (int i = 0; i < count; i++) {
109             elementLineNumberCache.put(parent.getElement(i), i+1);
110         }
111        result = elementLineNumberCache.get(element);
112         if (result != null) return result;
113         return -1;
114
115     }
116 }
Popular Tags