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