Package aurelienribon.ui

Source Code of aurelienribon.ui.CompactCheckBox$CompactCheckBoxProcessor

package aurelienribon.ui;

import aurelienribon.ui.components.ArProperties;
import aurelienribon.ui.css.DeclarationSet;
import aurelienribon.ui.css.DeclarationSetProcessor;
import aurelienribon.ui.css.Property;
import aurelienribon.ui.css.Style;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;

* @author Aurelien Ribon |
public class CompactCheckBox extends JPanel {
  private boolean isSelected = false;
  private final List<ActionListener> listeners = new CopyOnWriteArrayList<ActionListener>();
  private final JLabel label = new JLabel();
  private Paint stroke = Color.GRAY;
  private Paint fill = Color.WHITE;
  private int boxW = 14, boxH = 14;

  private static final Color disabledFg;

  static {
    Color c = (Color) UIManager.get("Label.disabledForeground");
    disabledFg = c != null ? c : Color.GRAY;
    Style.registerProcessor(CompactCheckBox.class, new CompactCheckBoxProcessor());

  public CompactCheckBox() {
    this("My Compact Checkbox");

  public CompactCheckBox(String text) {
    setLayout(new BorderLayout());
    add(label, BorderLayout.CENTER);

    add(Box.createHorizontalStrut(boxW + 5), BorderLayout.WEST);


  public void addActionListener(ActionListener listener) {
    if (!listeners.contains(listener)) listeners.add(listener);

  public void removeActionListener(ActionListener listener) {

  protected void paintComponent(Graphics g) {
    label.setForeground(isEnabled() ? getForeground() : disabledFg);

    Graphics2D gg = (Graphics2D) g.create();

    int y = (getHeight() - boxH) / 2;

    gg.fillRect(0, y, boxW-1, boxH-1);
    gg.drawRect(0, y, boxW-1, boxH-1);

    if (isSelected) {
      gg.fillRect(2, y+2, boxW-4, boxH-4);


  private final MouseAdapter mouseAdapter = new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
      if (isEnabled()) {
        isSelected = !isSelected;

        ActionEvent evt = new ActionEvent(CompactCheckBox.this, 0, null);
        for (ActionListener l : listeners) l.actionPerformed(evt);

  public void setSelected(boolean isSelected) {this.isSelected = isSelected;repaint();}
  public boolean isSelected() {return isSelected;}

  public void setText(String text) {label.setText(text);}
  public void setStroke(Paint stroke) {this.stroke = stroke;}
  public void setFill(Paint fill) {this.fill = fill;}
  public String getText() {return label.getText();}
  public Paint getStroke() {return stroke;}
  public Paint getFill() {return fill;}

  private static class CompactCheckBoxProcessor implements DeclarationSetProcessor<CompactCheckBox>, ArProperties {
    public void process(CompactCheckBox t, DeclarationSet ds) {
      Property p;
      p = stroke; if (ds.contains(p)) t.setStroke(ds.getValue(p, Paint.class));
      p = fill; if (ds.contains(p)) t.setFill(ds.getValue(p, Paint.class));

Related Classes of aurelienribon.ui.CompactCheckBox$CompactCheckBoxProcessor

Copyright © 2018 All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact