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