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