001package jmri.jmrix.mrc;
002
003import java.util.Comparator;
004import java.util.ResourceBundle;
005
006import jmri.*;
007import jmri.util.NamedBeanComparator;
008
009/**
010 * Lightweight class to denote that a system is active, and provide general
011 * information.
012 * <p>
013 * Objects of specific subtypes are registered in the instance manager to
014 * activate their particular system.
015 *
016 * @author Ken Cameron Copyright (C) 2014
017 * @author Kevin Dickerson Copyright (C) 2014
018 *
019 */
020public class MrcSystemConnectionMemo extends jmri.jmrix.DefaultSystemConnectionMemo {
021
022    public MrcSystemConnectionMemo() {
023        super("M", "MRC"); // NOI18N
024        register(); // registers general type
025        InstanceManager.store(this, MrcSystemConnectionMemo.class); // also register as specific type
026
027        // create and register the ComponentFactory
028        InstanceManager.store(componentFactory = new jmri.jmrix.mrc.swing.MrcComponentFactory(this),
029                jmri.jmrix.swing.ComponentFactory.class);
030    }
031
032    jmri.jmrix.swing.ComponentFactory componentFactory = null;
033
034    /**
035     * Provides access to the TrafficController for this particular connection.
036     *
037     * @return current traffic controller for this connection
038     */
039    public MrcTrafficController getMrcTrafficController() {
040        if (mrcTrafficController == null) log.error("found tc null in request", new Exception("traceback"));
041        return mrcTrafficController;
042    }
043    private MrcTrafficController mrcTrafficController;
044
045    public void setMrcTrafficController(MrcTrafficController tc) {
046        mrcTrafficController = tc;
047        if (tc != null) {
048            tc.setAdapterMemo(this);
049        }
050    }
051
052    public MrcProgrammerManager getProgrammerManager() {
053        //Do not want to return a programmer if the system is disabled
054        if (getDisabled()) {
055            return null;
056        }
057        return (MrcProgrammerManager)classObjectMap.computeIfAbsent(MrcProgrammerManager.class,
058                (Class c) -> new MrcProgrammerManager(new MrcProgrammer(this), this));
059    }
060
061    public void setProgrammerManager(MrcProgrammerManager p) {
062        store(p, MrcProgrammerManager.class);
063    }
064
065    /**
066     * Configure the common managers for MRC connections. This puts the common
067     * manager config in one place.
068     */
069    public void configureManagers() {
070        PowerManager powerManager = new jmri.jmrix.mrc.MrcPowerManager(this);
071        store(powerManager,PowerManager.class);
072        InstanceManager.store(powerManager, PowerManager.class);
073
074        TurnoutManager turnoutManager = new jmri.jmrix.mrc.MrcTurnoutManager(this);
075        store(turnoutManager,TurnoutManager.class);
076        InstanceManager.setTurnoutManager(turnoutManager);
077
078        ThrottleManager throttleManager = new jmri.jmrix.mrc.MrcThrottleManager(this);
079        store(throttleManager,ThrottleManager.class);
080        InstanceManager.setThrottleManager(throttleManager);
081
082        if (getProgrammerManager().isAddressedModePossible()) {
083            store(getProgrammerManager(),AddressedProgrammerManager.class);
084            InstanceManager.store(getProgrammerManager(), AddressedProgrammerManager.class);
085        }
086        if (getProgrammerManager().isGlobalProgrammerAvailable()) {
087            store(getProgrammerManager(),GlobalProgrammerManager.class);
088            InstanceManager.store(getProgrammerManager(), GlobalProgrammerManager.class);
089        }
090
091        ClockControl clockManager = new jmri.jmrix.mrc.MrcClockControl(getMrcTrafficController(), getSystemPrefix());
092        store(clockManager,ClockControl.class);
093        InstanceManager.store(clockManager, jmri.ClockControl.class);
094        InstanceManager.setDefault(jmri.ClockControl.class, clockManager);
095
096    }
097
098    public MrcPowerManager getPowerManager() {
099        return get(PowerManager.class);
100    }
101
102    public MrcTurnoutManager getTurnoutManager() {
103        return get(TurnoutManager.class);
104    }
105
106    public MrcThrottleManager getThrottleManager() {
107        return get(ThrottleManager.class);
108    }
109
110    public MrcClockControl getClockControl() {
111        return get(ClockControl.class);
112    }
113
114    @Override
115    protected ResourceBundle getActionModelResourceBundle() {
116        return ResourceBundle.getBundle("jmri.jmrix.mrc.MrcActionListBundle"); //NO18N
117    }
118
119    @Override
120    public <B extends NamedBean> Comparator<B> getNamedBeanComparator(Class<B> type) {
121        return new NamedBeanComparator<>();
122    }
123
124    @Override
125    public void dispose() {
126        mrcTrafficController = null;
127        InstanceManager.deregister(this, MrcSystemConnectionMemo.class);
128        if (componentFactory != null) {
129            InstanceManager.deregister(componentFactory, jmri.jmrix.swing.ComponentFactory.class);
130        }
131
132        super.dispose();
133    }
134
135    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MrcSystemConnectionMemo.class.getName());
136
137}