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