001package jmri.jmrit.throttle;
002
003import java.awt.Color;
004
005import jmri.DccThrottle;
006import jmri.InstanceManager;
007import jmri.LocoAddress;
008import jmri.jmrit.roster.RosterEntry;
009import jmri.util.swing.ResizableImagePanel;
010
011/**
012 * A panel to be used as background for JMRI throttle frames 
013 * 
014 * @author Lionel Jeanson - 2009-
015 * 
016 */
017
018public class BackgroundPanel extends ResizableImagePanel implements AddressListener {
019
020    AddressPanel addressPanel = null;
021
022    public BackgroundPanel() {
023        super();
024        initGUI();
025        applyPreferences();
026    }
027    
028    private void initGUI() {
029        setBackground(Color.GRAY);
030        setRespectAspectRatio(true);
031    }
032    
033    public void applyPreferences() {
034        setResizingContainer(InstanceManager.getDefault(ThrottlesPreferences.class).isResizingWindow());
035    }
036
037    public void setAddressPanel(AddressPanel addressPanel) {
038        this.addressPanel = addressPanel;
039    }
040
041    @Override
042    public void notifyAddressThrottleFound(DccThrottle t) {
043        RosterEntry rosterEntry = null;
044        if (addressPanel != null) {
045            rosterEntry = addressPanel.getRosterEntry();
046        }
047        if (rosterEntry != null) {
048            setImagePath(rosterEntry.getImagePath());
049        } else {
050            if (t.getLocoAddress().toString().compareTo("3(S)") == 0) // default DCC address
051            {
052                setImagePath(jmri.util.FileUtil.getExternalFilename("resources/icons/throttles/DCCImage.png"));
053            }
054            if (t.getLocoAddress().toString().compareTo("0(S)") == 0) // default DC address
055            {
056                setImagePath(jmri.util.FileUtil.getExternalFilename("resources/icons/throttles/DCImage.png"));
057            }
058        }
059    }
060
061    @Override
062    public void notifyAddressReleased(LocoAddress la) {
063        setImagePath(null);
064        setVisible(false);
065    }
066
067    @Override
068    public void notifyAddressChosen(LocoAddress l) {
069    }
070
071    @Override
072    public void notifyConsistAddressChosen(LocoAddress l) {
073        notifyAddressChosen(l);
074    }
075
076    @Override
077    public void notifyConsistAddressReleased(LocoAddress l) {
078         notifyAddressReleased(l);
079    }
080
081    @Override
082    public void notifyConsistAddressThrottleFound(DccThrottle t) {
083        notifyAddressThrottleFound(t);
084    }
085
086    public void destroy() {
087        if (addressPanel != null) {
088            addressPanel.removeAddressListener(this);
089            addressPanel = null;
090        }
091    }
092
093    // private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(BackgroundPanel.class);    
094}