001package jmri.jmrit.beantable;
002
003import java.awt.Component;
004import javax.swing.JCheckBox;
005import javax.swing.JTable;
006import javax.swing.table.TableCellRenderer;
007
008/**
009 * Handle painting checkbox classes in a JTable.
010 * <p>
011 * Beyond the normal behavior of providing a checkbox to show the value, this
012 * disables the JCheckBox if the cell is not editable. This makes the visual
013 * behavior more in line with user expectations.
014 * The checkbox cell background is set to match selected status.
015 *
016 * @author Bob Jacobsen
017 */
018public class EnablingCheckboxRenderer extends JCheckBox implements TableCellRenderer {
019
020    public EnablingCheckboxRenderer() {
021        super();
022        super.setHorizontalAlignment(0);
023    }
024
025    /**
026     * Override this method from the parent class.
027     * {@inheritDoc}
028     *
029     * @param table      the JTable component
030     * @param value      the cell content's object
031     * @param isSelected boolean so we know if this is the currently selected
032     *                   row
033     * @param hasFocus   does this cell currently have focus?
034     * @param row        the row number
035     * @param column     the column number
036     * @return the JCheckBox to display
037     */
038    @Override
039    public Component getTableCellRendererComponent(JTable table, java.lang.Object value, boolean isSelected, boolean hasFocus, int row, int column) {
040        setSelected(value != null && ((Boolean) value));
041        setEnabled(table.isCellEditable(row, column));
042        setBackground(isSelected ? table.getSelectionBackground() : table.getBackground() );
043        return this;
044    }
045}