diff options
Diffstat (limited to 'src/cppcheckplus/text/LineNumberHeaderView.java')
-rw-r--r-- | src/cppcheckplus/text/LineNumberHeaderView.java | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/cppcheckplus/text/LineNumberHeaderView.java b/src/cppcheckplus/text/LineNumberHeaderView.java new file mode 100644 index 0000000..85819fa --- /dev/null +++ b/src/cppcheckplus/text/LineNumberHeaderView.java | |||
@@ -0,0 +1,80 @@ | |||
1 | package cppcheckplus.text; | ||
2 | |||
3 | import java.awt.Color; | ||
4 | import java.awt.Dimension; | ||
5 | import java.awt.Font; | ||
6 | import java.awt.FontMetrics; | ||
7 | import java.awt.Graphics; | ||
8 | import java.awt.Rectangle; | ||
9 | |||
10 | public class LineNumberHeaderView extends javax.swing.JComponent { | ||
11 | |||
12 | private final Font DEFAULT_FONT = new Font("Serif", 0, 16); | ||
13 | public final Color DEFAULT_BACKGROUD = new Color(228, 228, 228); | ||
14 | public final Color DEFAULT_FOREGROUD = Color.BLACK; | ||
15 | public final int nHEIGHT = Integer.MAX_VALUE - 1000000; | ||
16 | public final int MARGIN = 2; | ||
17 | private int lineHeight; | ||
18 | private int fontLineHeight; | ||
19 | private int currentRowWidth; | ||
20 | private FontMetrics fontMetrics; | ||
21 | |||
22 | public LineNumberHeaderView() { | ||
23 | setFont(DEFAULT_FONT); | ||
24 | setForeground(DEFAULT_FOREGROUD); | ||
25 | setBackground(DEFAULT_BACKGROUD); | ||
26 | setPreferredSize(9999); | ||
27 | } | ||
28 | |||
29 | public void setPreferredSize(int row) { | ||
30 | int width = fontMetrics.stringWidth(String.valueOf(row)); | ||
31 | if (currentRowWidth < width) { | ||
32 | currentRowWidth = width; | ||
33 | setPreferredSize(new Dimension(2 * MARGIN + width, nHEIGHT)); | ||
34 | } | ||
35 | } | ||
36 | |||
37 | @Override | ||
38 | public void setFont(Font font) { | ||
39 | super.setFont(font); | ||
40 | fontMetrics = getFontMetrics(getFont()); | ||
41 | fontLineHeight = fontMetrics.getHeight(); | ||
42 | } | ||
43 | |||
44 | public int getLineHeight() { | ||
45 | if (lineHeight == 0) { | ||
46 | return fontLineHeight; | ||
47 | } | ||
48 | return lineHeight; | ||
49 | } | ||
50 | |||
51 | public void setLineHeight(int lineHeight) { | ||
52 | if (lineHeight > 0) { | ||
53 | this.lineHeight = lineHeight; | ||
54 | } | ||
55 | } | ||
56 | |||
57 | public int getStartOffset() { | ||
58 | return 0; | ||
59 | } | ||
60 | |||
61 | @Override | ||
62 | protected void paintComponent(Graphics g) { | ||
63 | int nlineHeight = getLineHeight(); | ||
64 | int startOffset = getStartOffset(); | ||
65 | Rectangle drawHere = g.getClipBounds(); | ||
66 | g.setColor(getBackground()); | ||
67 | g.fillRect(drawHere.x, drawHere.y, drawHere.width, drawHere.height); | ||
68 | g.setColor(getForeground()); | ||
69 | int startLineNum = (drawHere.y / nlineHeight) + 1; | ||
70 | int endLineNum = startLineNum + (drawHere.height / nlineHeight); | ||
71 | int start = (drawHere.y / nlineHeight) * nlineHeight + nlineHeight - startOffset; | ||
72 | for (int i = startLineNum; i <= endLineNum; ++i) { | ||
73 | String lineNum = String.valueOf(i); | ||
74 | int width = fontMetrics.stringWidth(lineNum); | ||
75 | g.drawString(lineNum + " ", MARGIN + currentRowWidth - width - 1, start); | ||
76 | start += nlineHeight; | ||
77 | } | ||
78 | setPreferredSize(endLineNum); | ||
79 | } | ||
80 | } | ||