001package jmri.jmrit.roster.swing;
002
003import java.awt.Component;
004import javax.swing.ImageIcon;
005import javax.swing.JLabel;
006import javax.swing.JList;
007import javax.swing.ListCellRenderer;
008import jmri.InstanceManager;
009import jmri.jmrit.roster.RosterEntry;
010import jmri.jmrit.roster.RosterIconFactory;
011
012/**
013 * Render RosterEntry objects or Strings as cells in a JComboBox.
014 *
015 * @see jmri.jmrit.roster.swing.RosterEntryComboBox
016 */
017public class RosterEntryListCellRenderer extends JLabel implements ListCellRenderer<Object> {
018
019    public RosterEntryListCellRenderer() {
020        super();
021        setOpaque(true);
022    }
023
024    @Override
025    public Component getListCellRendererComponent(JList<?> list, Object value,
026            int index, boolean isSelected, boolean cellHasFocus) {
027        if (value != null) {
028            if (value instanceof RosterEntry) {
029                String rosterEntryTitle = ((RosterEntry) value).titleString();
030
031                ImageIcon icon = InstanceManager.getDefault(RosterIconFactory.class).getIcon(rosterEntryTitle);
032                if (icon != null) {
033                    icon.setImageObserver(list);
034                }
035                setIcon(icon);
036                setText(rosterEntryTitle);
037            } else {
038                setText(value.toString());
039                setIcon(null);
040            }
041        }
042        return this;
043    }
044}