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