001package jmri.jmrix.srcp;
002
003import java.util.ArrayList;
004import java.util.Comparator;
005import java.util.ResourceBundle;
006import jmri.InstanceManager;
007import jmri.NamedBean;
008import jmri.ShutDownManager;
009import jmri.jmrix.DefaultSystemConnectionMemo;
010import jmri.jmrix.srcp.swing.SRCPComponentFactory;
011import jmri.jmrix.swing.ComponentFactory;
012import jmri.util.NamedBeanComparator;
013
014/**
015 * Lightweight class to denote that a system is active, and provide general
016 * information.
017 * <p>
018 * Objects of specific subtypes are registered in the instance manager to
019 * activate their particular system.
020 *
021 * @author Bob Jacobsen Copyright (C) 2010
022 * @author Paul Bender Copyright (C) 2015-2016
023 */
024public class SRCPSystemConnectionMemo extends DefaultSystemConnectionMemo {
025
026    public SRCPSystemConnectionMemo(String prefix, String name, SRCPTrafficController et) {
027        super(prefix, name);
028        if (et != null) {
029            setTrafficController(et);
030        }
031        register();
032        InstanceManager.store(this, SRCPSystemConnectionMemo.class); // also register as specific type
033        InstanceManager.store(cf = new SRCPComponentFactory(this), ComponentFactory.class);
034    }
035
036    public SRCPSystemConnectionMemo(SRCPTrafficController et) {
037        this("D", "SRCP", et);
038    }
039
040    public SRCPSystemConnectionMemo() {
041        this("D", "SRCP", null);
042    }
043
044    jmri.jmrix.swing.ComponentFactory cf = null;
045
046    /**
047     * Provides access to the TrafficController for this particular connection.
048     * @return SRCP traffic controller.
049     */
050    public SRCPTrafficController getTrafficController() {
051        return et;
052    }
053
054    public void setTrafficController(SRCPTrafficController et) {
055        this.et = et;
056        this.et.setSystemConnectionMemo(this);
057    }
058    private SRCPTrafficController et;
059
060    /**
061     * Configure the programming manager and "command station" objects
062     */
063    public void configureCommandStation() {
064        // start the connection
065        et.sendSRCPMessage(new SRCPMessage("SET PROTOCOL SRCP 0.8.3\n"), null);
066        et.sendSRCPMessage(new SRCPMessage("SET CONNECTIONMODE SRCP COMMAND\n"), null);
067        et.sendSRCPMessage(new SRCPMessage("GO\n"), null);
068        // for now, limit to 10 busses.
069        for (int i = 1; i < 11; i++) {
070            et.sendSRCPMessage(new SRCPMessage("GET " + i + " DESCRIPTION\n"), null);
071        }
072    }
073
074    // keep track of the current mode.
075    private int mode = SRCPTrafficController.HANDSHAKEMODE;
076
077    public void setMode(int m) {
078        mode = m;
079    }
080
081    public int getMode() {
082        return mode;
083    }
084
085    @Override
086    public <T> T get(Class<T> T) {
087        return null; // nothing, by default
088    }
089
090    /**
091     * {@inheritDoc}
092     */
093    @Override
094    public boolean provides(Class<?> type) {
095        return false; // nothing, by default
096    }
097
098    @Override
099    protected ResourceBundle getActionModelResourceBundle() {
100        return ResourceBundle.getBundle("jmri.jmrix.srcp.SrcpActionListBundle");
101    }
102
103    @Override
104    public <B extends NamedBean> Comparator<B> getNamedBeanComparator(Class<B> type) {
105        return new NamedBeanComparator<>();
106    }
107
108    @Override
109    public void dispose() {
110        if (et != null) {
111            InstanceManager.getDefault(ShutDownManager.class).deregister(et.shutDownTask);
112        }
113        et = null;
114        InstanceManager.deregister(this, SRCPSystemConnectionMemo.class);
115        if (cf != null) {
116            InstanceManager.deregister(cf, ComponentFactory.class);
117        }
118        super.dispose();
119    }
120
121    // private list of busMemos, so the parser visitor can pass information
122    // to the bus representation.
123    private ArrayList<SRCPBusConnectionMemo> busMemos = null;
124
125    public SRCPBusConnectionMemo getMemo(int i) {
126        if (busMemos == null) {
127            busMemos = new ArrayList<>();
128            // there is always a bus 0, so add it now.
129            busMemos.add(0, new SRCPBusConnectionMemo(getTrafficController(), getSystemPrefix(), 0));
130        }
131        try {
132            return busMemos.get(i);
133        } catch (IndexOutOfBoundsException ie) {
134            // this memo must not exist in the list, add it and return it.
135            busMemos.add(i, new SRCPBusConnectionMemo(getTrafficController(), getSystemPrefix(), i));
136            return busMemos.get(i);
137        }
138    }
139
140}