package edu.princeton.swing; import java.awt.*; import javax.swing.*; /** * PTristateCheckBox is a subclass of JCheckBox which supports the rendering of a * TristateButtonModel. * * @author btsang * @version 7.1 */ public class PTristateCheckBox extends JCheckBox { private JCheckBox tristateComponent; /** * Create a new PTristateCheckBox. */ public PTristateCheckBox() { this(null, null, false, false); } /** * Create a new PTristateCheckBox. */ public PTristateCheckBox(Icon icon) { this(null, icon, false, false); } /** * Create a new PTristateCheckBox. */ public PTristateCheckBox(Icon icon, boolean selected) { this(null, icon, selected, false); } /** * Create a new PTristateCheckBox. */ public PTristateCheckBox(Icon icon, boolean selected, boolean tristate) { this(null, icon, selected, tristate); } /** * Create a new PTristateCheckBox. */ public PTristateCheckBox(String text) { this(text, null, false, false); } /** * Create a new PTristateCheckBox. */ public PTristateCheckBox(Action a) { this(null, null, false, false); setAction(a); } /** * Create a new PTristateCheckBox. */ public PTristateCheckBox(String text, boolean selected) { this(text, null, selected, false); } /** * Create a new PTristateCheckBox. */ public PTristateCheckBox(String text, boolean selected, boolean tristate) { this(text, null, selected, tristate); } /** * Create a new PTristateCheckBox. */ public PTristateCheckBox(String text, Icon icon) { this(text, icon, false, false); } /** * Create a new PTristateCheckBox. */ public PTristateCheckBox(String text, Icon icon, boolean selected) { this(text, icon, selected, false); } /** * Create a new PTristateCheckBox. */ public PTristateCheckBox(String text, Icon icon, boolean selected, boolean tristate) { super(text, icon, selected); setModel(new DefaultTristateButtonModel(getModel())); setBorderPainted(false); setHorizontalAlignment(LEADING); tristateComponent = new JCheckBox(); } /** * Override paint() to paint the tristate checkbox differently if it's in tristate. */ public void paint(Graphics g) { ButtonModel model = getModel(); if (model instanceof TristateButtonModel && ((TristateButtonModel)model).isTristate()) { tristateComponent.setBackground(getBackground()); tristateComponent.setForeground(getForeground()); tristateComponent.setFont(getFont()); tristateComponent.setText(getText()); tristateComponent.setIcon(getIcon()); tristateComponent.setEnabled(isEnabled()); tristateComponent.setBorder(getBorder()); tristateComponent.setBounds(getBounds()); ButtonModel componentModel = tristateComponent.getModel(); componentModel.setEnabled(model.isEnabled()); componentModel.setSelected(!model.isArmed()); componentModel.setArmed(true); componentModel.setPressed(true); componentModel.setRollover(model.isRollover()); tristateComponent.paint(g); } else { super.paint(g); } } }