001package jmri.jmrit.logixng.actions;
002
003import java.beans.PropertyChangeEvent;
004import java.beans.PropertyChangeListener;
005import java.util.Locale;
006import java.util.Map;
007
008import jmri.*;
009import jmri.jmrit.logixng.*;
010import jmri.jmrit.logixng.util.LogixNG_SelectEnum;
011import jmri.jmrit.logixng.util.parser.ParserException;
012
013/**
014 * This action sets the state of a turnout.
015 *
016 * @author Daniel Bergqvist Copyright 2018
017 */
018public class ShutdownComputer extends AbstractDigitalAction
019        implements PropertyChangeListener {
020
021    private final LogixNG_SelectEnum<Operation> _selectEnum =
022            new LogixNG_SelectEnum<>(this, Operation.values(), Operation.ShutdownJMRI, this);
023
024
025    public ShutdownComputer(String sys, String user)
026            throws BadUserNameException, BadSystemNameException {
027        super(sys, user);
028    }
029
030    @Override
031    public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) throws ParserException {
032        DigitalActionManager manager = InstanceManager.getDefault(DigitalActionManager.class);
033        String sysName = systemNames.get(getSystemName());
034        String userName = userNames.get(getSystemName());
035        if (sysName == null) sysName = manager.getAutoSystemName();
036        ShutdownComputer copy = new ShutdownComputer(sysName, userName);
037        copy.setComment(getComment());
038        _selectEnum.copy(copy._selectEnum);
039        return manager.registerAction(copy);
040    }
041
042    public LogixNG_SelectEnum<Operation> getSelectEnum() {
043        return _selectEnum;
044    }
045
046    /** {@inheritDoc} */
047    @Override
048    public Category getCategory() {
049        return Category.OTHER;
050    }
051
052    /** {@inheritDoc} */
053    @Override
054    public void execute() throws JmriException {
055        Operation operation = _selectEnum.evaluateEnum(getConditionalNG());
056
057        jmri.util.ThreadingUtil.runOnGUI(() -> {
058            switch (operation) {
059                case ShutdownComputer:
060                    InstanceManager.getDefault(ShutDownManager.class).shutdownOS();
061                    break;
062
063                case RebootComputer:
064                    InstanceManager.getDefault(ShutDownManager.class).restartOS();
065                    break;
066
067                case ShutdownJMRI:
068                    InstanceManager.getDefault(ShutDownManager.class).shutdown();
069                    break;
070
071                case RebootJMRI:
072                    InstanceManager.getDefault(ShutDownManager.class).restart();
073                    break;
074
075                default:
076                    throw new RuntimeException("_operation has invalid value: " + operation.name());
077            }
078        });
079    }
080
081    @Override
082    public FemaleSocket getChild(int index) throws IllegalArgumentException, UnsupportedOperationException {
083        throw new UnsupportedOperationException("Not supported.");
084    }
085
086    @Override
087    public int getChildCount() {
088        return 0;
089    }
090
091    @Override
092    public String getShortDescription(Locale locale) {
093        return Bundle.getMessage(locale, "ShutdownComputer_Short");
094    }
095
096    @Override
097    public String getLongDescription(Locale locale) {
098        return Bundle.getMessage(locale, "ShutdownComputer_Long", _selectEnum.getDescription(locale));
099    }
100
101    /** {@inheritDoc} */
102    @Override
103    public void setup() {
104        // Do nothing
105    }
106
107    /** {@inheritDoc} */
108    @Override
109    public void registerListenersForThisClass() {
110        _selectEnum.registerListeners();
111    }
112
113    /** {@inheritDoc} */
114    @Override
115    public void unregisterListenersForThisClass() {
116        _selectEnum.unregisterListeners();
117    }
118
119    /** {@inheritDoc} */
120    @Override
121    public void disposeMe() {
122    }
123
124
125
126    public enum Operation {
127        ShutdownComputer(Bundle.getMessage("ShutdownComputer_ShutdownComputer")),
128        RebootComputer(Bundle.getMessage("ShutdownComputer_RebootComputer")),
129        ShutdownJMRI(Bundle.getMessage("ShutdownComputer_ShutdownJMRI")),
130        RebootJMRI(Bundle.getMessage("ShutdownComputer_RebootJMRI"));
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(ShutdownComputer.class);
152}