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