001package jmri.jmrix.openlcb;
002
003import java.util.ResourceBundle;
004import jmri.GlobalProgrammerManager;
005import jmri.InstanceManager;
006import org.openlcb.OlcbInterface;
007import javax.annotation.Nonnull;
008import javax.annotation.CheckReturnValue;
009
010/**
011 * Lightweight class to denote that a system is active, and provide general
012 * information.
013 * <p>
014 * Objects of specific subtypes are registered in the instance manager to
015 * activate their particular system.
016 *
017 * @author Bob Jacobsen Copyright (C) 2015
018 */
019public class OlcbSystemConnectionMemo extends jmri.jmrix.can.CanSystemConnectionMemo {
020
021    public OlcbSystemConnectionMemo() {
022        //super("M", "OpenLCB");
023        register(); // registers general type
024        InstanceManager.store(this, OlcbSystemConnectionMemo.class); // also register as specific type
025    }
026
027    final jmri.jmrix.swing.ComponentFactory cf = null;
028
029    /**
030     * Tells which managers this class provides.
031     */
032    @Override
033    public boolean provides(Class<?> type) {
034        if (getDisabled()) {
035            return false;
036        }
037        if (type.equals(jmri.GlobalProgrammerManager.class)) {
038            return getProgrammerManager().isGlobalProgrammerAvailable();
039        }
040        if (type.equals(jmri.AddressedProgrammerManager.class)) {
041            return getProgrammerManager().isAddressedModePossible();
042        }
043
044        if (type.equals(jmri.SensorManager.class)) {
045            return true;
046        }
047        if (type.equals(jmri.TurnoutManager.class)) {
048            return true;
049        }
050        if (type.equals(jmri.ThrottleManager.class)) {
051            return true;
052        }
053        return super.provides(type);
054    }
055
056    @SuppressWarnings("unchecked")
057    @Override
058    public <T> T get(Class<?> T) {
059        if (getDisabled()) {
060            return null;
061        }
062        if (T.equals(jmri.GlobalProgrammerManager.class)) {
063            return (T) getProgrammerManager();
064        }
065        if (T.equals(jmri.AddressedProgrammerManager.class)) {
066            return (T) getProgrammerManager();
067        }
068
069        if (T.equals(jmri.SensorManager.class)) {
070            return (T) getSensorManager();
071        }
072        if (T.equals(jmri.TurnoutManager.class)) {
073            return (T) getTurnoutManager();
074        }
075        if (T.equals(jmri.ThrottleManager.class)) {
076            return (T) getThrottleManager();
077        }
078        if (T.equals(OlcbInterface.class)) {
079            return (T) getInterface();
080        }
081        return super.get(T); 
082    }
083
084    /**
085     * Configure the common managers for the connection. This puts the common
086     * manager config in one place.
087     */
088    @Override
089    public void configureManagers() {
090
091        InstanceManager.setSensorManager(getSensorManager());
092
093        InstanceManager.setTurnoutManager(getTurnoutManager());
094
095        if (getProgrammerManager().isAddressedModePossible()) {
096            InstanceManager.store(getProgrammerManager(), jmri.AddressedProgrammerManager.class);
097        }
098        if (getProgrammerManager().isGlobalProgrammerAvailable()) {
099            InstanceManager.store(getProgrammerManager(), GlobalProgrammerManager.class);
100        }
101        InstanceManager.store(getThrottleManager(), jmri.ThrottleManager.class);
102
103    }
104
105    /*
106     * Provides access to the ... for this particular connection.
107     */
108    protected OlcbProgrammerManager programmerManager;
109
110    public OlcbProgrammerManager getProgrammerManager() {
111        if (programmerManager == null) {
112            programmerManager = new OlcbProgrammerManager(new OlcbProgrammer());
113        }
114        return programmerManager;
115    }
116
117    public void setProgrammerManager(OlcbProgrammerManager p) {
118        programmerManager = p;
119    }
120
121    protected OlcbTurnoutManager turnoutManager;
122
123    public OlcbTurnoutManager getTurnoutManager() {
124        if (getDisabled()) {
125            return null;
126        }
127        if (turnoutManager == null) {
128            turnoutManager = new OlcbTurnoutManager(this);
129        }
130        return turnoutManager;
131    }
132
133    protected OlcbThrottleManager throttleManager;
134
135    public OlcbThrottleManager getThrottleManager() {
136        if (getDisabled()) {
137            return null;
138        }
139        if (throttleManager == null) {
140            throttleManager = new OlcbThrottleManager();
141        }
142        return throttleManager;
143    }
144
145    protected OlcbSensorManager sensorManager;
146
147    public OlcbSensorManager getSensorManager() {
148        if (getDisabled()) {
149            return null;
150        }
151        if (sensorManager == null) {
152            sensorManager = new OlcbSensorManager(this);
153        }
154        return sensorManager;
155    }
156
157    protected OlcbInterface olcbInterface;
158
159    public OlcbInterface getInterface() {
160        return olcbInterface;
161    }
162
163    public void setInterface(OlcbInterface iface) {
164        olcbInterface = iface;
165    }
166
167    @Override
168    protected ResourceBundle getActionModelResourceBundle() {
169        return ResourceBundle.getBundle("jmri.jmrix.openlcb.OlcbActionListBundle");
170    }
171
172    @Override
173    public void dispose() {
174        InstanceManager.deregister(this, OlcbSystemConnectionMemo.class);
175        if (cf != null) {
176            InstanceManager.deregister(cf, jmri.jmrix.swing.ComponentFactory.class);
177        }
178        if (turnoutManager != null) {
179            InstanceManager.deregister(turnoutManager, OlcbTurnoutManager.class);
180        }
181        if (sensorManager != null) {
182            InstanceManager.deregister(sensorManager, OlcbSensorManager.class);
183        }
184        if (throttleManager != null) {
185            InstanceManager.deregister(throttleManager, OlcbThrottleManager.class);
186        }
187        super.dispose();
188    }
189
190    /**
191     * See {@link jmri.NamedBean#compareSystemNameSuffix} for background.
192     * This is a common implementation for OpenLCB Sensors and Turnouts 
193     * of the comparison method.
194     *
195     * @param suffix1 1st suffix to compare.
196     * @param suffix2 2nd suffix to compare.
197     * @return true if suffixes match, else false.
198     */
199    @CheckReturnValue
200    public static int compareSystemNameSuffix(@Nonnull String suffix1, @Nonnull String suffix2) {
201        
202        // extract addresses
203        OlcbAddress[] array1 = new OlcbAddress(suffix1).split();
204        OlcbAddress[] array2 = new OlcbAddress(suffix2).split();
205
206        // compare on content
207        for (int i = 0; i < Math.min(array1.length, array2.length); i++) {
208            int c = array1[i].compare(array2[i]);
209            if (c != 0) return c;
210        }
211        // check for different length (shorter sorts first)
212        return Integer.signum(array1.length - array2.length);
213    }
214}