001package jmri.jmrix.dccpp;
002
003import java.util.Comparator;
004import java.util.ResourceBundle;
005import javax.annotation.Nonnull;
006
007import jmri.*;
008import jmri.jmrix.DefaultSystemConnectionMemo;
009import jmri.util.NamedBeanComparator;
010
011import org.slf4j.Logger;
012import org.slf4j.LoggerFactory;
013
014/**
015 * Lightweight class to denote that a system is active and provide general
016 * information.
017 * <p>
018 * Objects of specific subtypes are registered in the instance manager to
019 * activate their particular system.
020 * Based on XNetSystemConnectionMemo by Paul Bender.
021 *
022 * @author Paul Bender Copyright (C) 2010
023 * @author Mark Underwood Copyright (C) 2015
024 */
025public class DCCppSystemConnectionMemo extends DefaultSystemConnectionMemo {
026
027    public DCCppSystemConnectionMemo(@Nonnull DCCppTrafficController xt) {
028        super("D", "DCC++");
029        this.xt = xt;
030        xt.setSystemConnectionMemo(this);
031        InstanceManager.store(this, DCCppSystemConnectionMemo.class); // also register as specific type
032
033        // create and register the DCCppComponentFactory
034        InstanceManager.store(cf = new jmri.jmrix.dccpp.swing.DCCppComponentFactory(this),
035                jmri.jmrix.swing.ComponentFactory.class);
036
037        log.debug("Created DCCppSystemConnectionMemo");
038    }
039
040    public DCCppSystemConnectionMemo() {
041        super("D", "DCC++");
042        InstanceManager.store(this, DCCppSystemConnectionMemo.class); // also register as specific type
043
044        // create and register the DCCppComponentFactory
045        InstanceManager.store(cf = new jmri.jmrix.dccpp.swing.DCCppComponentFactory(this),
046                jmri.jmrix.swing.ComponentFactory.class);
047
048        log.debug("Created DCCppSystemConnectionMemo");
049    }
050
051    jmri.jmrix.swing.ComponentFactory cf = null;
052
053    /**
054     * Provide access to the TrafficController for this particular connection.
055     * @return traffic controller, one is provided if null.
056     */
057    public DCCppTrafficController getDCCppTrafficController() {
058        if (xt == null) {
059            setDCCppTrafficController(new DCCppPacketizer(new DCCppCommandStation(this))); // default to DCCppPacketizer TrafficController
060            log.debug("Auto create of DCCppTrafficController for initial configuration");
061        }
062        return xt;
063    }
064
065    private DCCppTrafficController xt;
066
067    /**
068     * Set the traffic controller instance associated with this connection memo.
069     *
070     * @param xt the {@link jmri.jmrix.dccpp.DCCppTrafficController} object to use.
071     */
072    public void setDCCppTrafficController(@Nonnull DCCppTrafficController xt) {
073        this.xt = xt;
074        // in addition to setting the traffic controller in this object,
075        // set the systemConnectionMemo in the traffic controller
076        xt.setSystemConnectionMemo(this);
077    }
078
079    /**
080     * Provides access to the Programmer for this particular connection.
081     * NOTE: Programmer defaults to null
082     * @return programmer manager.
083     */
084    public DCCppProgrammerManager getProgrammerManager() {
085        return get(DCCppProgrammerManager.class);
086    }
087
088    public void setProgrammerManager(DCCppProgrammerManager p) {
089        store(p,DCCppProgrammerManager.class);
090        store(p,GlobalProgrammerManager.class);
091        store(p,AddressedProgrammerManager.class);
092    }
093
094    /*
095     * Provides access to the Throttle Manager for this particular connection.
096     */
097    public ThrottleManager getThrottleManager() {
098        return (ThrottleManager) classObjectMap.computeIfAbsent(ThrottleManager.class,
099                (Class<?> c) -> new DCCppThrottleManager(this));
100    }
101
102    public void setThrottleManager(ThrottleManager t) {
103        store(t,ThrottleManager.class);
104    }
105    /*
106     * Provides access to the Clock Control for this particular connection.
107     * NOTE: May return null if the Clock Control has not been set.
108     */
109    public ClockControl getClockControl() {
110        return get(ClockControl.class);
111    }
112
113    public void setClockControl(ClockControl t) {
114        store(t,ClockControl.class);
115        InstanceManager.store(t, ClockControl.class);
116        InstanceManager.setDefault(ClockControl.class, t);
117    }
118
119    /*
120     * Provides access to the PowerManager for this particular connection.
121     */
122    @Nonnull
123    public PowerManager getPowerManager() {
124        return (PowerManager) classObjectMap.computeIfAbsent(PowerManager.class, (Class<?> c) -> {
125            PowerManager powerManager = new DCCppPowerManager(this);
126            log.debug("power manager created: {}", powerManager);
127            return powerManager;
128        });
129    }
130
131    public void setPowerManager(@Nonnull PowerManager p) {
132        store(p,PowerManager.class);
133    }
134
135    /*
136     * Provides access to the SensorManager for this particular connection.
137     * NOTE: SensorManager defaults to NULL
138     */
139    public SensorManager getSensorManager() {
140        return get(SensorManager.class);
141
142    }
143
144    public void setSensorManager(SensorManager s) {
145        store(s,SensorManager.class);
146    }
147
148    /*
149     * Provides access to the TurnoutManager for this particular connection.
150     * NOTE: TurnoutManager defaults to NULL
151     */
152    public TurnoutManager getTurnoutManager() {
153        return get(TurnoutManager.class);
154
155    }
156
157    public void setTurnoutManager(TurnoutManager t) {
158        store(t,TurnoutManager.class);
159    }
160
161    /*
162     * Provides access to the LightManager for this particular connection.
163     * NOTE: Light manager defaults to NULL
164     */
165    public LightManager getLightManager() {
166        return get(LightManager.class);
167
168    }
169
170    public void setLightManager(LightManager l) {
171        store(l,LightManager.class);
172    }
173
174    /*
175     * Provides access to the Command Station for this particular connection.
176     * NOTE: Command Station defaults to NULL
177     */
178    public CommandStation getCommandStation() {
179        return get(CommandStation.class);
180    }
181
182    public void setCommandStation(@Nonnull CommandStation c) {
183        store(c,CommandStation.class);
184        if ( c instanceof DCCppCommandStation ) {
185            ((DCCppCommandStation) c).setTrafficController(xt);
186            ((DCCppCommandStation) c).setSystemConnectionMemo(this);
187        }
188    }
189
190    @Override
191    @Nonnull
192    protected ResourceBundle getActionModelResourceBundle() {
193        return ResourceBundle.getBundle("jmri.jmrix.dccpp.DCCppActionListBundle");
194    }
195
196    @Override
197    public <B extends NamedBean> Comparator<B> getNamedBeanComparator(Class<B> type) {
198        return new NamedBeanComparator<>();
199    }
200
201    @Override
202    public void dispose() {
203        xt = null;
204        InstanceManager.deregister(this, DCCppSystemConnectionMemo.class);
205        if (cf != null) {
206            InstanceManager.deregister(cf, jmri.jmrix.swing.ComponentFactory.class);
207        }
208        super.dispose();
209    }
210
211    private final static Logger log = LoggerFactory.getLogger(DCCppSystemConnectionMemo.class);
212
213}
214