|
78 | 78 | import javax.swing.text.Document;
|
79 | 79 | import javax.swing.text.Highlighter;
|
80 | 80 | import javax.swing.text.JTextComponent;
|
| 81 | +import javax.swing.text.View; |
81 | 82 | import org.graalvm.visualvm.lib.profiler.api.ActionsSupport;
|
82 | 83 | import org.graalvm.visualvm.lib.profiler.api.icons.GeneralIcons;
|
83 | 84 | import org.graalvm.visualvm.lib.profiler.api.icons.Icons;
|
@@ -230,7 +231,7 @@ void highlightResults(Highlighter hl) {
|
230 | 231 | clearHighlightedResults(hl);
|
231 | 232 |
|
232 | 233 | for (int offset : result) {
|
233 |
| - try { highlights.add(hl.addHighlight(offset, offset + search.length(), new ResultsHighlightPainter())); } |
| 234 | + try { highlights.add(hl.addHighlight(offset, offset + search.length(), new CustomHighlightPainter())); } |
234 | 235 | catch (BadLocationException ex) {}
|
235 | 236 | }
|
236 | 237 | }
|
@@ -270,14 +271,50 @@ int getPreviousIndex(int offset) {
|
270 | 271 | }
|
271 | 272 |
|
272 | 273 |
|
273 |
| - // NOTE: must not be direct subclass of DefaultHighlightPainter to not overlap selection |
274 |
| - private static final class ResultsHighlightPainter implements Highlighter.HighlightPainter { |
| 274 | +// // NOTE: must not be direct subclass of DefaultHighlightPainter to not overlap selection |
| 275 | +// private static final class ResultsHighlightPainter implements Highlighter.HighlightPainter { |
| 276 | +// |
| 277 | +// private static final Highlighter.HighlightPainter IMPL = new DefaultHighlightPainter(Color.ORANGE); |
| 278 | +// |
| 279 | +// @Override |
| 280 | +// public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c) { |
| 281 | +// IMPL.paint(g, p0, p1, bounds, c); |
| 282 | +// } |
| 283 | +// |
| 284 | +// } |
| 285 | + |
| 286 | + |
| 287 | + private static final class CustomHighlightPainter extends DefaultHighlightPainter { |
275 | 288 |
|
276 |
| - private static final Highlighter.HighlightPainter IMPL = new DefaultHighlightPainter(Color.ORANGE); |
277 |
| - |
278 |
| - @Override |
279 |
| - public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c) { |
280 |
| - IMPL.paint(g, p0, p1, bounds, c); |
| 289 | + CustomHighlightPainter() { |
| 290 | + super(Color.ORANGE); |
| 291 | + } |
| 292 | + |
| 293 | + public Shape paintLayer(Graphics g, int offs0, int offs1, |
| 294 | + Shape bounds, JTextComponent c, View view) { |
| 295 | + |
| 296 | + int selStart = c.getSelectionStart(); |
| 297 | + int selEnd = c.getSelectionEnd(); |
| 298 | + |
| 299 | + // No selection or selection fully outside of the highlight |
| 300 | + if (selEnd - selStart == 0 || offs0 >= selEnd || offs1 <= selStart) return super.paintLayer(g, offs0, offs1, bounds, c, view); |
| 301 | + |
| 302 | + // Selection fully covers the highlight |
| 303 | + if (offs0 >= selStart && offs1 <= selEnd) return bounds; |
| 304 | + |
| 305 | + // Selection partially covers the highlight |
| 306 | + if (offs0 < selStart || offs1 > selEnd) { |
| 307 | + // Selection ends inside of the highlight |
| 308 | + if (offs0 >= selStart) return super.paintLayer(g, selEnd, offs1, bounds, c, view); |
| 309 | + // Selection starts inside of the highlight |
| 310 | + else if (offs1 <= selEnd) return super.paintLayer(g, offs0, selStart, bounds, c, view); |
| 311 | + |
| 312 | + // Selection fully inside of the highlight |
| 313 | + super.paintLayer(g, offs0, selStart, bounds, c, view); |
| 314 | + super.paintLayer(g, selEnd, offs1, bounds, c, view); |
| 315 | + } |
| 316 | + |
| 317 | + return bounds; |
281 | 318 | }
|
282 | 319 |
|
283 | 320 | }
|
|
0 commit comments