001package jmri.jmrit.logixng.actions;
002
003import java.util.Locale;
004import java.util.Map;
005
006import jmri.InstanceManager;
007import jmri.ShutDownManager;
008import jmri.jmrit.logixng.*;
009
010/**
011 * This action sets the state of a turnout.
012 * 
013 * @author Daniel Bergqvist Copyright 2018
014 */
015public class ShutdownComputer extends AbstractDigitalAction {
016
017    private Operation _operation = Operation.ShutdownJMRI;
018    
019    
020    public ShutdownComputer(String sys, String user)
021            throws BadUserNameException, BadSystemNameException {
022        super(sys, user);
023    }
024    
025    @Override
026    public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) {
027        DigitalActionManager manager = InstanceManager.getDefault(DigitalActionManager.class);
028        String sysName = systemNames.get(getSystemName());
029        String userName = userNames.get(getSystemName());
030        if (sysName == null) sysName = manager.getAutoSystemName();
031        ShutdownComputer copy = new ShutdownComputer(sysName, userName);
032        copy.setComment(getComment());
033        copy.setOperation(_operation);
034        return manager.registerAction(copy);
035    }
036    
037    public void setOperation(Operation operation) {
038        _operation = operation;
039    }
040    
041    public Operation getOperation() {
042        return _operation;
043    }
044    
045    /** {@inheritDoc} */
046    @Override
047    public Category getCategory() {
048        return Category.OTHER;
049    }
050
051    /** {@inheritDoc} */
052    @Override
053    public void execute() {
054        jmri.util.ThreadingUtil.runOnGUI(() -> {
055            switch (_operation) {
056                case ShutdownComputer:
057                    InstanceManager.getDefault(ShutDownManager.class).shutdownOS();
058                    break;
059                    
060                case RebootComputer:
061                    InstanceManager.getDefault(ShutDownManager.class).restartOS();
062                    break;
063                    
064                case ShutdownJMRI:
065                    InstanceManager.getDefault(ShutDownManager.class).shutdown();
066                    break;
067                    
068                case RebootJMRI:
069                    InstanceManager.getDefault(ShutDownManager.class).restart();
070                    break;
071                    
072                default:
073                    throw new RuntimeException("_operation has invalid value: "+_operation.name());
074            }
075        });
076
077        // If we are here, shutdown has failed
078        log.error("Shutdown failed");  // NOI18N
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", _operation._text);
099    }
100    
101    /** {@inheritDoc} */
102    @Override
103    public void setup() {
104        // Do nothing
105    }
106    
107    /** {@inheritDoc} */
108    @Override
109    public void registerListenersForThisClass() {
110    }
111    
112    /** {@inheritDoc} */
113    @Override
114    public void unregisterListenersForThisClass() {
115    }
116    
117    /** {@inheritDoc} */
118    @Override
119    public void disposeMe() {
120    }
121    
122    
123    
124    public enum Operation {
125        ShutdownComputer(Bundle.getMessage("ShutdownComputer_ShutdownComputer")),
126        RebootComputer(Bundle.getMessage("ShutdownComputer_RebootComputer")),
127        ShutdownJMRI(Bundle.getMessage("ShutdownComputer_ShutdownJMRI")),
128        RebootJMRI(Bundle.getMessage("ShutdownComputer_RebootJMRI"));
129        
130        private final String _text;
131        
132        private Operation(String text) {
133            this._text = text;
134        }
135        
136        @Override
137        public String toString() {
138            return _text;
139        }
140        
141    }
142    
143    
144    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ShutdownComputer.class);
145}