This repository was archived by the owner on Nov 16, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathLinePainter.java
More file actions
171 lines (140 loc) · 4.65 KB
/
LinePainter.java
File metadata and controls
171 lines (140 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* By attaching this document to the given files (the �work�), you, the licensee,
* are hereby granted free usage in both personal and commerical environments,
* without any obligation of attribution or payment (monetary or otherwise).
*
* The licensee is free to use, copy, modify, publish, distribute, sublicence,
* and/or merchandise the work, subject to the licensee inflecting a positive
* message unto someone. This includes (but is not limited to): smiling,
* being nice, saying �thank you�, assisting other persons, or any
* similar actions percolating the given concept.
*
* The above copyright notice serves as a permissions notice also,
* and may optionally be included in copies or portions of the work.
*
* The work is provided �as is�, without warranty or support, express or implied.
* The author(s) are not liable for any damages, misuse, or other claim, whether
* from or as a consequence of usage of the given work.
*/
package Proiect;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
/**
* Track the movement of the Caret by painting a background line at the
* current caret position.
*/
public class LinePainter implements Highlighter.HighlightPainter,
CaretListener, MouseListener, MouseMotionListener {
private JTextComponent component;
private Color color;
private Rectangle lastView;
/*
* The line color will be calculated automatically by attempting to make the
* current selection lighter by a factor of 1.2.
*
* @param component text component that requires background line painting
*/
public LinePainter(JTextComponent component) {
this(component, null);
setLighter(component.getSelectionColor());
}
/*
* Manually control the line color
*
* @param component text component that requires background line painting
*
* @param color the color of the background line
*/
public LinePainter(JTextComponent component, Color color) {
this.component = component;
setColor(color);
// Add listeners so we know when to change highlighting
component.addCaretListener(this);
component.addMouseListener(this);
component.addMouseMotionListener(this);
// Turn highlighting on by adding a dummy highlight
try {
component.getHighlighter().addHighlight(0, 0, this);
} catch (BadLocationException ble) {
}
}
/*
* You can reset the line color at any time
*
* @param color the color of the background line
*/
public void setColor(Color color) {
this.color = color;
}
/*
* Calculate the line color by making the selection color lighter
*
* @return the color of the background line
*/
public void setLighter(Color color) {
int red = Math.min(211, (int) (color.getRed() * 1.2));
int green = Math.min(211, (int) (color.getGreen() * 1.2));
int blue = Math.min(211, (int) (color.getBlue() * 1.2));
setColor(new Color(red, green, blue));
}
// Paint the background highlight
public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c) {
try {
Rectangle r = c.modelToView(c.getCaretPosition());
g.setColor(color);
g.fillRect(0, r.y, c.getWidth(), r.height);
if (lastView == null)
lastView = r;
} catch (BadLocationException ble) {
System.out.println(ble);
}
}
/*
* Caret position has changed, remove the highlight
*/
private void resetHighlight() {
// Use invokeLater to make sure updates to the Document are completed,
// otherwise Undo processing causes the modelToView method to loop.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
int offset = component.getCaretPosition();
Rectangle currentView = component.modelToView(offset);
// Remove the highlighting from the previously highlighted
// line
if (lastView.y != currentView.y) {
component.repaint(0, lastView.y, component.getWidth(),
lastView.height);
lastView = currentView;
}
} catch (BadLocationException ble) {
}
}
});
}
// Implement CaretListener
public void caretUpdate(CaretEvent e) {
resetHighlight();
}
// Implement MouseListener
public void mousePressed(MouseEvent e) {
resetHighlight();
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
// Implement MouseMotionListener
public void mouseDragged(MouseEvent e) {
resetHighlight();
}
public void mouseMoved(MouseEvent e) {
}
}