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

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

/**
 * The options page pattern: a column of titled sections parted by hairlines,
 * inside a scroll pane. Built on {@link Sections}, so a settings page lines up
 * with every other page in the suite: the help mark in the gutter, the bold
 * title, a wrapping paragraph, then the controls on one left edge.
 */
public class OptionsPage extends JPanel {

    private final JPanel page = Sections.newPage();
    private final JScrollPane scroll;

    /** Client property on a row made by {@link #fieldRow} or {@link #labelRow}: its label. */
    private static final String ROW_LABEL = "opensuite.options.rowLabel";

    public OptionsPage() {
        super(new BorderLayout());
        scroll = Sections.scroll(page);
        // The page scrolls only when the user scrolls it: open at the top.
        addHierarchyListener(e -> {
            if ((e.getChangeFlags() & java.awt.event.HierarchyEvent.SHOWING_CHANGED) != 0 && isShowing()) {
                SwingUtilities.invokeLater(() -> scroll.getViewport().setViewPosition(new Point(0, 0)));
            }
        });
        add(scroll, BorderLayout.CENTER);
    }

    /** The mark under a section's help icon, or null for none. Pages that can restore a section override this. */
    protected JComponent secondMark(String title) { return null; }

    /**
     * Add a section with a heading and a body of stacked components. A leading
     * wrapping note becomes the section's description.
     */
    public JPanel section(String title, Component... parts) {
        return section(title, null, secondMark(title), null, parts);
    }

    /**
     * The full form: the description given outright, a mark for under the help
     * mark and a component for the right end of the heading row (either may be null).
     */
    public JPanel section(String title, String description, JComponent second, JComponent trailing, Component... parts) {
        title = Ui.sentence(title);
        List<Component> body = new ArrayList<>();
        int labelWidth = 0;
        for (Component c : parts) {
            if (c == null) continue;
            if (c instanceof JTextArea ta && ta.getLineWrap()) {
                if (description == null && body.isEmpty()) { description = ta.getText(); continue; }
                // Text already made for a section keeps what was set on it; a note is restyled.
                if (!Sections.isFill(ta)) c = Sections.text(ta.getText());
            }
            if (c instanceof JComponent jc && jc.getClientProperty(ROW_LABEL) instanceof JLabel l) {
                labelWidth = Math.max(labelWidth, l.getPreferredSize().width);
            }
            // A tick box or radio standing on its own line is 20px tall and set 2px in.
            if (c instanceof JCheckBox || c instanceof JRadioButton) {
                AbstractButton b = (AbstractButton) c;
                b.setBorder(Ui.pad(0, 2, 0, 0));
                b.setPreferredSize(new Dimension(b.getPreferredSize().width, Ui.tall(20)));
            }
            if (!body.isEmpty()) body.add(Sections.gap(Sections.ROW_GAP));
            body.add(c);
        }
        // Labels in one section share a column, so their fields line up.
        for (Component c : body) {
            if (c instanceof JComponent jc && jc.getClientProperty(ROW_LABEL) instanceof JLabel l) {
                l.setPreferredSize(new Dimension(labelWidth, l.getPreferredSize().height));
            }
        }
        if (title == null || title.isEmpty()) {
            JPanel raw = Sections.column(0, body.toArray(new Component[0]));
            raw.setBorder(Ui.pad(8, Sections.MARGIN, 15, Sections.MARGIN));
            Sections.append(page, raw);
            return raw;
        }
        JPanel s = Sections.section(title, description, second, trailing, body.toArray(new Component[0]));
        s.setBorder(Ui.pad(8, Sections.MARGIN, 15, Sections.MARGIN));
        Sections.append(page, s);
        sections.add(s);
        return s;
    }

    private final List<JPanel> sections = new ArrayList<>();

    /** The titled sections of this page, in order. */
    public List<JPanel> sections() { return sections; }

    /**
     * Extra space above and beside the page, inside its scroll pane: the
     * settings window sets a page 10px in on each of those sides.
     */
    public void setPageInsets(int top, int left, int right) {
        page.setBorder(top == 0 && left == 0 && right == 0 ? null : Ui.pad(top, left, 0, right));
        page.revalidate();
        page.repaint();
    }

    /** The least width of the page: in a narrower window it scrolls sideways, as the reference's wide pages do. */
    public void setMinWidth(int width) { Ui.columnMinWidth(scroll, width); }

    /** How far one notch of the wheel moves the page, in pixels (a notch is three of the bar's units). */
    public void setWheelStep(int px) { scroll.getVerticalScrollBar().setUnitIncrement(Math.max(1, px / 3)); }

    /** Add an already-built component as its own section. */
    public void addSection(Component c) {
        if (c instanceof JComponent jc) Sections.append(page, jc);
    }

    private static JPanel row(String label, Component control, String suffix) {
        JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
        p.setOpaque(false);
        JLabel l = Ui.label(label);
        l.setPreferredSize(new Dimension(l.getPreferredSize().width, Ui.tall(22)));
        p.add(l);
        p.add(Box.createHorizontalStrut(5));
        p.add(control);
        if (suffix != null && !suffix.isEmpty()) {
            p.add(Box.createHorizontalStrut(5));
            // The unit after a box is written as given ("seconds"), not started with a capital.
            JLabel unit = new JLabel(suffix);
            unit.setFont(Ui.PLAIN);
            unit.setForeground(Theme.TEXT);
            p.add(unit);
        }
        p.putClientProperty(ROW_LABEL, l);
        return p;
    }

    /** A labelled numeric or text field row; labels in one section share a column. */
    public static JPanel fieldRow(String label, JComponent field, String suffix) { return row(label, field, suffix); }

    /** A labelled control (combo box, etc.) row sharing the same label column. */
    public static JPanel labelRow(String label, Component control) { return row(label, control, null); }

    public static JPanel indent(Component c, int px) {
        JPanel p = new JPanel(new BorderLayout());
        p.setOpaque(false);
        p.setBorder(Ui.pad(0, px, 0, 0));
        p.add(c, BorderLayout.CENTER);
        return p;
    }
}