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.

8.1 KB
Raw Blame
package opensuite.ui.settings;

import opensuite.ui.Theme;
import opensuite.ui.Ui;
import opensuite.ui.theme.CheckIcon;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;

/**
 * A settings table of named rows with tick-box columns: each tick column's
 * header carries a box that sets the whole column, a row can be locked, and
 * Up and Down at the right reorder the rows.
 */
public final class CheckTable extends JPanel {

    /** What the table shows and changes. Tick columns are numbered from 0. */
    public interface Rows {
        int count();
        String name(int row);
        /** False leaves the cell empty: the row has no such setting. */
        default boolean has(int row, int column) { return true; }
        boolean get(int row, int column);
        void set(int row, int column, boolean value);
        /** A locked row is always as it is, and is marked with a padlock. */
        default boolean locked(int row) { return false; }
        /** A cell that is shown ticked or not but cannot be changed. */
        default boolean fixed(int row, int column) { return locked(row); }
        void move(int from, int to);
    }

    private static final int HEADER = 27, ROW = Ui.tall(18), BOX = 14 + Ui.fontGrowth();

    private final Rows rows;
    private final String first;
    private final String[] ticks;
    /** Column widths: the padlock column (0 for none), the name column, then each tick column. */
    private final int[] widths;
    /** The second of the two row colours, a shade under the page. */
    private static final Color STRIPE = new Color(0xFA, 0xFA, 0xFA);
    private int selected = -1;
    private final AbstractButton stamp = new JCheckBox();
    private final Icon box = new CheckIcon(false);
    private final JComponent grid = new JComponent() {
        @Override public Dimension getPreferredSize() { return new Dimension(total(), HEADER + 1 + rows.count() * ROW); }
        @Override protected void paintComponent(Graphics g) { paintGrid((Graphics2D) g); }
    };

    public CheckTable(Rows rows, String first, String[] ticks, int... widths) {
        super(new FlowLayout(FlowLayout.LEFT, 0, 0));
        this.rows = rows;
        this.first = first;
        this.ticks = ticks;
        // Column widths are given as they are at the smallest font; at the usual
        // 13px this table is measured at just under a quarter wider.
        this.widths = new int[widths.length];
        for (int i = 0; i < widths.length; i++) this.widths[i] = Math.round(widths[i] * (1 + 0.1167f * Ui.fontGrowth()));   // 737 becomes 909, leaving room for the buttons
        setOpaque(false);
        JButton up = Ui.button("Up"), down = Ui.button("Down");
        Ui.sizedAlike(73, up, down);
        up.addActionListener(e -> move(-1));
        down.addActionListener(e -> move(1));
        JPanel buttons = Ui.buttonColumn(up, down);
        buttons.setBorder(Ui.pad(0, 5, 0, 0));
        JPanel side = new JPanel(new BorderLayout());
        side.setOpaque(false);
        side.add(buttons, BorderLayout.NORTH);
        add(grid);
        add(side);
        // FlowLayout centres a short component; the buttons belong at the top.
        side.setPreferredSize(new Dimension(up.getPreferredSize().width + 5, grid.getPreferredSize().height));
        grid.addMouseListener(new MouseAdapter() {
            @Override public void mousePressed(MouseEvent e) { pressed(e.getX(), e.getY()); }
        });
    }

    private int total() { int t = 0; for (int w : widths) t += w; return t; }
    private int left(int column) { int x = 0; for (int i = 0; i < column; i++) x += widths[i]; return x; }

    private void move(int by) {
        int to = selected + by;
        if (selected < 0 || to < 0 || to >= rows.count() || rows.locked(selected) || rows.locked(to)) return;
        rows.move(selected, to);
        selected = to;
        grid.repaint();
    }

    /** 1 when every changeable box of the column is ticked, 0 when none is, -1 when they differ. */
    private int columnState(int column) {
        int on = 0, all = 0;
        for (int r = 0; r < rows.count(); r++) {
            if (!rows.has(r, column) || rows.fixed(r, column)) continue;
            all++;
            if (rows.get(r, column)) on++;
        }
        return all == 0 || on == 0 ? 0 : on == all ? 1 : -1;
    }

    private void pressed(int x, int y) {
        for (int c = 0; c < ticks.length; c++) {
            int cx = left(c + 2);
            if (x < cx || x >= cx + widths[c + 2]) continue;
            if (y < HEADER) {
                // The header box ticks the whole column, or clears it once all are ticked.
                boolean to = columnState(c) != 1;
                for (int r = 0; r < rows.count(); r++) if (rows.has(r, c) && !rows.fixed(r, c)) rows.set(r, c, to);
                grid.repaint();
                return;
            }
            int r = (y - HEADER - 1) / ROW;
            if (r >= 0 && r < rows.count() && x < cx + 5 + BOX + 4) {
                if (rows.has(r, c) && !rows.fixed(r, c)) rows.set(r, c, !rows.get(r, c));
                grid.repaint();
                return;
            }
        }
        int r = y < HEADER ? -1 : (y - HEADER - 1) / ROW;
        selected = r >= 0 && r < rows.count() ? r : -1;
        grid.repaint();
    }

    private void box(Graphics2D g, int x, int y, boolean on, boolean enabled) {
        stamp.setSelected(on);
        stamp.setEnabled(enabled);
        box.paintIcon(stamp, g, x, y);
    }

    private void paintGrid(Graphics2D g) {
        opensuite.ui.Theme.textSmoothing(g);
        g.setFont(Ui.PLAIN);
        FontMetrics fm = g.getFontMetrics();
        int w = total();
        // ---- header ----
        g.setColor(Theme.SURFACE);
        g.fillRect(0, 0, w, HEADER);
        g.setColor(Theme.BORDER);
        g.fillRect(0, HEADER, w, 1);
        int base = (HEADER - fm.getHeight()) / 2 + fm.getAscent();
        for (int c = 1; c < widths.length; c++) {
            int x = left(c);
            if (x > 0) { g.setColor(Theme.BORDER); g.fillRect(x, 0, 1, HEADER); }
            g.setColor(Theme.TEXT);
            if (c == 1) { g.drawString(first, x + (x == 0 ? 8 : 4), base); continue; }
            int state = columnState(c - 2);
            int by = (HEADER - BOX) / 2;
            if (state >= 0) box(g, x + 5, by, state == 1, true);
            else {
                Graphics2D g2 = (Graphics2D) g.create();
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2.setColor(Theme.CONTROL);
                g2.fill(new RoundRectangle2D.Float(x + 5, by, BOX, BOX, 6, 6));
                g2.setColor(Color.WHITE);
                g2.fillRect(x + 5 + 3, by + BOX / 2 - 1, BOX - 6, 2);
                g2.dispose();
            }
            g.setColor(Theme.TEXT);
            g.drawString(ticks[c - 2], x + 29 + Ui.fontGrowth(), base);
        }
        // ---- rows ----
        for (int r = 0; r < rows.count(); r++) {
            int y = HEADER + 1 + r * ROW;
            g.setColor(r == selected ? Ui.SELECTION : r % 2 == 0 ? Theme.CONTENT : Theme.dark ? Theme.BG : STRIPE);
            g.fillRect(0, y, w, ROW);
            boolean locked = rows.locked(r);
            if (locked && widths[0] > 0) padlock(g, widths[0] / 2 - 2, y + 4);
            g.setColor(Theme.TEXT);
            g.drawString(rows.name(r), left(1) + (left(1) == 0 ? 8 : 4), y + (ROW - fm.getHeight()) / 2 + fm.getAscent());
            for (int c = 0; c < ticks.length; c++) {
                if (rows.has(r, c)) box(g, left(c + 2) + 5, y + (ROW - BOX) / 2, rows.get(r, c), !rows.fixed(r, c));
            }
        }
    }

    /** A small closed padlock, 9 by 10, with its top-left at x, y. */
    private static void padlock(Graphics2D g, int x, int y) {
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(Theme.TEXT);
        g2.setStroke(new BasicStroke(1f));
        g2.drawArc(x + 3, y, 5, 7, 0, 180);
        g2.draw(new RoundRectangle2D.Float(x + 0.5f, y + 4.5f, 10, 5, 2, 2));
        g2.fillRect(x + 5, y + 6, 1, 2);
        g2.dispose();
    }
}