001package jmri.jmrix.direct;
002
003import java.util.Comparator;
004import java.util.ResourceBundle;
005import javax.annotation.Nonnull;
006
007import jmri.CommandStation;
008import jmri.jmrix.ConfiguringSystemConnectionMemo;
009import jmri.InstanceManager;
010import jmri.NamedBean;
011import jmri.jmrix.DefaultSystemConnectionMemo;
012import jmri.util.NamedBeanComparator;
013
014import org.slf4j.Logger;
015import org.slf4j.LoggerFactory;
016
017/**
018 * Minimum required SystemConnectionMemo.
019 *
020 * @author Randall Wood randall.h.wood@alexandriasoftware.com
021 */
022public class DirectSystemConnectionMemo extends DefaultSystemConnectionMemo implements ConfiguringSystemConnectionMemo {
023
024    jmri.jmrix.swing.ComponentFactory cf = null;
025
026    public DirectSystemConnectionMemo() {
027        this("N", "Others");
028    }
029
030    public DirectSystemConnectionMemo(@Nonnull String prefix, @Nonnull String userName) {
031        super(prefix, userName);
032
033        InstanceManager.store(this, DirectSystemConnectionMemo.class);
034
035        // create and register the ComponentFactory
036        InstanceManager.store(cf = new jmri.jmrix.direct.swing.DirectComponentFactory(this),
037                jmri.jmrix.swing.ComponentFactory.class);
038
039        log.debug("Created DirectSystemConnectionMemo");
040    }
041
042    private TrafficController tc = null;
043
044    /**
045     * Set the traffic controller instance associated with this connection memo.
046     *
047     * @param s jmri.jmrix.direct.TrafficController object to use.
048     */
049    public void setTrafficController(TrafficController s){
050        tc = s;
051        store(tc, CommandStation.class);
052        InstanceManager.store(tc,CommandStation.class);
053    }
054
055    /**
056     * Get the traffic controller instance associated with this connection memo.
057     * @return traffic controller, provided if null.
058     */
059    public TrafficController getTrafficController(){
060        if (tc == null) {
061            setTrafficController(new TrafficController(this));
062            log.debug("Auto create of TrafficController for initial configuration");
063        }
064        return tc;
065    }
066
067    /**
068     * Set the traffic controller instance associated with this connection memo.
069     *
070     * @param s jmri.jmrix.direct.ThrottleManager object to use.
071     */
072    public void setThrottleManager(ThrottleManager s){
073        store(s,ThrottleManager.class);
074        InstanceManager.store(get(ThrottleManager.class),ThrottleManager.class);
075    }
076
077    /**
078     * Get the ThrottleManager instance associated with this connection memo.
079     * @return throttle manager, provided if null.
080     */
081    public ThrottleManager getThrottleManager(){
082        return (ThrottleManager) classObjectMap.computeIfAbsent(ThrottleManager.class,
083                (Class<?> c) -> {
084                    setThrottleManager(new ThrottleManager(this));
085                    log.debug("Auto create of ThrottleManager for initial configuration");
086                    return get(ThrottleManager.class);
087                });
088    }
089
090    @Override
091    protected ResourceBundle getActionModelResourceBundle() {
092        return null;
093    }
094
095    @Override
096    public <B extends NamedBean> Comparator<B> getNamedBeanComparator(Class<B> type) {
097        return new NamedBeanComparator<>();
098    }
099
100    @Override
101    public void configureManagers(){
102        setThrottleManager(new ThrottleManager(this));
103        register();
104    }
105
106    private final static Logger log = LoggerFactory.getLogger(DirectSystemConnectionMemo.class);
107
108}