001package jmri.jmrit.logixng.actions;
002
003import java.beans.PropertyChangeEvent;
004import java.beans.PropertyChangeListener;
005import java.util.*;
006
007import jmri.*;
008import jmri.jmrit.logixng.*;
009import jmri.jmrit.logixng.util.LogixNG_SelectNamedBean;
010
011/**
012 * Sets the light intensity.
013 *
014 * @author Daniel Bergqvist Copyright 2021
015 */
016public class AnalogActionLightIntensity extends AbstractAnalogAction
017        implements PropertyChangeListener {
018
019    public static final int INTENSITY_SOCKET = 0;
020
021    private final LogixNG_SelectNamedBean<VariableLight> _selectNamedBean =
022            new LogixNG_SelectNamedBean<>(
023                    this, VariableLight.class, InstanceManager.getDefault(VariableLightManager.class), this);
024
025
026    public AnalogActionLightIntensity(String sys, String user) {
027        super(sys, user);
028    }
029
030    @Override
031    public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) throws JmriException {
032        AnalogActionManager manager = InstanceManager.getDefault(AnalogActionManager.class);
033        String sysName = systemNames.get(getSystemName());
034        String userName = userNames.get(getSystemName());
035        if (sysName == null) sysName = manager.getAutoSystemName();
036        AnalogActionLightIntensity copy = new AnalogActionLightIntensity(sysName, userName);
037        copy.setComment(getComment());
038        _selectNamedBean.copy(copy._selectNamedBean);
039        return manager.registerAction(copy).deepCopyChildren(this, systemNames, userNames);
040    }
041
042    public LogixNG_SelectNamedBean<VariableLight> getSelectNamedBean() {
043        return _selectNamedBean;
044    }
045
046    /** {@inheritDoc} */
047    @Override
048    public Category getCategory() {
049        return Category.ITEM;
050    }
051
052    /** {@inheritDoc} */
053    @Override
054    public void setValue(double value) throws JmriException {
055        VariableLight light = _selectNamedBean.evaluateNamedBean(getConditionalNG());
056
057        if (light == null) {
058//            log.warn("light is null");
059            return;
060        }
061
062        double intensity = value;
063
064        if (intensity < 0.0) intensity = 0.0;
065        if (intensity > 100.0) intensity = 100.0;
066
067        light.setTargetIntensity(intensity/100.0);
068    }
069
070    @Override
071    public FemaleSocket getChild(int index) throws IllegalArgumentException, UnsupportedOperationException {
072        throw new UnsupportedOperationException("Not supported.");
073    }
074
075    @Override
076    public int getChildCount() {
077        return 0;
078    }
079
080    @Override
081    public String getShortDescription(Locale locale) {
082        return Bundle.getMessage(locale, "AnalogActionLightIntensity_Short");
083    }
084
085    @Override
086    public String getLongDescription(Locale locale) {
087        String namedBean = _selectNamedBean.getDescription(locale);
088
089        return Bundle.getMessage(locale, "AnalogActionLightIntensity_Long", namedBean);
090    }
091
092    /** {@inheritDoc} */
093    @Override
094    public void setup() {
095        // Do nothing
096    }
097
098    /** {@inheritDoc} */
099    @Override
100    public void registerListenersForThisClass() {
101        _selectNamedBean.registerListeners();
102        _listenersAreRegistered = true;
103    }
104
105    /** {@inheritDoc} */
106    @Override
107    public void unregisterListenersForThisClass() {
108        _selectNamedBean.unregisterListeners();
109        _listenersAreRegistered = false;
110    }
111
112    /** {@inheritDoc} */
113    @Override
114    public void disposeMe() {
115    }
116
117    /** {@inheritDoc} */
118    @Override
119    public void propertyChange(PropertyChangeEvent evt) {
120        getConditionalNG().execute();
121    }
122
123//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AnalogActionLightIntensity.class);
124
125}