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

import opensuite.core.Settings;
import opensuite.ui.Glyphs;
import opensuite.ui.Hotkeys;
import opensuite.ui.Sections;
import opensuite.ui.Theme;
import opensuite.ui.Ui;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.awt.geom.RoundRectangle2D;
import java.util.ArrayList;
import java.util.List;

/** Settings > User interface > Hotkeys: every action that can have a hotkey, and the key it has. */
public final class HotkeysPage extends SettingsPage {

    private final Settings s;
    private final List<Hotkeys.Action> shown = new ArrayList<>(Hotkeys.ACTIONS);
    private final AbstractTableModel model = new AbstractTableModel() {
        @Override public int getRowCount() { return shown.size(); }
        @Override public int getColumnCount() { return 3; }
        @Override public String getColumnName(int c) { return c == 0 ? "Action" : c == 1 ? "Category" : "Hotkey"; }
        @Override public Object getValueAt(int r, int c) {
            Hotkeys.Action a = shown.get(r);
            return c == 0 ? a.name() : c == 1 ? a.category() : Hotkeys.binding(s, a);
        }
    };

    public HotkeysPage(Settings s) {
        this.s = s;
        JTextField search = Ui.searchField(20);
        Ui.sized(search, 233);
        search.getDocument().addDocumentListener(new javax.swing.event.DocumentListener() {
            public void insertUpdate(javax.swing.event.DocumentEvent e) { filter(search.getText()); }
            public void removeUpdate(javax.swing.event.DocumentEvent e) { filter(search.getText()); }
            public void changedUpdate(javax.swing.event.DocumentEvent e) { filter(search.getText()); }
        });

        JTable t = new JTable(model);
        Ui.styleTable(t);
        t.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        Ui.proportionalWidths(t, 490, 248, 255);
        t.getColumnModel().getColumn(0).setCellRenderer(new DefaultTableCellRenderer() {
            @Override public Component getTableCellRendererComponent(JTable tb, Object v, boolean sel, boolean focus, int row, int col) {
                super.getTableCellRendererComponent(tb, v, sel, focus, row, col);
                setIcon(mark(shown.get(row).icon()));
                setIconTextGap(8);
                setBorder(Ui.pad(0, 8, 0, 0));
                return this;
            }
        });
        t.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override public void mouseClicked(java.awt.event.MouseEvent e) {
                int r = t.getSelectedRow();
                if (e.getClickCount() == 2 && r >= 0) edit(shown.get(r));
            }
        });
        t.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("ENTER"), "edit-hotkey");
        t.getActionMap().put("edit-hotkey", new AbstractAction() {
            @Override public void actionPerformed(java.awt.event.ActionEvent e) { int r = t.getSelectedRow(); if (r >= 0) edit(shown.get(r)); }
        });
        JScrollPane sp = Ui.scroll(t);
        // The table spans the page: 815 by 475 at the smallest font, 993 by 529 at the usual 13px.
        sp.setPreferredSize(new Dimension(815 + 89 * Ui.fontGrowth(), 475 + 27 * Ui.fontGrowth()));

        JTextArea note = Sections.text("Select an action and double-click it to change its hotkey. All hotkeys must use the Control key "
                + "(or the Command key on a Mac), and may also use modifiers such as Shift.");
        note.setBorder(Ui.pad(1, 0, 0, 0));
        JPanel searchBox = new JPanel(new BorderLayout());
        searchBox.setOpaque(false);
        searchBox.add(search, BorderLayout.CENTER);
        Ui.sized(searchBox, 233);
        block("Hotkeys", Kind.USER,
                "Use these settings to configure hotkeys for common actions.",
                () -> { clearPrefix(s, "hotkey."); model.fireTableDataChanged(); },
                note, Sections.gap(6), searchBox, Sections.gap(10), sp);
    }

    private void filter(String q) {
        shown.clear();
        String n = q.strip().toLowerCase();
        for (Hotkeys.Action a : Hotkeys.ACTIONS) {
            if (n.isEmpty() || a.name().toLowerCase().contains(n) || a.category().toLowerCase().contains(n)
                    || Hotkeys.binding(s, a).toLowerCase().contains(n)) shown.add(a);
        }
        model.fireTableDataChanged();
    }

    /** Ask for an action's new hotkey: the next key pressed with Control is it. */
    private void edit(Hotkeys.Action a) {
        JTextField f = Ui.field(Hotkeys.binding(s, a));
        f.setEditable(false);
        f.setColumns(18);
        f.putClientProperty(Hotkeys.CAPTURING, Boolean.TRUE);
        JLabel problem = Ui.label(" ");
        problem.setForeground(Theme.ACCENT);
        f.addKeyListener(new java.awt.event.KeyAdapter() {
            @Override public void keyPressed(java.awt.event.KeyEvent e) {
                int code = e.getKeyCode();
                if (code == java.awt.event.KeyEvent.VK_CONTROL || code == java.awt.event.KeyEvent.VK_SHIFT || code == java.awt.event.KeyEvent.VK_ALT
                        || code == java.awt.event.KeyEvent.VK_TAB || code == java.awt.event.KeyEvent.VK_ENTER || code == java.awt.event.KeyEvent.VK_ESCAPE) return;
                String pressed = Hotkeys.text(e);
                e.consume();
                if (pressed == null) { problem.setText("A hotkey must use the Control key."); return; }
                f.setText(pressed);
                Hotkeys.Action other = Hotkeys.bound(s, pressed);
                problem.setText(other != null && other != a ? "Already assigned to \"" + other.name() + "\", which will be unassigned." : " ");
            }
        });
        JButton reset = Ui.button("Reset to default");
        reset.addActionListener(e -> { f.setText(a.def()); problem.setText(" "); f.requestFocusInWindow(); });
        JButton clear = Ui.button("Clear");
        clear.addActionListener(e -> { f.setText(""); problem.setText(" "); f.requestFocusInWindow(); });
        JPanel form = Ui.vbox(Ui.label("Press the new hotkey for \"" + a.name() + "\":"), Ui.row(f, reset, clear), problem);
        form.setBorder(Ui.pad(10, 10, 10, 10));
        if (JOptionPane.showConfirmDialog(this, form, "Edit hotkey", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE) != JOptionPane.OK_OPTION) return;
        String chosen = f.getText().strip();
        // One key, one action: whichever had it before gives it up.
        if (!chosen.isEmpty()) {
            for (Hotkeys.Action other : Hotkeys.ACTIONS) {
                if (other != a && chosen.equalsIgnoreCase(Hotkeys.binding(s, other))) s.set(Hotkeys.key(other), "");
            }
        }
        if (chosen.equals(a.def())) s.remove(Hotkeys.key(a)); else s.set(Hotkeys.key(a), chosen);
        model.fireTableDataChanged();
    }

    // ---- the marks beside the actions ----

    private static final java.util.Map<String, Icon> MARKS = new java.util.HashMap<>();

    private static Icon mark(String name) {
        if (name == null || name.isEmpty()) return BLANK;
        return MARKS.computeIfAbsent(name + Theme.dark, k -> switch (name) {
            case "send" -> Glyphs.arrow(14, Theme.TEXT, 0);
            case "plus" -> Glyphs.plusRing(14, Theme.TEXT);
            case "copy" -> Glyphs.copy(14, Theme.TEXT);
            case "pencil" -> Glyphs.pencil(14, Theme.TEXT);
            case "trash" -> Glyphs.trash(14, Theme.TEXT);
            default -> new Drawn(name);
        });
    }

    private static final Icon BLANK = new Icon() {
        @Override public int getIconWidth() { return 14; }
        @Override public int getIconHeight() { return 14; }
        @Override public void paintIcon(Component c, Graphics g, int x, int y) { }
    };

    /** The marks the shared glyph set does not have, drawn in the same 14px line style. */
    private record Drawn(String name) implements Icon {
        @Override public int getIconWidth() { return 14; }
        @Override public int getIconHeight() { return 14; }
        @Override public void paintIcon(Component c, Graphics g, int x, int y) {
            Graphics2D g2 = (Graphics2D) g.create();
            g2.translate(x, y);
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
            g2.setColor(Theme.TEXT);
            g2.setStroke(new BasicStroke(1.2f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
            switch (name) {
                case "minus" -> {
                    g2.draw(new Ellipse2D.Double(1, 1, 11.5, 11.5));
                    g2.draw(new Line2D.Double(4, 7, 10, 7));
                }
                case "layers" -> {
                    for (int i = 0; i < 3; i++) {
                        Path2D p = new Path2D.Double();
                        double top = 1.5 + i * 3.2;
                        p.moveTo(7, top); p.lineTo(12.5, top + 2.6); p.lineTo(7, top + 5.2); p.lineTo(1.5, top + 2.6); p.closePath();
                        if (i == 0) g2.draw(p);
                        else { g2.draw(new Line2D.Double(1.5, top + 2.6, 7, top + 5.2)); g2.draw(new Line2D.Double(7, top + 5.2, 12.5, top + 2.6)); }
                    }
                }
                case "save" -> {
                    Path2D p = new Path2D.Double();
                    p.moveTo(2, 2); p.lineTo(9.5, 2); p.lineTo(12, 4.5); p.lineTo(12, 12); p.lineTo(2, 12); p.closePath();
                    g2.draw(p);
                    g2.draw(new RoundRectangle2D.Double(4.5, 7.5, 5, 4.5, 1, 1));
                    g2.draw(new Line2D.Double(4.5, 2, 4.5, 4.5));
                    g2.draw(new Line2D.Double(4.5, 4.5, 8.5, 4.5));
                }
                case "sitemap" -> {
                    g2.draw(new Line2D.Double(3, 2, 3, 12));
                    g2.draw(new Line2D.Double(3, 4.5, 9, 4.5));
                    g2.draw(new Line2D.Double(3, 9.5, 7, 9.5));
                    g2.fill(new Ellipse2D.Double(9, 3, 3, 3));
                    g2.draw(new Line2D.Double(9, 9.5, 12.5, 9.5));
                    g2.draw(new Line2D.Double(10.75, 7.75, 10.75, 11.25));
                }
                case "globe" -> {
                    g2.draw(new Ellipse2D.Double(1, 1, 12, 12));
                    g2.draw(new Ellipse2D.Double(4.5, 1, 5, 12));
                    g2.draw(new Line2D.Double(1, 7, 13, 7));
                }
                case "note" -> {
                    Path2D p = new Path2D.Double();
                    p.moveTo(2.5, 1.5); p.lineTo(8.5, 1.5); p.lineTo(11.5, 4.5); p.lineTo(11.5, 12.5); p.lineTo(2.5, 12.5); p.closePath();
                    g2.draw(p);
                    g2.draw(new Line2D.Double(5, 6.5, 9, 6.5));
                    g2.draw(new Line2D.Double(5, 9.5, 9, 9.5));
                }
                default -> { }
            }
            g2.dispose();
        }
    }
}