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