|
| 1 | +/* This file is part of KeY - https://key-project.org |
| 2 | + * KeY is licensed under the GNU General Public License Version 2 |
| 3 | + * SPDX-License-Identifier: GPL-2.0-only */ |
| 4 | +package de.uka.ilkd.key.gui.utilities; |
| 5 | + |
| 6 | +import java.awt.*; |
| 7 | +import javax.swing.text.BadLocationException; |
| 8 | +import javax.swing.text.Highlighter; |
| 9 | +import javax.swing.text.JTextComponent; |
| 10 | + |
| 11 | +/** |
| 12 | + * Swing highlighter that highlights the current line in a JTextComponent. |
| 13 | + * |
| 14 | + * It delegates all other methods to the standard Highlighter such that it can be used as a drop-in |
| 15 | + * replacement. |
| 16 | + * |
| 17 | + * @author Mattias Ulbrich |
| 18 | + */ |
| 19 | +public class CurrentLineHighlighter implements Highlighter { |
| 20 | + private static final Color DEFAULT_COLOR = new Color(0, 0, 0, 15); |
| 21 | + |
| 22 | + private final Highlighter delegate; |
| 23 | + private final Color color; |
| 24 | + |
| 25 | + private JTextComponent component; |
| 26 | + |
| 27 | + public CurrentLineHighlighter(Highlighter delegate) { |
| 28 | + this(delegate, DEFAULT_COLOR); |
| 29 | + } |
| 30 | + |
| 31 | + public CurrentLineHighlighter(Highlighter delegate, Color color) { |
| 32 | + this.delegate = delegate; |
| 33 | + this.color = color; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void install(JTextComponent jTextComponent) { |
| 38 | + delegate.install(jTextComponent); |
| 39 | + this.component = jTextComponent; |
| 40 | + component.addCaretListener(e -> component.repaint()); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void deinstall(JTextComponent jTextComponent) { |
| 45 | + delegate.deinstall(jTextComponent); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void paint(Graphics graphics) { |
| 50 | + delegate.paint(graphics); |
| 51 | + if (component != null) { |
| 52 | + int caretPosition = component.getCaretPosition(); |
| 53 | + int currentRow = |
| 54 | + component.getDocument().getDefaultRootElement().getElementIndex(caretPosition); |
| 55 | + try { |
| 56 | + Rectangle modelToView = component.modelToView(component.getDocument() |
| 57 | + .getDefaultRootElement().getElement(currentRow).getStartOffset()); |
| 58 | + graphics.setColor(color); |
| 59 | + graphics.fillRect(0, modelToView.y, component.getWidth(), modelToView.height); |
| 60 | + } catch (BadLocationException e) { |
| 61 | + e.printStackTrace(); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public Object addHighlight(int i, int i1, HighlightPainter highlightPainter) |
| 68 | + throws BadLocationException { |
| 69 | + return delegate.addHighlight(i, i1, highlightPainter); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void removeHighlight(Object o) { |
| 74 | + delegate.removeHighlight(o); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void removeAllHighlights() { |
| 79 | + delegate.removeAllHighlights(); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void changeHighlight(Object o, int i, int i1) throws BadLocationException { |
| 84 | + delegate.changeHighlight(o, i, i1); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public Highlight[] getHighlights() { |
| 89 | + return delegate.getHighlights(); |
| 90 | + } |
| 91 | +} |
0 commit comments