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