1 7 8 package org.gjt.jclasslib.browser.detail.attributes.code; 9 10 import javax.swing.*; 11 import java.awt.*; 12 import java.awt.font.*; 13 import java.text.AttributedCharacterIterator ; 14 import java.text.AttributedString ; 15 import java.util.HashMap ; 16 import java.util.Map ; 17 18 24 public class CounterDisplay extends JPanel { 25 26 private static final Map STYLE; 27 private static final Color COLOR_BACKGROUND = UIManager.getColor("Panel.background"); 28 29 static { 30 Font baseFont = UIManager.getFont("TextArea.font"); 31 32 STYLE = new HashMap (3); 33 STYLE.put(TextAttribute.FAMILY, baseFont.getFamily()); 34 STYLE.put(TextAttribute.SIZE, new Float (baseFont.getSize() - 2)); 35 STYLE.put(TextAttribute.FOREGROUND, new Color(92, 92, 92)); 36 } 37 38 private int maxCount; 39 private int lineHeight; 40 private int ascent; 41 42 private int maxChars; 43 private FontRenderContext frc; 44 45 48 public CounterDisplay() { 49 setBorder(ByteCodeDisplay.BORDER); 50 setDoubleBuffered(false); 51 setOpaque(false); 52 } 53 54 58 public void init(ByteCodeDisplay byteCodeDisplay) { 59 60 this.maxCount = byteCodeDisplay.getLineCount(); 61 this.lineHeight = byteCodeDisplay.getLineHeight(); 62 this.ascent = byteCodeDisplay.getAscent(); 63 64 frc = ((Graphics2D)getGraphics()).getFontRenderContext(); 65 maxChars = Math.max(1, String.valueOf(maxCount).length()); 66 67 TextLayout textLayout = new TextLayout(getCharacterIterator(maxCount), frc); 68 69 setPreferredSize(new Dimension((int)textLayout.getAdvance() + 2 * ByteCodeDisplay.MARGIN_X, maxCount * lineHeight + 2 * ByteCodeDisplay.MARGIN_Y)); 70 invalidate(); 71 } 72 73 private AttributedCharacterIterator getCharacterIterator(int number) { 74 AttributedString attrSting = new AttributedString (ByteCodeDisplay.getPaddedValue(number, maxChars), STYLE); 75 76 return attrSting.getIterator(); 77 } 78 79 protected void paintComponent(Graphics graphics) { 80 81 if (maxCount == 0 || lineHeight == 0) { 82 return; 83 } 84 85 Graphics2D g = (Graphics2D)graphics; 86 g.translate(ByteCodeDisplay.MARGIN_X, ByteCodeDisplay.MARGIN_Y); 87 Rectangle clipBounds = graphics.getClipBounds(); 88 Paint oldPaint = g.getPaint(); 89 g.setPaint(COLOR_BACKGROUND); 90 g.fill(clipBounds); 91 g.setPaint(oldPaint); 92 93 int startLine = Math.max(0, clipBounds.y / lineHeight - 1); 94 int endLine = Math.min(maxCount, (clipBounds.y + clipBounds.height) / lineHeight + 1); 95 for (int i = startLine; i < endLine; i++) { 96 TextLayout textLayout = new TextLayout(getCharacterIterator(i + 1), frc); 97 textLayout.draw(g, 0, i * lineHeight + ascent); 98 } 99 100 g.translate(-ByteCodeDisplay.MARGIN_X, -ByteCodeDisplay.MARGIN_Y); 101 } 102 } 103 | Popular Tags |