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.

10.3 KB
Raw Blame
package opensuite.ui;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.util.ArrayList;
import java.util.List;

/**
 * A question or a warning in the suite's own frame: a plain panel under a
 * coloured bar, with a mark and a title, a paragraph, an optional list under a
 * heading, and a footer of buttons that may begin with a tick box.
 *
 * <p>The figures are those measured at the usual 13px and are scaled from
 * there for other font sizes.
 */
public final class NoticeDialog extends JDialog {

    /** What the notice is about, which decides its colour and its mark. */
    public enum Kind { INFO, ERROR }

    private static final Color RED = new Color(0xD6, 0x21, 0x3F);
    private static final Color BLUE = new Color(0x26, 0x64, 0x9D);
    private static final Color EDGE = Theme.dark ? new Color(0x55, 0x55, 0x55) : new Color(0xDF, 0xDF, 0xDF);

    private int chosen = -1;
    private final JCheckBox tick;

    private static int px(int at13) { return Math.round(at13 * Theme.FONT_SIZE / 13f); }

    private NoticeDialog(Window owner, Kind kind, String title, String text, String listHeading, List<String> list,
                         String tickLabel, int primary, String... buttons) {
        super(owner, title, ModalityType.APPLICATION_MODAL);
        setUndecorated(true);
        final Color colour = kind == Kind.ERROR ? RED : BLUE;
        final Font titleFont = Ui.HEADING.deriveFont(Font.BOLD, (float) Ui.em(16));
        final Font headingFont = Ui.HEADING.deriveFont(Font.BOLD, (float) Ui.em(15));
        final List<String> items = list == null ? List.of() : list;

        JPanel p = new JPanel(null);
        FontMetrics fm = p.getFontMetrics(Ui.PLAIN);
        int width = px(456);
        for (String item : items) width = Math.max(width, px(85) + fm.stringWidth(item) + px(30));
        final List<String> lines = wrap(text, fm, width - px(60) - px(22));

        // Lines of the paragraph stand 18px apart from 77px down; a list has its
        // heading 36px under the last of them and its items 28px apart from 36px
        // under that. The rule above the buttons is 24px under a paragraph's last
        // line and 52px under a list's last item, and the footer is 53px.
        final int lastLine = px(77) + px(18) * Math.max(0, lines.size() - 1);
        final int heading = lastLine + px(36);
        final int firstItem = heading + px(36);
        final int rule = items.isEmpty() ? lastLine + px(24) + (lines.isEmpty() ? 0 : 0)
                : firstItem + px(28) * (items.size() - 1) + px(52);
        final int height = rule + px(54);
        final int w = width;

        JButton[] made = new JButton[buttons.length];
        for (int i = 0; i < buttons.length; i++) {
            final int index = i;
            made[i] = i == primary ? Ui.primaryButton(buttons[i], null) : Ui.button(buttons[i]);
            // Beside a tick box the quiet button is the usual grey; on its own it is white inside its outline.
            if (i != primary && tickLabel == null) made[i].putClientProperty(opensuite.ui.theme.FlatButtonUI.OUTLINED, Boolean.TRUE);
            made[i].addActionListener(e -> { chosen = index; dispose(); });
        }
        tick = tickLabel == null ? null : Ui.check(tickLabel, false);

        JPanel body = new JPanel(null) {
            @Override protected void paintComponent(Graphics g) {
                super.paintComponent(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);
                Theme.textSmoothing(g2);
                g2.setColor(EDGE);
                g2.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
                g2.setColor(colour);
                g2.fillRect(0, 1, getWidth(), px(6));
                g2.setColor(EDGE);
                g2.fillRect(0, rule, getWidth(), 1);

                if (kind == Kind.ERROR) warning(g2, colour, px(21), px(32), px(23), px(21));
                else info(g2, colour, px(21), px(32), px(23));

                g2.setColor(Theme.TAB_TEXT_ON);
                g2.setFont(titleFont);
                g2.drawString(title, px(60), px(51));
                g2.setColor(Theme.TEXT);
                g2.setFont(Ui.PLAIN);
                int y = px(82);
                for (String line : lines) { g2.drawString(line, px(60), y); y += px(18); }
                if (!items.isEmpty()) {
                    g2.setFont(headingFont);
                    g2.drawString(listHeading == null ? "" : listHeading, px(60), heading + px(6));
                    g2.setFont(Ui.PLAIN);
                    y = firstItem + px(5);
                    for (String item : items) {
                        int r = Math.max(2, px(2));
                        g2.fillOval(px(73), y - px(5) - r / 2, 2 * r, 2 * r);
                        g2.drawString(item, px(85), y);
                        y += px(28);
                    }
                }
                g2.dispose();
            }

            @Override public void doLayout() {
                int x = getWidth() - px(16);
                int h = made.length == 0 ? 0 : made[made.length - 1].getPreferredSize().height;
                int top = rule + (getHeight() - rule - h) / 2 + 1;
                for (int i = made.length - 1; i >= 0; i--) {
                    int bw = Math.max(made[i].getPreferredSize().width, px(86));
                    x -= bw;
                    made[i].setBounds(x, top, bw, h);
                    x -= px(10);
                }
                if (tick != null) {
                    Dimension d = tick.getPreferredSize();
                    tick.setBounds(px(17), rule + (getHeight() - rule - d.height) / 2 + 1, d.width, d.height);
                }
            }
        };
        body.setBackground(Theme.BG);
        for (JButton b : made) body.add(b);
        if (tick != null) { tick.setOpaque(false); body.add(tick); }
        // Wide enough for the footer whatever it holds.
        int footer = px(16) + px(17);
        for (JButton b : made) footer += Math.max(b.getPreferredSize().width, px(86)) + px(10);
        if (tick != null) footer += tick.getPreferredSize().width + px(12);
        body.setPreferredSize(new Dimension(Math.max(w, footer), height));
        setContentPane(body);
        if (primary >= 0 && primary < made.length) getRootPane().setDefaultButton(made[primary]);
        getRootPane().registerKeyboardAction(e -> dispose(), KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0),
                JComponent.WHEN_IN_FOCUSED_WINDOW);
        pack();
        setLocationRelativeTo(owner);
    }

    /** Break a paragraph into lines no wider than {@code width}; a newline in it always breaks. */
    static List<String> wrap(String text, FontMetrics fm, int width) {
        List<String> out = new ArrayList<>();
        if (text == null || text.isEmpty()) return out;
        for (String para : text.split("\n", -1)) {
            StringBuilder line = new StringBuilder();
            for (String word : para.split(" ")) {
                String tried = line.length() == 0 ? word : line + " " + word;
                if (line.length() > 0 && fm.stringWidth(tried) > width) {
                    out.add(line.toString());
                    line = new StringBuilder(word);
                } else {
                    line = new StringBuilder(tried);
                }
            }
            out.add(line.toString());
        }
        return out;
    }

    /** Our own warning mark: a round-cornered triangle in outline with an exclamation mark. */
    private static void warning(Graphics2D g2, Color c, int x, int y, int w, int h) {
        g2.setColor(c);
        g2.setStroke(new BasicStroke(Math.max(1.6f, w / 12f), BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
        Path2D.Double t = new Path2D.Double();
        double r = w * 0.13;
        t.moveTo(x + w / 2.0 - r * 0.6, y + r * 0.9);
        t.quadTo(x + w / 2.0, y - r * 0.2, x + w / 2.0 + r * 0.6, y + r * 0.9);
        t.lineTo(x + w - r * 0.5, y + h - r * 1.4);
        t.quadTo(x + w, y + h, x + w - r * 1.3, y + h);
        t.lineTo(x + r * 1.3, y + h);
        t.quadTo(x, y + h, x + r * 0.5, y + h - r * 1.4);
        t.closePath();
        g2.draw(t);
        double cx = x + w / 2.0;
        g2.draw(new Line2D.Double(cx, y + h * 0.38, cx, y + h * 0.62));
        g2.fill(new Ellipse2D.Double(cx - w * 0.055, y + h * 0.76, w * 0.11, w * 0.11));
    }

    /** Our own information mark: a ring with a lower-case i. */
    private static void info(Graphics2D g2, Color c, int x, int y, int d) {
        g2.setColor(c);
        float s = Math.max(1.6f, d / 12f);
        g2.setStroke(new BasicStroke(s, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
        g2.draw(new Ellipse2D.Double(x + s / 2, y + s / 2, d - s, d - s));
        double cx = x + d / 2.0;
        g2.draw(new Line2D.Double(cx, y + d * 0.46, cx, y + d * 0.70));
        g2.fill(new Ellipse2D.Double(cx - d * 0.055, y + d * 0.26, d * 0.11, d * 0.11));
    }

    /** What was chosen, with whether the tick box (if there was one) was ticked. */
    public record Answer(int button, boolean ticked) {
        /** True when the notice was dismissed without a button being chosen. */
        public boolean dismissed() { return button < 0; }
    }

    /**
     * Show a notice and wait for the answer.
     *
     * @param text        the paragraph; newlines break it, long lines wrap
     * @param listHeading the heading over the list, or null when there is none
     * @param list        the items listed under the heading, or null
     * @param tickLabel   a tick box at the left of the footer, or null
     * @param primary     the index of the button filled with the accent, which Enter chooses
     */
    public static Answer ask(Component owner, Kind kind, String title, String text, String listHeading, List<String> list,
                             String tickLabel, int primary, String... buttons) {
        Window w = owner instanceof Window win ? win : owner == null ? null : SwingUtilities.getWindowAncestor(owner);
        NoticeDialog d = new NoticeDialog(w, kind, title, text, listHeading, list, tickLabel, primary, buttons);
        d.setVisible(true);
        return new Answer(d.chosen, d.tick != null && d.tick.isSelected());
    }
}