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