001package jmri.jmrit.operations.setup.backup;
002
003import java.awt.event.ActionEvent;
004import java.io.IOException;
005
006import javax.swing.AbstractAction;
007
008import jmri.InstanceManager;
009import jmri.jmrit.operations.OperationsManager;
010import jmri.jmrit.operations.OperationsXml;
011import jmri.util.swing.*;
012
013/**
014 * Swing action to load the operation demo files.
015 *
016 * @author Bob Jacobsen Copyright (C) 2001
017 * @author Daniel Boudreau Copyright (C) 2010
018 * @author Gregory Madsen Copyright (C) 2012
019 */
020public class ResetAction extends AbstractAction {
021
022    public ResetAction() {
023        super(Bundle.getMessage("ResetOperations"));
024    }
025
026    @Override
027    public void actionPerformed(ActionEvent e) {
028        // check to see if files are dirty
029        if (OperationsXml.areFilesDirty()) {
030            if (JmriJOptionPane.showConfirmDialog(null, Bundle.getMessage("OperationsFilesModified"),
031                    Bundle.getMessage("SaveOperationFiles"), JmriJOptionPane.YES_NO_OPTION) == JmriJOptionPane.YES_OPTION) {
032                OperationsXml.save();
033            }
034        }
035
036        int results = JmriJOptionPane.showConfirmDialog(null, Bundle.getMessage("AreYouSureDeleteAll"),
037                Bundle.getMessage("ResetOperations"), JmriJOptionPane.OK_CANCEL_OPTION);
038        if (results != JmriJOptionPane.OK_OPTION) {
039            return;
040        }
041
042        AutoBackup backup = InstanceManager.getDefault(AutoBackup.class);
043
044        try {
045            backup.autoBackup();
046
047            // now delete the operations files
048            backup.deleteOperationsFiles();
049
050            // now deregister shut down task
051            // If Trains window was opened, then task is active
052            // otherwise it is normal to not have the task running
053            InstanceManager.getDefault(OperationsManager.class).setShutDownTask(null);
054
055            JmriJOptionPane.showMessageDialog(null, Bundle.getMessage("YouMustRestartAfterReset"),
056                    Bundle.getMessage("ResetSuccessful"), JmriJOptionPane.INFORMATION_MESSAGE);
057
058            try {
059                InstanceManager.getDefault(jmri.ShutDownManager.class).restart();
060            } catch (Exception er) {
061                log.error("Continuing after error in handleRestart", er);
062            }
063
064
065        } catch (IOException ex) {
066            UnexpectedExceptionContext context = new UnexpectedExceptionContext(ex,
067                    "Deleting Operations files"); // NOI18N
068            ExceptionDisplayFrame.displayExceptionDisplayFrame(null, context);
069        }
070    }
071
072    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ResetAction.class);
073}
074
075