001package jmri.jmrit.display.controlPanelEditor;
002
003import java.awt.Component;
004import java.awt.Dimension;
005import java.beans.PropertyChangeEvent;
006import java.beans.PropertyChangeListener;
007import java.util.ArrayList;
008import java.util.List;
009import javax.swing.AbstractListModel;
010import javax.swing.JLabel;
011import javax.swing.JList;
012import javax.swing.ListCellRenderer;
013
014import org.slf4j.Logger;
015import org.slf4j.LoggerFactory;
016
017import jmri.jmrit.logix.OBlock;
018import jmri.jmrit.logix.Portal;
019
020/**
021 *
022 * @author Pete Cressman Copyright: Copyright (c) 2014
023 *
024 */
025public class PortalList extends JList<Portal> {
026
027    private final PortalListModel _portalListModel;
028
029    PortalList(OBlock block, EditFrame parent) {
030        super();
031        _portalListModel = new PortalListModel(block, parent);
032        setModel(_portalListModel);
033        setCellRenderer(new PortalCellRenderer());
034        setPreferredSize(new Dimension(300, 120));
035        setVisibleRowCount(5);
036    }
037
038    void dataChange() {
039        _portalListModel.dataChange();
040    }
041
042    void setSelected(Portal portal) {
043        List<Portal> list = _portalListModel._homeBlock.getPortals();
044        for (int i = 0; i < list.size(); i++) {
045            if (list.get(i).equals(portal)) {
046                setSelectedIndex(i);
047                return;
048            }
049        }
050        clearSelection();
051    }
052
053    private static class PortalCellRenderer extends JLabel implements ListCellRenderer<Portal>{
054
055        @Override
056        public Component getListCellRendererComponent(
057                JList<? extends Portal> list, // the list
058                Portal value, // value to display
059                int index, // cell index
060                boolean isSelected, // is the cell selected
061                boolean cellHasFocus) // does the cell have focus
062        {
063            String s = value.getDescription();
064            setText(s);
065            if (isSelected) {
066                setBackground(list.getSelectionBackground());
067                setForeground(list.getSelectionForeground());
068            } else {
069                setBackground(list.getBackground());
070                setForeground(list.getForeground());
071            }
072            setEnabled(list.isEnabled());
073            setFont(list.getFont());
074            setOpaque(true);
075            return this;
076        }
077    }
078
079    static class PortalListModel extends AbstractListModel<Portal> implements PropertyChangeListener {
080
081        OBlock _homeBlock;
082        private final EditFrame _parent;
083        List<Portal> _list = new ArrayList<>();
084
085        PortalListModel(OBlock block, EditFrame parent) {
086            _homeBlock = block;
087            _parent = parent;
088            _homeBlock.addPropertyChangeListener(this);
089            makeList();
090        }
091        
092        private void makeList() {
093            for (Portal p : _list) {
094                p.removePropertyChangeListener(this);
095            }
096            _list = _homeBlock.getPortals();
097            for (Portal p : _list) {
098                p.addPropertyChangeListener(this);
099            }
100        }
101
102        @Override
103        public int getSize() {
104            return _homeBlock.getPortals().size();
105        }
106
107        @Override
108        public Portal getElementAt(int index) {
109            if (index < getSize()) {
110                return _homeBlock.getPortals().get(index);
111            }
112            return null;
113        }
114
115        public void dataChange() {
116            fireContentsChanged(this, 0, 0);
117        }
118
119        @Override
120        public void propertyChange(PropertyChangeEvent e) {
121            Object source = e.getSource();
122            String property = e.getPropertyName();
123            if (log.isDebugEnabled()) {
124                log.debug("property = {} source= {}", property, source.getClass().getName());                
125            }
126            if (source instanceof OBlock && property.equals("deleted")) {
127                _homeBlock.removePropertyChangeListener(this);
128                _parent.closingEvent(true);
129            } else {
130                makeList();
131                fireContentsChanged(this, 0, 0);
132                if (property.equals("signalChange") || property.equals("NameChange")) {
133                    _parent.clearListSelection();
134                }
135            }
136        }
137    }
138    private final static Logger log = LoggerFactory.getLogger(PortalList.class);
139}