38 |
|
|
39 |
package javax.swing.text; |
package javax.swing.text; |
40 |
|
|
41 |
|
import java.awt.Color; |
42 |
import java.awt.Graphics; |
import java.awt.Graphics; |
43 |
|
import java.awt.Rectangle; |
44 |
import java.awt.Shape; |
import java.awt.Shape; |
45 |
import java.util.Vector; |
import java.util.Vector; |
46 |
|
|
50 |
|
|
51 |
public class DefaultHighlighter extends LayeredHighlighter |
public class DefaultHighlighter extends LayeredHighlighter |
52 |
{ |
{ |
53 |
|
public static class DefaultHighlightPainter |
54 |
|
extends LayerPainter |
55 |
|
{ |
56 |
|
private Color color; |
57 |
|
|
58 |
|
public DefaultHighlightPainter(Color c) |
59 |
|
{ |
60 |
|
super(); |
61 |
|
color = c; |
62 |
|
} |
63 |
|
|
64 |
|
public Color getColor() |
65 |
|
{ |
66 |
|
return color; |
67 |
|
} |
68 |
|
|
69 |
|
private void paintHighlight(Graphics g, Rectangle rect) |
70 |
|
{ |
71 |
|
g.fillRect(rect.x, rect.y, rect.width, rect.height); |
72 |
|
} |
73 |
|
|
74 |
|
public void paint(Graphics g, int p0, int p1, Shape bounds, |
75 |
|
JTextComponent c) |
76 |
|
{ |
77 |
|
Rectangle r0 = null; |
78 |
|
Rectangle r1 = null; |
79 |
|
Rectangle rect = bounds.getBounds(); |
80 |
|
|
81 |
|
try |
82 |
|
{ |
83 |
|
r0 = c.modelToView(p0); |
84 |
|
r1 = c.modelToView(p1); |
85 |
|
} |
86 |
|
catch (BadLocationException e) |
87 |
|
{ |
88 |
|
// This should never occur. |
89 |
|
return; |
90 |
|
} |
91 |
|
|
92 |
|
if (r0 == null || r1 == null) |
93 |
|
return; |
94 |
|
|
95 |
|
if (color == null) |
96 |
|
g.setColor(c.getSelectionColor()); |
97 |
|
else |
98 |
|
g.setColor(color); |
99 |
|
|
100 |
|
// Check if only one line to highlight. |
101 |
|
if (r0.y == r1.y) |
102 |
|
{ |
103 |
|
r0.width = r1.x - r0.x; |
104 |
|
paintHighlight(g, r0); |
105 |
|
return; |
106 |
|
} |
107 |
|
|
108 |
|
// First line, from p0 to end-of-line. |
109 |
|
r0.width = rect.x + rect.width - r0.x; |
110 |
|
paintHighlight(g, r0); |
111 |
|
|
112 |
|
// FIXME: All the full lines in between, if any (assumes that all lines |
113 |
|
// have the same height -- not a good assumption with JEditorPane/JTextPane). |
114 |
|
r0.y += r0.height; |
115 |
|
r0.x = rect.x; |
116 |
|
|
117 |
|
while (r0.y < r1.y) |
118 |
|
{ |
119 |
|
paintHighlight(g, r0); |
120 |
|
r0.y += r0.height; |
121 |
|
} |
122 |
|
|
123 |
|
// Last line, from beginnin-of-line to p1. |
124 |
|
paintHighlight(g, r1); |
125 |
|
} |
126 |
|
|
127 |
|
public Shape paintLayer(Graphics g, int p0, int p1, Shape bounds, |
128 |
|
JTextComponent c, View view) |
129 |
|
{ |
130 |
|
throw new InternalError(); |
131 |
|
} |
132 |
|
} |
133 |
|
|
134 |
private class HighlightEntry |
private class HighlightEntry |
135 |
{ |
{ |
136 |
int p0; |
int p0; |
160 |
} |
} |
161 |
} |
} |
162 |
|
|
163 |
|
/** |
164 |
|
* @specnote final as of 1.4 |
165 |
|
*/ |
166 |
|
public static final LayeredHighlighter.LayerPainter DefaultPainter = |
167 |
|
new DefaultHighlightPainter(null); |
168 |
|
|
169 |
private JTextComponent textComponent; |
private JTextComponent textComponent; |
170 |
private Vector highlights = new Vector(); |
private Vector highlights = new Vector(); |
171 |
|
private boolean drawsLayeredHighlights = true; |
172 |
|
|
173 |
public DefaultHighlighter() |
public DefaultHighlighter() |
174 |
{ |
{ |
175 |
} |
} |
176 |
|
|
177 |
|
public boolean getDrawsLayeredHighlights() |
178 |
|
{ |
179 |
|
return drawsLayeredHighlights; |
180 |
|
} |
181 |
|
|
182 |
|
public void setDrawsLayeredHighlights(boolean newValue) |
183 |
|
{ |
184 |
|
drawsLayeredHighlights = newValue; |
185 |
|
} |
186 |
|
|
187 |
private void checkPositions(int p0, int p1) |
private void checkPositions(int p0, int p1) |
188 |
throws BadLocationException |
throws BadLocationException |
189 |
{ |
{ |
246 |
|
|
247 |
public void paint(Graphics g) |
public void paint(Graphics g) |
248 |
{ |
{ |
249 |
|
// Check if there are any highlights. |
250 |
|
if (highlights.size() == 0) |
251 |
|
return; |
252 |
|
|
253 |
|
Shape bounds = textComponent.getBounds(); |
254 |
|
|
255 |
|
for (int index = 0; index < highlights.size(); ++index) |
256 |
|
{ |
257 |
|
HighlightEntry entry = (HighlightEntry) highlights.get(index); |
258 |
|
entry.painter.paint(g, entry.p0, entry.p1, bounds, textComponent); |
259 |
|
} |
260 |
} |
} |
261 |
} |
} |