KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > browser > detail > attributes > code > CounterDisplay


1 /*
2     This library is free software; you can redistribute it and/or
3     modify it under the terms of the GNU General Public
4     License as published by the Free Software Foundation; either
5     version 2 of the license, or (at your option) any later version.
6 */

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 JavaDoc;
14 import java.text.AttributedString JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17
18 /**
19     Line number renderer used as a row header view for a <tt>BytecodeDisplay</tt>.
20
21     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
22     @version $Revision: 1.1 $ $Date: 2003/08/18 08:19:37 $
23 */

24 public class CounterDisplay extends JPanel {
25
26     private static final Map JavaDoc 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 JavaDoc(3);
33         STYLE.put(TextAttribute.FAMILY, baseFont.getFamily());
34         STYLE.put(TextAttribute.SIZE, new Float JavaDoc(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     /**
46      * Constructor.
47      */

48     public CounterDisplay() {
49         setBorder(ByteCodeDisplay.BORDER);
50         setDoubleBuffered(false);
51         setOpaque(false);
52     }
53
54     /**
55      * Initialize with the properties of a given bytecode display.
56      * @param byteCodeDisplay the bytecode display.
57      */

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 JavaDoc getCharacterIterator(int number) {
74         AttributedString JavaDoc attrSting = new AttributedString JavaDoc(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