001package jmri.jmrix.loconet.hexfile;
002
003import jmri.GlobalProgrammerManager;
004import jmri.managers.ManagerDefaultSelector;
005
006import javax.annotation.OverridingMethodsMustInvokeSuper;
007
008/**
009 * Lightweight class to denote that a system is "active" via a LocoNet hexfile emulator.
010 * <p>
011 * Objects of specific subtypes are registered in the instance manager to
012 * activate their particular system.
013 *
014 * @author Kevin Dickerson Copyright (C) 2010
015 * @author Egbert Broerse (C) 2020
016 */
017public class HexFileSystemConnectionMemo extends jmri.jmrix.loconet.LocoNetSystemConnectionMemo {
018
019    /**
020     * Use the simulation (hexfile) LocoNet sensor manager instead of the standard LocoNet sensor manager.
021     */
022    @Override
023    public LnSensorManager getSensorManager() {
024        if (getDisabled()) {
025            return null;
026        }
027        return (LnSensorManager) classObjectMap.computeIfAbsent(jmri.SensorManager.class, (Class c) -> new LnSensorManager(this));
028    }
029
030    /**
031     * Substitute the jmri.progdebugger.DebugProgrammerManager when this connection
032     * is set to provide the default (Service Mode) GlobalProgrammerManager, replacing
033     * the LocoNet LnProgrammerManager that the super class would return.
034     * For setting up this connection the substitution was already done in
035     * {@link HexFileFrame#configure()} to allow for debugging and mocking replies
036     * from the layout connection.
037     * Since the {@link ManagerDefaultSelector} directly calls this memo, and not
038     * the InstanceManager, we prevent to return the default class which does not match
039     * the active instance, creating an extra programmer that shows up as an extra
040     * line in the Program over combo.
041     *
042     * @param <T>  Type of manager to get
043     * @param type Type of manager to get
044     * @return The manager or null if provides() is false for T
045     * @see #provides(java.lang.Class)
046     */
047    @OverridingMethodsMustInvokeSuper
048    @SuppressWarnings("unchecked")
049    @Override
050    public <T> T get(Class<?> type) {
051        if (type.equals(GlobalProgrammerManager.class)) {
052            log.debug("Hex memo returned Global(Ops)ModeProgrammer");
053            return (T) getProgrammerManager();
054        } else {
055            return super.get(type);
056        }
057    }
058
059    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(HexFileSystemConnectionMemo.class);
060
061}