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