001package jmri.jmrix.acela;
002
003import java.util.Comparator;
004import java.util.ResourceBundle;
005import javax.annotation.Nonnull;
006
007import jmri.*;
008import jmri.jmrix.ConfiguringSystemConnectionMemo;
009import jmri.jmrix.DefaultSystemConnectionMemo;
010import jmri.util.NamedBeanComparator;
011
012import org.slf4j.Logger;
013import org.slf4j.LoggerFactory;
014
015/**
016 * Lightweight class to denote that a system is active, and provide general
017 * information.
018 * <p>
019 * Objects of specific subtypes are registered in the instance manager to
020 * activate their particular system.
021 *
022 * @author Bob Jacobsen Copyright (C) 2010
023 */
024public class AcelaSystemConnectionMemo extends DefaultSystemConnectionMemo implements ConfiguringSystemConnectionMemo {
025
026    public AcelaSystemConnectionMemo() {
027        this("A", AcelaConnectionTypeList.CTI); // default to A
028    }
029
030    public AcelaSystemConnectionMemo(@Nonnull String prefix, @Nonnull String userName) {
031        super(prefix, userName);
032
033        InstanceManager.store(this, AcelaSystemConnectionMemo.class);
034
035        // create and register the AcelaComponentFactory for the GUI
036        InstanceManager.store(cf = new jmri.jmrix.acela.swing.AcelaComponentFactory(this),
037                jmri.jmrix.swing.ComponentFactory.class);
038        log.debug("Created AcelaSystemConnectionMemo");
039    }
040
041    public AcelaSystemConnectionMemo(AcelaTrafficController tc) {
042        super("A", AcelaConnectionTypeList.CTI); // default to A
043        this.tc = tc;
044
045        InstanceManager.store(this, AcelaSystemConnectionMemo.class);
046
047        // create and register the AcelaComponentFactory for the GUI
048        InstanceManager.store(cf = new jmri.jmrix.acela.swing.AcelaComponentFactory(this),
049                jmri.jmrix.swing.ComponentFactory.class);
050        log.debug("Created AcelaSystemConnectionMemo");
051    }
052
053    jmri.jmrix.swing.ComponentFactory cf = null;
054
055    /**
056     * Provides access to the TrafficController for this particular connection.
057     * @return traffic controller, provided if null.
058     */
059    public AcelaTrafficController getTrafficController() {
060        if (tc == null) {
061            setAcelaTrafficController(new AcelaTrafficController());
062            log.debug("Auto create of AcelaTrafficController for initial configuration");
063        }
064        return tc;
065    }
066
067    /**
068     * Set the traffic controller instance associated with this connection memo.
069     *
070     * @param tc jmri.jmrix.acela.AcelaTrafficController object to use
071     */
072    public void setAcelaTrafficController(AcelaTrafficController tc) {
073        this.tc = tc;
074    }
075
076    private AcelaTrafficController tc;
077
078    /**
079     * Configure the common managers for Acela connections. This puts the
080     * common manager config in one place.
081     */
082    public void configureManagers() {
083
084        InstanceManager.setLightManager(getLightManager());
085
086        InstanceManager.setSensorManager(getSensorManager());
087        getTrafficController().setSensorManager(getSensorManager());
088
089        InstanceManager.setTurnoutManager(getTurnoutManager());
090        getTrafficController().setTurnoutManager(getTurnoutManager());
091
092        register(); // registers general type
093    }
094
095    public AcelaTurnoutManager getTurnoutManager() {
096        if (getDisabled()) {
097            return null;
098        }
099        return (AcelaTurnoutManager) classObjectMap.computeIfAbsent(TurnoutManager.class, (Class c) -> new AcelaTurnoutManager(this));
100    }
101
102    public AcelaSensorManager getSensorManager() {
103        if (getDisabled()) {
104            return null;
105        }
106        return (AcelaSensorManager) classObjectMap.computeIfAbsent(SensorManager.class,(Class c) -> new AcelaSensorManager(this));
107    }
108
109    public AcelaLightManager getLightManager() {
110        if (getDisabled()) {
111            return null;
112        }
113        return (AcelaLightManager) classObjectMap.computeIfAbsent(LightManager.class,(Class c) -> new AcelaLightManager(this));
114    }
115
116    @Override
117    protected ResourceBundle getActionModelResourceBundle() {
118        return ResourceBundle.getBundle("jmri.jmrix.acela.AcelaActionListBundle");
119    }
120
121    @Override
122    public <B extends NamedBean> Comparator<B> getNamedBeanComparator(Class<B> type) {
123        return new NamedBeanComparator<>();
124    }
125
126    @Override
127    public void dispose() {
128        tc = null;
129        InstanceManager.deregister(this, AcelaSystemConnectionMemo.class);
130        if (cf != null) {
131            InstanceManager.deregister(cf, jmri.jmrix.swing.ComponentFactory.class);
132        }
133        super.dispose();
134    }
135
136    private final static Logger log = LoggerFactory.getLogger(AcelaSystemConnectionMemo.class);
137
138}