001package jmri.util.table;
002
003import javax.swing.*;
004import javax.swing.border.Border;
005import javax.swing.table.TableCellRenderer;
006import java.awt.*;
007
008/**
009 * Works with {@link ToggleButtonRenderer}.
010 * <p>
011 * Adapted from {@link ButtonRenderer}
012 * @author Egbert Broerse 2020
013*/
014
015// A holder for data and and associated state display text strings
016public class ToggleButtonRenderer extends JToggleButton
017        implements TableCellRenderer {
018
019    public ToggleButtonRenderer(String on, String off) {
020        onText = on;
021        offText = off;
022        this.border = getBorder();
023        this.setOpaque(true);
024        putClientProperty("JComponent.sizeVariant", "small");
025        putClientProperty("JToggleButton.buttonType", "square");
026    }
027
028    @Override
029    public void setForeground(Color foreground) {
030        this.foreground = foreground;
031        super.setForeground(foreground);
032    }
033
034    @Override
035    public void setBackground(Color background) {
036        this.background = background;
037        super.setBackground(background);
038    }
039
040    @Override
041    public void setFont(Font font) {
042        this.font = font;
043        super.setFont(font);
044    }
045
046    @Override
047    public Component getTableCellRendererComponent(JTable table,
048            Object value, boolean isSelected,
049            boolean hasFocus,
050            int row, int column) {
051        Color cellForeground = foreground != null ? foreground
052                : table.getForeground();
053        Color cellBackground = background != null ? background
054                : table.getBackground();
055
056        if (hasFocus) {
057            setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
058            if (table.isCellEditable(row, column)) {
059                cellForeground = UIManager.getColor("Table.focusCellForeground");
060                cellBackground = UIManager.getColor("Table.focusCellBackground");
061            }
062        } else {
063            setBorder(border);
064        }
065
066        super.setForeground(cellForeground);
067        super.setBackground(cellBackground);
068
069        // Customize the component's appearance
070        setValue(value, isSelected);
071
072        return this;
073    }
074
075    protected void setValue(Object value, Boolean sel) {
076        switch (value.toString()) {
077            case "true":
078                setText(onText);
079                setSelected(sel);
080                setEnabled(true);
081                break;
082            case "false":
083                setText(offText);
084                setSelected(sel);
085                setEnabled(true);
086                break;
087            default:
088                setText("?");
089                setEnabled(false);
090        }
091    }
092
093    protected String onText;
094    protected String offText;
095    protected Color foreground;
096    protected Color background;
097    protected Font font;
098    protected Border border;
099}