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