001package jmri.jmrix.lenz;
002
003import java.util.Locale;
004import javax.annotation.Nonnull;
005import jmri.Light;
006import jmri.managers.AbstractLightManager;
007
008/**
009 * Implement LightManager for XpressNet systems.
010 * <p>
011 * System names are "XLnnn", where X is the user configurable system prefix,
012 * nnn is the bit number without padding.
013 * <p>
014 * Based in part on SerialLightManager.java
015 *
016 * @author Paul Bender Copyright (C) 2008
017 * @navassoc 1 - * jmri.jmrix.lenz.XNetLight
018 */
019public class XNetLightManager extends AbstractLightManager {
020
021    private XNetTrafficController tc;
022
023    public XNetLightManager(XNetSystemConnectionMemo memo) {
024        super(memo);
025        this.tc = memo.getXNetTrafficController();
026    }
027
028    /**
029     * {@inheritDoc}
030     */
031    @Override
032    @Nonnull
033    public XNetSystemConnectionMemo getMemo() {
034        return (XNetSystemConnectionMemo) memo;
035    }
036
037    // XNet-specific methods
038
039    /**
040     * Create a new Light based on the system name.
041     * Assumes calling method has checked that a Light with this
042     * system name does not already exist.
043     *
044     * @return null if the system name is not in a valid format
045     */
046    @Override
047    @Nonnull
048    protected Light createNewLight(@Nonnull String systemName, String userName) throws IllegalArgumentException {
049        // check if the output bit is available
050        int bitNum = XNetAddress.getBitFromSystemName(systemName, getSystemPrefix());
051        if (bitNum == -1) {
052            throw new IllegalArgumentException("Invalid Bit from System Name: " + systemName);
053        }
054        // Normalize the System Name
055        String sName = getSystemNamePrefix() + bitNum; // removes any leading zeros
056        // create the new Light object
057        return new XNetLight(tc, this, sName, userName);
058    }
059
060    /**
061     * Get the bit address from the system name.
062     *
063     * @param systemName system name for turnout
064     * @return index value for light, -1 if an error occurred
065     */
066    public int getBitFromSystemName(@Nonnull String systemName) {
067        return XNetAddress.getBitFromSystemName(systemName, getSystemPrefix());
068    }
069
070    /**
071     * {@inheritDoc}
072     */
073    @Override
074    @Nonnull
075    public String validateSystemNameFormat(@Nonnull String name, @Nonnull Locale locale) {
076        return validateIntegerSystemNameFormat(name,
077                XNetAddress.MINSENSORADDRESS,
078                XNetAddress.MAXSENSORADDRESS,
079                locale);
080    }
081    /**
082     * {@inheritDoc}
083     */
084    @Override
085    public NameValidity validSystemNameFormat(@Nonnull String systemName) {
086        return (getBitFromSystemName(systemName) != 0) ? NameValidity.VALID : NameValidity.INVALID;
087    }
088
089    /**
090     * Validate system name for configuration.
091     *
092     * @return 'true' if system name has a valid meaning in current configuration, else returns
093     * 'false'. For now, this method always returns 'true'; it is needed for the
094     * Abstract Light class.
095     */
096    @Override
097    public boolean validSystemNameConfig(@Nonnull String systemName) {
098        return (true);
099    }
100
101    /**
102     * Determine if it is possible to add a range of lights in
103     * numerical order eg 11 thru 18, primarily used to enable/disable the Add
104     * range checkbox in the Add Light pane.
105     */
106    @Override
107    public boolean allowMultipleAdditions(@Nonnull String systemName) {
108        return true;
109    }
110
111    /**
112     * {@inheritDoc}
113     */
114    @Override
115    public String getEntryToolTip() {
116        return Bundle.getMessage("AddOutputEntryToolTip");
117    }
118
119    // private static final Logger log = LoggerFactory.getLogger(XNetLightManager.class);
120
121}