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.

3.3 KB
Raw Blame
package opensuite.ui.theme;

import opensuite.ui.Theme;

import javax.swing.*;
import javax.swing.plaf.UIResource;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Path2D;
import java.awt.geom.RoundRectangle2D;

/** Flat check box and radio button marks: an outline, filled with the accent when on. */
public class CheckIcon implements Icon, UIResource {

    /** 14px at the reference's smallest font, 16px at the usual 13px. */
    private static final int BASE = 14;
    private static final int SIZE = BASE + opensuite.ui.Ui.fontGrowth();
    private final boolean round;

    public CheckIcon(boolean round) { this.round = round; }

    @Override public int getIconWidth()  { return SIZE; }
    @Override public int getIconHeight() { return SIZE; }

    @Override public void paintIcon(Component c, Graphics g, int px, int py) {
        AbstractButton b = (AbstractButton) c;
        ButtonModel m = b.getModel();
        boolean on = m.isSelected(), enabled = m.isEnabled();
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
        // Drawn on a 14px grid and scaled to the size in use.
        g2.translate(px, py);
        g2.scale(SIZE / (double) BASE, SIZE / (double) BASE);
        final int x = 0, y = 0;

        Shape box = round
                ? new Ellipse2D.Float(x + 0.5f, y + 0.5f, BASE - 1, BASE - 1)
                : new RoundRectangle2D.Float(x + 0.5f, y + 0.5f, BASE - 1, BASE - 1, 4, 4);
        if (round) {
            // A radio button is a ring with a dot: blue when on, grey when disabled.
            Color ring = !enabled ? Theme.BORDER : on || m.isRollover() ? Theme.CONTROL
                    : Theme.dark ? Theme.SURFACE_3 : new Color(0xB5, 0xB5, 0xB5);
            g2.setColor(Theme.CONTENT);
            g2.fill(box);
            g2.setColor(ring);
            g2.setStroke(new BasicStroke(1f));
            g2.draw(box);
            if (on) {
                g2.setColor(enabled ? Theme.CONTROL : Theme.SURFACE_3);
                g2.fill(new Ellipse2D.Float(x + 3.5f, y + 3.5f, BASE - 7, BASE - 7));
            }
        } else {
            // An empty box is a crisp 1px outline with nothing inside it, so the
            // surface behind shows through; a ticked one is filled.
            box = new RoundRectangle2D.Float(x + 0.5f, y + 0.5f, BASE - 1, BASE - 1, 6, 6);
            Color fill = enabled ? Theme.CONTROL : Theme.SURFACE_3;
            Color edge = on ? fill : !enabled ? Theme.BORDER : Theme.dark ? Theme.SURFACE_3 : new Color(0xB5, 0xB5, 0xB5);
            if (m.isRollover() && enabled && !on) edge = Theme.CONTROL;
            if (on) { g2.setColor(fill); g2.fill(box); }
            g2.setColor(edge);
            g2.setStroke(new BasicStroke(1f));
            g2.draw(box);
            if (on) {
                g2.setColor(Color.WHITE);
                Path2D p = new Path2D.Float();
                p.moveTo(x + 3.2, y + 7.2);
                p.lineTo(x + 5.9, y + 9.8);
                p.lineTo(x + 10.8, y + 4.4);
                g2.setStroke(new BasicStroke(1.8f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
                g2.draw(p);
            }
        }
        g2.dispose();
    }
}