001package jmri.jmrit.logixng.actions;
002
003import jmri.InstanceManager;
004import jmri.JmriException;
005import jmri.Manager;
006import jmri.jmrit.logixng.Base;
007import jmri.jmrit.logixng.implementation.AbstractBase;
008import jmri.jmrit.logixng.AnalogActionBean;
009import jmri.jmrit.logixng.AnalogActionManager;
010
011/**
012 * The base class for LogixNG AnalogActions
013 *
014 * @author Daniel Bergqvist Copyright 2018
015 */
016public abstract class AbstractAnalogAction extends AbstractBase
017        implements AnalogActionBean {
018
019    private Base _parent = null;
020    private int _state = AnalogActionBean.UNKNOWN;
021
022
023    public AbstractAnalogAction(String sys, String user)
024            throws BadUserNameException, BadSystemNameException {
025        super(sys, user);
026
027        // Do this test here to ensure all the tests are using correct system names
028        Manager.NameValidity isNameValid = InstanceManager.getDefault(AnalogActionManager.class).validSystemNameFormat(mSystemName);
029        if (isNameValid != Manager.NameValidity.VALID) {
030            throw new IllegalArgumentException("system name is not valid");
031        }
032    }
033
034    /** {@inheritDoc} */
035    @Override
036    public Base getParent() {
037        return _parent;
038    }
039
040    /** {@inheritDoc} */
041    @Override
042    public void setParent(Base parent) {
043        _parent = parent;
044    }
045
046    @Override
047    public String getBeanType() {
048        return Bundle.getMessage("BeanNameAnalogAction");
049    }
050
051    @Override
052    public void setState(int s) throws JmriException {
053        log.warn("Unexpected call to setState in AbstractAnalogAction.");  // NOI18N
054        _state = s;
055    }
056
057    @Override
058    public int getState() {
059        log.warn("Unexpected call to getState in AbstractAnalogAction.");  // NOI18N
060        return _state;
061    }
062
063    public String getNewSocketName() {
064        int x = 1;
065        while (x < 10000) {     // Protect from infinite loop
066            String name = "A" + Integer.toString(x);
067            boolean validName = true;
068            for (int i=0; i < getChildCount(); i++) {
069                if (name.equals(getChild(i).getName())) {
070                    validName = false;
071                    break;
072                }
073            }
074            if (validName) {
075                return name;
076            }
077            x++;
078        }
079        throw new RuntimeException("Unable to find a new socket name");
080    }
081
082
083    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AbstractAnalogAction.class);
084}