Skip to content

A free, open-source alternative to Burp Suite: an intercepting proxy and web app security testing suite with Repeater, Intruder, Collaborator and a built-in OWASP ZAP scanner. Linux, AGPL-3.0.

1.4 KB
Raw Blame
package opensuite.ui;

import javax.swing.text.BadLocationException;
import javax.swing.text.Highlighter;
import javax.swing.text.JTextComponent;
import java.awt.*;

/**
 * The faint band behind the line the caret is on, as the reference's editors
 * draw it. It is a highlight, so it sits under the text and under a selection.
 */
public final class CurrentLine {
    private CurrentLine() {}

    public static void install(JTextComponent c) { install(c, true); }

    /** Add the band; {@code listen} false when re-adding it after the highlights were cleared. */
    public static void install(JTextComponent c, boolean listen) {
        Highlighter.HighlightPainter band = (g, p0, p1, bounds, comp) -> {
            try {
                java.awt.geom.Rectangle2D r = comp.modelToView2D(comp.getCaretPosition());
                if (r == null || (comp.getDocument().getLength() == 0 && !comp.isEditable())) return;
                g.setColor(Theme.CURRENT_LINE);
                g.fillRect(0, (int) r.getY(), comp.getWidth(), (int) Math.ceil(r.getHeight()));
            } catch (BadLocationException | RuntimeException ignored) {
                // The document changed under the paint; the next repaint is right.
            }
        };
        try { c.getHighlighter().addHighlight(0, 0, band); }
        catch (BadLocationException ignored) { return; }
        if (listen) c.addCaretListener(e -> c.repaint());
    }
}