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