001package jmri.jmrix.oaktree;
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 Oak Tree serial systems.
012 * <p>
013 * System names are "OLnnn", where O 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
020 */
021public class SerialLightManager extends AbstractLightManager {
022
023    public SerialLightManager(OakTreeSystemConnectionMemo memo) {
024        super(memo);
025    }
026
027    /**
028     * {@inheritDoc}
029     */
030    @Override
031    @Nonnull
032    public OakTreeSystemConnectionMemo getMemo() {
033        return (OakTreeSystemConnectionMemo) memo;
034    }
035
036    /**
037     * Create a new Light based on the system name.
038     * Assumes calling method has checked that a Light with this system name
039     * does not already exist.
040     * {@inheritDoc}
041     * @throws IllegalArgumentException if the system name is not in a valid format or if the
042     * system name does not correspond to a configured OakTree digital output bit
043     */
044    @Override
045    @Nonnull
046    protected Light createNewLight(@Nonnull String systemName, String userName) throws IllegalArgumentException {
047        // Validate the systemName
048        if (SerialAddress.validSystemNameFormat(systemName, 'L', getSystemPrefix()) == NameValidity.VALID) {
049            Light lgt = new SerialLight(systemName, userName, getMemo());
050            if (!SerialAddress.validSystemNameConfig(systemName, 'L', getMemo())) {
051                log.warn("Light system Name does not refer to configured hardware: {}", systemName);
052            }
053            return lgt;
054        } else {
055            log.error("Invalid Light system Name format: {}", systemName);
056            throw new IllegalArgumentException("Invalid Light system Name format: " + systemName);
057        }
058    }
059
060    /**
061     * {@inheritDoc}
062     */
063    @Override
064    @Nonnull
065    public String validateSystemNameFormat(@Nonnull String systemName, @Nonnull Locale locale) {
066        return SerialAddress.validateSystemNameFormat(systemName, getSystemNamePrefix(), locale);
067    }
068
069    /**
070     * {@inheritDoc}
071     */
072    @Override
073    public NameValidity validSystemNameFormat(@Nonnull String systemName) {
074        return (SerialAddress.validSystemNameFormat(systemName, typeLetter(), getSystemPrefix()));
075    }
076
077    /**
078     * {@inheritDoc}
079     */
080    @Override
081    public boolean validSystemNameConfig(@Nonnull String systemName) {
082        return (SerialAddress.validSystemNameConfig(systemName, typeLetter(), getMemo()));
083    }
084
085    /**
086     * Convert system name to its alternate format.
087     *
088     * @return a normalized system name if system name is valid and has a valid
089     * alternate representation, else return ""
090     */
091    @Override
092    @Nonnull
093    public String convertSystemNameToAlternate(@Nonnull String systemName) {
094        return (SerialAddress.convertSystemNameToAlternate(systemName, getSystemPrefix()));
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(SerialLightManager.class);
106
107}