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    @Override
073    public void configureManagers() {
074
075        DefaultProgrammerManager programmerManager = getProgrammerManager();
076        store(programmerManager,DefaultProgrammerManager.class);
077        if (programmerManager.isAddressedModePossible()) {
078            store(programmerManager, AddressedProgrammerManager.class);
079            InstanceManager.store(programmerManager, AddressedProgrammerManager.class);
080        }
081        if (programmerManager.isGlobalProgrammerAvailable()) {
082            store(programmerManager,GlobalProgrammerManager.class);
083            InstanceManager.store(programmerManager, GlobalProgrammerManager.class);
084        }
085
086        PowerManager powerManager = new Mx1PowerManager(this);
087        store(powerManager, PowerManager.class);
088        jmri.InstanceManager.store(powerManager, PowerManager.class);
089
090        ThrottleManager throttleManager = new jmri.jmrix.zimo.Mx1ThrottleManager(this);
091        store(throttleManager,ThrottleManager.class);
092        InstanceManager.setThrottleManager(throttleManager);
093        if (getProtocol() == Mx1Packetizer.BINARY) {
094            TurnoutManager turnoutManager = new Mx1TurnoutManager(this);
095            store(turnoutManager,TurnoutManager.class);
096            InstanceManager.setTurnoutManager(turnoutManager);
097        }
098        register(); // registers general type
099    }
100
101    boolean getProtocol() {
102        if (getMx1TrafficController() != null) {
103            return getMx1TrafficController().getProtocol();
104        }
105        return Mx1Packetizer.ASCII;
106    }
107
108    public DefaultProgrammerManager getProgrammerManager() {
109        return (DefaultProgrammerManager) classObjectMap.computeIfAbsent(DefaultProgrammerManager.class, (Class<?> c) -> {
110            return generateDefaultProgrammerManagerForConnection();
111        });
112    }
113
114    private DefaultProgrammerManager generateDefaultProgrammerManagerForConnection() {
115        DefaultProgrammerManager programmerManager;
116        if (getProtocol() == Mx1Packetizer.BINARY) {
117            programmerManager = new Mx1ProgrammerManager(new Mx1Programmer(getMx1TrafficController()), this);
118        } else {
119            programmerManager = new DefaultProgrammerManager(new Mx1Programmer(getMx1TrafficController()), this);
120        }
121        return programmerManager;
122    }
123
124    public void setProgrammerManager(DefaultProgrammerManager p) {
125        store(p,DefaultProgrammerManager.class);
126    }
127
128    public Mx1TurnoutManager getTurnoutManager() {
129        return (Mx1TurnoutManager) classObjectMap.get(TurnoutManager.class);
130    }
131
132    public Mx1PowerManager getPowerManager() {
133        return (Mx1PowerManager) classObjectMap.get(PowerManager.class);
134    }
135
136    public Mx1ThrottleManager getThrottleManager() {
137        return (Mx1ThrottleManager) classObjectMap.get(ThrottleManager.class);
138    }
139
140    @Override
141    protected ResourceBundle getActionModelResourceBundle() {
142        return ResourceBundle.getBundle("jmri.jmrix.zimo.ZimoActionListBundle");
143    }
144
145    @Override
146    public <B extends NamedBean> Comparator<B> getNamedBeanComparator(Class<B> type) {
147        return new NamedBeanComparator<>();
148    }
149
150    @Override
151    public void dispose() {
152        st = null;
153        InstanceManager.deregister(this, Mx1SystemConnectionMemo.class);
154        if (componentFactory != null) {
155            InstanceManager.deregister(componentFactory, jmri.jmrix.swing.ComponentFactory.class);
156        }
157        Mx1ThrottleManager throttleManager = (Mx1ThrottleManager) classObjectMap.get(ThrottleManager.class);
158        if (throttleManager != null) {
159            deregister(throttleManager,ThrottleManager.class);
160            InstanceManager.deregister(throttleManager, jmri.jmrix.zimo.Mx1ThrottleManager.class);
161        }
162        super.dispose();
163    }
164
165}