001package jmri.jmrix.maple;
002
003import java.util.Locale;
004import javax.annotation.Nonnull;
005import jmri.Light;
006import jmri.managers.AbstractLightManager;
007
008/**
009 * Implement LightManager for Maple serial systems.
010 * <p>
011 * System names are "KLnnn", where K is the user configurable system prefix,
012 * nnn is the bit number without padding.
013 * <p>
014 * Based in part on SerialTurnoutManager.java
015 *
016 * @author Bob Jacobsen Copyright (C) 2008
017 * @author Dave Duchamp Copyright (C) 2004, 2010
018 */
019public class SerialLightManager extends AbstractLightManager {
020
021    public SerialLightManager(MapleSystemConnectionMemo memo) {
022        super(memo);
023    }
024
025    /**
026     * {@inheritDoc}
027     */
028    @Override
029    @Nonnull
030    public MapleSystemConnectionMemo getMemo() {
031        return (MapleSystemConnectionMemo) memo;
032    }
033
034    /**
035     * {@inheritDoc}
036     */
037    @Override
038    @Nonnull
039    protected Light createNewLight(@Nonnull String systemName, String userName) throws IllegalArgumentException {
040        
041        // check if the output bit is available
042        int bitNum = SerialAddress.getBitFromSystemName(systemName, getSystemPrefix());
043        if (bitNum == 0) {
044            throw new IllegalArgumentException("Invalid Bit from System Name: " + systemName);
045        }
046        String conflict = SerialAddress.isOutputBitFree(bitNum, getSystemPrefix());
047        if (!conflict.isEmpty()) {
048            log.error("Assignment conflict with '{}'. Light not created.", conflict);
049            throw new IllegalArgumentException("The output bit, " + bitNum + ", is currently assigned to " + conflict);
050        }
051        // Validate the System Name
052        String sysName = SerialAddress.normalizeSystemName(systemName, getSystemPrefix());
053        if (sysName.isEmpty()) {
054            log.error("error when normalizing system name {}", systemName);
055            throw new IllegalArgumentException("Error when normalizing system name: "+systemName);
056        }
057        if (SerialAddress.validSystemNameFormat(systemName, 'L', getSystemPrefix()) == NameValidity.VALID) {
058            Light lgt = new SerialLight(sysName, userName, getMemo());
059            if (!SerialAddress.validSystemNameConfig(sysName, 'L', getMemo())) {
060                log.warn("Light system Name '{}' does not refer to configured hardware.", sysName);
061                jmri.util.swing.JmriJOptionPane.showMessageDialog(null, "WARNING - The Light just added, " + sysName
062                        + ", refers to an unconfigured output bit.", "Configuration Warning",
063                        jmri.util.swing.JmriJOptionPane.INFORMATION_MESSAGE);
064            }
065            return lgt;
066        } else {
067            log.error("Invalid Light system Name format: {}", systemName);
068            throw new IllegalArgumentException("Invalid Light system Name format: " + systemName);
069        }
070    }
071
072    /**
073     * {@inheritDoc}
074     */
075    @Override
076    @Nonnull
077    public String validateSystemNameFormat(@Nonnull String name, @Nonnull Locale locale) {
078        return SerialAddress.validateSystemNameFormat(name, this, locale);
079    }
080
081    /**
082     * {@inheritDoc}
083     */
084    @Override
085    public NameValidity validSystemNameFormat(@Nonnull String systemName) {
086        return (SerialAddress.validSystemNameFormat(systemName, typeLetter(), getSystemPrefix()));
087    }
088
089    /**
090     * {@inheritDoc}
091     */
092    @Override
093    public boolean validSystemNameConfig(@Nonnull String systemName) {
094        return (SerialAddress.validSystemNameConfig(systemName, 'L', getMemo()));
095    }
096
097    /**
098     * {@inheritDoc}
099     */
100    @Override
101    public String getEntryToolTip() {
102        return Bundle.getMessage("AddOutputEntryToolTip");
103    }
104
105    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SerialLightManager.class);
106
107}