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    /*
107     * Provides access to the PowerManager for this particular connection.
108     */
109    @Nonnull
110    public PowerManager getPowerManager() {
111        return (PowerManager) classObjectMap.computeIfAbsent(PowerManager.class, (Class<?> c) -> {
112            PowerManager powerManager = new DCCppPowerManager(this);
113            log.debug("power manager created: {}", powerManager);
114            return powerManager;
115        });
116    }
117
118    public void setPowerManager(@Nonnull PowerManager p) {
119        store(p,PowerManager.class);
120    }
121
122    /*
123     * Provides access to the SensorManager for this particular connection.
124     * NOTE: SensorManager defaults to NULL
125     */
126    public SensorManager getSensorManager() {
127        return get(SensorManager.class);
128
129    }
130
131    public void setSensorManager(SensorManager s) {
132        store(s,SensorManager.class);
133    }
134
135    /*
136     * Provides access to the TurnoutManager for this particular connection.
137     * NOTE: TurnoutManager defaults to NULL
138     */
139    public TurnoutManager getTurnoutManager() {
140        return get(TurnoutManager.class);
141
142    }
143
144    public void setTurnoutManager(TurnoutManager t) {
145        store(t,TurnoutManager.class);
146    }
147
148    /*
149     * Provides access to the LightManager for this particular connection.
150     * NOTE: Light manager defaults to NULL
151     */
152    public LightManager getLightManager() {
153        return get(LightManager.class);
154
155    }
156
157    public void setLightManager(LightManager l) {
158        store(l,LightManager.class);
159    }
160
161    /*
162     * Provides access to the Command Station for this particular connection.
163     * NOTE: Command Station defaults to NULL
164     */
165    public CommandStation getCommandStation() {
166        return get(CommandStation.class);
167    }
168
169    public void setCommandStation(@Nonnull CommandStation c) {
170        store(c,CommandStation.class);
171        if ( c instanceof DCCppCommandStation ) {
172            ((DCCppCommandStation) c).setTrafficController(xt);
173            ((DCCppCommandStation) c).setSystemConnectionMemo(this);
174        }
175    }
176
177    @Override
178    @Nonnull
179    protected ResourceBundle getActionModelResourceBundle() {
180        return ResourceBundle.getBundle("jmri.jmrix.dccpp.DCCppActionListBundle");
181    }
182
183    @Override
184    public <B extends NamedBean> Comparator<B> getNamedBeanComparator(Class<B> type) {
185        return new NamedBeanComparator<>();
186    }
187
188    @Override
189    public void dispose() {
190        xt = null;
191        InstanceManager.deregister(this, DCCppSystemConnectionMemo.class);
192        if (cf != null) {
193            InstanceManager.deregister(cf, jmri.jmrix.swing.ComponentFactory.class);
194        }
195        super.dispose();
196    }
197
198    private final static Logger log = LoggerFactory.getLogger(DCCppSystemConnectionMemo.class);
199
200}
201