Merge branch 'develop' into update_212_Dec_merge_with_21125_chamges
[jalview.git] / src / jalview / io / BackupFiles.java
index c8d1359..828bd0d 100644 (file)
@@ -20,7 +20,6 @@
  */
 package jalview.io;
 
-
 import java.io.File;
 import java.io.IOException;
 import java.nio.file.Files;
@@ -34,10 +33,12 @@ import java.util.Map;
 import java.util.TreeMap;
 
 import jalview.bin.Cache;
+import jalview.bin.Console;
 import jalview.gui.Desktop;
 import jalview.gui.JvOptionPane;
 import jalview.util.MessageManager;
 import jalview.util.Platform;
+
 /*
  * BackupFiles used for manipulating (naming rolling/deleting) backup/version files when an alignment or project file is saved.
  * User configurable options are:
@@ -103,6 +104,7 @@ public class BackupFiles
   private static final String newTempFileSuffix = "_newfile";
 
   private static final String oldTempFileSuffix = "_oldfile_tobedeleted";
+
   public BackupFiles(String filename)
   {
     this(new File(filename));
@@ -130,17 +132,17 @@ public class BackupFiles
       {
         String tempfilename = file.getName();
         File tempdir = file.getParentFile();
-        Cache.trace(
+        Console.trace(
                 "BACKUPFILES [file!=null] attempting to create temp file for "
                         + tempfilename + " in dir " + tempdir);
         temp = File.createTempFile(tempfilename,
                 TEMP_FILE_EXT + newTempFileSuffix, tempdir);
-        Cache.debug(
+        Console.debug(
                 "BACKUPFILES using temp file " + temp.getAbsolutePath());
       }
       else
       {
-        Cache.trace(
+        Console.trace(
                 "BACKUPFILES [file==null] attempting to create default temp file "
                         + DEFAULT_TEMP_FILE + " with extension "
                         + TEMP_FILE_EXT);
@@ -148,29 +150,29 @@ public class BackupFiles
       }
     } catch (IOException e)
     {
-      Cache.error("Could not create temp file to save to (IOException)");
-      Cache.error(e.getMessage());
-      Cache.debug(Cache.getStackTraceString(e));
+      Console.error("Could not create temp file to save to (IOException)");
+      Console.error(e.getMessage());
+      Console.debug(Cache.getStackTraceString(e));
     } catch (Exception e)
     {
-      Cache.error("Exception creating temp file for saving");
-      Cache.debug(Cache.getStackTraceString(e));
+      Console.error("Exception creating temp file for saving");
+      Console.debug(Cache.getStackTraceString(e));
     }
     this.setTempFile(temp);
   }
 
   public static void classInit()
   {
-    Cache.initLogger();
-    Cache.trace("BACKUPFILES classInit");
+    Console.initLogger();
+    Console.trace("BACKUPFILES classInit");
     boolean e = Cache.getDefault(ENABLED, !Platform.isJS());
     setEnabled(e);
-    Cache.trace("BACKUPFILES " + (e ? "enabled" : "disabled"));
+    Console.trace("BACKUPFILES " + (e ? "enabled" : "disabled"));
     BackupFilesPresetEntry bfpe = BackupFilesPresetEntry
             .getSavedBackupEntry();
-    Cache.trace("BACKUPFILES preset scheme " + bfpe.toString());
+    Console.trace("BACKUPFILES preset scheme " + bfpe.toString());
     setConfirmDelete(bfpe.confirmDelete);
-    Cache.trace("BACKUPFILES confirm delete " + bfpe.confirmDelete);
+    Console.trace("BACKUPFILES confirm delete " + bfpe.confirmDelete);
   }
 
   public static void setEnabled(boolean flag)
@@ -214,9 +216,9 @@ public class BackupFiles
       path = this.getTempFile().getCanonicalPath();
     } catch (IOException e)
     {
-      Cache.error("IOException when getting Canonical Path of temp file '"
+      Console.error("IOException when getting Canonical Path of temp file '"
               + this.getTempFile().getName() + "'");
-      Cache.debug(Cache.getStackTraceString(e));
+      Console.debug(Cache.getStackTraceString(e));
     }
     return path;
   }
@@ -252,7 +254,7 @@ public class BackupFiles
             || suffix.length() == 0)
     {
       // nothing to do
-      Cache.debug("BACKUPFILES rollBackupFiles nothing to do." + ", "
+      Console.debug("BACKUPFILES rollBackupFiles nothing to do." + ", "
               + "filename: " + (file != null ? file.getName() : "null")
               + ", " + "file exists: " + file.exists() + ", " + "enabled: "
               + enabled + ", " + "max: " + max + ", " + "suffix: '" + suffix
@@ -260,25 +262,26 @@ public class BackupFiles
       return true;
     }
 
-    Cache.trace("BACKUPFILES rollBackupFiles starting");
+    Console.trace("BACKUPFILES rollBackupFiles starting");
+
     String dir = "";
     File dirFile;
     try
     {
       dirFile = file.getParentFile();
       dir = dirFile.getCanonicalPath();
-      Cache.trace("BACKUPFILES dir: " + dir);
+      Console.trace("BACKUPFILES dir: " + dir);
     } catch (Exception e)
     {
-      Cache.error("Could not get canonical path for file '" + file + "'");
-      Cache.error(e.getMessage());
-      Cache.debug(Cache.getStackTraceString(e));
+      Console.error("Could not get canonical path for file '" + file + "'");
+      Console.error(e.getMessage());
+      Console.debug(Cache.getStackTraceString(e));
       return false;
     }
     String filename = file.getName();
     String basename = filename;
 
-    Cache.trace("BACKUPFILES filename is " + filename);
+    Console.trace("BACKUPFILES filename is " + filename);
     boolean ret = true;
     // Create/move backups up one
 
@@ -290,11 +293,11 @@ public class BackupFiles
     File[] backupFiles = dirFile.listFiles(bff);
     int nextIndexNum = 0;
 
-    Cache.trace("BACKUPFILES backupFiles.length: " + backupFiles.length);
+    Console.trace("BACKUPFILES backupFiles.length: " + backupFiles.length);
     if (backupFiles.length == 0)
     {
       // No other backup files. Just need to move existing file to backupfile_1
-      Cache.trace(
+      Console.trace(
               "BACKUPFILES no existing backup files, setting index to 1");
       nextIndexNum = 1;
     }
@@ -308,8 +311,7 @@ public class BackupFiles
       if (reverseOrder)
       {
         // backup style numbering
-
-        Cache.trace("BACKUPFILES rolling files in reverse order");
+        Console.trace("BACKUPFILES rolling files in reverse order");
 
         int tempMax = noMax ? -1 : max;
         // noMax == true means no limits
@@ -326,6 +328,7 @@ public class BackupFiles
             tempMax = i;
           }
         }
+
         File previousFile = null;
         File fileToBeDeleted = null;
         for (int n = tempMax; n > 0; n--)
@@ -340,7 +343,7 @@ public class BackupFiles
             // no "oldest" file to delete
             previousFile = backupfile_n;
             fileToBeDeleted = null;
-            Cache.trace("BACKUPFILES No oldest file to delete");
+            Console.trace("BACKUPFILES No oldest file to delete");
             continue;
           }
 
@@ -351,22 +354,23 @@ public class BackupFiles
             File replacementFile = backupfile_n;
             long fileToBeDeletedLMT = fileToBeDeleted.lastModified();
             long replacementFileLMT = replacementFile.lastModified();
-            Cache.trace("BACKUPFILES fileToBeDeleted is "
+            Console.trace("BACKUPFILES fileToBeDeleted is "
                     + fileToBeDeleted.getAbsolutePath());
-            Cache.trace("BACKUPFILES replacementFile is "
+            Console.trace("BACKUPFILES replacementFile is "
                     + backupfile_n.getAbsolutePath());
 
             try
             {
               File oldestTempFile = nextTempFile(fileToBeDeleted.getName(),
                       dirFile);
+
               if (fileToBeDeletedLMT > replacementFileLMT)
               {
                 String fileToBeDeletedLMTString = sdf
                         .format(fileToBeDeletedLMT);
                 String replacementFileLMTString = sdf
                         .format(replacementFileLMT);
-                Cache.warn("WARNING! I am set to delete backupfile "
+                Console.warn("WARNING! I am set to delete backupfile "
                         + fileToBeDeleted.getName()
                         + " has modification time "
                         + fileToBeDeletedLMTString
@@ -377,8 +381,8 @@ public class BackupFiles
 
                 boolean delete = confirmNewerDeleteFile(fileToBeDeleted,
                         replacementFile, true);
-                Cache.trace("BACKUPFILES " + (delete ? "confirmed" : "not")
-                        + " deleting file "
+                Console.trace("BACKUPFILES "
+                        + (delete ? "confirmed" : "not") + " deleting file "
                         + fileToBeDeleted.getAbsolutePath()
                         + " which is newer than "
                         + replacementFile.getAbsolutePath());
@@ -390,7 +394,7 @@ public class BackupFiles
                 }
                 else
                 {
-                  Cache.debug("BACKUPFILES moving "
+                  Console.debug("BACKUPFILES moving "
                           + fileToBeDeleted.getAbsolutePath() + " to "
                           + oldestTempFile.getAbsolutePath());
                   moveFileToFile(fileToBeDeleted, oldestTempFile);
@@ -398,7 +402,7 @@ public class BackupFiles
               }
               else
               {
-                Cache.debug("BACKUPFILES going to move "
+                Console.debug("BACKUPFILES going to move "
                         + fileToBeDeleted.getAbsolutePath() + " to "
                         + oldestTempFile.getAbsolutePath());
                 moveFileToFile(fileToBeDeleted, oldestTempFile);
@@ -407,10 +411,10 @@ public class BackupFiles
 
             } catch (Exception e)
             {
-              Cache.error(
+              Console.error(
                       "Error occurred, probably making new temp file for '"
                               + fileToBeDeleted.getName() + "'");
-              Cache.error(Cache.getStackTraceString(e));
+              Console.error(Cache.getStackTraceString(e));
             }
 
             // reset
@@ -452,12 +456,12 @@ public class BackupFiles
           }
           bfsb.append(backupFiles[i].getName());
         }
-        Cache.trace("BACKUPFILES backupFiles: " + bfsb.toString());
+        Console.trace("BACKUPFILES backupFiles: " + bfsb.toString());
 
         // noMax == true means keep all backup files
         if ((!noMax) && bfTreeMap.size() >= max)
         {
-          Cache.trace("BACKUPFILES noMax: " + noMax + ", " + "max: " + max
+          Console.trace("BACKUPFILES noMax: " + noMax + ", " + "max: " + max
                   + ", " + "bfTreeMap.size(): " + bfTreeMap.size());
           // need to delete some files to keep number of backups to designated
           // max.
@@ -467,7 +471,7 @@ public class BackupFiles
           int numToDelete = suffix.indexOf(NUM_PLACEHOLDER) > -1
                   ? bfTreeMap.size() - max + 1
                   : 0;
-          Cache.trace("BACKUPFILES numToDelete: " + numToDelete);
+          Console.trace("BACKUPFILES numToDelete: " + numToDelete);
           // the "replacement" file is the latest backup file being kept (it's
           // not replacing though)
           File replacementFile = numToDelete < backupFiles.length
@@ -480,7 +484,9 @@ public class BackupFiles
             File fileToBeDeleted = backupFiles[i];
             boolean delete = true;
 
-            Cache.trace("BACKUPFILES fileToBeDeleted: " + fileToBeDeleted);
+            Console.trace(
+                    "BACKUPFILES fileToBeDeleted: " + fileToBeDeleted);
+
             boolean newer = false;
             if (replacementFile != null)
             {
@@ -495,7 +501,7 @@ public class BackupFiles
                 String replacementFileLMTString = sdf
                         .format(replacementFileLMT);
 
-                Cache.warn("WARNING! I am set to delete backupfile '"
+                Console.warn("WARNING! I am set to delete backupfile '"
                         + fileToBeDeleted.getName()
                         + "' has modification time "
                         + fileToBeDeletedLMTString
@@ -510,14 +516,14 @@ public class BackupFiles
                 {
                   // User has confirmed delete -- no need to add it to the list
                   fileToBeDeleted.delete();
-                  Cache.debug("BACKUPFILES deleting fileToBeDeleted: "
+                  Console.debug("BACKUPFILES deleting fileToBeDeleted: "
                           + fileToBeDeleted);
                   delete = false;
                 }
                 else
                 {
                   // keeping file, nothing to do!
-                  Cache.debug("BACKUPFILES keeping fileToBeDeleted: "
+                  Console.debug("BACKUPFILES keeping fileToBeDeleted: "
                           + fileToBeDeleted);
                 }
               }
@@ -525,7 +531,7 @@ public class BackupFiles
             if (delete)
             {
               addDeleteFile(fileToBeDeleted);
-              Cache.debug("BACKUPFILES addDeleteFile(fileToBeDeleted): "
+              Console.debug("BACKUPFILES addDeleteFile(fileToBeDeleted): "
                       + fileToBeDeleted);
             }
 
@@ -541,17 +547,17 @@ public class BackupFiles
     String latestBackupFilename = dir + File.separatorChar
             + BackupFilenameParts.getBackupFilename(nextIndexNum, basename,
                     suffix, digits);
-    Cache.trace("BACKUPFILES Moving old file [" + file
+    Console.trace("BACKUPFILES Moving old file [" + file
             + "] to latestBackupFilename [" + latestBackupFilename + "]");
     // using boolean '&' instead of '&&' as don't want moveFileToFile attempt to
     // be conditional (short-circuit)
     ret = ret & moveFileToFile(file, new File(latestBackupFilename));
-    Cache.debug("BACKUPFILES moving " + file + " to " + latestBackupFilename
-            + " was " + (ret ? "" : "NOT ") + "successful");
-
+    Console.debug(
+            "BACKUPFILES moving " + file + " to " + latestBackupFilename
+                    + " was " + (ret ? "" : "NOT ") + "successful");
     if (tidyUp)
     {
-      Cache.debug("BACKUPFILES tidying up files");
+      Console.debug("BACKUPFILES tidying up files");
       tidyUpFiles();
     }
 
@@ -607,7 +613,7 @@ public class BackupFiles
         saveFile = nextTempFile(ftbd.getName(), ftbd.getParentFile());
       } catch (Exception e)
       {
-        Cache.error(
+        Console.error(
                 "Error when confirming to keep backup file newer than other backup files.");
         e.printStackTrace();
       }
@@ -663,7 +669,6 @@ public class BackupFiles
               null, options, options[0]);
     }
 
-
     // return should be TRUE if file is to be deleted
     return (confirmButton == JvOptionPane.YES_OPTION);
   }
@@ -695,7 +700,7 @@ public class BackupFiles
         }
 
         int confirmButton = Platform.isHeadless() ? JvOptionPane.YES_OPTION
-                : JvOptionPane.showConfirmDialog(Desktop.getDesktopPane(),
+           : JvOptionPane.showConfirmDialog(Desktop.getDesktopPane(),
                         messageSB.toString(),
                         MessageManager.getString(
                                 "label.backupfiles_confirm_delete"),
@@ -715,10 +720,10 @@ public class BackupFiles
         for (int i = 0; i < deleteFiles.size(); i++)
         {
           File fileToDelete = deleteFiles.get(i);
-          Cache.trace("BACKUPFILES about to delete fileToDelete:"
+          Console.trace("BACKUPFILES about to delete fileToDelete:"
                   + fileToDelete);
           fileToDelete.delete();
-          Cache.warn("deleted '" + fileToDelete.getName() + "'");
+          Console.warn("deleted '" + fileToDelete.getName() + "'");
         }
       }
 
@@ -827,7 +832,7 @@ public class BackupFiles
       dirFile = file.getParentFile();
     } catch (Exception e)
     {
-      Cache.error("Could not get canonical path for file '" + file + "'");
+      Console.error("Could not get canonical path for file '" + file + "'");
       return new TreeMap<>();
     }
 
@@ -868,14 +873,14 @@ public class BackupFiles
     int pos = deleteFiles.indexOf(fileToBeDeleted);
     if (pos > -1)
     {
-      Cache.debug("BACKUPFILES not adding file "
+      Console.debug("BACKUPFILES not adding file "
               + fileToBeDeleted.getAbsolutePath()
               + " to the delete list (already at index" + pos + ")");
       return true;
     }
     else
     {
-      Cache.debug("BACKUPFILES adding file "
+      Console.debug("BACKUPFILES adding file "
               + fileToBeDeleted.getAbsolutePath() + " to the delete list");
       deleteFiles.add(fileToBeDeleted);
     }
@@ -884,31 +889,31 @@ public class BackupFiles
 
   public static boolean moveFileToFile(File oldFile, File newFile)
   {
-    Cache.initLogger();
+    Console.initLogger();
     boolean ret = false;
     Path oldPath = Paths.get(oldFile.getAbsolutePath());
     Path newPath = Paths.get(newFile.getAbsolutePath());
     try
     {
       // delete destination file - not usually necessary but Just In Case...
-      Cache.trace("BACKUPFILES deleting " + newFile.getAbsolutePath());
+      Console.trace("BACKUPFILES deleting " + newFile.getAbsolutePath());
       newFile.delete();
-      Cache.trace("BACKUPFILES moving " + oldFile.getAbsolutePath() + " to "
-              + newFile.getAbsolutePath());
+      Console.trace("BACKUPFILES moving " + oldFile.getAbsolutePath()
+              + " to " + newFile.getAbsolutePath());
       Files.move(oldPath, newPath, StandardCopyOption.REPLACE_EXISTING);
       ret = true;
-      Cache.trace("BACKUPFILES move seems to have succeeded");
+      Console.trace("BACKUPFILES move seems to have succeeded");
     } catch (IOException e)
     {
-      Cache.warn("Could not move file '" + oldPath.toString() + "' to '"
+      Console.warn("Could not move file '" + oldPath.toString() + "' to '"
               + newPath.toString() + "'");
-      Cache.error(e.getMessage());
-      Cache.debug(Cache.getStackTraceString(e));
+      Console.error(e.getMessage());
+      Console.debug(Cache.getStackTraceString(e));
       ret = false;
     } catch (Exception e)
     {
-      Cache.error(e.getMessage());
-      Cache.debug(Cache.getStackTraceString(e));
+      Console.error(e.getMessage());
+      Console.debug(Cache.getStackTraceString(e));
       ret = false;
     }
     return ret;