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        InstanceManager.store(getConsistManager(), ConsistManager.class);
109
110        register();
111    }
112
113    public ThrottleManager getThrottleManager() {
114        if (getDisabled()) {
115            return null;
116        }
117        return (SerialThrottleManager) classObjectMap.computeIfAbsent(ThrottleManager.class, (Class<?> c) -> new SerialThrottleManager(this));
118    }
119
120    public void setThrottleManager(ThrottleManager t) {
121        classObjectMap.put(ThrottleManager.class,t);
122    }
123
124    public SerialTurnoutManager getTurnoutManager() {
125        if (getDisabled()) {
126            return null;
127        }
128        return (SerialTurnoutManager) classObjectMap.computeIfAbsent(TurnoutManager.class,(Class<?> c) -> new SerialTurnoutManager(this));
129    }
130
131    public TmccProgrammerManager getProgrammerManager() {
132         return (TmccProgrammerManager) classObjectMap.computeIfAbsent(DefaultProgrammerManager.class,
133                 (Class<?> c) -> new TmccProgrammerManager(new TmccProgrammer(this), this));
134    }
135
136    public void setProgrammerManager(TmccProgrammerManager p) {
137        store(p, DefaultProgrammerManager.class);
138    }
139
140    @Override
141    public TmccConsistManager getConsistManager() {
142        if (getDisabled()) {
143            return null;
144        }
145        return (TmccConsistManager) classObjectMap.computeIfAbsent(ConsistManager.class,(Class<?> c) -> {
146                    var t = new TmccConsistManager(this);
147                    return t;
148                });
149    }
150
151    @Override
152    public void setConsistManager(@Nonnull ConsistManager c) {
153        store(c,ConsistManager.class);
154    }
155
156    @Override
157    public void dispose() {
158        trafficController = null;
159        InstanceManager.deregister(this, TmccSystemConnectionMemo.class);
160        super.dispose();
161    }
162
163    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TmccSystemConnectionMemo.class);
164
165}