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    @Override
083    public void configureManagers() {
084
085        InstanceManager.setLightManager(getLightManager());
086
087        InstanceManager.setSensorManager(getSensorManager());
088        getTrafficController().setSensorManager(getSensorManager());
089
090        InstanceManager.setTurnoutManager(getTurnoutManager());
091        getTrafficController().setTurnoutManager(getTurnoutManager());
092
093        register(); // registers general type
094    }
095
096    public AcelaTurnoutManager getTurnoutManager() {
097        if (getDisabled()) {
098            return null;
099        }
100        return (AcelaTurnoutManager) classObjectMap.computeIfAbsent(TurnoutManager.class, (Class<?> c) -> new AcelaTurnoutManager(this));
101    }
102
103    public AcelaSensorManager getSensorManager() {
104        if (getDisabled()) {
105            return null;
106        }
107        return (AcelaSensorManager) classObjectMap.computeIfAbsent(SensorManager.class,(Class<?> c) -> new AcelaSensorManager(this));
108    }
109
110    public AcelaLightManager getLightManager() {
111        if (getDisabled()) {
112            return null;
113        }
114        return (AcelaLightManager) classObjectMap.computeIfAbsent(LightManager.class,(Class<?> c) -> new AcelaLightManager(this));
115    }
116
117    @Override
118    protected ResourceBundle getActionModelResourceBundle() {
119        return ResourceBundle.getBundle("jmri.jmrix.acela.AcelaActionListBundle");
120    }
121
122    @Override
123    public <B extends NamedBean> Comparator<B> getNamedBeanComparator(Class<B> type) {
124        return new NamedBeanComparator<>();
125    }
126
127    @Override
128    public void dispose() {
129        tc = null;
130        InstanceManager.deregister(this, AcelaSystemConnectionMemo.class);
131        if (cf != null) {
132            InstanceManager.deregister(cf, jmri.jmrix.swing.ComponentFactory.class);
133        }
134        super.dispose();
135    }
136
137    private final static Logger log = LoggerFactory.getLogger(AcelaSystemConnectionMemo.class);
138
139}