001package jmri.jmrix.internal;
002
003import javax.annotation.Nonnull;
004import jmri.NamedBean;
005import jmri.Sensor;
006import jmri.implementation.AbstractSensor;
007import jmri.util.PreferNumericComparator;
008
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012/**
013 * Implementation of the InternalSensorManager interface.
014 *
015 * @author Bob Jacobsen Copyright (C) 2001, 2003, 2006
016 */
017public class InternalSensorManager extends jmri.managers.AbstractSensorManager {
018
019    public InternalSensorManager(InternalSystemConnectionMemo memo) {
020        super(memo);
021    }
022
023    /** {@inheritDoc} */
024    @Override
025    public boolean allowMultipleAdditions(@Nonnull String systemName) {
026        return true;
027    }
028
029    /**
030     * {@inheritDoc}
031     *
032     * @return a new (dummy) Internal sensor
033     */
034    @Override
035    @Nonnull
036    protected Sensor createNewSensor(@Nonnull String systemName, String userName) {
037        Sensor sen = new AbstractSensor(systemName, userName) {
038
039            @Override
040            public void requestUpdateFromLayout() {
041                // nothing to do
042            }
043
044            @Override
045            public int compareSystemNameSuffix(@Nonnull String suffix1, @Nonnull String suffix2, NamedBean n) {
046                return (new PreferNumericComparator()).compare(suffix1, suffix2);
047            }
048        };
049        try {
050            sen.setKnownState(getDefaultStateForNewSensors());
051        } catch (jmri.JmriException ex) {
052            log.error("An error occurred while trying to set initial state for sensor {}", sen.getDisplayName());
053            log.error(ex.toString());
054        }
055        log.debug("Internal Sensor \"{}\", \"{}\" created", systemName, userName);
056        return sen;
057    }
058
059    static int defaultState = NamedBean.UNKNOWN;
060
061    public static synchronized void setDefaultStateForNewSensors(int defaultSetting) {
062        log.debug("Default new-Sensor state set to {}", defaultSetting);
063        defaultState = defaultSetting;
064    }
065
066    public static synchronized int getDefaultStateForNewSensors() {
067        return defaultState;
068    }
069
070    protected String prefix = "I";
071
072    /** {@inheritDoc} */
073    @Override
074    public String getEntryToolTip() {
075        return Bundle.getMessage("AddInputEntryToolTip");
076    }
077
078    /**
079     * {@inheritDoc}
080     */
081    @Override
082    @Nonnull
083    public InternalSystemConnectionMemo getMemo() {
084        return (InternalSystemConnectionMemo) memo;
085    }
086    
087    /**
088     * No validation for Internal Sensors.
089     * {@inheritDoc}
090     */
091    @Override
092    @Nonnull
093    public String createSystemName(@Nonnull String curAddress, @Nonnull String prefix) throws jmri.JmriException {
094        return prefix + typeLetter() + curAddress;
095    }
096
097    private static final Logger log = LoggerFactory.getLogger(InternalSensorManager.class);
098
099}