001package jmri.jmrit.logixng.actions;
002
003import java.util.*;
004
005import jmri.InstanceManager;
006import jmri.jmrit.logixng.*;
007
008/**
009 * This action sets some local variables. It is used internally.
010 *
011 * @author Daniel Bergqvist Copyright 2025
012 */
013public class SetLocalVariables extends AbstractDigitalAction {
014
015    private final Map<String, Object> _variablesWithValues = new HashMap<>();
016
017    public SetLocalVariables(String sysName)
018            throws BadUserNameException {
019        super(sysName, null);
020    }
021
022    public SetLocalVariables(String sysName, String userName)
023            throws BadUserNameException {
024        super(sysName, userName);
025    }
026
027    public Map<String, Object> getMap() {
028        return _variablesWithValues;
029    }
030
031    @Override
032    public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) {
033        DigitalActionManager manager = InstanceManager.getDefault(DigitalActionManager.class);
034        String sysName = systemNames.get(getSystemName());
035        String userName = userNames.get(getSystemName());
036        if (sysName == null) sysName = manager.getAutoSystemName();
037        DigitalActionBean copy = new SetLocalVariables(sysName, userName);
038        copy.setComment(getComment());
039        return manager.registerAction(copy);
040    }
041
042    /** {@inheritDoc} */
043    @Override
044    public LogixNG_Category getCategory() {
045        return LogixNG_Category.OTHER;
046    }
047
048    /** {@inheritDoc} */
049    @Override
050    public void execute() {
051        ConditionalNG cng = this.getConditionalNG();
052        for (var entry : _variablesWithValues.entrySet()) {
053            cng.getSymbolTable().setValue(entry.getKey(), entry.getValue());
054        }
055    }
056
057    @Override
058    public FemaleSocket getChild(int index) throws IllegalArgumentException, UnsupportedOperationException {
059        throw new UnsupportedOperationException("Not supported.");
060    }
061
062    @Override
063    public int getChildCount() {
064        return 0;
065    }
066
067    @Override
068    public String getShortDescription(Locale locale) {
069        return Bundle.getMessage(locale, "SetLocalVariables_Short");
070    }
071
072    @Override
073    public String getLongDescription(Locale locale) {
074        return Bundle.getMessage(locale, "SetLocalVariables_Long");
075    }
076
077    /** {@inheritDoc} */
078    @Override
079    public void setup() {
080    }
081
082    /** {@inheritDoc} */
083    @Override
084    public void registerListenersForThisClass() {
085    }
086
087    /** {@inheritDoc} */
088    @Override
089    public void unregisterListenersForThisClass() {
090    }
091
092    /** {@inheritDoc} */
093    @Override
094    public void disposeMe() {
095    }
096
097}