001package jmri.jmrix.rps;
002
003import java.util.Locale;
004import javax.annotation.Nonnull;
005import jmri.JmriException;
006import jmri.Reporter;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010/**
011 * RPS implementation of a ReporterManager.
012 *
013 * @author Bob Jacobsen Copyright (C) 2008, 2019
014 * @since 2.3.1
015 */
016public class RpsReporterManager extends jmri.managers.AbstractReporterManager {
017
018    public RpsReporterManager(RpsSystemConnectionMemo memo) {
019        super(memo);
020    }
021
022    /**
023     * {@inheritDoc}
024     */
025    @Override
026    @Nonnull
027    public RpsSystemConnectionMemo getMemo() {
028        return (RpsSystemConnectionMemo) memo;
029    }
030
031    /**
032     * Create a new reporter if all checks are passed.
033     * System name is normalized to ensure uniqueness.
034     */
035    @Override
036    @Nonnull
037    protected Reporter createNewReporter(@Nonnull String systemName, String userName) throws IllegalArgumentException {
038        log.debug("creating {}", userName);
039        RpsReporter r = new RpsReporter(systemName, userName, getSystemPrefix());
040        Distributor.instance().addMeasurementListener(r);
041        return r;
042    }
043
044    @Override
045    public String createSystemName(String curAddress, String prefix) throws JmriException {
046        if (!prefix.equals(getSystemPrefix())) {
047            log.warn("prefix does not match memo.prefix");
048            throw new JmriException("Unable to convert " + curAddress + ", Prefix does not match");
049        }
050        String sys = getSystemPrefix() + typeLetter() + curAddress;
051        // first, check validity
052        try {
053            validSystemNameFormat(sys);
054        } catch (IllegalArgumentException e) {
055            throw new JmriException(e.getMessage());
056        }
057        return sys;
058    }
059    
060    /**
061     * {@inheritDoc}
062     */
063    @Override
064    @Nonnull
065    public String validateSystemNameFormat(@Nonnull String name, @Nonnull Locale locale) {
066        return getMemo().validateSystemNameFormat(name, this, locale);
067    }
068    
069    /**
070     * {@inheritDoc}
071     */
072    @Override
073    public NameValidity validSystemNameFormat(@Nonnull String systemName) {
074        return getMemo().validSystemNameFormat(systemName, typeLetter());
075    }
076
077    /**
078     * {@inheritDoc}
079     */
080    @Override
081    public String getEntryToolTip() {
082        return Bundle.getMessage("AddReporterEntryToolTip");
083    }
084
085    private final static Logger log = LoggerFactory.getLogger(RpsReporterManager.class);
086
087}