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