001package jmri.util.table;
002
003import java.awt.Component;
004
005import javax.swing.*;
006import javax.swing.border.Border;
007import javax.swing.table.TableCellRenderer;
008
009import jmri.NamedBean;
010import jmri.swing.NamedBeanComboBox;
011import jmri.util.SystemType;
012
013/**
014 * NamedBeanBoxRenderer renders a NamedBeanComboBox within a JTable Cell.
015 * @author Steve
016 * @param <T> a Bean which extends from NamedBean, e.g. Sensor or Turnout.
017 */
018public class NamedBeanBoxRenderer<T extends NamedBean> extends NamedBeanComboBox<T> implements TableCellRenderer {
019
020    private final Border normalBorder;
021    private final Border errorBorder = BorderFactory.createLineBorder(java.awt.Color.RED);
022
023    public NamedBeanBoxRenderer(jmri.Manager<T> mgr) {
024        super(mgr);
025        normalBorder = getBorder();
026        setAllowNull(true);
027    }
028
029    public void setNormalBorder() {
030        setBorder(normalBorder);
031    }
032
033    public void setErrorBorder() {
034        setBorder(errorBorder);
035    }
036
037    @Override
038    public Component getTableCellRendererComponent(JTable table, Object value,
039        boolean isSelected, boolean hasFocus, int row, int column) {
040        if (!(SystemType.isMacOSX() && UIManager.getLookAndFeel().isNativeLookAndFeel())) {
041            if (isSelected) {
042                setForeground(table.getSelectionForeground());
043                setBackground(table.getSelectionBackground());
044            } else {
045                setForeground(table.getForeground());
046                setBackground(table.getBackground());
047            }
048        }
049        setSelectedItem(value instanceof NamedBean ? ((NamedBean) value) : null);
050        return this;
051    }
052
053}