001package jmri.jmrix.dcc4pc;
002
003import java.util.Comparator;
004import java.util.ResourceBundle;
005
006import jmri.*;
007import jmri.jmrix.ConfiguringSystemConnectionMemo;
008import jmri.jmrix.DefaultSystemConnectionMemo;
009import jmri.util.NamedBeanComparator;
010
011/**
012 * Lightweight class to denote that a system is active, and provide general
013 * information.
014 * <p>
015 * Objects of specific subtypes are registered in the instance manager to
016 * activate their particular system.
017 *
018 * @author Kevin Dickerson Copyright (C) 2012
019 */
020public class Dcc4PcSystemConnectionMemo extends DefaultSystemConnectionMemo implements ConfiguringSystemConnectionMemo {
021
022    public Dcc4PcSystemConnectionMemo(Dcc4PcTrafficController tc) {
023        super("D", "Dcc4Pc");
024        this.tc = tc;
025        tc.setAdapterMemo(this);
026    }
027
028    public Dcc4PcSystemConnectionMemo() {
029        super("D", "Dcc4Pc");
030        InstanceManager.store(this, Dcc4PcSystemConnectionMemo.class);
031        //Needs to be implemented
032        InstanceManager.store(cf = new jmri.jmrix.dcc4pc.swing.Dcc4PcComponentFactory(this),
033                jmri.jmrix.swing.ComponentFactory.class);
034    }
035
036    jmri.jmrix.swing.ComponentFactory cf = null;
037
038    /**
039     * Provides access to the TrafficController for this particular connection.
040     * @return traffic controller.
041     */
042    public Dcc4PcTrafficController getDcc4PcTrafficController() {
043        return tc;
044    }
045
046    public void setDcc4PcTrafficController(Dcc4PcTrafficController tc) {
047        this.tc = tc;
048    }
049    private Dcc4PcTrafficController tc;
050
051    /**
052     * Configure the common managers for Dcc4Pc connections. This puts the
053     * common manager config in one place. This method is static so that it can
054     * be referenced from classes that don't inherit, including
055     * hexfile.HexFileFrame and locormi.LnMessageClient
056     */
057    @Override
058    public void configureManagers() {
059
060        InstanceManager.setReporterManager(
061                getReporterManager());
062
063        InstanceManager.setSensorManager(
064                getSensorManager());
065
066        register();
067    }
068
069    public Dcc4PcReporterManager getReporterManager() {
070        if (getDisabled()) {
071            return null;
072        }
073        return (Dcc4PcReporterManager) classObjectMap.computeIfAbsent((ReporterManager.class), (Class<?> c) ->
074            new Dcc4PcReporterManager(getDcc4PcTrafficController(), this));
075    }
076
077    public Dcc4PcSensorManager getSensorManager() {
078        if (getDisabled()) {
079            return null;
080        }
081        return (Dcc4PcSensorManager) classObjectMap.computeIfAbsent(SensorManager.class,
082                (Class<?> c) -> new Dcc4PcSensorManager(getDcc4PcTrafficController(), this));
083    }
084
085    @Override
086    protected ResourceBundle getActionModelResourceBundle() {
087        return ResourceBundle.getBundle("jmri.jmrix.dcc4pc.Dcc4PcActionListBundle");
088    }
089
090    @Override
091    public <B extends NamedBean> Comparator<B> getNamedBeanComparator(Class<B> type) {
092        return new NamedBeanComparator<>();
093    }
094
095    @Override
096    public void dispose() {
097        tc = null;
098        InstanceManager.deregister(this, Dcc4PcSystemConnectionMemo.class);
099        if (cf != null) {
100            InstanceManager.deregister(cf, jmri.jmrix.swing.ComponentFactory.class);
101        }
102        Dcc4PcSensorManager sm = (Dcc4PcSensorManager) classObjectMap.get(SensorManager.class);
103        if (sm != null) {
104            sm.dispose();
105        }
106        super.dispose();
107    }
108
109    public <T extends AddressedProgrammerManager & GlobalProgrammerManager> void setRealProgramManager(T dpm) {
110        store(dpm,GlobalProgrammerManager.class);
111        store(dpm, AddressedProgrammerManager.class);
112    }
113}