From f92024670a97156f6167bb22f5c776a8ab72e35d Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Mon, 22 Oct 2018 10:25:09 +0100 Subject: [PATCH] JAL-3141 Tidying --- src/jalview/io/JalviewFileChooser.java | 36 ++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/jalview/io/JalviewFileChooser.java b/src/jalview/io/JalviewFileChooser.java index 983800b..052cf68 100755 --- a/src/jalview/io/JalviewFileChooser.java +++ b/src/jalview/io/JalviewFileChooser.java @@ -353,18 +353,31 @@ public class JalviewFileChooser extends JFileChooser } rollBackupFiles(ourselectedFile); - } return ret; } // attempts to roll backup files for this file (before overwriting). Returns - // true if it rolled all the files, false otherwise. - private static boolean rollBackupFiles(File file) + // true if it rolled all the files it was supposed to, false otherwise. + public static boolean rollBackupFiles(String filename) { + File file = new File(filename); + return rollBackupFiles(file); + } + + public static boolean rollBackupFiles(File file) + { + return rollBackupFiles(file, true, "-bak-%n", 4); + } - if (!file.exists()) + public static boolean rollBackupFiles(File file, boolean defaultDo, + String defaultSuffix, int defaultMax) + { + + if (!(file.exists() + && jalview.bin.Cache.getDefault("BACKUP_FILES", + defaultDo))) { // nothing to do return true; @@ -373,8 +386,9 @@ public class JalviewFileChooser extends JFileChooser // split filename up to insert suffix template in the right place. template // and backupMax can be set in .jalview_properties String backupSuffixTemplate = jalview.bin.Cache - .getDefault("BACKUP_SUFFIX", "-bak-%n"); - int backupMax = jalview.bin.Cache.getDefault("BACKUP_MAX", 10); + .getDefault("BACKUP_SUFFIX", defaultSuffix); + int backupMax = jalview.bin.Cache.getDefault("BACKUP_ROLL_MAX", + defaultMax); String dir = ""; try { @@ -418,10 +432,10 @@ public class JalviewFileChooser extends JFileChooser continue; } - if (n == backupMax-1) + if (n == backupMax) { // Move the max backup to /tmp instead of deleting (Just In // Case) - String tmpfile = "tmp-" + backupfilename; + String tmpfile = "tmp-" + backupfile_n.getName(); try { File tmpFile = File.createTempFile(tmpfile, ".tmp"); @@ -434,7 +448,11 @@ public class JalviewFileChooser extends JFileChooser } else { - ret = ret && backupfile_n.renameTo(lastfile); + // Just In Case + if (lastfile != null) + { + ret = ret && backupfile_n.renameTo(lastfile); + } } lastfile = backupfile_n; -- 1.7.10.2