001package jmri.jmrix.sprog;
002
003import java.util.Locale;
004import javax.annotation.Nonnull;
005import jmri.Turnout;
006
007/**
008 * Implement turnout manager for Sprog systems.
009 * <p>
010 * System names are "STnnn", where S is the user configurable system prefix,
011 * nnn is the turnout number without padding.
012 *
013 * @author Bob Jacobsen Copyright (C) 2001
014 */
015public class SprogTurnoutManager extends jmri.managers.AbstractTurnoutManager {
016
017    public SprogTurnoutManager(SprogSystemConnectionMemo memo) {
018        super(memo);
019    }
020
021    /**
022     * {@inheritDoc}
023     */
024    @Override
025    @Nonnull
026    public SprogSystemConnectionMemo getMemo() {
027        return (SprogSystemConnectionMemo) memo;
028    }
029
030    // Sprog-specific methods
031
032    /**
033     * {@inheritDoc}
034     */
035    @Nonnull
036    @Override
037    protected Turnout createNewTurnout(@Nonnull String systemName, String userName) throws IllegalArgumentException {
038        int addr;
039        try {
040            addr = Integer.parseInt(systemName.substring(getSystemPrefix().length() + 1)); // multi char prefix
041        } catch (NumberFormatException e) {
042            throw new IllegalArgumentException("Failed to convert systemName '"+systemName+"' to a Turnout address");
043        }
044        Turnout t;
045        if (getMemo().getSprogMode() == SprogConstants.SprogMode.OPS ) {
046            t = new SprogCSTurnout(addr, getMemo());
047        } else {
048            t = new SprogTurnout(addr, getMemo());
049        }
050        t.setUserName(userName);
051        return t;
052    }
053
054    /**
055     * {@inheritDoc}
056     */
057    @Override
058    public NameValidity validSystemNameFormat(@Nonnull String systemName) {
059        return (getBitFromSystemName(systemName) != 0) ? NameValidity.VALID : NameValidity.INVALID;
060    }
061
062    /**
063     * {@inheritDoc}
064     */
065    @Override
066    @Nonnull
067    public String validateSystemNameFormat(@Nonnull String systemName, @Nonnull Locale locale) {
068        return validateIntegerSystemNameFormat(systemName, 1, SprogConstants.MAX_ACC_DECODER_JMRI_ADDR, locale);
069    }
070
071    /**
072     * Get the bit address from the system name.
073     * @param systemName a valid LocoNet-based Turnout System Name
074     * @return the turnout number extracted from the system name
075     */
076    public int getBitFromSystemName(String systemName) {
077        try {
078            validateSystemNameFormat(systemName, Locale.getDefault());
079        } catch (IllegalArgumentException ex) {
080            return 0;
081        }
082        return Integer.parseInt(systemName.substring(getSystemNamePrefix().length()));
083    }
084
085    @Override
086    public boolean allowMultipleAdditions(@Nonnull String systemName) {
087        return true;
088    }
089
090    /**
091     * {@inheritDoc}
092     */
093    @Override
094    public String getEntryToolTip() {
095        return Bundle.getMessage("AddOutputEntryToolTip");
096    }
097
098}