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