JAL-1988 Added a saves in progress check for quit
[jalview.git] / src / jalview / io / BackupFiles.java
index 2039d3c..529e1b2 100644 (file)
@@ -29,6 +29,7 @@ import java.nio.file.StandardCopyOption;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
 
@@ -105,6 +106,52 @@ public class BackupFiles
 
   private static final String oldTempFileSuffix = "_oldfile_tobedeleted";
 
+  private static List<BackupFiles> savesInProgress = new ArrayList<>();
+
+  private boolean addSaveInProgress()
+  {
+    if (savesInProgress.contains(this))
+    {
+      return false;
+    }
+    else
+    {
+      savesInProgress.add(this);
+      return true;
+    }
+  }
+
+  private boolean removeSaveInProgress()
+  {
+    if (savesInProgress.contains(this))
+    {
+      // remove all occurrences
+      while (savesInProgress.remove(this))
+      {
+      }
+      return true;
+    }
+    else
+    {
+      return false;
+    }
+  }
+
+  public static boolean hasSavesInProgress()
+  {
+    return savesInProgress.size() > 0;
+  }
+
+  public static List<File> savesInProgressFiles()
+  {
+    List<File> files = new ArrayList<>();
+    for (BackupFiles bfile : savesInProgress)
+    {
+      files.add(bfile.getFile());
+    }
+    return files;
+  }
+
   public BackupFiles(String filename)
   {
     this(new File(filename));
@@ -116,6 +163,10 @@ public class BackupFiles
   {
     classInit();
     this.file = file;
+
+    // add this file from the save in progress stack
+    addSaveInProgress();
+
     BackupFilesPresetEntry bfpe = BackupFilesPresetEntry
             .getSavedBackupEntry();
     this.suffix = bfpe.suffix;
@@ -819,6 +870,9 @@ public class BackupFiles
       tidyUpFiles();
     }
 
+    // remove this file from the save in progress stack
+    removeSaveInProgress();
+
     return rename;
   }
 
@@ -890,6 +944,11 @@ public class BackupFiles
     return ret;
   }
 
+  public File getFile()
+  {
+    return file;
+  }
+
   public static boolean moveFileToFile(File oldFile, File newFile)
   {
     Console.initLogger();