001package jmri.jmrix.ecos;
002
003import java.util.Comparator;
004import java.util.ResourceBundle;
005
006import jmri.*;
007import jmri.util.NamedBeanComparator;
008
009
010/**
011 * Lightweight class to denote that a system is active, and provide general
012 * information.
013 * <p>
014 * Objects of specific subtypes are registered in the instance manager to
015 * activate their particular system.
016 *
017 * @author Bob Jacobsen Copyright (C) 2010
018 */
019public class EcosSystemConnectionMemo extends jmri.jmrix.DefaultSystemConnectionMemo {
020
021    public EcosSystemConnectionMemo(EcosTrafficController et) {
022        super("U", "ECoS");
023        this.et = et;
024        et.setAdapterMemo(EcosSystemConnectionMemo.this);
025        init();
026    }
027
028    public EcosSystemConnectionMemo() {
029        super("U", "ECoS");
030        init();
031    }
032
033    private void init() {
034        InstanceManager.store(this, EcosSystemConnectionMemo.class); // also register as specific type
035        InstanceManager.store(cf = new jmri.jmrix.ecos.swing.EcosComponentFactory(this),
036                jmri.jmrix.swing.ComponentFactory.class);
037        store(new EcosPreferences(this), EcosPreferences.class);
038    }
039
040    private jmri.jmrix.swing.ComponentFactory cf = null;
041
042    /**
043     * Provides access to the TrafficController for this particular connection.
044     * @return Ecos traffic controller.
045     */
046    public EcosTrafficController getTrafficController() {
047        return et;
048    }
049
050    public void setEcosTrafficController(EcosTrafficController et) {
051        this.et = et;
052        et.setAdapterMemo(this);
053    }
054    private EcosTrafficController et;
055
056    /**
057     * This puts the common manager config in one place.
058     */
059    public void configureManagers() {
060
061        PowerManager powerManager = new EcosPowerManager(getTrafficController());
062        InstanceManager.store(powerManager, PowerManager.class);
063        store(powerManager, PowerManager.class);
064
065        EcosLocoAddressManager locoManager = new EcosLocoAddressManager(this);
066        store(locoManager,EcosLocoAddressManager.class);
067
068        ThrottleManager throttleManager = new EcosDccThrottleManager(this);
069        InstanceManager.setThrottleManager(throttleManager);
070        store(throttleManager,ThrottleManager.class);
071
072        ReporterManager reporterManager = new EcosReporterManager(this);
073        InstanceManager.setReporterManager(reporterManager);
074        store(reporterManager,ReporterManager.class);
075
076        InstanceManager.store(getProgrammerManager(), GlobalProgrammerManager.class);
077        store(getProgrammerManager(), GlobalProgrammerManager.class);
078
079        InstanceManager.store(getProgrammerManager(), AddressedProgrammerManager.class);
080        store(getProgrammerManager(), AddressedProgrammerManager.class);
081
082        SensorManager sensorManager = new EcosSensorManager(this);
083        InstanceManager.setSensorManager(sensorManager);
084        store(sensorManager, SensorManager.class);
085
086        TurnoutManager turnoutManager = new EcosTurnoutManager(this);
087        InstanceManager.setTurnoutManager(turnoutManager);
088        store(turnoutManager,TurnoutManager.class);
089
090        register(); // registers general type
091    }
092
093    @Override
094    protected ResourceBundle getActionModelResourceBundle() {
095        return ResourceBundle.getBundle("jmri.jmrix.ecos.EcosActionListBundle");
096    }
097
098    @Override
099    public <B extends NamedBean> Comparator<B> getNamedBeanComparator(Class<B> type) {
100        return new NamedBeanComparator<>();
101    }
102
103    public EcosLocoAddressManager getLocoAddressManager() {
104        return get(EcosLocoAddressManager.class);
105    }
106
107    public EcosTurnoutManager getTurnoutManager() {
108        return (EcosTurnoutManager)get(TurnoutManager.class);
109    }
110
111    public EcosSensorManager getSensorManager() {
112        return (EcosSensorManager)get(SensorManager.class);
113    }
114
115    public EcosPreferences getPreferenceManager() {
116        return get(EcosPreferences.class);
117    }
118
119    public EcosDccThrottleManager getThrottleManager() {
120        return (EcosDccThrottleManager)get(ThrottleManager.class);
121    }
122
123    public EcosPowerManager getPowerManager() {
124        return (EcosPowerManager)get(PowerManager.class);
125    }
126
127    public EcosReporterManager getReporterManager() {
128        return (EcosReporterManager)get(ReporterManager.class);
129    }
130
131    public EcosProgrammerManager getProgrammerManager() {
132        return (EcosProgrammerManager) classObjectMap.computeIfAbsent(EcosProgrammerManager.class, (Class<?> c) ->
133            new EcosProgrammerManager(new EcosProgrammer(getTrafficController()), this));
134    }
135
136    @Override
137    public void dispose() {
138        EcosLocoAddressManager locoManager = get(EcosLocoAddressManager.class);
139        if (locoManager != null) {
140            locoManager.dispose();
141            deregister(locoManager, EcosLocoAddressManager.class);
142        }
143        et = null;
144        InstanceManager.deregister(this, EcosSystemConnectionMemo.class);
145        if (cf != null) {
146            InstanceManager.deregister(cf, jmri.jmrix.swing.ComponentFactory.class);
147        }
148        EcosPreferences prefManager = get(EcosPreferences.class);
149        if (prefManager != null) {
150            InstanceManager.getDefault(ShutDownManager.class).deregister(prefManager.ecosPreferencesShutDownTask);
151            deregister(prefManager, EcosPreferences.class);
152        }
153        super.dispose();
154    }
155
156    // private final static Logger log = LoggerFactory.getLogger(EcosSystemConnectionMemo.class);
157
158}