JAL-3141 Preferences 'Backups' tab now working. Doesn't fit in default space though...
[jalview.git] / src / jalview / io / BackupFiles.java
index ff23cbf..784d8a1 100644 (file)
@@ -16,6 +16,7 @@ import java.util.TreeMap;
  * User configurable options are:
  * BACKUPFILES_ENABLED - boolean flag as to whether to use this mechanism or act as before, including overwriting files as saved.
  * BACKUPFILES_SUFFIX - a template to insert just before the file extension.  Use '%n' to be replaced by a 0-led SUFFIX_DIGITS long integer.
+ * BACKUPFILES_NO_MAX - flag to turn off setting a maximum number of backup files to keep.
  * BACKUPFILES_ROLL_MAX - the maximum number of backupfiles to keep for any one alignment or project file.
  * BACKUPFILES_SUFFIX_DIGITS - the number of digits to insert replace %n with (e.g. BACKUPFILES_SUFFIX_DIGITS = 3 would make "001", "002", etc)
  * BACKUPFILES_REVERSE_ORDER - if true then "logfile" style numbering and file rolling will occur. If false then ever-increasing version numbering will occur, but old files will still be deleted if there are more than ROLL_MAX backup files. 
@@ -32,6 +33,8 @@ public class BackupFiles
 
   public static String SUFFIX = NS + "_SUFFIX";
 
+  public static String NO_MAX = NS + "_NO_MAX";
+
   public static String ROLL_MAX = NS + "_ROLL_MAX";
 
   public static String SUFFIX_DIGITS = NS + "_SUFFIX_DIGITS";
@@ -60,6 +63,9 @@ public class BackupFiles
   // defaultSuffix - default template to use to append to basename of file
   private String suffix;
 
+  // noMax - flag to turn off a maximum number of files
+  private boolean noMax;
+
   // defaultMax - default max number of backup files
   private int max;
 
@@ -81,19 +87,22 @@ public class BackupFiles
     this(new File(filename));
   }
 
-  // first time defaults for SUFFIX, ROLL_MAX, SUFFIX_DIGITS and REVERSE_ORDER
+  // first time defaults for SUFFIX, NO_MAX, ROLL_MAX, SUFFIX_DIGITS and
+  // REVERSE_ORDER
   public BackupFiles(File file)
   {
-    this(file, "-v" + NUM_PLACEHOLDER, 4, 3, false);
+    this(file, "-v" + NUM_PLACEHOLDER, false, 4, 3, false);
   }
 
   public BackupFiles(File file,
-          String defaultSuffix, int defaultMax, int defaultDigits,
+          String defaultSuffix, boolean defaultNoMax, int defaultMax,
+          int defaultDigits,
           boolean defaultReverseOrder)
   {
     classInit();
     this.file = file;
     this.suffix = Cache.getDefault(SUFFIX, defaultSuffix);
+    this.noMax = Cache.getDefault(NO_MAX, defaultNoMax);
     this.max = Cache.getDefault(ROLL_MAX, defaultMax);
     this.digits = Cache.getDefault(SUFFIX_DIGITS, defaultDigits);
     this.reverseOrder = Cache.getDefault(REVERSE_ORDER,
@@ -182,6 +191,11 @@ public class BackupFiles
     return path;
   }
 
+  public static String getNumPlaceHolder()
+  {
+    return NUM_PLACEHOLDER;
+  }
+
   public boolean setWriteSuccess(boolean flag)
   {
     boolean old = this.tempFileWriteSuccess;
@@ -205,7 +219,7 @@ public class BackupFiles
   {
 
     // file doesn't yet exist or backups are not enabled
-    if ((!file.exists()) || (!enabled) || (max < -1))
+    if ((!file.exists()) || (!enabled) || (max < 0))
     {
       // nothing to do
       return true;
@@ -275,24 +289,28 @@ public class BackupFiles
         // backup style numbering
 
         File lastfile = null;
-        int tempMax = max;
-        // max == -1 means no limits
+        int tempMax = noMax ? -1 : max;
+        // noMax == true means no limits
         // look for first "gap" in backupFiles
-        // if tempMax is -1 at this stage just keep going until there's a gap...
+        // if tempMax is -1 at this stage just keep going until there's a gap,
+        // then hopefully tempMax gets set to the right index (a positive
+        // integer so the loop breaks)...
         // why do I feel a little uneasy about this loop?..
-        for (int i = 1; tempMax < 0 || (max >= 0 && i <= max); i++)
+        for (int i = 1; tempMax < 0 || i <= max; i++)
         {
-          if (!bfTreeMap.containsKey(i)) // first non-existent backupfile
+          if (!bfTreeMap.containsKey(i)) // first index without existent
+                                         // backupfile
           {
             tempMax = i;
           }
         }
 
-        for (int m = 0; m < tempMax; m++)
+        // for (int m = 0; m < tempMax; m++)
+        for (int n = tempMax; n > 0; n--)
         {
-          int n = tempMax - m;
+          // int n = tempMax - m;
           String backupfilename = dir + File.separatorChar
-                  + BackupFilenameParts.getBackupFilename(n, basename,
+                  + BackupFilenameFilter.getBackupFilename(n, basename,
                           suffix, digits, extension);
           File backupfile_n = new File(backupfilename);
 
@@ -302,7 +320,8 @@ public class BackupFiles
             continue;
           }
 
-          if (m == 0 && backupfile_n.exists())
+          // 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
             try
@@ -343,8 +362,8 @@ public class BackupFiles
 
         bfTreeMap.values().toArray(backupFiles);
 
-        // max value of -1 means keep all backup files
-        if (bfTreeMap.size() >= max && max != -1)
+        // noMax == true means keep all backup files
+        if ((!noMax) && bfTreeMap.size() >= max)
         {
           // need to delete some files to keep number of backups to designated
           // max
@@ -409,7 +428,7 @@ public class BackupFiles
 
     // Let's make the new backup file!! yay, got there at last!
     String latestBackupFilename = dir + File.separatorChar
-            + BackupFilenameParts.getBackupFilename(nextIndexNum, basename,
+            + BackupFilenameFilter.getBackupFilename(nextIndexNum, basename,
                     suffix, digits, extension);
     File latestBackupFile = new File(latestBackupFilename);
     ret = ret && file.renameTo(latestBackupFile);