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