001package jmri.jmrit.logixng.actions;
002
003import java.util.*;
004
005import jmri.*;
006import jmri.Logix;
007import jmri.jmrit.logixng.*;
008import jmri.jmrit.logixng.util.*;
009import jmri.jmrit.logixng.util.parser.*;
010import jmri.util.ThreadingUtil;
011
012/**
013 * This action enables/disables a Logix.
014 *
015 * @author Daniel Bergqvist Copyright 2021
016 */
017public class EnableLogix extends AbstractDigitalAction {
018
019    private final LogixNG_SelectNamedBean<Logix> _selectNamedBean =
020            new LogixNG_SelectNamedBean<>(
021                    this, Logix.class, InstanceManager.getDefault(LogixManager.class));
022
023    private final LogixNG_SelectEnum<Operation> _selectEnum =
024            new LogixNG_SelectEnum<>(this, Operation.values(), Operation.Disable);
025
026
027    public EnableLogix(String sys, String user)
028            throws BadUserNameException, BadSystemNameException {
029        super(sys, user);
030    }
031
032    @Override
033    public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) throws ParserException {
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        EnableLogix copy = new EnableLogix(sysName, userName);
039        copy.setComment(getComment());
040        _selectNamedBean.copy(copy._selectNamedBean);
041        _selectEnum.copy(copy._selectEnum);
042        return manager.registerAction(copy);
043    }
044
045    public LogixNG_SelectNamedBean<Logix> getSelectNamedBean() {
046        return _selectNamedBean;
047    }
048
049    public LogixNG_SelectEnum<Operation> getSelectEnum() {
050        return _selectEnum;
051    }
052
053    /** {@inheritDoc} */
054    @Override
055    public LogixNG_Category getCategory() {
056        return LogixNG_Category.ITEM;
057    }
058
059    /** {@inheritDoc} */
060    @Override
061    public void execute() throws JmriException {
062        Logix logix = _selectNamedBean.evaluateNamedBean(getConditionalNG());
063
064        if (logix == null) return;
065
066        Operation lock = _selectEnum.evaluateEnum(getConditionalNG());
067
068        // Variables used in lambda must be effectively final
069        Operation theLock = lock;
070
071        ThreadingUtil.runOnLayoutWithJmriException(() -> {
072            logix.setEnabled(theLock == Operation.Enable);
073        });
074    }
075
076    @Override
077    public FemaleSocket getChild(int index) throws IllegalArgumentException, UnsupportedOperationException {
078        throw new UnsupportedOperationException("Not supported.");
079    }
080
081    @Override
082    public int getChildCount() {
083        return 0;
084    }
085
086    @Override
087    public String getShortDescription(Locale locale) {
088        return Bundle.getMessage(locale, "EnableLogix_Short");
089    }
090
091    @Override
092    public String getLongDescription(Locale locale) {
093        String namedBean = _selectNamedBean.getDescription(locale);
094        String state = _selectEnum.getDescription(locale);
095
096        return Bundle.getMessage(locale, "EnableLogix_Long", namedBean, state);
097    }
098
099    /** {@inheritDoc} */
100    @Override
101    public void setup() {
102        // Do nothing
103    }
104
105    /** {@inheritDoc} */
106    @Override
107    public void disposeMe() {
108    }
109
110
111    public enum Operation {
112        Enable(Bundle.getMessage("EnableLogix_Enable")),
113        Disable(Bundle.getMessage("EnableLogix_Disable"));
114
115        private final String _text;
116
117        private Operation(String text) {
118            this._text = text;
119        }
120
121        @Override
122        public String toString() {
123            return _text;
124        }
125
126    }
127
128//    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(EnableLogix.class);
129
130}