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.

5.0 KB
Raw Blame
package opensuite.ui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;

/**
 * The question asked before the application closes, drawn inside the main
 * window over a dimmed view of it.
 *
 * <p>It is not a window of its own, and that is the point. The close button
 * in a title bar belongs to the desktop, so to the application no click has
 * happened; a window it then opens is, to a desktop that guards against
 * windows pushing themselves forward, uninvited, and is kept underneath. The
 * main window then waits for an answer to a question nobody can see. A panel
 * inside the window cannot be put behind it.
 */
final class ExitPrompt extends JPanel {

    /**
     * What closing now would cost, as a sentence, or empty when nothing would
     * be lost and the application can simply close.
     */
    static String risk(boolean scanRunning, boolean unsavedData) {
        if (scanRunning && unsavedData) return "A scan is running and this project is not saved to a file.";
        if (scanRunning) return "A scan is running and will be stopped.";
        if (unsavedData) return "This project is not saved to a file, so its contents will be lost.";
        return "";
    }

    private static final String KEY = "opensuite.exitPrompt";

    /** Ask in {@code frame} whether to exit. A second request while one is showing changes nothing. */
    static void ask(JFrame frame, String risk, Runnable onExit) {
        ask(frame, "Exit Open Suite?", risk, "Exit", "Close Open Suite", "Keep Open Suite open", onExit, null);
    }

    /**
     * Ask a question of the same kind in any window: {@code onYes} runs when it is agreed to,
     * {@code onNo} (which may be null) when it is not.
     */
    static void ask(RootPaneContainer frame, String heading, String risk, String yes, String yesTip, String noTip,
                    Runnable onYes, Runnable onNo) {
        Runnable onExit = onYes;
        JLayeredPane layers = frame.getLayeredPane();
        if (layers.getClientProperty(KEY) != null) return;
        ExitPrompt p = new ExitPrompt(heading, risk, yes, yesTip, noTip);
        layers.putClientProperty(KEY, p);
        Runnable close = () -> {
            layers.remove(p);
            layers.putClientProperty(KEY, null);
            layers.revalidate();
            layers.repaint();
        };
        Runnable refuse = () -> { close.run(); if (onNo != null) onNo.run(); };
        p.cancel.addActionListener(e -> refuse.run());
        p.exit.addActionListener(e -> { close.run(); onExit.run(); });
        p.registerKeyboardAction(e -> refuse.run(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), WHEN_IN_FOCUSED_WINDOW);
        p.setBounds(0, 0, layers.getWidth(), layers.getHeight());
        layers.add(p, JLayeredPane.MODAL_LAYER);
        // The panel covers the window whatever size it is taken to while the question stands.
        layers.addComponentListener(new java.awt.event.ComponentAdapter() {
            @Override public void componentResized(java.awt.event.ComponentEvent e) {
                if (layers.getClientProperty(KEY) == p) p.setBounds(0, 0, layers.getWidth(), layers.getHeight());
                else layers.removeComponentListener(this);
            }
        });
        layers.revalidate();
        layers.repaint();
        p.cancel.requestFocusInWindow();
    }

    private final JButton cancel;
    private final JButton exit;

    private ExitPrompt(String heading, String risk, String yes, String yesTip, String noTip) {
        super(new GridBagLayout());
        cancel = Ui.button("Cancel", noTip);
        exit = Ui.button(yes, yesTip);
        setOpaque(false);
        // Nothing underneath takes a click or the pointer while the question stands.
        addMouseListener(new MouseAdapter() { });
        addMouseMotionListener(new MouseAdapter() { });
        addMouseWheelListener(e -> { });
        setFocusCycleRoot(true);

        JLabel title = Ui.label(heading);
        title.setFont(Ui.BOLD.deriveFont(Ui.BOLD.getSize2D() + 2f));
        JLabel what = Ui.label("<html><div style='width:" + Ui.em(300) + "px'>" + risk + "</div></html>");
        Theme.primary(exit);
        Ui.sameSize(cancel, exit);
        JPanel buttons = new JPanel(new FlowLayout(FlowLayout.RIGHT, 8, 0));
        buttons.setOpaque(false);
        buttons.add(cancel);
        buttons.add(exit);

        JPanel card = new JPanel(new BorderLayout(0, Ui.em(10)));
        card.setBackground(Theme.BG);
        card.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(Theme.BORDER), Ui.pad(18, 20, 14, 20)));
        JPanel text = new JPanel(new BorderLayout(0, Ui.em(8)));
        text.setOpaque(false);
        text.add(title, BorderLayout.NORTH);
        text.add(what, BorderLayout.CENTER);
        card.add(text, BorderLayout.CENTER);
        card.add(buttons, BorderLayout.SOUTH);
        add(card);
    }

    @Override protected void paintComponent(Graphics g) {
        g.setColor(new Color(0, 0, 0, 110));
        g.fillRect(0, 0, getWidth(), getHeight());
    }
}