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.

2.7 KB
Raw Blame
package opensuite.ui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Path2D;
import java.awt.geom.RoundRectangle2D;

/**
 * A step button joined to a menu arrow in one outline: the wide part steps
 * back or forward, the narrow part opens a list to jump further. 45 by 23,
 * the two parts divided 28px in.
 */
public final class SplitButton extends JComponent {
    private static final int W = Ui.roomy(45), H = Ui.tall(23), SPLIT = 28 + Ui.fontGrowth();
    private final boolean forward;
    private Runnable action = () -> {};
    private Runnable menu = () -> {};

    public SplitButton(boolean forward, String tip) {
        this.forward = forward;
        setToolTipText(Ui.tip(tip));
        setPreferredSize(new Dimension(W, H));
        addMouseListener(new MouseAdapter() {
            @Override public void mousePressed(MouseEvent e) {
                if (!SwingUtilities.isLeftMouseButton(e)) return;
                if (e.getX() >= SPLIT) menu.run();
                else if (isEnabled()) action.run();
            }
        });
    }

    public void onAction(Runnable r) { action = r; }
    public void onMenu(Runnable r) { menu = r; }

    @Override public void setEnabled(boolean on) { super.setEnabled(on); repaint(); }

    @Override protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
        int arc = Theme.RADIUS * 2;
        g2.setColor(Theme.CONTENT);
        g2.fill(new RoundRectangle2D.Float(0, 0, W, H, arc, arc));
        g2.setColor(Theme.BORDER);
        g2.draw(new RoundRectangle2D.Float(0.5f, 0.5f, W - 1, H - 1, arc, arc));
        g2.fillRect(SPLIT, 1, 1, H - 2);
        Color mark = isEnabled() ? Theme.TEXT : Theme.dark ? Theme.MUTED : new Color(0xB4, 0xB4, 0xB4);
        g2.setColor(mark);
        g2.setStroke(new BasicStroke(isEnabled() ? 1.5f : 1.2f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
        Path2D p = new Path2D.Double();
        double cy = H / 2.0, d = forward ? 1 : -1, cx = forward ? 15.5 : 13;
        p.moveTo(cx - d * 2.5, cy - 5); p.lineTo(cx + d * 2.5, cy); p.lineTo(cx - d * 2.5, cy + 5);
        g2.draw(p);
        g2.setStroke(new BasicStroke(1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
        g2.setColor(isEnabled() ? Theme.TEXT : Theme.dark ? Theme.MUTED : new Color(0xC0, 0xC0, 0xC0));
        Path2D m = new Path2D.Double();
        m.moveTo(33.5, cy - 1.5); m.lineTo(36.5, cy + 1.5); m.lineTo(39.5, cy - 1.5);
        g2.draw(m);
        g2.dispose();
    }
}