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