Skip to content

Commit 4ef708f

Browse files
authored
highlight current line in issue editor pane (#3573)
2 parents 9062741 + 59ceff6 commit 4ef708f

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

key.ui/src/main/java/de/uka/ilkd/key/gui/actions/EditSourceFileAction.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import de.uka.ilkd.key.gui.sourceview.KeYEditorLexer;
2626
import de.uka.ilkd.key.gui.sourceview.SourceHighlightDocument;
2727
import de.uka.ilkd.key.gui.sourceview.TextLineNumber;
28+
import de.uka.ilkd.key.gui.utilities.CurrentLineHighlighter;
2829
import de.uka.ilkd.key.java.Position;
2930
import de.uka.ilkd.key.parser.Location;
3031
import de.uka.ilkd.key.util.ExceptionTools;
@@ -135,6 +136,7 @@ public void addNotify() {
135136
textAreaGoto(this, location.getPosition());
136137
}
137138
};
139+
textPane.setHighlighter(new CurrentLineHighlighter(textPane.getHighlighter()));
138140
Optional<URI> fileOpt = location.getFileURI();
139141
if (fileOpt.isEmpty()) {
140142
JTextPane jTextPane = new JTextPane();
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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

Comments
 (0)