import java.io.File;
import java.io.IOException;
import java.util.Arrays;
+import java.util.Map;
import java.util.HashMap;
import java.util.TreeMap;
{
// labels for saved params in Cache and .jalview_properties
- private static String NS = "BACKUPFILES";
+ private static final String NS = "BACKUPFILES";
- public static String ENABLED = NS + "_ENABLED";
+ public static final String ENABLED = NS + "_ENABLED";
- public static String SUFFIX = NS + "_SUFFIX";
+ public static final String SUFFIX = NS + "_SUFFIX";
- public static String NO_MAX = NS + "_NO_MAX";
+ public static final String NO_MAX = NS + "_NO_MAX";
- public static String ROLL_MAX = NS + "_ROLL_MAX";
+ public static final String ROLL_MAX = NS + "_ROLL_MAX";
- public static String SUFFIX_DIGITS = NS + "_SUFFIX_DIGITS";
+ public static final String SUFFIX_DIGITS = NS + "_SUFFIX_DIGITS";
- protected static String NUM_PLACEHOLDER = "%n";
+ public static final String NUM_PLACEHOLDER = "%n";
- public static String REVERSE_ORDER = NS + "_REVERSE_ORDER";
+ public static final String REVERSE_ORDER = NS + "_REVERSE_ORDER";
- public static String CONFIRM_DELETE_OLD = NS + "_CONFIRM_DELETE_OLD";
+ public static final String CONFIRM_DELETE_OLD = NS + "_CONFIRM_DELETE_OLD";
- private static String DEFAULT_TEMP_FILE = "jalview_temp_file_" + NS;
+ private static final String DEFAULT_TEMP_FILE = "jalview_temp_file_" + NS;
// file - File object to be backed up and then updated (written over)
private File file;
// deleting old backup/version files
private static boolean confirmDelete;
- private static boolean classInit = false;
-
// defaultSuffix - default template to use to append to basename of file
private String suffix;
public static void classInit()
{
- if (!classInit)
- {
- setEnabled(Cache.getDefault(ENABLED, true));
- setConfirmDelete(Cache.getDefault(CONFIRM_DELETE_OLD, true));
- classInit = true;
- }
+ setEnabled(Cache.getDefault(ENABLED, true));
+ setConfirmDelete(Cache.getDefault(CONFIRM_DELETE_OLD, true));
}
public static void setEnabled(boolean flag)
return path;
}
- public static String getNumPlaceHolder()
- {
- return NUM_PLACEHOLDER;
- }
-
public boolean setWriteSuccess(boolean flag)
{
boolean old = this.tempFileWriteSuccess;
String basename = filename;
String extension = "";
int dotcharpos = filename.lastIndexOf('.');
- // don't split of filenames with the last '.' at the very beginning or
+ // don't split filenames with the last '.' at the very beginning or
// very end of the filename
if ((dotcharpos > 0) && (dotcharpos < filename.length() - 1))
{
}
else
{
-
- // sort the backup files (based on integer found in the suffix) using a
- // precomputed Hashmap for speed
- HashMap<Integer, File> bfHashMap = new HashMap<>();
- for (int i = 0; i < backupFiles.length; i++)
- {
- File f = backupFiles[i];
- BackupFilenameParts bfp = new BackupFilenameParts(f, basename, suffix, digits, extension);
- bfHashMap.put(bfp.indexNum(), f);
- }
- TreeMap<Integer, File> bfTreeMap = new TreeMap<>();
- bfTreeMap.putAll(bfHashMap);
+ TreeMap<Integer, File> bfTreeMap = sortBackupFilesAsTreeMap(backupFiles, basename, extension);
if (reverseOrder)
{
}
}
+ deleteOldFiles(oldFilesToDelete, confirmDeleteExtraInfo);
+
+ // Let's make the new backup file!! yay, got there at last!
+ String latestBackupFilename = dir + File.separatorChar
+ + BackupFilenameFilter.getBackupFilename(nextIndexNum, basename,
+ suffix, digits, extension);
+ File latestBackupFile = new File(latestBackupFilename);
+ ret = ret && file.renameTo(latestBackupFile);
+
+ return ret;
+ }
+
+ private void deleteOldFiles(File[] oldFilesToDelete, String confirmDeleteExtraInfo) {
if (oldFilesToDelete != null && oldFilesToDelete.length > 0)
{
// delete old backup/version files
}
}
+ }
- // Let's make the new backup file!! yay, got there at last!
- String latestBackupFilename = dir + File.separatorChar
- + BackupFilenameFilter.getBackupFilename(nextIndexNum, basename,
- suffix, digits, extension);
- File latestBackupFile = new File(latestBackupFilename);
- ret = ret && file.renameTo(latestBackupFile);
-
- return ret;
+ private TreeMap sortBackupFilesAsTreeMap(File[] backupFiles, String basename, String extension) {
+ // sort the backup files (based on integer found in the suffix) using a
+ // precomputed Hashmap for speed
+ Map<Integer, File> bfHashMap = new HashMap<>();
+ for (int i = 0; i < backupFiles.length; i++)
+ {
+ File f = backupFiles[i];
+ BackupFilenameParts bfp = new BackupFilenameParts(f, basename, suffix, digits, extension);
+ bfHashMap.put(bfp.indexNum(), f);
+ }
+ TreeMap<Integer, File> bfTreeMap = new TreeMap<>();
+ bfTreeMap.putAll(bfHashMap);
+ return bfTreeMap;
}
public boolean rollBackupsAndRenameTempFile()
return rename;
}
+ public static TreeMap<Integer, File> getBackupFilesAsTreeMap(
+ String fileName,
+ String suffix, int digits)
+ {
+ File[] backupFiles = null;
+
+ File file = new File(fileName);
+
+ String dir = "";
+ File dirFile;
+ try
+ {
+ dirFile = file.getParentFile();
+ dir = dirFile.getCanonicalPath();
+ } catch (Exception e)
+ {
+ System.out.println(
+ "Could not get canonical path for file '" + file + "'");
+ return new TreeMap<>();
+ }
+
+ String filename = file.getName();
+ String basename = filename;
+ String extension = "";
+ int dotcharpos = filename.lastIndexOf('.');
+ // don't split of filenames with the last '.' at the very beginning or
+ // very end of the filename
+ if ((dotcharpos > 0) && (dotcharpos < filename.length() - 1))
+ {
+ basename = filename.substring(0, dotcharpos);
+ extension = filename.substring(dotcharpos); // NOTE this includes the '.'
+ }
+
+ // find existing backup files
+ BackupFilenameFilter bff = new BackupFilenameFilter(basename, suffix, digits, extension);
+ 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<Integer, File> bfHashMap = new HashMap<>();
+ for (int i = 0; i < backupFiles.length; i++)
+ {
+ File f = backupFiles[i];
+ BackupFilenameParts bfp = new BackupFilenameParts(f, basename, suffix,
+ digits, extension);
+ bfHashMap.put(bfp.indexNum(), f);
+ }
+ TreeMap<Integer, File> bfTreeMap = new TreeMap<>();
+ bfTreeMap.putAll(bfHashMap);
+
+ return bfTreeMap;
+ }
+
+
}