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.jmrit.operations.OperationsXml; 011import jmri.util.swing.JmriJOptionPane; 012 013/** 014 * Swing action to backup operation files to a directory selected by the user. 015 * 016 * @author Daniel Boudreau Copyright (C) 2011 017 * @author Gregory Madsen Copyright (C) 2012 018 */ 019public class BackupFilesAction extends AbstractAction { 020 021 public BackupFilesAction() { 022 super(Bundle.getMessage("Backup")); 023 } 024 025 @Override 026 public void actionPerformed(ActionEvent e) { 027 backUp(); 028 } 029 030 private void backUp() { 031 // check to see if files are dirty 032 if (OperationsXml.areFilesDirty()) { 033 if (JmriJOptionPane.showConfirmDialog(null, 034 Bundle.getMessage("OperationsFilesModified"), 035 Bundle.getMessage("SaveOperationFiles"), 036 JmriJOptionPane.YES_NO_OPTION) == JmriJOptionPane.YES_OPTION) { 037 OperationsXml.save(); 038 } 039 } 040 BackupBase backup = new DefaultBackup(); 041 042 // get directory to write to 043 JFileChooser fc = new jmri.util.swing.JmriJFileChooser(backup.getBackupRoot()); 044 fc.addChoosableFileFilter(new FileFilter()); 045 046 File fs = new File(backup.suggestBackupSetName()); 047 fc.setSelectedFile(fs); 048 049 int retVal = fc.showSaveDialog(null); 050 if (retVal != JFileChooser.APPROVE_OPTION || fc.getSelectedFile() == null) { 051 return; // Canceled 052 } 053 File directory = fc.getSelectedFile(); 054 055 // Fix this later....... UGH!! 056 try { 057 backup.backupFilesToDirectory(directory); 058 } catch (IOException ex) { 059 log.error("backup failed: {}", ex.getLocalizedMessage()); 060 } 061 } 062 063 private static class FileFilter extends javax.swing.filechooser.FileFilter { 064 065 @Override 066 public boolean accept(File f) { 067 if (f.isDirectory()) { 068 return true; 069 } 070 String name = f.getName(); 071 return name.matches(".*\\.xml"); // NOI18N 072 } 073 074 @Override 075 public String getDescription() { 076 return Bundle.getMessage("BackupFolders"); 077 } 078 } 079 080 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(BackupFilesAction.class); 081 082} 083 084