001package jmri.jmrix.tmcc;
002
003import java.util.Comparator;
004import java.util.ResourceBundle;
005import javax.annotation.Nonnull;
006
007import jmri.*;
008import jmri.jmrix.ConfiguringSystemConnectionMemo;
009import jmri.jmrix.DefaultSystemConnectionMemo;
010import jmri.util.NamedBeanComparator;
011
012/**
013 * Provide the required SystemConnectionMemo.
014 *
015 * Migrated to multi-system support. Added a configureManagers() method
016 * that creates local objects and remove the instance() variables.
017 *
018 * @author Randall Wood randall.h.wood@alexandriasoftware.com
019 * @author Egbert Broerse Copyright (C) 2017
020 */
021public class TmccSystemConnectionMemo extends DefaultSystemConnectionMemo implements ConfiguringSystemConnectionMemo {
022
023    public TmccSystemConnectionMemo() {
024        this("T", "Lionel TMCC");
025    }
026
027    /**
028     * Ctor
029     *
030     * @param tc the associated TrafficController
031     */
032    public TmccSystemConnectionMemo(SerialTrafficController tc) {
033        super("T", "Lionel TMCC");
034        trafficController = tc;
035        log.debug("TMCC SystemConnectionMemo with TC");
036        InstanceManager.store(this, TmccSystemConnectionMemo.class);
037        // create and register the ComponentFactory for the GUI (menu)
038        InstanceManager.store(cf = new jmri.jmrix.tmcc.swing.TmccComponentFactory(this),
039                jmri.jmrix.swing.ComponentFactory.class);
040
041        log.debug("Created TMCCSystemConnectionMemo");
042    }
043
044    public TmccSystemConnectionMemo(@Nonnull String prefix, @Nonnull String name) {
045        super(prefix, name);
046        log.debug("TMCC SystemConnectionMemo prefix={}", prefix);
047        InstanceManager.store(this, TmccSystemConnectionMemo.class);
048        // create and register the ComponentFactory for the GUI (menu)
049        InstanceManager.store(cf = new jmri.jmrix.tmcc.swing.TmccComponentFactory(this),
050                jmri.jmrix.swing.ComponentFactory.class);
051
052        log.debug("Created TMCCSystemConnectionMemo");
053    }
054
055    jmri.jmrix.swing.ComponentFactory cf;
056
057    @Override
058    protected ResourceBundle getActionModelResourceBundle() {
059        return null;
060    }
061
062    @Override
063    public <B extends NamedBean> Comparator<B> getNamedBeanComparator(Class<B> type) {
064        return new NamedBeanComparator<>();
065    }
066
067    private SerialTrafficController trafficController;
068
069    /**
070     * Provide access to the TrafficController for this particular connection.
071     *
072     * @return the traffic controller for this connection
073     */
074    public SerialTrafficController getTrafficController() {
075        if (trafficController == null) {
076            setTrafficController(new SerialTrafficController(this));
077            log.debug("Auto create of TMCC SerialTrafficController for initial configuration");
078        }
079        return trafficController;
080    }
081
082    /**
083     * @param tc the new TrafficController to set
084     */
085    public void setTrafficController(SerialTrafficController tc) {
086        trafficController = tc;
087        // in addition to setting the TrafficController in this object,
088        // set the systemConnectionMemo in the traffic controller
089        tc.setSystemConnectionMemo(this);
090    }
091
092    /**
093     * Configure the common managers for tmcc connections. This puts the common
094     * manager config in one place.
095     */
096    public void configureManagers() {
097        log.debug("configureManagers");
098        TurnoutManager turnoutManager = getTurnoutManager();
099        store(turnoutManager,TurnoutManager.class);
100        InstanceManager.setTurnoutManager(getTurnoutManager());
101        ThrottleManager throttleManager = getThrottleManager();
102        store(throttleManager,ThrottleManager.class);
103        InstanceManager.setThrottleManager(getThrottleManager());
104        register();
105    }
106
107    public ThrottleManager getThrottleManager() {
108        if (getDisabled()) {
109            return null;
110        }
111        return (SerialThrottleManager) classObjectMap.computeIfAbsent(ThrottleManager.class, (Class c) -> new SerialThrottleManager(this));
112    }
113
114    public void setThrottleManager(ThrottleManager t) {
115        classObjectMap.put(ThrottleManager.class,t);
116    }
117
118    public SerialTurnoutManager getTurnoutManager() {
119        if (getDisabled()) {
120            return null;
121        }
122        return (SerialTurnoutManager) classObjectMap.computeIfAbsent(TurnoutManager.class,(Class c) -> new SerialTurnoutManager(this));
123    }
124
125
126    @Override
127    public void dispose() {
128        trafficController = null;
129        InstanceManager.deregister(this, TmccSystemConnectionMemo.class);
130        super.dispose();
131    }
132
133    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TmccSystemConnectionMemo.class);
134
135}