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.

21.3 KB
Raw Blame
package opensuite.ui;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * The startup screen, shown before the main window, where the user chooses a
 * project: a temporary in-memory session, a new project saved to disk, or an
 * existing project to reopen. Shown as a welcome dialog when the app starts.
 */
public final class ProjectStartupDialog extends JDialog {

    public enum Mode { TEMPORARY, NEW, OPEN }

    /** Fixed width of the "Name:" / "File:" label column so every field aligns. */
    private static final int LABEL_W = 56;

    private final JRadioButton temporary = new JRadioButton("Temporary project in memory", true);
    private final JRadioButton newOnDisk = new JRadioButton("New project on disk");
    private final JRadioButton openExisting = new JRadioButton("Open existing project");
    private final JTextField newName = Ui.field("");
    private final JTextField newFile = Ui.field("");
    private final JTextField openFile = Ui.field("");
    private final JCheckBox trust = new JCheckBox("Trust this project file", true);
    private final JCheckBox pause = new JCheckBox("Pause automated tasks");
    private final List<RecentProjects.Entry> recents = RecentProjects.load();
    private final JTable recentsTable = new JTable();

    private boolean proceeded;

    public ProjectStartupDialog(Frame owner, String title) {
        super(owner, title, ModalityType.APPLICATION_MODAL);
        // Shown alone at startup it has no owner: then it is an ordinary window with its own taskbar entry.
        if (owner == null) setType(Type.NORMAL);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setIconImages(AppIcon.images());

        ButtonGroup g = new ButtonGroup();
        g.add(temporary); g.add(newOnDisk); g.add(openExisting);
        for (JRadioButton r : new JRadioButton[]{temporary, newOnDisk, openExisting}) {
            r.setFont(Ui.BOLD); r.setOpaque(false); r.setFocusable(false);
        }

        // The Name and the file name are one thing: edit either and the other
        // follows, so a project can never be shown under a name its file does
        // not carry.
        newName.getDocument().addDocumentListener(new javax.swing.event.DocumentListener() {
            public void insertUpdate(javax.swing.event.DocumentEvent e)  { syncFileToName(); }
            public void removeUpdate(javax.swing.event.DocumentEvent e)  { syncFileToName(); }
            public void changedUpdate(javax.swing.event.DocumentEvent e) { syncFileToName(); }
        });
        newFile.getDocument().addDocumentListener(new javax.swing.event.DocumentListener() {
            public void insertUpdate(javax.swing.event.DocumentEvent e)  { syncNameToFile(); }
            public void removeUpdate(javax.swing.event.DocumentEvent e)  { syncNameToFile(); }
            public void changedUpdate(javax.swing.event.DocumentEvent e) { syncNameToFile(); }
        });

        // Only fill in a default name once "New project on disk" is actually
        // chosen (by click or by the folder picker), never while on a temporary
        // or existing project.
        newOnDisk.addItemListener(e -> {
            if (!newOnDisk.isSelected()) return;
            if (newName.getText().trim().isEmpty()) newName.setText(defaultProjectName());
            // Default the file to the user's home directory, shown in full.
            if (newFile.getText().trim().isEmpty()) {
                newFile.setText(new File(System.getProperty("user.home"), projectFileName()).getAbsolutePath());
            }
        });

        // A two-column grid: radio labels in a left column, their inputs in an
        // aligned right column, with a thin separator between each section.
        JPanel body = new JPanel(new GridBagLayout());
        body.setBackground(Theme.BG);
        body.setBorder(Ui.pad(14, 20, 10, 20));
        int y = 0;

        addSpan(body, header(), y++);
        addRow(body, temporary, null, y++);
        addSeparator(body, y++);
        addRow(body, newOnDisk, newFields(), y++);
        addSeparator(body, y++);
        addRow(body, openExisting, openFields(), y++);

        setLayout(new BorderLayout());
        add(body, BorderLayout.CENTER);
        add(buttons(), BorderLayout.SOUTH);
        // Open at the form's natural size so the whole thing is visible at once,
        // with no scroll bar. The dialog stays resizable if the user wants room.
        pack();
        setSize(Math.max(900, getWidth()), getHeight());
        setLocationRelativeTo(null);

        // pack() runs before the window manager adds its title bar, so on many
        // Linux desktops the decoration insets are still zero and the window
        // opens a title-bar's-worth too short, clipping the bottom controls.
        // Once the window is actually shown the real insets are known, so grow
        // it then to guarantee the whole form fits.
        addWindowListener(new java.awt.event.WindowAdapter() {
            private boolean fixed;
            @Override public void windowOpened(java.awt.event.WindowEvent e) {
                if (fixed) return;
                fixed = true;
                // Make sure the dialog is raised above other windows and focused.
                toFront();
                requestFocus();
                SwingUtilities.invokeLater(() -> {
                    Dimension pref = getContentPane().getPreferredSize();
                    Insets in = getInsets();
                    int top = in.top > 0 ? in.top : 34;
                    int wantW = Math.max(900, pref.width + in.left + in.right);
                    int wantH = pref.height + top + in.bottom + 6;
                    if (getWidth() < wantW || getHeight() < wantH) {
                        setSize(Math.max(getWidth(), wantW), Math.max(getHeight(), wantH));
                        setLocationRelativeTo(null);
                    }
                });
            }
        });
    }

    // ---- grid helpers ----

    /** A section row: the radio label at the top of the left column, inputs on the right. */
    private void addRow(JPanel grid, JComponent label, JComponent fields, int y) {
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0; c.gridy = y; c.anchor = GridBagConstraints.NORTHWEST;
        c.insets = new Insets(8, 0, 8, 20);
        grid.add(label, c);
        if (fields != null) {
            c.gridx = 1; c.weightx = 1; c.fill = GridBagConstraints.HORIZONTAL;
            c.insets = new Insets(8, 0, 8, 0);
            grid.add(fields, c);
        }
    }

    private void addSpan(JPanel grid, JComponent comp, int y) {
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0; c.gridy = y; c.gridwidth = 2; c.weightx = 1;
        c.fill = GridBagConstraints.HORIZONTAL; c.insets = new Insets(0, 0, 14, 0);
        grid.add(comp, c);
    }

    private void addSeparator(JPanel grid, int y) {
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0; c.gridy = y; c.gridwidth = 2; c.weightx = 1;
        c.fill = GridBagConstraints.HORIZONTAL; c.insets = new Insets(8, 0, 8, 0);
        JSeparator s = new JSeparator();
        s.setForeground(Ui.GRID);
        grid.add(s, c);
    }

    /** A labelled input row with a fixed-width right-aligned label so fields align. */
    private JComponent formRow(String label, JComponent field) {
        JPanel row = new JPanel(new BorderLayout(8, 0));
        row.setOpaque(false);
        JLabel l = new JLabel(label, SwingConstants.RIGHT);
        l.setFont(Ui.PLAIN);
        l.setPreferredSize(new Dimension(LABEL_W, l.getPreferredSize().height));
        row.add(l, BorderLayout.WEST);
        row.add(field, BorderLayout.CENTER);
        row.setAlignmentX(Component.LEFT_ALIGNMENT);
        row.setMaximumSize(new Dimension(Integer.MAX_VALUE, row.getPreferredSize().height));
        return row;
    }

    // ---- sections ----

    private JComponent header() {
        JPanel p = new JPanel(new BorderLayout());
        p.setOpaque(false);
        JPanel leftSide = new JPanel(new FlowLayout(FlowLayout.LEFT, 8, 0));
        leftSide.setOpaque(false);
        leftSide.add(helpGlyph());
        JLabel welcome = new JLabel("Welcome to Open Suite. Use the options below to create or open a project.");
        welcome.setFont(Ui.PLAIN);
        leftSide.add(welcome);
        p.add(leftSide, BorderLayout.WEST);
        // The name set large at the right, as a wordmark: the mark, then the name in two tones.
        JLabel logo = new JLabel("<html><span style='color:" + hex(Theme.TEXT) + "'>Open</span> <span style='color:"
                + hex(Theme.ACCENT) + "'>Suite</span></html>", AppIcon.icon(52), SwingConstants.RIGHT);
        logo.setFont(Ui.HEADING.deriveFont(Font.BOLD, 34f));
        logo.setIconTextGap(10);
        logo.setBorder(Ui.pad(4, 0, 10, 0));
        JPanel top = new JPanel(new BorderLayout());
        top.setOpaque(false);
        top.add(leftSide, BorderLayout.NORTH);
        p.remove(leftSide);
        p.add(top, BorderLayout.WEST);
        p.add(logo, BorderLayout.EAST);
        return p;
    }

    private static String hex(Color c) { return String.format("#%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue()); }

    private JComponent newFields() {
        JPanel p = new JPanel();
        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
        p.setOpaque(false);
        // The name runs the full width; the file box below it stops short for its button.
        p.add(formRow("Name:", newName));
        p.add(Box.createVerticalStrut(6));
        p.add(formRow("File:", fileRow(newFile)));
        return p;
    }

    private JComponent openFields() {
        JPanel p = new JPanel();
        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
        p.setOpaque(false);

        // Recent-projects table: Name, File, Last Accessed, newest first. Clicking a
        // row fills the File field and selects this option. Always shown, empty on a
        // first launch.
        String[] cols = {"Name", "File", "Last Accessed"};
        DefaultTableModel model = new DefaultTableModel(cols, 0) {
            @Override public boolean isCellEditable(int r, int c) { return false; }
            @Override public Class<?> getColumnClass(int c) { return c == 2 ? java.util.Date.class : String.class; }
        };
        for (RecentProjects.Entry e : recents) {
            model.addRow(new Object[]{e.name(), e.file().getAbsolutePath(), new java.util.Date(e.lastAccessed())});
        }
        recentsTable.setModel(model);
        Ui.styleTable(recentsTable);
        Ui.leftAlign(recentsTable);
        recentsTable.setFont(Ui.PLAIN);
        recentsTable.setShowGrid(false);
        recentsTable.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);   // the columns fill the table: nothing to scroll sideways
        recentsTable.setFillsViewportHeight(true);
        recentsTable.getTableHeader().setReorderingAllowed(false);
        recentsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        recentsTable.getColumnModel().getColumn(0).setPreferredWidth(130);
        recentsTable.getColumnModel().getColumn(1).setPreferredWidth(330);
        recentsTable.getColumnModel().getColumn(2).setPreferredWidth(130);
        // The day alone, in the short form of the machine's own language: 2/12/2026.
        final SimpleDateFormat fmt = shortDate();
        recentsTable.getColumnModel().getColumn(2).setCellRenderer(new javax.swing.table.DefaultTableCellRenderer() {
            @Override protected void setValue(Object v) { setText(v instanceof java.util.Date d ? fmt.format(d) : ""); }
        });
        recentsTable.setRowSorter(new opensuite.ui.NumberSorter<>(recentsTable.getModel()));
        recentsTable.getRowSorter().setSortKeys(List.of(new RowSorter.SortKey(2, SortOrder.DESCENDING)));
        recentsTable.getSelectionModel().addListSelectionListener(ev -> {
            if (ev.getValueIsAdjusting()) return;
            int row = recentsTable.getSelectedRow();
            if (row >= 0) {
                openFile.setText((String) model.getValueAt(recentsTable.convertRowIndexToModel(row), 1));
                openExisting.setSelected(true);
            }
        });
        JScrollPane sp = new JScrollPane(recentsTable);
        sp.setPreferredSize(new Dimension(590, 210));
        sp.setMaximumSize(new Dimension(Integer.MAX_VALUE, 210));
        // The table starts where the boxes start: the labels hang to its left.
        JPanel tableRow = new JPanel(new BorderLayout());
        tableRow.setOpaque(false);
        tableRow.setBorder(Ui.pad(0, LABEL_W + 8, 0, 0));
        tableRow.add(sp, BorderLayout.CENTER);
        tableRow.setAlignmentX(Component.LEFT_ALIGNMENT);
        tableRow.setMaximumSize(new Dimension(Integer.MAX_VALUE, 210));
        p.add(tableRow);
        p.add(Box.createVerticalStrut(8));
        p.add(formRow("File:", fileRow(openFile)));

        // Checkboxes, indented so they line up under the field, not the label.
        trust.setFont(Ui.PLAIN); trust.setOpaque(false);
        pause.setFont(Ui.PLAIN); pause.setOpaque(false);
        // Both belong to opening a file: greyed until that option is the one chosen.
        openExisting.addItemListener(e -> enableChecks());
        enableChecks();
        trust.setAlignmentX(Component.LEFT_ALIGNMENT);
        pause.setAlignmentX(Component.LEFT_ALIGNMENT);
        JPanel checks = new JPanel();
        checks.setLayout(new BoxLayout(checks, BoxLayout.Y_AXIS));
        checks.setOpaque(false);
        checks.setBorder(Ui.pad(6, LABEL_W + 8, 0, 0));
        checks.setAlignmentX(Component.LEFT_ALIGNMENT);
        checks.add(trust);
        checks.add(pause);
        p.add(checks);
        return p;
    }

    private boolean trustEditable;

    private void enableChecks() {
        pause.setEnabled(openExisting.isSelected());
        trust.setEnabled(openExisting.isSelected() && trustEditable);
    }

    /** The short date of the machine's language with the year in full. */
    static SimpleDateFormat shortDate() {
        java.text.DateFormat d = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT);
        if (d instanceof SimpleDateFormat f) {
            String pat = f.toPattern();
            if (!pat.contains("yyyy")) pat = pat.replaceAll("y+", "yyyy");
            return new SimpleDateFormat(pat);
        }
        return new SimpleDateFormat("d/M/yyyy");
    }

    private JComponent fileRow(JTextField field) {
        boolean isNew = field == newFile;
        JButton choose = Ui.button("Choose file...");
        choose.addActionListener(e -> {
            JFileChooser fc = new opensuite.ui.FileChooser();
            if (isNew) {
                // Pick where the project file goes and what it is called; the Name follows it.
                fc.setDialogTitle("Choose a file for the new project");
                String cur = newFile.getText().trim();
                File start = cur.isEmpty() ? new File(System.getProperty("user.home"), defaultProjectName()) : new File(cur);
                if (start.getParentFile() != null && start.getParentFile().isDirectory()) {
                    fc.setCurrentDirectory(start.getParentFile());
                    fc.setSelectedFile(start);
                }
                if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
                    File f = fc.getSelectedFile();
                    if (!f.getName().contains(".")) f = new File(f.getParentFile(), f.getName() + ".osuite");
                    newOnDisk.setSelected(true);
                    newFile.setText(f.getAbsolutePath());
                }
            } else {
                if (fc.showDialog(this, "Select") == JFileChooser.APPROVE_OPTION) {
                    field.setText(fc.getSelectedFile().getAbsolutePath());
                    openExisting.setSelected(true);
                }
            }
        });
        JPanel row = new JPanel(new BorderLayout(8, 0));
        row.setOpaque(false);
        row.add(field, BorderLayout.CENTER);
        row.add(choose, BorderLayout.EAST);
        return row;
    }

    /** The default project name, shown with its .osuite extension so the user sees the full filename. */
    private static String defaultProjectName() {
        return StateIO.defaultProjectFileName();
    }

    /**
     * The on-disk file name derived from the Name field. The Name already shows
     * the .osuite extension, so this mirrors it exactly (just making the characters
     * filesystem-safe) rather than re-appending an extension, which would double
     * it up to ".osuit.osuite" as the user edits.
     */
    private String projectFileName() {
        String n = newName.getText().trim();
        if (n.isEmpty()) n = defaultProjectName();
        return n.replaceAll("[^A-Za-z0-9._-]", "_");
    }

    // Set while one field is being written from the other, so the two document
    // listeners do not feed each other.
    private boolean syncing;

    /** Keep the chosen file in step with the Name: edit the name, the file follows. */
    private void syncFileToName() {
        if (syncing) return;
        String path = newFile.getText().trim();
        if (path.isEmpty()) return;
        File parent = new File(path).getParentFile();
        if (parent == null) return;
        String want = new File(parent, projectFileName()).getAbsolutePath();
        if (want.equals(path)) return;
        syncing = true;
        try { SwingUtilities.invokeLater(() -> { newFile.setText(want); syncing = false; }); }
        catch (RuntimeException e) { syncing = false; throw e; }
    }

    /** Keep the Name in step with the chosen file: edit the file, the name follows. */
    private void syncNameToFile() {
        if (syncing) return;
        String path = newFile.getText().trim();
        if (path.isEmpty()) return;
        String want = new File(path).getName();
        if (want.isEmpty() || want.equals(newName.getText().trim())) return;
        syncing = true;
        try { SwingUtilities.invokeLater(() -> { newName.setText(want); syncing = false; }); }
        catch (RuntimeException e) { syncing = false; throw e; }
    }

    /** A small grey question-mark glyph, matching the welcome header. */
    private static JComponent helpGlyph() {
        JComponent c = new JComponent() {
            @Override protected void paintComponent(Graphics g) {
                Graphics2D g2 = (Graphics2D) g.create();
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2.setColor(Theme.MUTED);
                g2.setStroke(new BasicStroke(1.4f));
                g2.drawOval(2, 2, 19, 19);
                g2.setFont(Ui.BOLD);
                FontMetrics fm = g2.getFontMetrics();
                int sw = fm.stringWidth("?");
                g2.drawString("?", (24 - sw) / 2f, 2 + (19 + fm.getAscent()) / 2f - 1);
                g2.dispose();
            }
        };
        c.setPreferredSize(new Dimension(24, 24));
        return c;
    }

    private JComponent buttons() {
        JButton cancel = Ui.button("Cancel");
        JButton next = Ui.primaryButton("Next", "open the project");
        cancel.addActionListener(e -> { proceeded = false; dispose(); });
        next.addActionListener(e -> onNext());
        Ui.sameSize(cancel, next);
        JPanel bar = new JPanel(new FlowLayout(FlowLayout.RIGHT, 6, 8));
        // The buttons end where the boxes above them end.
        bar.setBackground(Theme.BG);
        bar.setBorder(Ui.pad(0, 0, 4, 14));
        bar.add(cancel); bar.add(next);
        return bar;
    }

    private void onNext() {
        if (newOnDisk.isSelected() && newFile.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Choose a file for the new project.",
                    "New project", JOptionPane.WARNING_MESSAGE);
            return;
        }
        if (openExisting.isSelected() && openFile.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Choose a project file to open.",
                    "Open project", JOptionPane.WARNING_MESSAGE);
            return;
        }
        proceeded = true;
        dispose();
    }

    // ---- result ----

    public boolean proceeded() { return proceeded; }

    public Mode mode() {
        if (newOnDisk.isSelected()) return Mode.NEW;
        if (openExisting.isSelected()) return Mode.OPEN;
        return Mode.TEMPORARY;
    }

    public File file() {
        String t = mode() == Mode.NEW ? newFile.getText().trim()
                 : mode() == Mode.OPEN ? openFile.getText().trim() : "";
        return t.isEmpty() ? null : new File(t);
    }

    /** The project's name, which is its file name. */
    public String projectName() {
        File f = file();
        if (f != null && !f.getName().isBlank()) return f.getName();
        return newName.getText().trim();
    }

    /** Whether to open without automatically starting scanning tasks. */
    public boolean pauseTasks() { return pause.isSelected(); }

    /** Whether the owner ticked "Trust this project file". */
    public boolean trustFile() { return trust.isSelected(); }

    /**
     * Settings > Suite > Startup behavior: whether "Pause automated tasks" starts
     * ticked, and whether project files are trusted without asking. When they
     * are not, the trust box is the owner's to tick.
     */
    public void applyDefaults(boolean pauseByDefault, boolean alwaysTrust) {
        pause.setSelected(pauseByDefault);
        trustEditable = !alwaysTrust;
        trust.setSelected(alwaysTrust);
        enableChecks();
    }
}