001package jmri.jmrit.operations.setup;
002
003import java.awt.event.ActionEvent;
004import java.io.File;
005import java.io.IOException;
006
007import javax.swing.AbstractAction;
008import javax.swing.JFileChooser;
009
010import jmri.InstanceManager;
011import jmri.jmrit.operations.OperationsManager;
012import jmri.jmrit.operations.OperationsXml;
013import jmri.util.swing.*;
014
015/**
016 * Swing action to restore operation files from a directory selected by the
017 * user.
018 *
019 * @author Daniel Boudreau Copyright (C) 2011
020 * @author Gregory Madsen Copyright (C) 2012
021 */
022public class RestoreFilesAction extends AbstractAction {
023
024    public RestoreFilesAction() {
025        super(Bundle.getMessage("Restore"));
026    }
027
028    @Override
029    public void actionPerformed(ActionEvent e) {
030        restore();
031    }
032
033    private void restore() {
034        // This method can restore files from any directory selected by the File
035        // Chooser.
036
037        // check to see if files are dirty
038        if (OperationsXml.areFilesDirty()) {
039            if (JmriJOptionPane
040                    .showConfirmDialog(
041                            null,
042                            Bundle.getMessage("OperationsFilesModified"),
043                            Bundle.getMessage("SaveOperationFiles"),
044                            JmriJOptionPane.YES_NO_OPTION) == JmriJOptionPane.YES_OPTION) {
045                OperationsXml.save();
046            }
047        }
048
049        // first backup the users data in case they forgot
050        BackupBase backup = new DefaultBackup();
051
052        // get file to write to
053        JFileChooser fc = new jmri.util.swing.JmriJFileChooser(backup.getBackupRoot());
054        fc.addChoosableFileFilter(new FileFilter());
055        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
056
057        int retVal = fc.showOpenDialog(null);
058        if (retVal != JFileChooser.APPROVE_OPTION) {
059            return; // Canceled
060        }
061        if (fc.getSelectedFile() == null) {
062            return; // Canceled
063        }
064        // now backup files
065        AutoBackup autoBackup = new AutoBackup();
066
067        try {
068            autoBackup.autoBackup();
069
070            File directory = fc.getSelectedFile();
071
072            // now delete the current operations files in case the restore isn't a full set of files
073            backup.deleteOperationsFiles();
074
075            backup.restoreFilesFromDirectory(directory);
076
077            JmriJOptionPane.showMessageDialog(null,
078                    Bundle.getMessage("YouMustRestartAfterRestore"),
079                    Bundle.getMessage("RestoreSuccessful"), JmriJOptionPane.INFORMATION_MESSAGE);
080
081            // now deregister shut down task
082            // If Trains window was opened, then task is active
083            // otherwise it is normal to not have the task running
084            InstanceManager.getDefault(OperationsManager.class).setShutDownTask(null);
085
086            try {
087                InstanceManager.getDefault(jmri.ShutDownManager.class).restart();
088            } catch (Exception er) {
089                log.error("Continuing after error in handleRestart", er);
090            }
091
092        } catch (IOException ex) {
093            ExceptionContext context = new ExceptionContext(ex,
094                    Bundle.getMessage("RestoreDialog.restore.files"),
095                    Bundle.getMessage("RestoreDialog.makeSure"));
096            ExceptionDisplayFrame.displayExceptionDisplayFrame(null, context);
097        }
098    }
099
100    private static class FileFilter extends javax.swing.filechooser.FileFilter {
101
102        @Override
103        public boolean accept(File f) {
104            if (f.isDirectory()) {
105                return true;
106            }
107            String name = f.getName();
108            if (name.matches(".*\\.xml")) // NOI18N
109            {
110                return true;
111            } else {
112                return false;
113            }
114        }
115
116        @Override
117        public String getDescription() {
118            return Bundle.getMessage("BackupFolders");
119        }
120    }
121
122    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(RestoreFilesAction.class);
123}