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    @Override
097    public void configureManagers() {
098        log.debug("configureManagers");
099        TurnoutManager turnoutManager = getTurnoutManager();
100        store(turnoutManager,TurnoutManager.class);
101        InstanceManager.setTurnoutManager(getTurnoutManager());
102        ThrottleManager throttleManager = getThrottleManager();
103        store(throttleManager,ThrottleManager.class);
104        InstanceManager.setThrottleManager(getThrottleManager());
105        register();
106    }
107
108    public ThrottleManager getThrottleManager() {
109        if (getDisabled()) {
110            return null;
111        }
112        return (SerialThrottleManager) classObjectMap.computeIfAbsent(ThrottleManager.class, (Class<?> c) -> new SerialThrottleManager(this));
113    }
114
115    public void setThrottleManager(ThrottleManager t) {
116        classObjectMap.put(ThrottleManager.class,t);
117    }
118
119    public SerialTurnoutManager getTurnoutManager() {
120        if (getDisabled()) {
121            return null;
122        }
123        return (SerialTurnoutManager) classObjectMap.computeIfAbsent(TurnoutManager.class,(Class<?> c) -> new SerialTurnoutManager(this));
124    }
125
126
127    @Override
128    public void dispose() {
129        trafficController = null;
130        InstanceManager.deregister(this, TmccSystemConnectionMemo.class);
131        super.dispose();
132    }
133
134    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TmccSystemConnectionMemo.class);
135
136}