001package jmri.jmrit.throttle.panels;
002
003import java.awt.BorderLayout;
004
005import javax.swing.ImageIcon;
006import javax.swing.JLabel;
007import javax.swing.JPanel;
008
009import org.jdom2.Element;
010
011import jmri.DccThrottle;
012import jmri.LocoAddress;
013import jmri.jmrit.consisttool.ConsistListCellRenderer;
014import jmri.jmrit.roster.RosterEntry;
015import jmri.jmrit.roster.RosterIconFactory;
016import jmri.jmrit.throttle.interfaces.AddressListener;
017
018/**
019 * A Panel that displays the loco icon and label as defined in its roster entry. 
020 * 
021 * <hr>
022 * This file is part of JMRI.
023 * <p>
024 * JMRI is free software; you can redistribute it and/or modify it under the
025 * terms of version 2 of the GNU General Public License as published by the Free
026 * Software Foundation. See the "COPYING" file for a copy of this license.
027 * <p>
028 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
029 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
030 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
031 *
032 */
033
034public class LocoIconPanel extends JPanel implements AddressListener {
035
036    private static final int IMAGE_HEIGHT = 32;
037    private static final RosterIconFactory ICN_FACT = new RosterIconFactory(IMAGE_HEIGHT); // same instance reused
038
039    AddressPanel addressPanel = null;
040    String description;
041    ImageIcon icon;
042    JLabel iconLabel = new JLabel();
043
044    public LocoIconPanel() {
045        super();
046        initGUI();
047    }
048
049    private void initGUI() {
050        setLayout(new BorderLayout());
051        iconLabel.setHorizontalAlignment(JLabel.CENTER);
052        iconLabel.setVerticalAlignment(JLabel.CENTER);
053        updateLabel();
054        add(iconLabel, BorderLayout.CENTER);        
055    }
056
057    public void setAddressPanel(AddressPanel addressPanel) {
058        if (this.addressPanel != null) {
059            this.addressPanel.removeAddressListener(this);
060        }
061        this.addressPanel = addressPanel;
062        if (this.addressPanel != null) {
063            this.addressPanel.addAddressListener(this);
064        }
065        updateLabel();
066    }
067
068    public String getDescription() {
069        return description;
070    }
071
072    public ImageIcon getIcon() {
073        return icon;
074    }
075
076    private void updateLabel() {
077        description = Bundle.getMessage("ThrottleNotAssigned");
078        icon = null;
079        if (addressPanel != null && (addressPanel.getThrottle() != null)) {
080            if (addressPanel.getConsistAddress() != null) { 
081                // consists                
082                icon = ConsistListCellRenderer.getConsistIcon(addressPanel.getConsistAddress(), ICN_FACT);
083                description = addressPanel.getConsistAddress().toString();
084            } else if (addressPanel.getRosterEntry() != null) {  
085                // regular locomotive                                   
086                icon = ICN_FACT.getIcon(addressPanel.getRosterEntry());
087                description = addressPanel.getRosterEntry().getId();
088            } else if (addressPanel.getCurrentAddress() != null)  {
089                switch (addressPanel.getCurrentAddress().getNumber()) {
090                    case 0:
091                        description = Bundle.getMessage("ThrottleDCControl") + " - " + addressPanel.getCurrentAddress();
092                        break;
093                    case 3:
094                        description = Bundle.getMessage("ThrottleDCCControl") + " - " + addressPanel.getCurrentAddress();
095                        break;
096                    default:
097                        description = Bundle.getMessage("ThrottleAddress") + " " + addressPanel.getCurrentAddress();
098                        break;
099                }
100            }
101        }
102        iconLabel.setIcon(icon);
103        iconLabel.setText(description);
104    }
105
106    @Override
107    public void notifyAddressThrottleFound(DccThrottle t) {
108        updateLabel();
109    }
110
111    @Override
112    public void notifyRosterEntrySelected(RosterEntry re) {
113        updateLabel();
114    }
115
116    @Override
117    public void notifyAddressReleased(LocoAddress la) {
118        updateLabel();
119    }
120
121    @Override
122    public void notifyAddressChosen(LocoAddress l) {
123        updateLabel();
124    }
125
126    @Override
127    public void notifyConsistAddressChosen(LocoAddress l) {
128        updateLabel();
129    }
130
131    @Override
132    public void notifyConsistAddressReleased(LocoAddress l) {
133        updateLabel();
134    }
135
136    @Override
137    public void notifyConsistAddressThrottleFound(DccThrottle t) {
138        updateLabel();
139    }
140
141    public void destroy() {        
142        if (addressPanel != null) {
143            addressPanel.removeAddressListener(this);
144            addressPanel = null;
145        }
146    }
147
148    public Element getXml() {
149        Element me = new Element("LocoIconPanel"); // NOI18N
150        // put nothing
151        return me;
152    }
153
154    public void setXml(Element e) {
155        // do nothing
156    }
157
158}