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.

7.9 KB
Raw Blame
package opensuite.ui;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.RoundRectangle2D;

/**
 * What a pane shows while it has nothing in it: a small illustration, a bold
 * 16px title, a centred paragraph about 300px wide and, where there is
 * somewhere useful to send the reader, a "Learn more" button. One builder, so
 * every empty pane in the suite is the same size and sits in the same place.
 */
public final class EmptyState {
    private EmptyState() {}

    public static JPanel of(String title, String text, Runnable learnMore) { return of(art(), title, text, learnMore); }

    public static JPanel of(Icon art, String title, String text, Runnable learnMore) {
        JButton more = null;
        if (learnMore != null) {
            more = Ui.button("Learn more");
            more.addActionListener(e -> learnMore.run());
        }
        return build(0, 26, art, 43, title, text, 300, Theme.CONTENT, more);
    }

    /** A "Learn more" button that opens the project's documentation. */
    public static JButton learnMore() {
        JButton more = Ui.button("Learn more", "open the project documentation");
        more.addActionListener(e -> openDocs());
        return more;
    }

    /**
     * The general form.
     *
     * @param artGap    the space between the illustration and the title
     * @param textWidth how wide the paragraph wraps, in pixels
     * @param bg        the pane's fill
     * @param buttons   the row under the paragraph, 20px apart; nulls are skipped
     */
    public static JPanel of(Icon art, int artGap, String title, String text, int textWidth, Color bg, JButton... buttons) {
        return build(11, 30, art, artGap, title, text, textWidth, bg, buttons);
    }

    private static JPanel build(int drop, int buttonGap, Icon art, int artGap, String title, String text, int textWidth,
                                Color bg, JButton... buttons) {
        // As measured at the usual 13px: the title is in proportion to the font,
        // the illustration an eighth larger, the paragraph 64px wider, and the
        // gaps beneath the title as they were.
        JLabel pic = new JLabel(scaled(art));
        artGap += Ui.fontGrowth(); textWidth += 32 * Ui.fontGrowth();
        JLabel head = new JLabel(title);
        head.setFont(Ui.HEADING.deriveFont((float) Ui.em(16)));
        head.setForeground(Theme.TAB_TEXT_ON);
        // HTML gives a centred, wrapping paragraph at a fixed width; its "px"
        // are points, so the figure is scaled to land on screen pixels.
        JLabel body = new JLabel("<html><div style='width:" + Math.round(textWidth * 230 / 300f) + "px; text-align:center'>"
                + escape(text) + "</div></html>");
        body.setFont(Ui.PLAIN);
        body.setForeground(Theme.TEXT);

        JPanel col = new JPanel();
        col.setLayout(new BoxLayout(col, BoxLayout.Y_AXIS));
        col.setOpaque(false);
        col.setBorder(Ui.pad(drop, 0, 0, 0));
        for (JComponent c : new JComponent[] { pic, head, body }) c.setAlignmentX(Component.CENTER_ALIGNMENT);
        col.add(pic);
        col.add(Box.createVerticalStrut(artGap));
        col.add(head);
        col.add(Box.createVerticalStrut(30));
        col.add(body);
        JPanel row = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
        row.setOpaque(false);
        row.setAlignmentX(Component.CENTER_ALIGNMENT);
        if (buttons != null) for (JButton b : buttons) {
            if (b == null) continue;
            if (row.getComponentCount() > 0) row.add(Box.createHorizontalStrut(20));
            row.add(b);
        }
        if (row.getComponentCount() > 0) {
            col.add(Box.createVerticalStrut(buttonGap));
            col.add(row);
        }
        JPanel wrap = new JPanel(new GridBagLayout());
        wrap.setOpaque(true);
        wrap.setBackground(bg);
        wrap.add(col, new GridBagConstraints());
        return wrap;
    }

    /** Put one more line, centred, under an empty state's buttons. */
    public static JPanel withFooter(JPanel state, JComponent footer, int gap) {
        if (state.getComponentCount() > 0 && state.getComponent(0) instanceof JPanel col) {
            footer.setAlignmentX(Component.CENTER_ALIGNMENT);
            col.add(Box.createVerticalStrut(gap));
            col.add(footer);
        }
        return state;
    }

    /** An illustration drawn in proportion to the font in use. */
    private static Icon scaled(Icon art) {
        if (art == null || Ui.fontGrowth() == 0) return art;
        double k = 1 + Ui.fontGrowth() / 16.0;
        return new Icon() {
            @Override public int getIconWidth() { return (int) Math.round(art.getIconWidth() * k); }
            @Override public int getIconHeight() { return (int) Math.round(art.getIconHeight() * k); }
            @Override public void paintIcon(Component c, Graphics g, int x, int y) {
                Graphics2D g2 = (Graphics2D) g.create();
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
                g2.translate(x, y);
                g2.scale(k, k);
                art.paintIcon(c, g2, 0, 0);
                g2.dispose();
            }
        };
    }

    /** Open the project's documentation in the system browser. */
    public static void openDocs() {
        try { Desktop.getDesktop().browse(java.net.URI.create(Unbuilt.PROJECT_URL)); } catch (Exception ignore) { }
    }

    private static String escape(String s) {
        // Text between \u0001 and \u0002 is set in bold.
        return s == null ? "" : s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
                .replace("\u0001", "<b>").replace("\u0002", "</b>");
    }

    /**
     * The suite's own illustration: a tilted sheet with a few ruled lines and a
     * soft shadow, in the control blue. Drawn here, so it needs no image file
     * and follows the theme.
     */
    public static Icon art() { return art(1.0); }

    /** The illustration at a fraction of its full size, for the panes that show it smaller. */
    public static Icon art(double scale) {
        return new Icon() {
            @Override public int getIconWidth() { return (int) Math.round(139 * scale); }
            @Override public int getIconHeight() { return (int) Math.round(94 * scale); }
            @Override public void paintIcon(Component c, Graphics g, int x, int y) {
                Graphics2D g2 = (Graphics2D) g.create();
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2.translate(x, y);
                g2.scale(scale, scale);
                g2.translate(69.5, 44);
                // Lay a flat sheet down as if seen from above and to one side.
                AffineTransform iso = new AffineTransform(0.86, 0.38, -0.86, 0.38, 0, 0);
                Shape sheet = iso.createTransformedShape(new RoundRectangle2D.Double(-44, -58, 88, 116, 10, 10));
                g2.translate(0, 12);
                g2.setColor(new Color(0, 0, 0, 26));
                g2.fill(sheet);
                g2.translate(0, -7);
                g2.setColor(Theme.CONTROL.darker());
                g2.fill(sheet);
                g2.translate(0, -5);
                g2.setColor(Theme.CONTROL);
                g2.fill(sheet);
                g2.setColor(Theme.ACCENT_LIGHT);
                g2.fill(iso.createTransformedShape(new RoundRectangle2D.Double(-37, -51, 74, 102, 6, 6)));
                g2.setColor(Theme.CONTROL);
                g2.setStroke(new BasicStroke(2.6f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
                for (int i = 0; i < 4; i++) {
                    double yy = -34 + i * 20;
                    g2.draw(iso.createTransformedShape(new java.awt.geom.Line2D.Double(-26, yy, i % 2 == 0 ? 20 : 6, yy)));
                }
                g2.dispose();
            }
        };
    }
}