001package jmri.jmrix.grapevine;
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 Grapevine serial systems.
012 * <p>
013 * System names are "GLnnn", where G is the (multichar) system connection 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(GrapevineSystemConnectionMemo memo) {
024        super(memo);
025    }
026
027    /**
028     * {@inheritDoc}
029     */
030    @Override
031    @Nonnull
032    public GrapevineSystemConnectionMemo getMemo() {
033        return (GrapevineSystemConnectionMemo) memo;
034    }
035
036    /**
037     * Method to create a new Light based on the system name.
038     * <p>
039     * Assumes calling method has checked that a Light with this
040     * system name does not already exist.
041     * {@inheritDoc}
042     * @throws IllegalArgumentException if system name is not in valid format or
043     * the system name does not correspond to a configured Grapevine
044     * digital output bit
045     */
046    @Override
047    @Nonnull
048    protected Light createNewLight(@Nonnull String systemName, String userName) throws IllegalArgumentException {
049        String prefix = getSystemPrefix();
050        // Validate the systemName
051        if (SerialAddress.validSystemNameFormat(systemName, 'L', prefix) == NameValidity.VALID) {
052            Light lgt = new SerialLight(systemName, userName, getMemo());
053            if (!SerialAddress.validSystemNameConfig(systemName, 'L', getMemo().getTrafficController())) {
054                log.warn("Light system Name does not refer to configured hardware: {}", systemName);
055            }
056            log.debug("new light {} for prefix {}", systemName, prefix);
057            return lgt;
058        } else {
059            log.warn("Invalid Light system Name format: {}", systemName);
060            throw new IllegalArgumentException("Invalid Light system Name format: " + systemName);
061        }
062    }
063
064    /**
065     * {@inheritDoc}
066     */
067    @Override
068    @Nonnull
069    public String validateSystemNameFormat(@Nonnull String name, @Nonnull Locale locale) {
070        return SerialAddress.validateSystemNameFormat(name, this, locale);
071    }
072
073    /**
074     * {@inheritDoc}
075     */
076    @Override
077    public NameValidity validSystemNameFormat(@Nonnull String systemName) {
078        return SerialAddress.validSystemNameFormat(systemName, typeLetter(), getSystemPrefix());
079    }
080
081    /**
082     * Public method to validate system name for configuration.
083     *
084     * @return 'true' if system name has a valid meaning in current
085     * configuration, else returns 'false'
086     */
087    @Override
088    public boolean validSystemNameConfig(@Nonnull String systemName) {
089        return (SerialAddress.validSystemNameConfig(systemName, 'L', getMemo().getTrafficController()));
090    }
091
092    /**
093     * Public method to convert system name to its alternate format.
094     * {@inheritDoc}
095     * @return a normalized system name if system name is valid and has a valid
096     * alternate representation, else return ""
097     */
098    @Override
099    @Nonnull
100    public String convertSystemNameToAlternate(@Nonnull String systemName) {
101        return (SerialAddress.convertSystemNameToAlternate(systemName, getSystemPrefix()));
102    }
103
104    /**
105     * {@inheritDoc}
106     */
107    @Override
108    public String getEntryToolTip() {
109        return Bundle.getMessage("AddOutputEntryToolTip");
110    }
111
112    private final static Logger log = LoggerFactory.getLogger(SerialLightManager.class);
113
114}