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    @Override
091    public void configureManagers() {
092
093        InstanceManager.store(getProgrammerManager(), GlobalProgrammerManager.class);
094        InstanceManager.store(getProgrammerManager(), AddressedProgrammerManager.class);
095
096        InstanceManager.store(getPowerManager(), jmri.PowerManager.class);
097
098        InstanceManager.setTurnoutManager(getTurnoutManager());
099
100        InstanceManager.setThrottleManager(getThrottleManager());
101
102        InstanceManager.store(getConsistManager(), ConsistManager.class);
103
104        EasyDccCommandStation commandStation = new jmri.jmrix.easydcc.EasyDccCommandStation(this);
105        InstanceManager.store(commandStation,CommandStation.class);
106        store(commandStation,CommandStation.class);
107
108        register(); // registers general type
109    }
110
111    public EasyDccPowerManager getPowerManager() {
112        if (getDisabled()) {
113            return null;
114        }
115        return (EasyDccPowerManager) classObjectMap.computeIfAbsent(PowerManager.class,
116                (Class<?> c) -> new jmri.jmrix.easydcc.EasyDccPowerManager(this));
117    }
118
119    public ThrottleManager getThrottleManager() {
120        if (getDisabled()) {
121            return null;
122        }
123        return (ThrottleManager) classObjectMap.computeIfAbsent(ThrottleManager.class,
124                (Class<?> c) -> new jmri.jmrix.easydcc.EasyDccThrottleManager(this));
125    }
126
127    public void setThrottleManager(ThrottleManager t) {
128        store(t,ThrottleManager.class);
129    }
130
131    public EasyDccTurnoutManager getTurnoutManager() {
132        if (getDisabled()) {
133            return null;
134        }
135        return (EasyDccTurnoutManager) classObjectMap.computeIfAbsent(TurnoutManager.class,
136                (Class<?> c) -> new jmri.jmrix.easydcc.EasyDccTurnoutManager(this));
137    }
138
139    @Override
140    public EasyDccConsistManager getConsistManager() {
141        if (getDisabled()) {
142            return null;
143        }
144        return (EasyDccConsistManager) classObjectMap.computeIfAbsent(ConsistManager.class,
145                (Class<?> c) -> new jmri.jmrix.easydcc.EasyDccConsistManager(this));
146    }
147
148    public EasyDccProgrammerManager getProgrammerManager() {
149         return (EasyDccProgrammerManager) classObjectMap.computeIfAbsent(DefaultProgrammerManager.class,
150                 (Class<?> c) -> new EasyDccProgrammerManager(new EasyDccProgrammer(this), this));
151    }
152
153    public void setProgrammerManager(EasyDccProgrammerManager p) {
154        store(p, DefaultProgrammerManager.class);
155    }
156
157    @Override
158    protected ResourceBundle getActionModelResourceBundle() {
159        return ResourceBundle.getBundle("jmri.jmrix.easydcc.EasyDccActionListBundle");
160    }
161
162    @Override
163    public <B extends NamedBean> Comparator<B> getNamedBeanComparator(Class<B> type) {
164        return new NamedBeanComparator<>();
165    }
166
167    @Override
168    public boolean provides(Class<?> c) {
169        if (!getDisabled() && c.equals(ConsistManager.class)) {
170            return true;
171        }
172        return super.provides(c);
173    }
174    
175    @Override
176    public void dispose() {
177        et = null;
178        InstanceManager.deregister(this, EasyDccSystemConnectionMemo.class);
179        if (cf != null) {
180            InstanceManager.deregister(cf, jmri.jmrix.swing.ComponentFactory.class);
181        }
182        super.dispose();
183    }
184
185    private final static Logger log = LoggerFactory.getLogger(EasyDccSystemConnectionMemo.class);
186
187}