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.0 KB
Raw Blame
package opensuite.ui;

import javax.swing.*;
import java.awt.*;
import java.util.List;

/**
 * A view with panes beside it: a strip of vertical tabs on the edge, the pane
 * in front under a heading line that names it, with the marks that stand the
 * whole thing at the left or the right and the mark that closes it, and a gear
 * at the foot of the strip. The Intruder tab and an attack's results use it.
 */
public final class SideDock {
    private SideDock() {}

    /** One pane: its name on the strip and in the heading, what it shows, and its mark. */
    public record Item(String name, Component pane, Icon mark) {}

    /** The width the pane opens at, as measured at the usual 13px. */
    public static final int WIDTH = 455;

    /**
     * @param main      the view the panes stand beside
     * @param left      true to start at the left edge
     * @param collapsed true to start with only the strip showing; otherwise the
     *                  first pane is open wherever there is room for it
     * @param gearTip   the tip on the gear at the foot of the strip
     * @param onGear    what the gear does
     */
    public static JSplitPane build(Component main, List<Item> items, boolean left, boolean collapsed,
                                   String gearTip, Runnable onGear) {
        return build(main, items, left, collapsed, WIDTH, gearTip, onGear);
    }

    /** As {@link #build(Component, List, boolean, boolean, String, Runnable)}, with the pane opening at {@code width}. */
    public static JSplitPane build(Component main, List<Item> items, boolean left, boolean collapsed, final int width,
                                   String gearTip, Runnable onGear) {
        final boolean[] leftSide = { left };
        final CardLayout cards = new CardLayout();
        final JPanel panes = new JPanel(cards);
        panes.setOpaque(false);
        JPanel strip = new JPanel();
        strip.setLayout(new BoxLayout(strip, BoxLayout.Y_AXIS));
        strip.setOpaque(false);
        ButtonGroup group = new ButtonGroup();

        final JPanel side = new JPanel(new BorderLayout());
        side.setBackground(Theme.BG);
        final JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        final Runnable[] dock = new Runnable[1];
        final Runnable place = () -> {
            int right = VerticalTab.WIDTH + 1 + (side.isVisible() ? width : 0);
            if (split.getWidth() > right + 200) split.setDividerLocation(leftSide[0] ? right : split.getWidth() - right - split.getDividerSize());
        };
        final Runnable[] retitle = new Runnable[1];
        final String[] shown = { items.get(0).name() };
        retitle[0] = () -> {
            Component old = ((BorderLayout) side.getLayout()).getLayoutComponent(BorderLayout.NORTH);
            if (old != null) side.remove(old);
            side.add(SidePanel.header(shown[0], leftSide[0] ? 0 : 1,
                    w -> { leftSide[0] = w == 0; dock[0].run(); },
                    () -> { group.clearSelection(); side.setVisible(false); place.run(); }), BorderLayout.NORTH);
            side.revalidate();
            side.repaint();
        };
        for (Item it : items) {
            final String name = it.name();
            panes.add(it.pane(), name);
            VerticalTab t = new VerticalTab(name, it.mark());
            t.addActionListener(e -> {
                cards.show(panes, name);
                if (!name.equals(shown[0])) { shown[0] = name; retitle[0].run(); }
                if (!side.isVisible()) { side.setVisible(true); place.run(); }
            });
            group.add(t);
            strip.add(t);
        }
        ((AbstractButton) strip.getComponent(0)).setSelected(true);
        side.add(panes, BorderLayout.CENTER);
        retitle[0].run();

        JPanel stripWrap = new JPanel(new BorderLayout());
        stripWrap.setBackground(Theme.BG);
        stripWrap.add(strip, BorderLayout.NORTH);
        stripWrap.add(SidePanel.gear(gearTip, onGear), BorderLayout.SOUTH);

        JPanel right = new JPanel(new BorderLayout());
        right.setBackground(Theme.BG);
        right.add(side, BorderLayout.CENTER);

        // Stand the panel and its strip of tabs at the chosen edge.
        final int[] last = { -1 };
        dock[0] = () -> {
            boolean l = leftSide[0];
            stripWrap.setBorder(BorderFactory.createMatteBorder(0, l ? 0 : 1, 0, l ? 1 : 0, Ui.GRID));
            right.remove(stripWrap);
            right.add(stripWrap, l ? BorderLayout.WEST : BorderLayout.EAST);
            split.setLeftComponent(null);
            split.setRightComponent(null);
            split.setLeftComponent(l ? right : main);
            split.setRightComponent(l ? main : right);
            split.setResizeWeight(l ? 0.0 : 1.0);
            last[0] = -1;
            split.revalidate();
            SwingUtilities.invokeLater(place);
        };
        split.setDividerSize(1);
        split.setBorder(null);
        dock[0].run();
        if (collapsed) { group.clearSelection(); side.setVisible(false); }
        // The side panel opens at the measured width whatever the window's.
        split.addComponentListener(new java.awt.event.ComponentAdapter() {
            boolean sized;
            @Override public void componentResized(java.awt.event.ComponentEvent e) {
                // Open only when there is room beside the main view.
                if (!sized && split.getWidth() > 0) {
                    sized = true;
                    if (!collapsed && split.getWidth() < VerticalTab.WIDTH + 1 + width + 420) { group.clearSelection(); side.setVisible(false); }
                }
                // Leave the divider alone once it has been dragged somewhere else.
                if (last[0] >= 0 && Math.abs(split.getDividerLocation() - last[0]) > 1) return;
                SwingUtilities.invokeLater(() -> {
                    place.run();
                    int w = VerticalTab.WIDTH + 1 + (side.isVisible() ? width : 0);
                    last[0] = leftSide[0] ? w : split.getWidth() - w - split.getDividerSize();
                });
            }
        });
        return split;
    }
}