001package jmri.jmrix.zimo;
002
003import java.util.Comparator;
004import java.util.ResourceBundle;
005
006import jmri.*;
007import jmri.jmrix.ConfiguringSystemConnectionMemo;
008import jmri.jmrix.DefaultSystemConnectionMemo;
009import jmri.managers.DefaultProgrammerManager;
010import jmri.util.NamedBeanComparator;
011
012/**
013 * Lightweight class to denote that a system is active, and provide general
014 * information.
015 * <p>
016 * Objects of specific subtypes are registered in the instance manager to
017 * activate their particular system.
018 *
019 * @author Kevin Dickerson Copyright (C) 2012
020 * @author Bob Jacobsen Copyright (C) 2010
021 * @author Paul Bender Copyright (C) 2020
022 */
023public class Mx1SystemConnectionMemo extends DefaultSystemConnectionMemo implements ConfiguringSystemConnectionMemo {
024
025    public Mx1SystemConnectionMemo(Mx1TrafficController st) {
026        super("Z", "MX-1");
027        this.st = st;
028        InstanceManager.store(this, Mx1SystemConnectionMemo.class);
029        // create and register the ComponentFactory
030        InstanceManager.store(componentFactory = new jmri.jmrix.zimo.swing.Mx1ComponentFactory(this),
031                jmri.jmrix.swing.ComponentFactory.class);
032    }
033
034    jmri.jmrix.swing.ComponentFactory componentFactory = null;
035
036    public Mx1SystemConnectionMemo() {
037        super("Z", "MX-1");
038        InstanceManager.store(this, Mx1SystemConnectionMemo.class);
039        // create and register the ComponentFactory
040        InstanceManager.store(componentFactory = new jmri.jmrix.zimo.swing.Mx1ComponentFactory(this),
041                jmri.jmrix.swing.ComponentFactory.class);
042    }
043
044    public final static int MX1 = 0x00;
045    public final static int MXULF = 0x01;
046    public final static int MX10 = 0x02;
047
048    int connectionType = 0x00;
049
050    public void setConnectionType(int connection) {
051        connectionType = connection;
052    }
053
054    public int getConnectionType() {
055        return connectionType;
056    }
057
058    /**
059     * Provide access to the TrafficController for this particular connection.
060     *
061     * @return the associated traffic controller
062     */
063    public Mx1TrafficController getMx1TrafficController() {
064        return st;
065    }
066
067    public void setMx1TrafficController(Mx1TrafficController st) {
068        this.st = st;
069    }
070    private Mx1TrafficController st;
071
072    /**
073     * Configure the "command station" object
074     * @deprecated since 4.21.1 without replacement
075     */
076    @Deprecated
077    public void configureCommandStation() {
078    }
079
080    @Override
081    public void configureManagers() {
082
083        DefaultProgrammerManager programmerManager = getProgrammerManager();
084        store(programmerManager,DefaultProgrammerManager.class);
085        if (programmerManager.isAddressedModePossible()) {
086            store(programmerManager, AddressedProgrammerManager.class);
087            InstanceManager.store(programmerManager, AddressedProgrammerManager.class);
088        }
089        if (programmerManager.isGlobalProgrammerAvailable()) {
090            store(programmerManager,GlobalProgrammerManager.class);
091            InstanceManager.store(programmerManager, GlobalProgrammerManager.class);
092        }
093
094        PowerManager powerManager = new Mx1PowerManager(this);
095        store(powerManager, PowerManager.class);
096        jmri.InstanceManager.store(powerManager, PowerManager.class);
097
098        ThrottleManager throttleManager = new jmri.jmrix.zimo.Mx1ThrottleManager(this);
099        store(throttleManager,ThrottleManager.class);
100        InstanceManager.setThrottleManager(throttleManager);
101        if (getProtocol() == Mx1Packetizer.BINARY) {
102            TurnoutManager turnoutManager = new Mx1TurnoutManager(this);
103            store(turnoutManager,TurnoutManager.class);
104            InstanceManager.setTurnoutManager(turnoutManager);
105        }
106        register(); // registers general type
107    }
108
109    boolean getProtocol() {
110        if (getMx1TrafficController() != null) {
111            return getMx1TrafficController().getProtocol();
112        }
113        return Mx1Packetizer.ASCII;
114    }
115
116    public DefaultProgrammerManager getProgrammerManager() {
117        return (DefaultProgrammerManager) classObjectMap.computeIfAbsent(DefaultProgrammerManager.class, (Class c) -> {
118            return generateDefaultProgrammerManagerForConnection();
119        });
120    }
121
122    private DefaultProgrammerManager generateDefaultProgrammerManagerForConnection() {
123        DefaultProgrammerManager programmerManager;
124        if (getProtocol() == Mx1Packetizer.BINARY) {
125            programmerManager = new Mx1ProgrammerManager(new Mx1Programmer(getMx1TrafficController()), this);
126        } else {
127            programmerManager = new DefaultProgrammerManager(new Mx1Programmer(getMx1TrafficController()), this);
128        }
129        return programmerManager;
130    }
131
132    public void setProgrammerManager(DefaultProgrammerManager p) {
133        store(p,DefaultProgrammerManager.class);
134    }
135
136    public Mx1TurnoutManager getTurnoutManager() {
137        return (Mx1TurnoutManager) classObjectMap.get(TurnoutManager.class);
138    }
139
140    /**
141     * @param cs command station object to set
142     * @deprecated since 4.21.1 without replacement
143     */
144    @Deprecated
145    public void setCommandStation(Mx1CommandStation cs) {
146    }
147
148    public Mx1PowerManager getPowerManager() {
149        return (Mx1PowerManager) classObjectMap.get(PowerManager.class);
150    }
151
152    public Mx1ThrottleManager getThrottleManager() {
153        return (Mx1ThrottleManager) classObjectMap.get(ThrottleManager.class);
154    }
155
156    @Override
157    protected ResourceBundle getActionModelResourceBundle() {
158        return ResourceBundle.getBundle("jmri.jmrix.zimo.ZimoActionListBundle");
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        st = null;
169        InstanceManager.deregister(this, Mx1SystemConnectionMemo.class);
170        if (componentFactory != null) {
171            InstanceManager.deregister(componentFactory, jmri.jmrix.swing.ComponentFactory.class);
172        }
173        Mx1ThrottleManager throttleManager = (Mx1ThrottleManager) classObjectMap.get(ThrottleManager.class);
174        if (throttleManager != null) {
175            deregister(throttleManager,ThrottleManager.class);
176            InstanceManager.deregister(throttleManager, jmri.jmrix.zimo.Mx1ThrottleManager.class);
177        }
178        super.dispose();
179    }
180
181}