001package jmri.jmrit.logixng.actions;
002
003import java.beans.PropertyChangeEvent;
004import java.beans.PropertyVetoException;
005import java.beans.VetoableChangeListener;
006import java.util.*;
007
008import javax.annotation.CheckForNull;
009import javax.annotation.Nonnull;
010
011import jmri.*;
012import jmri.jmrit.logixng.*;
013
014/**
015 * Sets a Memory.
016 *
017 * @author Daniel Bergqvist Copyright 2018
018 */
019public class StringActionMemory extends AbstractStringAction
020        implements VetoableChangeListener {
021
022    private NamedBeanHandle<Memory> _memoryHandle;
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) {
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        if (_memoryHandle != null) copy.setMemory(_memoryHandle);
037        return manager.registerAction(copy);
038    }
039
040    public void setMemory(@Nonnull String memoryName) {
041        assertListenersAreNotRegistered(log, "setMemory");
042        Memory memory = InstanceManager.getDefault(MemoryManager.class).getMemory(memoryName);
043        if (memory != null) {
044            setMemory(memory);
045        } else {
046            removeMemory();
047            log.error("memory \"{}\" is not found", memoryName);
048        }
049    }
050
051    public void setMemory(@Nonnull NamedBeanHandle<Memory> handle) {
052        assertListenersAreNotRegistered(log, "setMemory");
053        _memoryHandle = handle;
054        InstanceManager.memoryManagerInstance().addVetoableChangeListener(this);
055    }
056
057    public void setMemory(@Nonnull Memory memory) {
058        assertListenersAreNotRegistered(log, "setMemory");
059        setMemory(InstanceManager.getDefault(NamedBeanHandleManager.class)
060                .getNamedBeanHandle(memory.getDisplayName(), memory));
061    }
062
063    public void removeMemory() {
064        assertListenersAreNotRegistered(log, "setMemory");
065        if (_memoryHandle != null) {
066            InstanceManager.memoryManagerInstance().removeVetoableChangeListener(this);
067            _memoryHandle = null;
068        }
069    }
070
071    public NamedBeanHandle<Memory> getMemory() {
072        return _memoryHandle;
073    }
074
075    /** {@inheritDoc} */
076    @Override
077    public void setValue(String value) {
078        if (_memoryHandle != null) {
079            _memoryHandle.getBean().setValue(value);
080        }
081    }
082
083    @Override
084    public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
085        if ("CanDelete".equals(evt.getPropertyName())) { // No I18N
086            if (evt.getOldValue() instanceof Memory) {
087                if (evt.getOldValue().equals(getMemory().getBean())) {
088                    PropertyChangeEvent e = new PropertyChangeEvent(this, "DoNotDelete", null, null);
089                    throw new PropertyVetoException(Bundle.getMessage("StringMemory_MemoryInUseMemoryActionVeto", getDisplayName()), e); // NOI18N
090                }
091            }
092        } else if ("DoDelete".equals(evt.getPropertyName())) { // No I18N
093            if (evt.getOldValue() instanceof Memory) {
094                if (evt.getOldValue().equals(getMemory().getBean())) {
095                    removeMemory();
096                }
097            }
098        }
099    }
100
101    /** {@inheritDoc} */
102    @Override
103    public FemaleSocket getChild(int index) throws IllegalArgumentException, UnsupportedOperationException {
104        throw new UnsupportedOperationException("Not supported.");
105    }
106
107    /** {@inheritDoc} */
108    @Override
109    public int getChildCount() {
110        return 0;
111    }
112
113    /** {@inheritDoc} */
114    @Override
115    public Category getCategory() {
116        return Category.ITEM;
117    }
118
119    /** {@inheritDoc} */
120    @Override
121    public String getShortDescription(Locale locale) {
122        return Bundle.getMessage(locale, "StringActionMemory_Short");
123    }
124
125    /** {@inheritDoc} */
126    @Override
127    public String getLongDescription(Locale locale) {
128        if (_memoryHandle != null) {
129            return Bundle.getMessage(locale, "StringActionMemory_Long", _memoryHandle.getBean().getDisplayName());
130        } else {
131            return Bundle.getMessage(locale, "StringActionMemory_Long", "none");
132        }
133    }
134
135    /** {@inheritDoc} */
136    @Override
137    public void setup() {
138        // Do nothing
139    }
140
141    /** {@inheritDoc} */
142    @Override
143    public void registerListenersForThisClass() {
144    }
145
146    /** {@inheritDoc} */
147    @Override
148    public void unregisterListenersForThisClass() {
149    }
150
151    /** {@inheritDoc} */
152    @Override
153    public void disposeMe() {
154    }
155
156    /** {@inheritDoc} */
157    @Override
158    public void getUsageDetail(int level, NamedBean bean, List<NamedBeanUsageReport> report, NamedBean cdl) {
159        log.debug("getUsageReport :: StringActionMemory: bean = {}, report = {}", cdl, report);
160        if (getMemory() != null && bean.equals(getMemory().getBean())) {
161            report.add(new NamedBeanUsageReport("LogixNGAction", cdl, getLongDescription()));
162        }
163    }
164
165    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(StringActionMemory.class);
166
167}