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.

3.9 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.RoundRectangle2D;
import java.util.function.IntConsumer;

/**
 * The small grouped switches on a heading line: one grey pill cut into 24px
 * segments, the chosen segment filled with the control colour and its mark
 * drawn white. Used for the request/response layout (side by side, stacked,
 * one at a time) and for the Inspector's width.
 */
public final class LayoutIcons {
    private LayoutIcons() {}

    /** Side by side, stacked, one at a time. */
    public static JComponent create(IntConsumer onChoose) {
        return new Pill(new int[] { 0, 1, 2 }, new String[] { "Side by side", "Stacked", "One at a time" }, 0, onChoose);
    }

    /**
     * Which edge a side panel stands at: 0 for the left, 1 for the right.
     *
     * @param side the edge it stands at now
     */
    public static JComponent createDockSide(int side, IntConsumer onChoose) {
        return new Pill(new int[] { 3, 4 }, new String[] { "Dock on the left", "Dock on the right" }, side, onChoose);
    }

    private static final class Pill extends JComponent {
        private static final int SEG = 24 + Ui.fontGrowth(), H = 18 + Ui.fontGrowth();
        private final int[] marks;
        private int selected;

        Pill(int[] marks, String[] tips, int initial, IntConsumer onChoose) {
            this.marks = marks;
            this.selected = initial;
            setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            setToolTipText(tips[initial]);
            addMouseListener(new MouseAdapter() {
                @Override public void mousePressed(MouseEvent e) {
                    int i = Math.max(0, Math.min(marks.length - 1, e.getX() / SEG));
                    if (i == selected) return;
                    selected = i;
                    repaint();
                    onChoose.accept(i);
                }
            });
            addMouseMotionListener(new MouseAdapter() {
                @Override public void mouseMoved(MouseEvent e) {
                    setToolTipText(tips[Math.max(0, Math.min(tips.length - 1, e.getX() / SEG))]);
                }
            });
        }

        @Override public Dimension getPreferredSize() { return new Dimension(SEG * marks.length, H); }
        @Override public Dimension getMinimumSize() { return getPreferredSize(); }
        @Override public Dimension getMaximumSize() { return getPreferredSize(); }

        @Override protected void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            int w = SEG * marks.length, y = (getHeight() - H) / 2;
            RoundRectangle2D whole = new RoundRectangle2D.Float(0, y, w, H, 8, 8);
            g2.setColor(Theme.BORDER);
            g2.fill(whole);
            Shape clip = g2.getClip();
            g2.clip(whole);
            g2.setColor(Theme.CONTROL);
            g2.fillRect(selected * SEG, y, SEG, H);
            g2.setClip(clip);
            for (int i = 0; i < marks.length; i++) {
                g2.setColor(i == selected ? Color.WHITE : Theme.TEXT);
                int cx = i * SEG + SEG / 2, cy = y + H / 2;
                switch (marks[i]) {
                    case 0 -> { g2.fillRect(cx - 5, cy - 4, 4, 8); g2.fillRect(cx + 1, cy - 4, 4, 8); }
                    case 1 -> { g2.fillRect(cx - 5, cy - 4, 10, 3); g2.fillRect(cx - 5, cy + 1, 10, 3); }
                    case 2 -> g2.fillRect(cx - 5, cy - 4, 10, 8);
                    case 3 -> { g2.fillRect(cx - 6, cy - 4, 2, 8); g2.fillRect(cx - 3, cy - 4, 9, 8); }
                    default -> { g2.fillRect(cx - 6, cy - 4, 9, 8); g2.fillRect(cx + 4, cy - 4, 2, 8); }
                }
            }
            g2.dispose();
        }
    }
}