001package jmri.jmrit.display.palette;
002
003import java.awt.event.ActionListener;
004import java.util.Enumeration;
005import java.util.HashMap;
006
007import javax.swing.JPanel;
008import javax.swing.event.ListSelectionEvent;
009import jmri.SignalAppearanceMap;
010import jmri.SignalMast;
011import jmri.jmrit.catalog.NamedIcon;
012import jmri.jmrit.display.DisplayFrame;
013import jmri.jmrit.picker.PickListModel;
014import org.slf4j.Logger;
015import org.slf4j.LoggerFactory;
016
017/**
018 * TableItemPanel extension for placing of SignalMast items with a fixed set of icons.
019 *
020 * @author Pete Cressman Copyright (c) 2010, 2011, 2020
021 * @author Egbert Broerse 2017
022 */
023public class SignalMastItemPanel extends TableItemPanel<SignalMast> {
024
025    private SignalMast _mast;
026
027    public SignalMastItemPanel(DisplayFrame parentFrame, String type, String family, PickListModel<jmri.SignalMast> model) {
028        super(parentFrame, type, family, model);
029        _currentIconMap = new HashMap<>();
030    }
031
032    @Override
033    public void init() {
034        super.init();
035        _showIconsButton.setEnabled(_mast != null);
036    }
037
038    @Override
039    public void init(ActionListener doneAction, HashMap<String, NamedIcon> iconMap) {
040        super.init(doneAction, iconMap);
041    }
042
043    @Override
044    protected JPanel makeItemButtonPanel() {
045        JPanel panel = new JPanel();
046        panel.add(makeShowIconsButton());
047        return panel;
048    }
049
050    @Override
051    protected JPanel makeSpecialBottomPanel(boolean update) {
052        JPanel panel = new JPanel();
053        panel.add(makeShowIconsButton());
054        return panel;
055    }
056
057    private void makeIconMap() {
058        if (_mast == null) {
059            _currentIconMap.clear();
060            _showIconsButton.setEnabled(false);
061            return;
062        }
063        _showIconsButton.setEnabled(true);
064        _family = _mast.getSignalSystem().getSystemName();
065        SignalAppearanceMap appMap = _mast.getAppearanceMap();
066        Enumeration<String> e = _mast.getAppearanceMap().getAspects();
067        while (e.hasMoreElements()) {
068            String aspect = e.nextElement();
069            String s = appMap.getImageLink(aspect, _family);
070            if (s !=null && !s.equals("")) {
071                if (!s.contains("preference:")) {
072                    s = s.substring(s.indexOf("resources"));
073                }
074                NamedIcon n = new NamedIcon(s, s);
075                _currentIconMap.put(aspect, n);
076            }
077        }
078        if (log.isDebugEnabled()) {
079            log.debug("makeIconMap for {}  size= {}", _family, _currentIconMap.size());
080        }
081    }
082
083    /**
084     * ListSelectionListener action.
085     */
086    @Override
087    public void valueChanged(ListSelectionEvent e) {
088        _mast = getDeviceNamedBean();
089        makeIconMap();
090        super.valueChanged(e);
091        updateFamiliesPanel();
092        setFamily();
093        if (log.isDebugEnabled()) {
094            log.debug("table valueChanged for= {}, row = {}", _itemType, _table.getSelectedRow());
095        }
096    }
097
098    private final static Logger log = LoggerFactory.getLogger(SignalMastItemPanel.class);
099
100}