1 19 20 package edu.umd.cs.findbugs.sourceViewer; 21 22 import java.awt.Color ; 23 import java.awt.FontMetrics ; 24 import java.awt.Graphics ; 25 import java.awt.Graphics2D ; 26 import java.awt.Rectangle ; 27 import java.awt.Shape ; 28 import java.util.WeakHashMap ; 29 30 import javax.swing.text.AttributeSet ; 31 import javax.swing.text.Element ; 32 import javax.swing.text.ParagraphView ; 33 import javax.swing.text.StyleConstants ; 34 import javax.swing.text.View ; 35 36 import edu.umd.cs.findbugs.gui2.Driver; 37 38 39 41 class NumberedParagraphView extends ParagraphView { 42 public final static int NUMBERS_WIDTH= (int) Driver.getFontSize()*3+9; 43 HighlightInformation highlight; 44 public NumberedParagraphView(Element e, HighlightInformation highlight) { 45 super(e); 46 this.highlight = highlight; 47 } 48 49 public void paint(Graphics g, Shape allocation) { 55 Rectangle r = (allocation instanceof Rectangle ) ? 56 (Rectangle )allocation : allocation.getBounds(); 57 58 Color oldColor = g.getColor(); 59 Integer lineNumber = getLineNumber(); 60 Color 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 68 super.paint(g, r); 69 70 71 FontMetrics metrics = g.getFontMetrics(); 72 73 g.setColor(Color.GRAY); 74 String 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 84 } 85 86 public int getPreviousLineCount0() { 87 int lineCount = 0; 88 View 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 <Element , Integer > elementLineNumberCache = new WeakHashMap <Element , Integer >(); 101 public Integer getLineNumber() { 102 103 Element element = this.getElement(); 104 Integer result = elementLineNumberCache.get(element); 105 if (result != null) return result; 106 Element 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 |