001package jmri.util.table;
002
003// This was adapted from Core Swing Advanced Programming, Prentice Hall
004// Changes: Remove DataWithIcon reference.  Change package.
005import java.awt.Color;
006import java.awt.Component;
007import java.awt.Font;
008import javax.swing.Icon;
009import javax.swing.JButton;
010import javax.swing.JTable;
011import javax.swing.UIManager;
012import javax.swing.border.Border;
013import javax.swing.table.TableCellRenderer;
014
015// A holder for data and an associated icon
016public class ButtonRenderer extends JButton
017        implements TableCellRenderer {
018
019    public ButtonRenderer() {
020        this.border = getBorder();
021        this.setOpaque(true);
022        putClientProperty("JComponent.sizeVariant", "small");
023        putClientProperty("JButton.buttonType", "square");
024    }
025
026    @Override
027    public void setForeground(Color foreground) {
028        this.foreground = foreground;
029        super.setForeground(foreground);
030    }
031
032    @Override
033    public void setBackground(Color background) {
034        this.background = background;
035        super.setBackground(background);
036    }
037
038    @Override
039    public void setFont(Font font) {
040        this.font = font;
041        super.setFont(font);
042    }
043
044    @Override
045    public Component getTableCellRendererComponent(JTable table,
046            Object value, boolean isSelected,
047            boolean hasFocus,
048            int row, int column) {
049        Color cellForeground = foreground != null ? foreground
050                : table.getForeground();
051        Color cellBackground = background != null ? background
052                : table.getBackground();
053
054        setFont(font != null ? font : table.getFont());
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);
071
072        return this;
073    }
074
075    protected void setValue(Object value) {
076        if (value == null) {
077            setText("");
078            setIcon(null);
079            setEnabled(false);
080        } else if (value instanceof Icon) {
081            setText("");
082            setIcon((Icon) value);
083            setEnabled(true);
084        } else {
085            setText(value.toString());
086            setIcon(null);
087            setEnabled(true);
088        }
089    }
090
091    protected Color foreground;
092    protected Color background;
093    protected Font font;
094    protected Border border;
095}