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