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.

4.6 KB
Raw Blame
package opensuite.ui;

import javax.swing.*;
import java.awt.*;

/**
 * A tab button with vertical text, for the slim strips on the right edge of a
 * panel (the message editor's Inspector / Notes, Intruder's Payloads / Resource
 * pool / Settings). The text reads top to bottom, as in the reference; the
 * selected tab sits on the selection tint with its text unchanged.
 */
public final class VerticalTab extends JToggleButton {

    /** The strip is 33 wide at the reference's smallest font, 36 at the usual 13px. */
    public static final int WIDTH = Ui.roomy(33);

    private final Icon mark;

    public VerticalTab(String text) { this(text, null); }

    /** With a mark drawn above the rotated text, as Intruder's strip has. */
    public VerticalTab(String text, Icon mark) {
        super(text);
        this.mark = mark;
        setFont(Ui.PLAIN.deriveFont(Ui.PLAIN.getSize2D() + 2f));
        setFocusable(false);
        setContentAreaFilled(false);
        setBorder(BorderFactory.createEmptyBorder());
        setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    }
    /** 16 above the mark, the mark in a 16px box, 15 to the label, 27 beneath it, then a hairline. */
    @Override public Dimension getPreferredSize() {
        FontMetrics fm = getFontMetrics(getFont());
        int h = (mark == null ? 14 : 16 + 16 + Ui.fontGrowth() + 15) + fm.stringWidth(getText()) + 25 + 1;
        return new Dimension(WIDTH, h);
    }
    @Override public Dimension getMaximumSize() { return getPreferredSize(); }
    @Override protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        int w = getWidth(), h = getHeight() - 1;
        if (isSelected()) { g2.setColor(Theme.ACCENT_LIGHT); g2.fillRect(0, 0, w, h); }
        g2.setColor(Theme.BORDER);
        g2.fillRect(0, h, w, 1);
        int top = 14;
        if (mark != null) {
            int box = 16 + Ui.fontGrowth();
            mark.paintIcon(this, g2, (w - mark.getIconWidth()) / 2, 16 + (box - mark.getIconHeight()) / 2);
            top = 16 + box + 15;
        }
        g2.setColor(Theme.TEXT);
        g2.setFont(getFont());
        FontMetrics fm = g2.getFontMetrics();
        opensuite.ui.Theme.textSmoothing(g2);
        // Rotated a quarter turn: the label reads from the top down.
        g2.translate(w / 2.0, top);
        g2.rotate(Math.PI / 2);
        g2.drawString(getText(), 0f, (float) ((fm.getAscent() - fm.getDescent()) / 2.0));
        g2.dispose();
    }

    private static Icon mark(java.util.function.Consumer<Graphics2D> d) {
        java.awt.image.BufferedImage img = new java.awt.image.BufferedImage(16, 16, java.awt.image.BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = img.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
        g.setColor(Theme.TEXT);
        g.setStroke(new BasicStroke(1.3f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
        d.accept(g);
        g.dispose();
        return new ImageIcon(img);
    }

    /** Spectacles, turned to read down the strip like the label beneath them. */
    public static Icon inspectorMark() {
        return mark(g -> {
            g.draw(new java.awt.geom.Ellipse2D.Double(3.5, 1.5, 5.5, 5.5));
            g.draw(new java.awt.geom.Ellipse2D.Double(3.5, 9, 5.5, 5.5));
            g.draw(new java.awt.geom.Line2D.Double(11.5, 1.5, 11.5, 14.5));
            g.draw(new java.awt.geom.Line2D.Double(9, 4.2, 11.5, 4.2));
            g.draw(new java.awt.geom.Line2D.Double(9, 11.8, 11.5, 11.8));
        });
    }

    /** A sheet of paper with ruled lines. */
    public static Icon notesMark() {
        return mark(g -> {
            java.awt.geom.Path2D p = new java.awt.geom.Path2D.Double();
            p.moveTo(3, 1.5); p.lineTo(9.5, 1.5); p.lineTo(13, 5); p.lineTo(13, 14.5); p.lineTo(3, 14.5); p.closePath();
            g.draw(p);
            g.draw(new java.awt.geom.Line2D.Double(5.5, 7.5, 10.5, 7.5));
            g.draw(new java.awt.geom.Line2D.Double(5.5, 10, 10.5, 10));
            g.draw(new java.awt.geom.Line2D.Double(5.5, 12.3, 8.5, 12.3));
        });
    }

    /** A lightning bolt: something that runs. */
    public static Icon actionsMark() {
        return mark(g -> {
            java.awt.geom.Path2D p = new java.awt.geom.Path2D.Double();
            p.moveTo(9.5, 1.5); p.lineTo(3.5, 9); p.lineTo(8, 9); p.lineTo(6.5, 14.5); p.lineTo(12.5, 7); p.lineTo(8, 7); p.closePath();
            g.draw(p);
        });
    }
}