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.
Java application-securityappsecbug-bountyburp-alternativeburp-suitedasthttp-proxyinfosecintercepting-proxyjavaowaspowasp-zappenetration-testingpentestingsecuritysecurity-toolsvulnerability-scannerweb-application-securityweb-security
package opensuite.ui.settings;
import opensuite.core.Settings;
import opensuite.ui.Glyphs;
import opensuite.ui.OptionsPage;
import opensuite.ui.Theme;
import opensuite.ui.Ui;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
/**
* A settings page in the reference layout: a stack of blocks, each with a
* help mark and title on the left, a "User setting" / "Project setting" pill
* on the right, then a restore-defaults mark beside the description, then the
* controls. The page remembers which kinds of setting it holds so the
* dialog's All / User / Project filter can show or hide it.
*/
public class SettingsPage extends OptionsPage {
public enum Kind { USER, PROJECT, BOTH }
private final EnumSet<Kind> kinds = EnumSet.noneOf(Kind.class);
private final List<String> blockTitles = new ArrayList<>();
private final List<Runnable> restorers = new ArrayList<>();
public EnumSet<Kind> kinds() { return kinds; }
public List<String> blockTitles() { return blockTitles; }
/** Restore every block on this page to its defaults. */
public void restoreAll() { for (Runnable r : restorers) r.run(); }
/**
* Add a block. {@code restore} may be null when the block has nothing to
* reset (an informational block, or one whose controls save themselves).
*/
private boolean restoreMark;
/** The next block shows the restore mark in place of the gear, as the reference does where restoring is all there is. */
public SettingsPage restoreMark() { restoreMark = true; return this; }
public JPanel block(String title, Kind kind, String description, Runnable restore, Component... parts) {
kinds.add(kind);
blockTitles.add(title);
if (restore != null) restorers.add(restore);
// The gear under the help mark opens the section's own menu. A section whose only
// action is going back to its defaults shows that instead.
boolean plain = restoreMark && restore != null;
restoreMark = false;
JButton mark = plain ? Glyphs.button(Glyphs.restore(21, Theme.TEXT), "Restore defaults", null)
: Glyphs.button(Glyphs.gear(21, Theme.TEXT), "Settings for this section", null);
mark.setBorder(plain ? Ui.pad(6, 0, 0, 0) : null); // the arrow sits lower than the gear
mark.addActionListener(e -> {
if (plain) { restore.run(); return; }
JPopupMenu m = new JPopupMenu();
JMenuItem reset = new JMenuItem("Restore defaults");
reset.setFont(Ui.PLAIN);
reset.setEnabled(restore != null);
if (restore != null) reset.addActionListener(ev -> restore.run());
m.add(reset);
m.show(mark, 0, mark.getHeight());
});
JComponent badges = kind == Kind.BOTH ? opensuite.ui.Sections.badges("User setting", "Project setting")
: opensuite.ui.Sections.badges(kind == Kind.USER ? "User setting" : "Project setting");
// Where a block can be overridden for the project, the badge of the scope in force is lit.
if (kind == Kind.BOTH) {
opensuite.ui.SmallSwitch sw = null;
for (Component c : parts) {
if (c instanceof JComponent jc && jc.getClientProperty(OVERRIDE) instanceof opensuite.ui.SmallSwitch x) sw = x;
}
final opensuite.ui.SmallSwitch found = sw;
opensuite.ui.Sections.lightBadge(badges, found != null && found.isOn() ? 1 : 0);
if (found != null) {
Runnable was = (Runnable) found.getClientProperty("opensuite.switch.bound");
found.setOnToggle(() -> {
if (was != null) was.run();
opensuite.ui.Sections.lightBadge(badges, found.isOn() ? 1 : 0);
});
}
}
return section(title, description == null ? "" : description, mark, badges, parts);
}
// ---- small binding helpers shared by the pages ----
public static JCheckBox check(Settings s, String key, boolean def, String text) {
JCheckBox c = Ui.check(text, s.getBool(key, def));
c.addItemListener(e -> s.setBool(key, c.isSelected()));
s.addListener(k -> { if (k.equals(key)) SwingUtilities.invokeLater(() -> { boolean v = s.getBool(key, def); if (c.isSelected() != v) c.setSelected(v); }); });
return c;
}
public static JTextField text(Settings s, String key, String def, int cols) {
JTextField f = Ui.field(s.get(key, def), cols);
f.getDocument().addDocumentListener(new javax.swing.event.DocumentListener() {
public void insertUpdate(javax.swing.event.DocumentEvent e) { s.set(key, f.getText()); }
public void removeUpdate(javax.swing.event.DocumentEvent e) { s.set(key, f.getText()); }
public void changedUpdate(javax.swing.event.DocumentEvent e) { s.set(key, f.getText()); }
});
return f;
}
public static JTextField number(Settings s, String key, int def, int cols) {
JTextField f = Ui.field(String.valueOf(s.getInt(key, def)), cols);
f.getDocument().addDocumentListener(new javax.swing.event.DocumentListener() {
private void go() { try { s.setInt(key, Integer.parseInt(f.getText().trim())); } catch (NumberFormatException ignore) { } }
public void insertUpdate(javax.swing.event.DocumentEvent e) { go(); }
public void removeUpdate(javax.swing.event.DocumentEvent e) { go(); }
public void changedUpdate(javax.swing.event.DocumentEvent e) { go(); }
});
return f;
}
/** A radio group bound to one key; values[i] is stored when radios[i] is chosen. */
public static JRadioButton[] radios(Settings s, String key, String def, String[] values, String... labels) {
ButtonGroup g = new ButtonGroup();
JRadioButton[] out = new JRadioButton[labels.length];
String cur = s.get(key, def);
for (int i = 0; i < labels.length; i++) {
final String v = values[i];
out[i] = Ui.radio(labels[i], v.equals(cur));
out[i].addItemListener(e -> { if (out2(out, v, values).isSelected()) s.set(key, v); });
g.add(out[i]);
}
return out;
}
private static JRadioButton out2(JRadioButton[] out, String v, String[] values) {
for (int i = 0; i < values.length; i++) if (values[i].equals(v)) return out[i];
return out[0];
}
public static <T> JComboBox<T> combo(Settings s, String key, T[] items, T def) {
JComboBox<T> c = Ui.combo(items);
String cur = s.get(key, String.valueOf(def));
for (T t : items) if (String.valueOf(t).equals(cur)) c.setSelectedItem(t);
c.addActionListener(e -> s.set(key, String.valueOf(c.getSelectedItem())));
return c;
}
/** A path field with a chooser button. */
public static JPanel pathRow(Settings s, String key, String def, boolean folder, String buttonText) {
JComponent[] parts = path(s, key, def, folder, buttonText, 0);
return Ui.row(parts[0], parts[1]);
}
/**
* A path field of the given width (0 for its natural width) and the button
* that picks the path, for a caller that lays them out on a line of its own.
*/
public static JComponent[] path(Settings s, String key, String def, boolean folder, String buttonText, int width) {
JTextField f = text(s, key, def, 40);
if (width > 0) Ui.sized(f, width); // a field grows in proportion to the font
JButton pick = Ui.button(buttonText);
pick.addActionListener(e -> {
JFileChooser fc = new opensuite.ui.FileChooser(f.getText().isBlank() ? System.getProperty("user.home") : f.getText());
if (folder) fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (fc.showOpenDialog(f) == JFileChooser.APPROVE_OPTION) f.setText(fc.getSelectedFile().getAbsolutePath());
});
return new JComponent[] { f, pick };
}
/** Client property on an override row: its switch. */
private static final String OVERRIDE = "opensuite.settings.override";
/**
* The "Override options for this project only" row, as the reference draws
* it: a white box across the section, 42px tall, with a small switch.
*/
public static JPanel overrideRow(Settings s, String key) {
opensuite.ui.SmallSwitch sw = new opensuite.ui.SmallSwitch(s.getBool(key, false));
Runnable store = () -> s.setBool(key, sw.isOn());
sw.putClientProperty("opensuite.switch.bound", store);
sw.setOnToggle(store);
s.addListener(k -> { if (k.equals(key)) SwingUtilities.invokeLater(() -> sw.setOn(s.getBool(key, false))); });
JLabel text = Ui.label("Override options for this project only");
text.setForeground(Theme.TEXT);
text.addMouseListener(new java.awt.event.MouseAdapter() {
@Override public void mousePressed(java.awt.event.MouseEvent e) { sw.dispatchEvent(SwingUtilities.convertMouseEvent(text, e, sw)); }
});
JPanel p = new JPanel(null) {
@Override public void doLayout() {
sw.setBounds(13, (getHeight() - opensuite.ui.SmallSwitch.HEIGHT) / 2, opensuite.ui.SmallSwitch.WIDTH, opensuite.ui.SmallSwitch.HEIGHT);
int x = 47 + 2 * Ui.fontGrowth();
text.setBounds(x, 0, Math.max(0, getWidth() - x - 1), getHeight());
}
};
p.setBackground(Theme.CONTENT);
p.setBorder(BorderFactory.createLineBorder(Theme.BORDER));
p.setPreferredSize(new Dimension(300, Ui.tall(42)));
p.add(sw);
p.add(text);
p.putClientProperty(OVERRIDE, sw);
// The row is followed by a little more air than an ordinary control.
JPanel wrap = new JPanel(new BorderLayout());
wrap.setOpaque(false);
wrap.setBorder(Ui.pad(0, 0, 10, 0));
wrap.add(p, BorderLayout.CENTER);
wrap.putClientProperty(OVERRIDE, sw);
return opensuite.ui.Sections.fill(wrap);
}
/** A flow row of items with a leading label. */
/** Components on one line of the given height, centred on it, with gaps between: component, gap, component, ... */
public static JPanel line(int height, Object... parts) {
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
p.setOpaque(false);
int width = 0;
for (Object o : parts) {
Component c = o instanceof Integer gap ? Box.createHorizontalStrut(gap) : (Component) o;
if (c instanceof JComponent jc) {
jc.setAlignmentY(Component.CENTER_ALIGNMENT);
// BoxLayout sizes by the minimum and maximum, which a set preferred size does not change.
// A text field with a column count takes its width from that, whatever size it was given.
if (jc.isPreferredSizeSet() && jc instanceof JTextField tf) tf.setColumns(0);
if (jc.isPreferredSizeSet()) { jc.setMinimumSize(jc.getPreferredSize()); jc.setMaximumSize(jc.getPreferredSize()); }
}
width += c.getPreferredSize().width;
p.add(c);
}
p.setPreferredSize(new Dimension(width, Ui.tall(height))); // rows grow with the font
return p;
}
/** A caption over a row of radios, as the side panel layouts are set out. */
public static JPanel radioGroup(String caption, int between, JRadioButton[] radios) {
JPanel row = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
row.setOpaque(false);
for (int i = 0; i < radios.length; i++) {
if (i > 0) row.add(Box.createHorizontalStrut(between));
radios[i].setBorder(Ui.pad(0, 2, 0, 0));
radios[i].setIconTextGap(radios[i].getIconTextGap() + 3);
row.add(radios[i]);
}
JLabel head = opensuite.ui.Sections.caption(caption);
// Measured: the caption is 13px with 14 beneath it, 17 and 15 at the usual 13px.
head.setPreferredSize(new Dimension(head.getPreferredSize().width, 13 + 2 * Ui.fontGrowth()));
return opensuite.ui.Sections.column(0, head, opensuite.ui.Sections.gap(14 + Ui.fontGrowth() / 2), row);
}
/** The upright rule between two radio groups on one line. */
public static JComponent groupRule() {
return new JComponent() {
@Override public Dimension getPreferredSize() { return new Dimension(83, Ui.tall(46)); }
@Override protected void paintComponent(Graphics g) {
g.setColor(opensuite.ui.Theme.BORDER);
g.fillRect(42, 0, 1, getHeight());
}
};
}
public static JPanel labelled(String label, Component... items) {
// At the usual 13px the label stands at the left edge with the row's top, 11px from its field.
boolean big = Ui.fontGrowth() > 0;
JPanel p = new JPanel(big ? new FlowLayout(FlowLayout.LEFT, 0, 0) : new FlowLayout(FlowLayout.LEFT, 6, 2));
p.setOpaque(false);
JLabel l = Ui.label(label);
if (big) l.setBorder(Ui.pad(0, 0, 0, 10));
p.add(l);
for (Component c : items) p.add(c);
return p;
}
/** Remove every key under a prefix, so bound controls fall back to their defaults. */
public static void clearPrefix(Settings s, String prefix) {
for (String k : new ArrayList<>(s.keys())) if (k.startsWith(prefix)) s.remove(k);
}
}