001package jmri.jmrix.secsi;
002
003import java.util.Locale;
004import javax.annotation.Nonnull;
005import jmri.Turnout;
006import jmri.managers.AbstractTurnoutManager;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010/**
011 * Implement turnout manager for SECSI systems.
012 * <p>
013 * System names are "VTnnn", where V is the user configurable system prefix,
014 * nnn is the turnout number without padding.
015 *
016 * @author Bob Jacobsen Copyright (C) 2003, 2006, 2007
017 */
018public class SerialTurnoutManager extends AbstractTurnoutManager {
019
020    public SerialTurnoutManager(SecsiSystemConnectionMemo memo) {
021        super(memo);
022    }
023
024    /**
025     * {@inheritDoc}
026     */
027    @Override
028    @Nonnull
029    public SecsiSystemConnectionMemo getMemo() {
030        return (SecsiSystemConnectionMemo) memo;
031    }
032
033    /**
034     * {@inheritDoc}
035     */
036    @Nonnull
037    @Override
038    protected Turnout createNewTurnout(@Nonnull String systemName, String userName) throws IllegalArgumentException {
039        // validate the system name, and normalize it
040        String sName = SerialAddress.normalizeSystemName(systemName, getSystemPrefix());
041        if (sName.isEmpty()) {
042            // system name is not valid
043            throw new IllegalArgumentException("Cannot create System Name from " + systemName);
044        }
045        // does this turnout already exist
046        Turnout t = getBySystemName(sName);
047        if (t != null) {
048            return t;
049        }
050        // check under alternate name
051        String altName = SerialAddress.convertSystemNameToAlternate(sName, getSystemPrefix());
052        t = getBySystemName(altName);
053        if (t != null) {
054            return t;
055        }
056        // create the turnout
057        t = new SerialTurnout(sName, userName, getMemo());
058
059        // does system name correspond to configured hardware
060        if (!SerialAddress.validSystemNameConfig(sName, 'T', getMemo().getTrafficController())) {
061            // system name does not correspond to configured hardware
062            log.warn("Turnout '{}' refers to an undefined Serial Node.", sName);
063        }
064        return t;
065    }
066
067    /**
068     * {@inheritDoc}
069     */
070    @Override
071    @Nonnull
072    public String validateSystemNameFormat(@Nonnull String systemName, @Nonnull Locale locale) {
073        return SerialAddress.validateSystemNameFormat(systemName, getSystemNamePrefix(), locale);
074    }
075
076    /**
077     * {@inheritDoc}
078     */
079    @Override
080    public NameValidity validSystemNameFormat(@Nonnull String systemName) {
081        return (SerialAddress.validSystemNameFormat(systemName, typeLetter(), this.getSystemPrefix()));
082    }
083
084    /**
085     * {@inheritDoc}
086     */
087    @Override
088    public String getEntryToolTip() {
089        return Bundle.getMessage("AddOutputEntryToolTip");
090    }
091
092    private final static Logger log = LoggerFactory.getLogger(SerialTurnoutManager.class);
093
094}