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