1 | package com.framsticks.gui.console; |
---|
2 | |
---|
3 | import javax.swing.text.*; |
---|
4 | import java.awt.*; |
---|
5 | |
---|
6 | /** |
---|
7 | * Painter class to coloring text background. |
---|
8 | */ |
---|
9 | public class SimpleLinePainter extends |
---|
10 | DefaultHighlighter.DefaultHighlightPainter { |
---|
11 | |
---|
12 | private JTextComponent component; |
---|
13 | private DefaultHighlighter highlighter; |
---|
14 | |
---|
15 | /** |
---|
16 | * Constructor sets component reference. |
---|
17 | * |
---|
18 | * @param component Reference to painted component. |
---|
19 | * @param color Background color. |
---|
20 | */ |
---|
21 | public SimpleLinePainter(JTextComponent component, Color color) { |
---|
22 | super(color); |
---|
23 | |
---|
24 | this.component = component; |
---|
25 | |
---|
26 | highlighter = (DefaultHighlighter) component.getHighlighter(); |
---|
27 | highlighter.setDrawsLayeredHighlights(true); |
---|
28 | } |
---|
29 | |
---|
30 | /** |
---|
31 | * Adds highlight in line sets by offset. |
---|
32 | * |
---|
33 | * @param offset Line offset. |
---|
34 | */ |
---|
35 | public void addHighlight(int offset) { |
---|
36 | try { |
---|
37 | highlighter.addHighlight(offset, offset + 1, this); |
---|
38 | } catch (BadLocationException ignored) { |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | /** |
---|
43 | * Resets highlight. |
---|
44 | */ |
---|
45 | public void resetHighlight() { |
---|
46 | |
---|
47 | Element root = component.getDocument().getDefaultRootElement(); |
---|
48 | int line = root.getElementIndex(component.getCaretPosition()); |
---|
49 | Element lineElement = root.getElement(line); |
---|
50 | int start = lineElement.getStartOffset(); |
---|
51 | addHighlight(start); |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | public Shape paintLayer(Graphics g, int offs0, int offs1, Shape bounds, |
---|
56 | JTextComponent c, View view) { |
---|
57 | try { |
---|
58 | Rectangle r = c.modelToView(offs0); |
---|
59 | r.x = 0; |
---|
60 | r.width = c.getSize().width; |
---|
61 | |
---|
62 | g.setColor(getColor()); |
---|
63 | g.fillRect(r.x, r.y, r.width, r.height); |
---|
64 | return r; |
---|
65 | } catch (BadLocationException e) { |
---|
66 | return null; |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | } |
---|