001package apps.startup;
002
003import java.awt.Color;
004import java.awt.Component;
005
006import javax.swing.JTable;
007import javax.swing.table.DefaultTableCellRenderer;
008
009import jmri.util.startup.StartupModel;
010
011/**
012 * Render a {@link StartupModel} in a table.
013 * <p>
014 * An action that cannot be run, for example because its connection is not
015 * available, is shown in red so it stands out in the list. A selected row
016 * keeps the normal selection colours.
017 *
018 * @author Randall Wood Copyright (C) 2015, 2016
019 */
020class StartupModelCellRenderer extends DefaultTableCellRenderer {
021
022    static final Color INVALID_COLOR = Color.RED;
023
024    @Override
025    public Component getTableCellRendererComponent(JTable table, Object value,
026            boolean isSelected, boolean hasFocus, int row, int column) {
027        // clear any colour left from the previous cell, as the component is reused
028        setForeground(null);
029        Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
030        // a selected row keeps the selection colours so it stays readable
031        if (!isSelected && value instanceof StartupModel && !((StartupModel) value).isValid()) {
032            c.setForeground(INVALID_COLOR);
033        }
034        return c;
035    }
036
037    @Override
038    protected void setValue(Object value) {
039        // null anything is rendered as an empty cell.
040        if (value != null) {
041            String text = ((StartupModel) value).getName();
042            setText(text != null ? text : value.toString()); // NOI18N
043        } else {
044            setText(""); // NOI18N
045        }
046    }
047}