001package jmri.jmrix.tmcc;
002
003import java.util.Locale;
004
005import javax.annotation.Nonnull;
006
007import jmri.*;
008import jmri.managers.AbstractTurnoutManager;
009
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013/**
014 * Implement turnout manager for TMCC serial systems.
015 * <p>
016 * System names are "TTnnn", where T is the user configurable system prefix,
017 * nnn is the turnout number without padding.
018 *
019 * @author Bob Jacobsen Copyright (C) 2003, 2006
020 */
021public class SerialTurnoutManager extends AbstractTurnoutManager implements SerialListener {
022
023    public SerialTurnoutManager(TmccSystemConnectionMemo memo) {
024        super(memo);
025        memo.getTrafficController().addSerialListener(this);
026        log.debug("TMCC TurnoutManager prefix={}", getSystemPrefix());
027    }
028
029    /**
030     * {@inheritDoc}
031     */
032    @Override
033    @Nonnull
034    public TmccSystemConnectionMemo getMemo() {
035        return (TmccSystemConnectionMemo) memo;
036    }
037
038    /**
039     * {@inheritDoc}
040     */
041    @Nonnull
042    @Override
043    protected Turnout createNewTurnout(@Nonnull String systemName, String userName) throws IllegalArgumentException {
044        // validate the system name
045        String sName = validateSystemNameFormat(systemName);
046        // does this turnout already exist?
047        Turnout t = getBySystemName(sName);
048        if (t != null) {
049            log.debug("Turnout already exists");
050            return t;
051        }
052        // create the turnout
053        log.debug("new SerialTurnout with addr = {}", systemName.substring(getSystemPrefix().length() + 1));
054        int addr = Integer.parseInt(systemName.substring(getSystemPrefix().length() + 1));
055        t = new SerialTurnout(getSystemPrefix(), addr, getMemo());
056        t.setUserName(userName);
057        return t;
058    }
059
060    /**
061     * Listeners for messages from the command station.
062     */
063    @Override
064    public void message(SerialMessage m) {
065        log.debug("message received unexpectedly: {}", m.toString());
066    }
067
068    // Listen for status changes from TMCC system
069    @Override
070    public void reply(SerialReply r) {
071        // There isn't anything meaningful coming back at this time.
072        log.debug("reply received unexpectedly: {}", r.toString());
073    }
074
075    // Turnout address format is more than a simple number.
076    @Override
077    public boolean allowMultipleAdditions(@Nonnull String systemName) {
078        return true;
079    }
080
081    /**
082     * {@inheritDoc}
083     */
084    @Override
085    @Nonnull
086    public String validateSystemNameFormat(@Nonnull String name, @Nonnull Locale locale) {
087        return validateIntegerSystemNameFormat(name, 1, 99, locale);
088    }
089
090    /**
091     * {@inheritDoc}
092     */
093    @Override
094    public NameValidity validSystemNameFormat(@Nonnull String systemName) {
095        NameValidity validity = super.validSystemNameFormat(systemName);
096        if (validity == NameValidity.VALID) {
097            int num;
098            try {
099                num = Integer.parseInt(systemName.substring(getSystemNamePrefix().length()));
100                if (num < 0 || num > 99) {
101                    validity = NameValidity.INVALID;
102                }
103            } catch (NumberFormatException ex) {
104                validity = NameValidity.INVALID;
105            }
106        }
107        return validity;
108    }
109
110    /**
111     * {@inheritDoc}
112     */
113    @Override
114    public String getEntryToolTip() {
115        return Bundle.getMessage("AddOutputEntryToolTip");
116    }
117
118    private final static Logger log = LoggerFactory.getLogger(SerialTurnoutManager.class);
119
120}