001package jmri.jmrix.secsi;
002
003import java.util.Locale;
004import javax.annotation.Nonnull;
005import jmri.Light;
006import jmri.managers.AbstractLightManager;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010/**
011 * Implement LightManager for SECSI serial systems.
012 * <p>
013 * System names are "VLnnn", where V is the user configurable system prefix,
014 * nnn is the bit number without padding.
015 * <p>
016 * Based in part on SerialTurnoutManager.java
017 *
018 * @author Dave Duchamp Copyright (C) 2004
019 * @author Bob Jacobsen Copyright (C) 2006, 2007
020 */
021public class SerialLightManager extends AbstractLightManager {
022
023    public SerialLightManager(SecsiSystemConnectionMemo memo) {
024        super(memo);
025    }
026
027    /**
028     * {@inheritDoc}
029     */
030    @Override
031    @Nonnull
032    public SecsiSystemConnectionMemo getMemo() {
033        return (SecsiSystemConnectionMemo) memo;
034    }
035
036    /**
037     * Method to create a new Light based on the system name.
038     * Assumes calling method has checked that a Light with this system
039     * name does not already exist.
040     *
041     * @throws IllegalArgumentException if system name is not in a valid format 
042     * or if the
043     * system name does not correspond to a configured digital output bit
044     */
045    @Override
046    @Nonnull
047    protected Light createNewLight(@Nonnull String systemName, String userName) throws IllegalArgumentException {
048        // Validate the systemName
049        if (SerialAddress.validSystemNameFormat(systemName, 'L', getSystemPrefix()) == NameValidity.VALID) {
050            Light lgt = new SerialLight(systemName, userName,getMemo());
051            if (!SerialAddress.validSystemNameConfig(systemName, 'L', getMemo().getTrafficController())) {
052                log.warn("Light system Name does not refer to configured hardware: {}", systemName);
053            }
054            return lgt;
055        } else {
056            log.error("Invalid Light system Name format: {}", systemName);
057            throw new IllegalArgumentException("Invalid Light system Name format: " + systemName);
058        }
059    }
060
061    /**
062     * {@inheritDoc}
063     */
064    @Override
065    @Nonnull
066    public String validateSystemNameFormat(@Nonnull String systemName, @Nonnull Locale locale) {
067        return SerialAddress.validateSystemNameFormat(systemName, getSystemNamePrefix(), locale);
068    }
069
070    /**
071     * {@inheritDoc}
072     */
073    @Override
074    public NameValidity validSystemNameFormat(@Nonnull String systemName) {
075        return (SerialAddress.validSystemNameFormat(systemName, typeLetter(), this.getSystemPrefix()));
076    }
077
078    /**
079     * Public method to validate system name for configuration.
080     *
081     * @return 'true' if system name has a valid meaning in current
082     * configuration, else returns 'false'
083     */
084    @Override
085    public boolean validSystemNameConfig(@Nonnull String systemName) {
086        return (SerialAddress.validSystemNameConfig(systemName, 'L',getMemo().getTrafficController()));
087    }
088
089    /**
090     * Public method to convert system name to its alternate format
091     *
092     * @return a normalized system name if system name is valid and has a valid
093     * alternate representation, else returns ""
094     */
095    @Override
096    @Nonnull
097    public String convertSystemNameToAlternate(@Nonnull String systemName) {
098        return (SerialAddress.convertSystemNameToAlternate(systemName, getSystemPrefix()));
099    }
100
101    /**
102     * {@inheritDoc}
103     */
104    @Override
105    public String getEntryToolTip() {
106        return Bundle.getMessage("AddOutputEntryToolTip");
107    }
108
109    private final static Logger log = LoggerFactory.getLogger(SerialLightManager.class);
110
111}