001package jmri.jmrix.easydcc;
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
013import org.slf4j.Logger;
014import org.slf4j.LoggerFactory;
015
016/**
017 * Lightweight class to denote that a system is active, and provide general
018 * information.
019 * <p>
020 * Objects of specific subtypes are registered in the instance manager to
021 * activate their particular system.
022 * <p>
023 * Migrated for multiple connections, multi char connection prefix and Simulator.
024 *
025 * @author Bob Jacobsen Copyright (C) 2010
026 * @author Kevin Dickerson
027 * @author Egbert Broerse Copyright (C) 2017
028 */
029public class EasyDccSystemConnectionMemo extends DefaultSystemConnectionMemo implements ConfiguringSystemConnectionMemo {
030
031    public EasyDccSystemConnectionMemo() {
032        this("E", EasyDccConnectionTypeList.EASYDCC);
033    }
034
035    /**
036     * Ctor
037     *
038     * @param et the associated TrafficController
039     */
040    public EasyDccSystemConnectionMemo(EasyDccTrafficController et) {
041        super("E", EasyDccConnectionTypeList.EASYDCC);
042        this.et = et;
043        log.debug("EasyDCC SystemConnectionMemo with TC");
044        InstanceManager.store(this, EasyDccSystemConnectionMemo.class);
045        // create and register the ComponentFactory for the GUI (menu)
046        InstanceManager.store(cf = new jmri.jmrix.easydcc.swing.EasyDccComponentFactory(this),
047                jmri.jmrix.swing.ComponentFactory.class);
048
049        log.debug("Created EasyDccSystemConnectionMemo");
050    }
051
052    public EasyDccSystemConnectionMemo(@Nonnull String prefix, @Nonnull String name) {
053        super(prefix, name);
054        log.debug("EasyDCC SystemConnectionMemo prefix={}", prefix);
055        InstanceManager.store(this, EasyDccSystemConnectionMemo.class);
056        // create and register the ComponentFactory for the GUI (menu)
057        InstanceManager.store(cf = new jmri.jmrix.easydcc.swing.EasyDccComponentFactory(this),
058                jmri.jmrix.swing.ComponentFactory.class);
059
060        log.debug("Created EasyDccSystemConnectionMemo");
061    }
062
063    jmri.jmrix.swing.ComponentFactory cf = null;
064    private EasyDccTrafficController et;
065
066    /**
067     * Provide access to the TrafficController for this particular connection.
068     * @return traffic controller, provided if null.
069     */
070    public EasyDccTrafficController getTrafficController() {
071        if (et == null) {
072            setEasyDccTrafficController(new EasyDccTrafficController(this));
073            log.debug("Auto create of EasyDccTrafficController for initial configuration");
074        }
075        return et;
076    }
077
078    public void setEasyDccTrafficController(EasyDccTrafficController et) {
079        this.et = et;
080        // in addition to setting the TrafficController in this object,
081        // set the systemConnectionMemo in the traffic controller
082        et.setSystemConnectionMemo(this);
083    }
084
085    /**
086     * Configure the common managers for EasyDCC connections. This puts the
087     * common manager config in one place. This method is static so that it can
088     * be referenced from classes that don't inherit.
089     */
090    public void configureManagers() {
091
092        InstanceManager.store(getProgrammerManager(), GlobalProgrammerManager.class);
093        InstanceManager.store(getProgrammerManager(), AddressedProgrammerManager.class);
094
095        InstanceManager.store(getPowerManager(), jmri.PowerManager.class);
096
097        InstanceManager.setTurnoutManager(getTurnoutManager());
098
099        InstanceManager.setThrottleManager(getThrottleManager());
100
101        InstanceManager.store(getConsistManager(), ConsistManager.class);
102
103        EasyDccCommandStation commandStation = new jmri.jmrix.easydcc.EasyDccCommandStation(this);
104        InstanceManager.store(commandStation,CommandStation.class);
105        store(commandStation,CommandStation.class);
106
107        register(); // registers general type
108    }
109
110    public EasyDccPowerManager getPowerManager() {
111        if (getDisabled()) {
112            return null;
113        }
114        return (EasyDccPowerManager) classObjectMap.computeIfAbsent(PowerManager.class,
115                (Class c) -> new jmri.jmrix.easydcc.EasyDccPowerManager(this));
116    }
117
118    public ThrottleManager getThrottleManager() {
119        if (getDisabled()) {
120            return null;
121        }
122        return (ThrottleManager) classObjectMap.computeIfAbsent(ThrottleManager.class,
123                (Class c) -> new jmri.jmrix.easydcc.EasyDccThrottleManager(this));
124    }
125
126    public void setThrottleManager(ThrottleManager t) {
127        store(t,ThrottleManager.class);
128    }
129
130    public EasyDccTurnoutManager getTurnoutManager() {
131        if (getDisabled()) {
132            return null;
133        }
134        return (EasyDccTurnoutManager) classObjectMap.computeIfAbsent(TurnoutManager.class,
135                (Class c) -> new jmri.jmrix.easydcc.EasyDccTurnoutManager(this));
136    }
137
138    @Override
139    public EasyDccConsistManager getConsistManager() {
140        if (getDisabled()) {
141            return null;
142        }
143        return (EasyDccConsistManager) classObjectMap.computeIfAbsent(ConsistManager.class,
144                (Class c) -> new jmri.jmrix.easydcc.EasyDccConsistManager(this));
145    }
146
147    public EasyDccProgrammerManager getProgrammerManager() {
148         return (EasyDccProgrammerManager) classObjectMap.computeIfAbsent(DefaultProgrammerManager.class,
149                 (Class c) -> new EasyDccProgrammerManager(new EasyDccProgrammer(this), this));
150    }
151
152    public void setProgrammerManager(EasyDccProgrammerManager p) {
153        store(p, DefaultProgrammerManager.class);
154    }
155
156    @Override
157    protected ResourceBundle getActionModelResourceBundle() {
158        return ResourceBundle.getBundle("jmri.jmrix.easydcc.EasyDccActionListBundle");
159    }
160
161    @Override
162    public <B extends NamedBean> Comparator<B> getNamedBeanComparator(Class<B> type) {
163        return new NamedBeanComparator<>();
164    }
165
166    @Override
167    public void dispose() {
168        et = null;
169        InstanceManager.deregister(this, EasyDccSystemConnectionMemo.class);
170        if (cf != null) {
171            InstanceManager.deregister(cf, jmri.jmrix.swing.ComponentFactory.class);
172        }
173        super.dispose();
174    }
175
176    private final static Logger log = LoggerFactory.getLogger(EasyDccSystemConnectionMemo.class);
177
178}