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.

6.4 KB
Raw Blame
package opensuite.ui.theme;

import opensuite.ui.Theme;

import javax.swing.*;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicSplitPaneDivider;
import javax.swing.plaf.basic.BasicSplitPaneUI;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * A split pane whose divider is a single hairline, as in the reference: the
 * two panes meet at one pixel, with no band between them. A one-pixel divider
 * cannot be grabbed, so the pointer is watched instead: within a few pixels of
 * the line it becomes a resize cursor, and a drag from there moves the line.
 */
public class FlatSplitPaneUI extends BasicSplitPaneUI {

    /** How far either side of the hairline a drag may start. */
    private static final int REACH = 3;
    /** Marks the glass pane while one of the suite's edge handlers is using it. */
    public static final String GLASS_OWNER = "opensuite.glassOwner";

    public static ComponentUI createUI(JComponent c) {
        installDrag();
        return new FlatSplitPaneUI();
    }

    @Override public BasicSplitPaneDivider createDefaultDivider() {
        return new BasicSplitPaneDivider(this) {
            @Override public void paint(Graphics g) {
                g.setColor(Theme.BORDER);
                g.fillRect(0, 0, getWidth(), getHeight());
            }
        };
    }

    // ---- dragging a hairline ----

    private static boolean dragInstalled;
    private static JSplitPane armed;
    private static JSplitPane dragging;

    private static synchronized void installDrag() {
        if (dragInstalled) return;
        dragInstalled = true;
        Toolkit.getDefaultToolkit().addAWTEventListener(ev -> {
            if (ev instanceof MouseEvent e) { try { onMouse(e); } catch (RuntimeException ignore) { } }
        }, AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);
    }

    private static void onMouse(MouseEvent e) {
        if (!(e.getSource() instanceof Component src) || !src.isShowing()) return;
        Window win = src instanceof Window w ? w : SwingUtilities.getWindowAncestor(src);
        if (!(win instanceof RootPaneContainer rpc)) return;
        Component glass = rpc.getGlassPane();
        Point screen = e.getLocationOnScreen();
        switch (e.getID()) {
            case MouseEvent.MOUSE_MOVED -> {
                if (dragging != null) return;
                JSplitPane hit = hairlineAt(rpc, screen);
                if (hit != null && claim(glass)) {
                    armed = hit;
                    glass.setCursor(Cursor.getPredefinedCursor(hit.getOrientation() == JSplitPane.HORIZONTAL_SPLIT
                            ? Cursor.E_RESIZE_CURSOR : Cursor.N_RESIZE_CURSOR));
                    glass.setVisible(true);
                } else if (hit == null && armed != null) {
                    release(glass);
                }
            }
            case MouseEvent.MOUSE_PRESSED -> {
                if (armed != null && SwingUtilities.isLeftMouseButton(e)) dragging = armed;
            }
            case MouseEvent.MOUSE_DRAGGED -> {
                if (dragging == null || !dragging.isShowing()) return;
                Point p = new Point(screen);
                SwingUtilities.convertPointFromScreen(p, dragging);
                int at = dragging.getOrientation() == JSplitPane.HORIZONTAL_SPLIT ? p.x : p.y;
                at = Math.max(dragging.getMinimumDividerLocation(), Math.min(dragging.getMaximumDividerLocation(), at));
                dragging.setDividerLocation(at);
            }
            case MouseEvent.MOUSE_RELEASED -> {
                if (dragging != null) {
                    dragging = null;
                    if (hairlineAt(rpc, screen) == null) release(glass);
                }
            }
            case MouseEvent.MOUSE_EXITED -> {
                if (dragging == null && armed != null && src == glass && !glass.getBounds().contains(e.getPoint())) release(glass);
            }
            default -> { }
        }
    }

    /** Take the glass pane for the resize cursor, unless another handler holds it. */
    private static boolean claim(Component glass) {
        if (!(glass instanceof JComponent g)) return false;
        Object owner = g.getClientProperty(GLASS_OWNER);
        if (owner != null && !"split".equals(owner)) return false;
        if (g.getClientProperty("opensuite.glassSwallows") == null) {
            // With a listener of its own the glass pane takes the click, so a
            // press that starts a drag does not also land on what is beneath.
            g.putClientProperty("opensuite.glassSwallows", Boolean.TRUE);
            g.addMouseListener(new MouseAdapter() { });
            g.addMouseMotionListener(new MouseAdapter() { });
        }
        g.putClientProperty(GLASS_OWNER, "split");
        return true;
    }

    private static void release(Component glass) {
        armed = null;
        if (glass instanceof JComponent g && "split".equals(g.getClientProperty(GLASS_OWNER))) {
            g.putClientProperty(GLASS_OWNER, null);
            g.setVisible(false);
            g.setCursor(null);
        }
    }

    /** The innermost hairline split pane with its divider within reach of the pointer. */
    private static JSplitPane hairlineAt(RootPaneContainer rpc, Point screen) {
        Container content = rpc.getContentPane();
        if (content == null || !content.isShowing()) return null;
        Point p = new Point(screen);
        SwingUtilities.convertPointFromScreen(p, content);
        Component deep = SwingUtilities.getDeepestComponentAt(content, p.x, p.y);
        for (Component c = deep; c != null && c != content.getParent(); c = c.getParent()) {
            if (!(c instanceof JSplitPane sp) || !(sp.getUI() instanceof FlatSplitPaneUI) || !sp.isEnabled()) continue;
            int size = sp.getDividerSize();
            if (size < 1 || size > 2) continue;
            Component a = sp.getLeftComponent(), b = sp.getRightComponent();
            if (a == null || b == null || !a.isVisible() || !b.isVisible()) continue;
            Point q = new Point(screen);
            SwingUtilities.convertPointFromScreen(q, sp);
            if (q.x < 0 || q.y < 0 || q.x >= sp.getWidth() || q.y >= sp.getHeight()) continue;
            int at = sp.getOrientation() == JSplitPane.HORIZONTAL_SPLIT ? q.x : q.y;
            int line = sp.getDividerLocation();
            if (at >= line - REACH && at <= line + size - 1 + REACH) return sp;
        }
        return null;
    }
}