001package jmri.jmrix.nce;
002
003import java.util.Comparator;
004import java.util.ResourceBundle;
005
006import jmri.*;
007import jmri.jmrix.ConfiguringSystemConnectionMemo;
008import jmri.jmrix.DefaultSystemConnectionMemo;
009import jmri.util.NamedBeanComparator;
010
011/**
012 * Lightweight class to denote that a system is active, and provide general
013 * information.
014 * <p>
015 * Objects of specific subtypes are registered in the instance manager to
016 * activate their particular system.
017 *
018 * @author Bob Jacobsen Copyright (C) 2010
019 * @author ken cameron Copyright (C) 2013
020 */
021public class NceSystemConnectionMemo extends DefaultSystemConnectionMemo implements ConfiguringSystemConnectionMemo {
022
023    public NceSystemConnectionMemo() {
024        super("N", "NCE");
025        register(); // registers general type
026        InstanceManager.store(this, NceSystemConnectionMemo.class); // also register as specific type
027
028        // create and register the ComponentFactory
029        InstanceManager.store(componentFactory = new jmri.jmrix.nce.swing.NceComponentFactory(this),
030                jmri.jmrix.swing.ComponentFactory.class);
031    }
032
033    jmri.jmrix.swing.ComponentFactory componentFactory;
034
035    public void setNceUsbSystem(int result) {
036        getNceTrafficController().setUsbSystem(result);
037    }
038
039    public int getNceUsbSystem() {
040        if (getNceTrafficController() != null) {
041            return getNceTrafficController().getUsbSystem();
042        }
043        return NceTrafficController.USB_SYSTEM_NONE;
044    } // error no connection!
045
046    public void setNceCmdGroups(long result) {
047        getNceTrafficController().setCmdGroups(result);
048    }
049
050    public long getNceCmdGroups() {
051        if (getNceTrafficController() != null) {
052            return getNceTrafficController().getCmdGroups();
053        }
054        return NceTrafficController.CMDS_NONE;
055    } // error no connection!
056
057    /**
058     * Provides access to the TrafficController for this particular connection.
059     *
060     * @return tc for this connection
061     */
062    public NceTrafficController getNceTrafficController() {
063        return nceTrafficController;
064    }
065    private NceTrafficController nceTrafficController;
066
067    public void setNceTrafficController(NceTrafficController tc) {
068        nceTrafficController = tc;
069        if (tc != null) {
070            tc.setAdapterMemo(this);
071        }
072    }
073
074    public NceProgrammerManager getProgrammerManager() {
075        //Do not want to return a programmer if the system is disabled
076        if (getDisabled()) {
077            return null;
078        }
079        return (NceProgrammerManager) classObjectMap.computeIfAbsent(NceProgrammerManager.class,(Class<?> c) -> new NceProgrammerManager(this));
080    }
081
082    public void setProgrammerManager(NceProgrammerManager p) {
083        store(p,NceProgrammerManager.class);
084    }
085
086    /**
087     * Sets the NCE message option.
088     *
089     * @param val command option value
090     */
091    public void configureCommandStation(int val) {
092        getNceTrafficController().setCommandOptions(val);
093        store(nceTrafficController,CommandStation.class);
094        jmri.InstanceManager.store(nceTrafficController, jmri.CommandStation.class);
095    }
096
097    /**
098     * Configure the common managers for NCE connections. This puts the common
099     * manager config in one place.
100     */
101    @Override
102    public void configureManagers() {
103        log.trace("configureManagers() with: {} ", getNceUsbSystem());
104        PowerManager powerManager = new jmri.jmrix.nce.NcePowerManager(this);
105        store(powerManager,PowerManager.class);
106        InstanceManager.store(powerManager, jmri.PowerManager.class);
107
108        TurnoutManager turnoutManager = new jmri.jmrix.nce.NceTurnoutManager(this);
109        store(turnoutManager,TurnoutManager.class);
110        InstanceManager.setTurnoutManager(turnoutManager);
111
112        LightManager lightManager = new jmri.jmrix.nce.NceLightManager(this);
113        store(lightManager,LightManager.class);
114        InstanceManager.setLightManager(lightManager);
115
116        SensorManager sensorManager = new jmri.jmrix.nce.NceSensorManager(this);
117        store(sensorManager,SensorManager.class);
118        InstanceManager.setSensorManager(sensorManager);
119
120        NceThrottleManager throttleManager = new jmri.jmrix.nce.NceThrottleManager(this);
121        store(throttleManager,ThrottleManager.class);
122        InstanceManager.setThrottleManager(throttleManager);
123
124        // non-USB case
125        if (getProgrammerManager().isAddressedModePossible()) {
126            log.trace("store AddressedProgrammerManager");
127            store(getProgrammerManager(),AddressedProgrammerManager.class);
128            InstanceManager.store(getProgrammerManager(), AddressedProgrammerManager.class);
129        }
130        if (getProgrammerManager().isGlobalProgrammerAvailable()) {
131            log.trace("store GlobalProgrammerManager");
132            store(getProgrammerManager(),GlobalProgrammerManager.class);
133            InstanceManager.store(getProgrammerManager(), GlobalProgrammerManager.class);
134        }
135
136        NceClockControl clockManager = new NceClockControl(getNceTrafficController(), getSystemPrefix());
137        store(clockManager,ClockControl.class);
138        // make sure InstanceManager knows about that
139        InstanceManager.store(clockManager, jmri.ClockControl.class);
140        InstanceManager.setDefault(jmri.ClockControl.class, clockManager);
141
142        setConsistManager(new jmri.jmrix.nce.NceConsistManager(this));
143
144        log.trace("configureManagers() end");
145    }
146
147    public NcePowerManager getPowerManager() {
148        return (NcePowerManager)get(PowerManager.class);
149    }
150
151    public NceTurnoutManager getTurnoutManager() {
152        return (NceTurnoutManager)get(TurnoutManager.class);
153    }
154
155    public NceLightManager getLightManager() {
156        return (NceLightManager)get(LightManager.class);
157    }
158
159    public NceSensorManager getSensorManager() {
160        return (NceSensorManager)get(SensorManager.class);
161    }
162
163    public NceThrottleManager getThrottleManager() {
164        return (NceThrottleManager)get(ThrottleManager.class);
165    }
166
167    public NceClockControl getClockControl() {
168        return (NceClockControl)get(ClockControl.class);
169    }
170
171    /**
172     * {@inheritDoc}
173     */
174    @Override
175    protected ResourceBundle getActionModelResourceBundle() {
176        return ResourceBundle.getBundle("jmri.jmrix.nce.NceActionListBundle");
177    }
178
179    @Override
180    public <B extends NamedBean> Comparator<B> getNamedBeanComparator(Class<B> type) {
181        return new NamedBeanComparator<>();
182    }
183
184    /**
185     * {@inheritDoc}
186     */
187    @Override
188    public void dispose() {
189        nceTrafficController = null;
190        InstanceManager.deregister(this, NceSystemConnectionMemo.class);
191        if (componentFactory != null) {
192            InstanceManager.deregister(componentFactory, jmri.jmrix.swing.ComponentFactory.class);
193        }
194        super.dispose();
195    }
196
197    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(NceSystemConnectionMemo.class);
198}