001package jmri.jmrit.logixng.implementation;
002
003import java.util.*;
004
005import jmri.*;
006import jmri.jmrit.logixng.*;
007
008import javax.annotation.Nonnull;
009
010/**
011 * Every StringActionBean has an DefaultMaleStringActionSocket as its parent.
012 * 
013 * @author Daniel Bergqvist Copyright 2018
014 */
015public class DefaultMaleStringActionSocket extends AbstractMaleSocket implements MaleStringActionSocket {
016
017//    private final StringActionBean ((StringActionBean)getObject());
018    private DebugConfig _debugConfig = null;
019    private boolean _enabled = true;
020    
021    
022    public DefaultMaleStringActionSocket(@Nonnull BaseManager<? extends NamedBean> manager, @Nonnull StringActionBean stringAction) {
023        super(manager, stringAction);
024    }
025    
026    /** {@inheritDoc} */
027    @Override
028    /**
029     * Set a string value.
030     */
031    public void setValue(String value) throws JmriException {
032        if (! _enabled) {
033            return;
034        }
035        
036        if ((_debugConfig != null)
037                && ((StringActionDebugConfig)_debugConfig)._dontExecute) {
038            return;
039        }
040        
041        ConditionalNG conditionalNG = getConditionalNG();
042        
043        int currentStackPos = conditionalNG.getStack().getCount();
044        
045        try {
046            conditionalNG.getSymbolTable().createSymbols(_localVariables);
047            ((StringActionBean)getObject()).setValue(value);
048        } catch (JmriException e) {
049            if (e.getErrors() != null) {
050                handleError(this, Bundle.getMessage("ExceptionExecuteMulti"), e.getErrors(), e, log);
051            } else {
052                handleError(this, Bundle.getMessage("ExceptionSetValue", e.getLocalizedMessage()), e, log);
053            }
054        } catch (RuntimeException e) {
055            handleError(this, Bundle.getMessage("ExceptionSetValue", e.getLocalizedMessage()), e, log);
056        }
057        
058        conditionalNG.getStack().setCount(currentStackPos);
059        conditionalNG.getSymbolTable().removeSymbols(_localVariables);
060    }
061
062    @Override
063    public String getDisplayName() {
064        return ((StringActionBean)getObject()).getDisplayName();
065    }
066
067    @Override
068    public void disposeMe() {
069        ((StringActionBean)getObject()).dispose();
070    }
071
072    /**
073     * Register listeners if this object needs that.
074     */
075    @Override
076    public void registerListenersForThisClass() {
077        ((StringActionBean)getObject()).registerListeners();
078    }
079    
080    /**
081     * Register listeners if this object needs that.
082     */
083    @Override
084    public void unregisterListenersForThisClass() {
085        ((StringActionBean)getObject()).unregisterListeners();
086    }
087    
088    @Override
089    public void setState(int s) throws JmriException {
090        ((StringActionBean)getObject()).setState(s);
091    }
092
093    @Override
094    public int getState() {
095        return ((StringActionBean)getObject()).getState();
096    }
097
098    @Override
099    public String describeState(int state) {
100        return ((StringActionBean)getObject()).describeState(state);
101    }
102
103    @Override
104    public String getComment() {
105        return ((StringActionBean)getObject()).getComment();
106    }
107
108    @Override
109    public void setComment(String comment) {
110        ((StringActionBean)getObject()).setComment(comment);
111    }
112
113    @Override
114    public void setProperty(String key, Object value) {
115        ((StringActionBean)getObject()).setProperty(key, value);
116    }
117
118    @Override
119    public Object getProperty(String key) {
120        return ((StringActionBean)getObject()).getProperty(key);
121    }
122
123    @Override
124    public void removeProperty(String key) {
125        ((StringActionBean)getObject()).removeProperty(key);
126    }
127
128    @Override
129    public Set<String> getPropertyKeys() {
130        return ((StringActionBean)getObject()).getPropertyKeys();
131    }
132
133    @Override
134    public String getBeanType() {
135        return ((StringActionBean)getObject()).getBeanType();
136    }
137
138    @Override
139    public int compareSystemNameSuffix(String suffix1, String suffix2, NamedBean n2) {
140        return ((StringActionBean)getObject()).compareSystemNameSuffix(suffix1, suffix2, n2);
141    }
142
143    /** {@inheritDoc} */
144    @Override
145    public void setDebugConfig(DebugConfig config) {
146        _debugConfig = config;
147    }
148
149    /** {@inheritDoc} */
150    @Override
151    public DebugConfig getDebugConfig() {
152        return _debugConfig;
153    }
154
155    /** {@inheritDoc} */
156    @Override
157    public DebugConfig createDebugConfig() {
158        return new StringActionDebugConfig();
159    }
160
161    /** {@inheritDoc} */
162    @Override
163    public void setEnabled(boolean enable) {
164        _enabled = enable;
165        if (isActive()) {
166            registerListeners();
167        } else {
168            unregisterListeners();
169        }
170    }
171    
172    /** {@inheritDoc} */
173    @Override
174    public void setEnabledFlag(boolean enable) {
175        _enabled = enable;
176    }
177    
178    /** {@inheritDoc} */
179    @Override
180    public boolean isEnabled() {
181        return _enabled;
182    }
183
184
185
186    public static class StringActionDebugConfig implements MaleSocket.DebugConfig {
187        
188        // If true, the socket is not executing the action.
189        // It's useful if you want to test the LogixNG without affecting the
190        // layout (turnouts, sensors, and so on).
191        public boolean _dontExecute = false;
192        
193        @Override
194        public DebugConfig getCopy() {
195            StringActionDebugConfig config = new StringActionDebugConfig();
196            config._dontExecute = _dontExecute;
197            return config;
198        }
199        
200    }
201    
202    
203    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DefaultMaleStringActionSocket.class);
204
205}