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;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Path2D;
/**
* The one way a tool page lays out its settings: a column of sections, each
* with a help mark in a left gutter, a bold heading, a paragraph, then its
* controls, all starting on the same left edge. Sections are parted by a
* hairline. Every measurement here is taken from the reference, so a page
* built from these pieces lines up with it without any per-page nudging:
*
* <pre>
* 10px margin (11 with the window edge) | 31px gutter (20px icons) | content from x = 42
* heading row 22px, its centre 20px below the top of the page
* paragraph 27px below the heading, controls 15px below the paragraph
* 20px under the last control, a hairline, 10px, then the next section
* </pre>
*/
public final class Sections {
private Sections() {}
/** Space between the page edge and the gutter, and at the right of a rule. */
public static final int MARGIN = 10;
/** Width of the icon gutter; content starts at MARGIN + GUTTER. */
public static final int GUTTER = Ui.roomy(31);
/** The pitch of stacked controls: a 22px control and a 5px gap. */
public static final int ROW_GAP = 5;
/** Client property: this child takes the full width of its column. */
public static final String FILL = "opensuite.sections.fill";
/** Client property on a section: its heading row (a BorderLayout panel; EAST is the trailing component). */
public static final String HEAD = "opensuite.sections.head";
/** Client property on a section: its icon gutter (null layout; the second mark sits at 0,28 21x28). */
public static final String GUTTER_OF = "opensuite.sections.gutter";
/** Client property on a page column: how far its rules stop short of the right edge. */
public static final String RULE_RIGHT = "opensuite.sections.ruleRight";
/** Mark a component to be stretched across its column instead of keeping its own width. */
public static <T extends JComponent> T fill(T c) {
c.putClientProperty(FILL, Boolean.TRUE);
return c;
}
/** A wrapping paragraph that sizes its height from the width it is given. */
public static JTextArea text(String s) {
JTextArea a = new JTextArea(s == null ? "" : s);
a.setFont(Ui.PLAIN);
a.setForeground(Theme.TEXT);
a.setEditable(false);
a.setFocusable(false);
a.setOpaque(false);
a.setLineWrap(true);
a.setWrapStyleWord(true);
a.setBorder(null);
a.putClientProperty(FILL, Boolean.TRUE);
return a;
}
/** A plain (not bold) caption above a group of controls, as "Include in scope". */
public static JLabel caption(String s) {
JLabel l = new JLabel(s);
l.setFont(Ui.PLAIN);
l.setForeground(Theme.TEXT);
return l;
}
/** Wrapping text set {@code above} pixels lower than the column would put it. */
public static JTextArea text(String s, int above) {
JTextArea a = text(s);
a.setBorder(BorderFactory.createEmptyBorder(above, 0, 0, 0));
return a;
}
/** Whether a component was made to take the full width of its column. */
public static boolean isFill(Component c) {
return c instanceof JComponent jc && Boolean.TRUE.equals(jc.getClientProperty(FILL));
}
/** A fixed vertical gap between two controls of a section. */
public static Component gap(int px) { return Box.createRigidArea(new Dimension(0, px)); }
/** First in a section's body: bring the body {@code px} closer to the description than the usual 15. */
public static Component lift(int px) { return new Lift(px); }
private static final class Lift extends Component {
final int px;
Lift(int px) { this.px = px; }
@Override public Dimension getPreferredSize() { return new Dimension(0, 0); }
}
/**
* A vertical stack that gives every child the column's width to work with:
* wrapping text and children marked {@link #fill} take all of it, anything
* else keeps its own width, left-aligned.
*/
public static JPanel column(int gap, Component... parts) {
JPanel p = new JPanel(new Stack(gap));
p.setOpaque(false);
for (Component c : parts) if (c != null) p.add(c);
return p;
}
/** A page: sections stacked from the top, parted by hairlines, scrolling when too tall. */
public static JComponent page(JComponent... sections) {
JPanel col = new JPanel(new Stack(0));
col.setBackground(Theme.BG);
boolean first = true;
for (JComponent s : sections) {
if (s == null) continue;
if (!first) col.add(fill(new Rule()));
col.add(fill(s));
first = false;
}
JScrollPane sp = Ui.scrollColumn(col);
sp.setOpaque(true);
sp.setBackground(Theme.BG);
sp.getViewport().setOpaque(true);
sp.getViewport().setBackground(Theme.BG);
return sp;
}
/** An empty page column, for building a page one section at a time; see {@link #append} and {@link #scroll}. */
public static JPanel newPage() {
JPanel col = new JPanel(new Stack(0));
col.setBackground(Theme.BG);
return col;
}
/** Add a section to a page built with {@link #newPage}, with the hairline above it when it is not the first. */
public static void append(JPanel page, JComponent section) {
if (page.getComponentCount() > 0) page.add(fill(new Rule()));
page.add(fill(section));
}
/** The scroll pane for a page built with {@link #newPage}. */
public static JScrollPane scroll(JPanel page) {
JScrollPane sp = Ui.scrollColumn(page);
Ui.columnFitsFixedRows(sp);
sp.setOpaque(true);
sp.setBackground(Theme.BG);
sp.getViewport().setOpaque(true);
sp.getViewport().setBackground(Theme.BG);
return sp;
}
/** Put {@code second} under a section's help mark, replacing any mark already there. */
public static void setSecond(JComponent section, JComponent second) {
if (!(section.getClientProperty(GUTTER_OF) instanceof JPanel gutter)) return;
while (gutter.getComponentCount() > 1) gutter.remove(1);
if (second != null) {
if (second instanceof AbstractButton b) b.setVerticalAlignment(SwingConstants.TOP);
second.setBounds(0, 28, 21, 28);
gutter.add(second);
}
gutter.revalidate();
gutter.repaint();
}
/** Whether a section already has a mark under its help mark. */
public static boolean hasSecond(JComponent section) {
return section.getClientProperty(GUTTER_OF) instanceof JPanel gutter && gutter.getComponentCount() > 1;
}
/** Put {@code trailing} at the right end of a section's heading row; null clears it. */
public static void setTrailing(JComponent section, JComponent trailing) {
if (!(section.getClientProperty(HEAD) instanceof JPanel head)) return;
Component old = ((BorderLayout) head.getLayout()).getLayoutComponent(BorderLayout.EAST);
if (old != null) head.remove(old);
if (trailing != null) head.add(trailing, BorderLayout.EAST);
head.revalidate();
head.repaint();
}
/** Whether a section already carries something at the right of its heading row. */
public static boolean hasTrailing(JComponent section) {
return section.getClientProperty(HEAD) instanceof JPanel head
&& ((BorderLayout) head.getLayout()).getLayoutComponent(BorderLayout.EAST) != null;
}
/** Client property on a badge: whether it is the scope in force (blue) or not (grey). */
private static final String BADGE_ON = "opensuite.sections.badgeOn";
/** One scope badge: a pill 18px tall, light blue when its scope is in force and grey when it is not. */
public static JComponent badge(String text) {
JLabel l = new JLabel(text) {
@Override protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Boolean.FALSE.equals(getClientProperty(BADGE_ON)) ? Theme.SURFACE : Theme.ACCENT_LIGHT);
g2.fillRoundRect(0, 0, getWidth(), getHeight(), getHeight(), getHeight());
g2.dispose();
super.paintComponent(g);
}
};
l.setFont(Ui.PLAIN);
l.setForeground(Theme.TEXT);
l.setBorder(Ui.pad(0, 9, 1, 9));
l.setPreferredSize(new Dimension(l.getPreferredSize().width, Ui.tall(18)));
return l;
}
/** The badges of a section, 10px apart, for the right end of its heading row. */
public static JComponent badges(String... texts) {
JPanel p = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 5));
p.setOpaque(false);
for (int i = 0; i < texts.length; i++) {
if (i > 0) p.add(Box.createHorizontalStrut(10));
p.add(badge(texts[i]));
}
return p;
}
/** Light the badge at {@code index} of a {@link #badges} row and grey the others. */
public static void lightBadge(JComponent badges, int index) {
int n = 0;
for (Component c : badges.getComponents()) {
if (!(c instanceof JLabel l)) continue;
l.putClientProperty(BADGE_ON, n++ == index);
l.repaint();
}
}
/** A section with only the help mark in its gutter. */
public static JPanel section(String title, String description, Component... body) {
return section(title, description, null, null, body);
}
/**
* A section.
*
* @param second the icon under the help mark (a gear, a restore arrow), or null
* @param trailing a component at the right end of the heading row (badges), or null
*/
public static JPanel section(String title, String description, JComponent second, JComponent trailing,
Component... body) {
JPanel gutter = new JPanel(null);
gutter.setOpaque(false);
gutter.setPreferredSize(new Dimension(GUTTER, 56));
int mark = 21 + Ui.fontGrowth();
JLabel help = new JLabel(Glyphs.help(mark, Theme.TEXT));
help.setBounds(0, 2 + Ui.fontGrowth() / 2, mark, mark);
gutter.add(help);
if (second != null) {
// Top-aligned in a taller box, so a mark that sits lower than the
// gear (the restore arrow) can carry its own top inset.
if (second instanceof AbstractButton b) b.setVerticalAlignment(SwingConstants.TOP);
second.setBounds(0, 28, 21, 28);
gutter.add(second);
}
JLabel heading = Ui.heading(title);
JPanel head = new JPanel(new BorderLayout());
head.setOpaque(false);
// The row is 25px so that a badge can sit 5px down in it; the heading keeps to its top 22.
heading.setPreferredSize(new Dimension(heading.getPreferredSize().width, Ui.tall(22)));
JPanel headingBox = new JPanel(new BorderLayout());
headingBox.setOpaque(false);
headingBox.add(heading, BorderLayout.NORTH);
head.add(headingBox, BorderLayout.WEST);
if (trailing != null) head.add(trailing, BorderLayout.EAST);
head.setPreferredSize(new Dimension(10, Ui.roomy(25)));
fill(head);
JPanel content = new JPanel(new Stack(0));
content.setOpaque(false);
content.add(head);
if (description != null && !description.isBlank()) {
content.add(gap(5));
content.add(text(description));
// A body that opens with a lift() stands that much closer to the description.
int lift = body.length > 0 && body[0] instanceof Lift l ? l.px : 0;
content.add(gap(Math.max(0, 15 - lift)));
} else {
content.add(gap(12));
}
for (Component c : body) if (c != null) content.add(c);
JPanel p = new JPanel(new BorderLayout());
p.setOpaque(false);
p.setBorder(Ui.pad(8, MARGIN, 20, MARGIN));
p.add(gutter, BorderLayout.WEST);
p.add(content, BorderLayout.CENTER);
p.putClientProperty(HEAD, head);
p.putClientProperty(GUTTER_OF, gutter);
return p;
}
/**
* A table (in its scroll pane) with the small black arrow the reference
* draws at its right edge. Clicking the arrow grows the table to show more
* rows, and clicking again puts it back.
*/
public static JComponent withArrow(JComponent table, int widthAt11, int height) {
int width = Ui.wide(widthAt11);
table.setPreferredSize(new Dimension(width, height));
JLabel arrow = new JLabel(new Icon() {
@Override public int getIconWidth() { return 7; }
@Override public int getIconHeight() { return 9; }
@Override public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Theme.TAB_TEXT_ON);
Path2D p = new Path2D.Float();
p.moveTo(x, y); p.lineTo(x + 7, y + 4.5); p.lineTo(x, y + 9); p.closePath();
g2.fill(p);
g2.dispose();
}
});
arrow.setToolTipText("Show more rows");
arrow.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
JPanel row = new JPanel(new BorderLayout(4, 0));
row.setOpaque(false);
row.add(table, BorderLayout.CENTER);
row.add(arrow, BorderLayout.EAST);
row.putClientProperty(Ui.FIXED_ROW, Boolean.TRUE);
arrow.addMouseListener(new java.awt.event.MouseAdapter() {
boolean grown;
@Override public void mouseClicked(java.awt.event.MouseEvent e) {
grown = !grown;
table.setPreferredSize(new Dimension(width, grown ? height * 2 : height));
arrow.setToolTipText(grown ? "Show fewer rows" : "Show more rows");
for (Container c = row; c != null; c = c.getParent()) { c.invalidate(); }
row.revalidate();
Container top = row.getTopLevelAncestor();
if (top != null) { top.validate(); top.repaint(); }
}
});
return row;
}
/** Buttons beside a table: the column on the left, top-aligned, 5px from the table. */
public static JComponent besideTable(JComponent buttons, JComponent table) {
JPanel row = new JPanel(new BorderLayout(ROW_GAP, 0));
row.setOpaque(false);
row.add(buttons, BorderLayout.WEST);
row.add(table, BorderLayout.CENTER);
return row;
}
/** The hairline between two sections, inset by the page margin, with 10px beneath it. */
private static final class Rule extends JComponent {
@Override public Dimension getPreferredSize() { return new Dimension(10, 11); }
@Override protected void paintComponent(Graphics g) {
g.setColor(Theme.BORDER);
int right = getParent() instanceof JComponent pc && pc.getClientProperty(RULE_RIGHT) instanceof Integer r ? r : MARGIN;
g.fillRect(MARGIN, 0, getWidth() - MARGIN - right, 1);
}
}
/** See {@link #column}. */
private static final class Stack implements LayoutManager {
private final int gap;
private int lastNeed = -1;
Stack(int gap) { this.gap = gap; }
private static boolean fills(Component c) {
return c instanceof JComponent jc && Boolean.TRUE.equals(jc.getClientProperty(FILL));
}
/** The height a child needs when it is laid out {@code w} wide. */
private static int heightAt(Component c, int w) {
if (c instanceof JTextArea a && a.getLineWrap()) {
Insets pad = a.getInsets();
if (w <= 0) return a.getFontMetrics(a.getFont()).getHeight() + pad.top + pad.bottom;
javax.swing.text.View v = a.getUI().getRootView(a);
v.setSize(w - pad.left - pad.right, Integer.MAX_VALUE);
return (int) Math.ceil(v.getPreferredSpan(javax.swing.text.View.Y_AXIS)) + pad.top + pad.bottom;
}
if (fills(c) && c instanceof Container k && k.getLayout() instanceof Stack s) {
return s.layoutSize(k, w).height;
}
return c.getPreferredSize().height;
}
private Dimension layoutSize(Container parent, int width) {
Insets in = parent.getInsets();
int inner = width - in.left - in.right;
int w = 0, h = 0, n = 0;
for (Component c : parent.getComponents()) {
if (!c.isVisible()) continue;
if (n++ > 0) h += gap;
h += heightAt(c, inner);
if (!(c instanceof JTextArea)) w = Math.max(w, c.getPreferredSize().width);
}
return new Dimension(w + in.left + in.right, h + in.top + in.bottom);
}
@Override public Dimension preferredLayoutSize(Container parent) {
return layoutSize(parent, parent.getWidth());
}
@Override public Dimension minimumLayoutSize(Container parent) { return new Dimension(0, 0); }
@Override public void layoutContainer(Container parent) {
Insets in = parent.getInsets();
int inner = parent.getWidth() - in.left - in.right;
int y = in.top, n = 0;
for (Component c : parent.getComponents()) {
if (!c.isVisible()) continue;
if (n++ > 0) y += gap;
int h = heightAt(c, inner);
int w = fills(c) ? inner : Math.min(inner, c.getPreferredSize().width);
c.setBounds(in.left, y, Math.max(0, w), h);
y += h;
}
// A wrapped paragraph changes height with the width, so the height
// this column asked for may now be stale: ask again once.
int need = y + in.bottom;
if (need != parent.getHeight() && parent.getParent() != null && need != lastNeed) {
lastNeed = need;
SwingUtilities.invokeLater(parent::revalidate);
}
}
@Override public void addLayoutComponent(String name, Component comp) {}
@Override public void removeLayoutComponent(Component comp) {}
}
}