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;
010import jmri.jmrit.logixng.util.parser.ParserException;
011
012/**
013 * Sets a Memory.
014 *
015 * @author Daniel Bergqvist Copyright 2018
016 */
017public class AnalogActionMemory extends AbstractAnalogAction
018        implements PropertyChangeListener {
019
020    private final LogixNG_SelectNamedBean<Memory> _selectNamedBean =
021            new LogixNG_SelectNamedBean<>(
022                    this, Memory.class, InstanceManager.getDefault(MemoryManager.class), this);
023
024    public AnalogActionMemory(String sys, String user) {
025        super(sys, user);
026    }
027
028    @Override
029    public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) throws ParserException {
030        AnalogActionManager manager = InstanceManager.getDefault(AnalogActionManager.class);
031        String sysName = systemNames.get(getSystemName());
032        String userName = userNames.get(getSystemName());
033        if (sysName == null) sysName = manager.getAutoSystemName();
034        AnalogActionMemory copy = new AnalogActionMemory(sysName, userName);
035        copy.setComment(getComment());
036        _selectNamedBean.copy(copy._selectNamedBean);
037        return manager.registerAction(copy);
038    }
039
040    public LogixNG_SelectNamedBean<Memory> getSelectNamedBean() {
041        return _selectNamedBean;
042    }
043
044    /** {@inheritDoc} */
045    @Override
046    public void setValue(double value) throws JmriException {
047        Memory memory = _selectNamedBean.evaluateNamedBean(getConditionalNG());
048        if (memory != null) {
049            jmri.util.ThreadingUtil.runOnLayoutWithJmriException(() -> {
050                memory.setValue(value);
051            });
052        }
053    }
054
055    /** {@inheritDoc} */
056    @Override
057    public FemaleSocket getChild(int index)
058            throws IllegalArgumentException, UnsupportedOperationException {
059        throw new UnsupportedOperationException("Not supported.");
060    }
061
062    /** {@inheritDoc} */
063    @Override
064    public int getChildCount() {
065        return 0;
066    }
067
068    /** {@inheritDoc} */
069    @Override
070    public Category getCategory() {
071        return Category.ITEM;
072    }
073
074    /** {@inheritDoc} */
075    @Override
076    public String getShortDescription(Locale locale) {
077        return Bundle.getMessage(locale, "AnalogActionMemory_Short");
078    }
079
080    /** {@inheritDoc} */
081    @Override
082    public String getLongDescription(Locale locale) {
083        return Bundle.getMessage(locale, "AnalogActionMemory_Long", _selectNamedBean.getDescription(locale));
084    }
085
086    /** {@inheritDoc} */
087    @Override
088    public void setup() {
089        // Do nothing
090    }
091
092    /** {@inheritDoc} */
093    @Override
094    public void registerListenersForThisClass() {
095        _selectNamedBean.registerListeners();
096    }
097
098    /** {@inheritDoc} */
099    @Override
100    public void unregisterListenersForThisClass() {
101        _selectNamedBean.unregisterListeners();
102    }
103
104    /** {@inheritDoc} */
105    @Override
106    public void disposeMe() {
107    }
108
109    /** {@inheritDoc} */
110    @Override
111    public void getUsageDetail(int level, NamedBean bean, List<NamedBeanUsageReport> report, NamedBean cdl) {
112        _selectNamedBean.getUsageDetail(level, bean, report, cdl, this, LogixNG_SelectNamedBean.Type.Action);
113    }
114
115    /** {@inheritDoc} */
116    @Override
117    public void propertyChange(PropertyChangeEvent evt) {
118        getConditionalNG().execute();
119    }
120
121//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AnalogActionMemory.class);
122
123}