X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FBackupFiles.java;h=e913fbc3d4533af3dbdd83dcb4d1508e2c8b21d8;hb=656e362ece73c630f032d8bf30322143a297d92e;hp=aafe8f29de6eac2fc3b40da1c11d7f6fcfa507f3;hpb=c860648463c56ba9f75e9d239e2f56edea8e31e7;p=jalview.git diff --git a/src/jalview/io/BackupFiles.java b/src/jalview/io/BackupFiles.java index aafe8f2..e913fbc 100644 --- a/src/jalview/io/BackupFiles.java +++ b/src/jalview/io/BackupFiles.java @@ -8,8 +8,8 @@ import jalview.util.MessageManager; import java.io.File; import java.io.IOException; import java.util.Arrays; -import java.util.Map; import java.util.HashMap; +import java.util.Map; import java.util.TreeMap; /* @@ -44,7 +44,8 @@ public class BackupFiles public static final String REVERSE_ORDER = NS + "_REVERSE_ORDER"; - public static final String CONFIRM_DELETE_OLD = NS + "_CONFIRM_DELETE_OLD"; + public static final String CONFIRM_DELETE_OLD = NS + + "_CONFIRM_DELETE_OLD"; private static final String DEFAULT_TEMP_FILE = "jalview_temp_file_" + NS; @@ -95,10 +96,8 @@ public class BackupFiles this(file, ".v" + NUM_PLACEHOLDER, false, 4, 3, false); } - public BackupFiles(File file, - String defaultSuffix, boolean defaultNoMax, int defaultMax, - int defaultDigits, - boolean defaultReverseOrder) + public BackupFiles(File file, String defaultSuffix, boolean defaultNoMax, + int defaultMax, int defaultDigits, boolean defaultReverseOrder) { classInit(); this.file = file; @@ -205,7 +204,6 @@ public class BackupFiles return tempFile.renameTo(file); } - // roll the backupfiles public boolean rollBackupFiles() { @@ -238,14 +236,14 @@ public class BackupFiles // Create/move backups up one File[] oldFilesToDelete = null; - + // find existing backup files BackupFilenameFilter bff = new BackupFilenameFilter(basename, suffix, digits); File[] backupFiles = dirFile.listFiles(bff); int nextIndexNum = 0; String confirmDeleteExtraInfo = null; - + if (backupFiles.length == 0) { // No other backup files. Just need to move existing file to backupfile_1 @@ -253,7 +251,8 @@ public class BackupFiles } else { - TreeMap bfTreeMap = sortBackupFilesAsTreeMap(backupFiles, basename); + TreeMap bfTreeMap = sortBackupFilesAsTreeMap( + backupFiles, basename); if (reverseOrder) { @@ -294,11 +293,12 @@ public class BackupFiles // if (m == 0 && backupfile_n.exists()) if ((!noMax) && n == tempMax && backupfile_n.exists()) { - // move the largest (max) rolled file to a temp file and add to the delete list + // move the largest (max) rolled file to a temp file and add to the + // delete list try { File temp = File.createTempFile(backupfilename, TEMP_FILE_EXT, - dirFile); + dirFile); backupfile_n.renameTo(temp); oldFilesToDelete = new File[] { temp }; @@ -330,7 +330,6 @@ public class BackupFiles // version style numbering (with earliest file deletion if max files // reached) - bfTreeMap.values().toArray(backupFiles); // noMax == true means keep all backup files @@ -361,7 +360,9 @@ public class BackupFiles return ret; } - private void deleteOldFiles(File[] oldFilesToDelete, String confirmDeleteExtraInfo) { + private void deleteOldFiles(File[] oldFilesToDelete, + String confirmDeleteExtraInfo) + { if (oldFilesToDelete != null && oldFilesToDelete.length > 0) { // delete old backup/version files @@ -411,32 +412,38 @@ public class BackupFiles } } - private TreeMap sortBackupFilesAsTreeMap(File[] backupFiles, String basename) { - // sort the backup files (based on integer found in the suffix) using a - // precomputed Hashmap for speed - Map bfHashMap = new HashMap<>(); - for (int i = 0; i < backupFiles.length; i++) - { - File f = backupFiles[i]; - BackupFilenameParts bfp = new BackupFilenameParts(f, basename, suffix, digits); - bfHashMap.put(bfp.indexNum(), f); - } - TreeMap bfTreeMap = new TreeMap<>(); - bfTreeMap.putAll(bfHashMap); - return bfTreeMap; + private TreeMap sortBackupFilesAsTreeMap(File[] backupFiles, + String basename) + { + // sort the backup files (based on integer found in the suffix) using a + // precomputed Hashmap for speed + Map bfHashMap = new HashMap<>(); + for (int i = 0; i < backupFiles.length; i++) + { + File f = backupFiles[i]; + BackupFilenameParts bfp = new BackupFilenameParts(f, basename, suffix, + digits); + bfHashMap.put(bfp.indexNum(), f); + } + TreeMap bfTreeMap = new TreeMap<>(); + bfTreeMap.putAll(bfHashMap); + return bfTreeMap; } public boolean rollBackupsAndRenameTempFile() { boolean write = this.getWriteSuccess(); - + boolean roll = false; - if (write) { + if (write) + { roll = this.rollBackupFiles(); - } else { + } + else + { return false; } - + /* * Not sure that this confirmation is desirable. By this stage the new file is * already written successfully, but something (e.g. disk full) has happened while @@ -448,7 +455,8 @@ public class BackupFiles int confirm = JvOptionPane.showConfirmDialog(Desktop.desktop, MessageManager.getString( "label.backupfiles_confirm_save_file_backupfiles_roll_wrong"), - MessageManager.getString("label.backupfiles_confirm_save_file"), + MessageManager + .getString("label.backupfiles_confirm_save_file"), JvOptionPane.YES_NO_OPTION, JvOptionPane.WARNING_MESSAGE); if (confirm == JvOptionPane.YES_OPTION) @@ -467,8 +475,7 @@ public class BackupFiles } public static TreeMap getBackupFilesAsTreeMap( - String fileName, - String suffix, int digits) + String fileName, String suffix, int digits) { File[] backupFiles = null; @@ -489,11 +496,12 @@ public class BackupFiles String filename = file.getName(); String basename = filename; - + // find existing backup files - BackupFilenameFilter bff = new BackupFilenameFilter(basename, suffix, digits); + BackupFilenameFilter bff = new BackupFilenameFilter(basename, suffix, + digits); backupFiles = dirFile.listFiles(bff); // is clone needed? - + // sort the backup files (based on integer found in the suffix) using a // precomputed Hashmap for speed Map bfHashMap = new HashMap<>(); @@ -510,6 +518,4 @@ public class BackupFiles return bfTreeMap; } - } -