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 StringActionMemory extends AbstractStringAction
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 StringActionMemory(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        StringActionManager manager = InstanceManager.getDefault(StringActionManager.class);
031        String sysName = systemNames.get(getSystemName());
032        String userName = userNames.get(getSystemName());
033        if (sysName == null) sysName = manager.getAutoSystemName();
034        StringActionMemory copy = new StringActionMemory(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(String value) throws JmriException {
047        Memory memory = _selectNamedBean.evaluateNamedBean(getConditionalNG());
048        if (memory != null) {
049            memory.setValue(value);
050        }
051    }
052
053    /** {@inheritDoc} */
054    @Override
055    public FemaleSocket getChild(int index) throws IllegalArgumentException, UnsupportedOperationException {
056        throw new UnsupportedOperationException("Not supported.");
057    }
058
059    /** {@inheritDoc} */
060    @Override
061    public int getChildCount() {
062        return 0;
063    }
064
065    /** {@inheritDoc} */
066    @Override
067    public Category getCategory() {
068        return Category.ITEM;
069    }
070
071    /** {@inheritDoc} */
072    @Override
073    public String getShortDescription(Locale locale) {
074        return Bundle.getMessage(locale, "StringActionMemory_Short");
075    }
076
077    /** {@inheritDoc} */
078    @Override
079    public String getLongDescription(Locale locale) {
080        return Bundle.getMessage(locale, "StringActionMemory_Long", _selectNamedBean.getDescription(locale));
081    }
082
083    /** {@inheritDoc} */
084    @Override
085    public void setup() {
086        // Do nothing
087    }
088
089    /** {@inheritDoc} */
090    @Override
091    public void registerListenersForThisClass() {
092        _selectNamedBean.registerListeners();
093    }
094
095    /** {@inheritDoc} */
096    @Override
097    public void unregisterListenersForThisClass() {
098        _selectNamedBean.unregisterListeners();
099    }
100
101    /** {@inheritDoc} */
102    @Override
103    public void disposeMe() {
104    }
105
106    /** {@inheritDoc} */
107    @Override
108    public void getUsageDetail(int level, NamedBean bean, List<NamedBeanUsageReport> report, NamedBean cdl) {
109        _selectNamedBean.getUsageDetail(level, bean, report, cdl, this, LogixNG_SelectNamedBean.Type.Action);
110    }
111
112    /** {@inheritDoc} */
113    @Override
114    public void propertyChange(PropertyChangeEvent evt) {
115        getConditionalNG().execute();
116    }
117
118//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(StringActionMemory.class);
119
120}