JAL-1988 added a saveInProgress stack to BackupFiles
authorBen Soares <bsoares@dundee.ac.uk>
Wed, 31 Aug 2022 16:19:03 +0000 (17:19 +0100)
committerBen Soares <bsoares@dundee.ac.uk>
Wed, 31 Aug 2022 16:19:03 +0000 (17:19 +0100)
src/jalview/io/BackupFiles.java
src/jalview/jbgui/QuitHandler.java [new file with mode: 0644]

index 2039d3c..05a1ac4 100644 (file)
@@ -105,6 +105,37 @@ public class BackupFiles
 
   private static final String oldTempFileSuffix = "_oldfile_tobedeleted";
 
+  private static ArrayList<File> savesInProgress = new ArrayList<>();
+
+  private boolean addSaveInProgress()
+  {
+    if (savesInProgress.contains(file))
+    {
+      return false;
+    }
+    else
+    {
+      savesInProgress.add(file);
+      return true;
+    }
+  }
+
+  private boolean removeSaveInProgress()
+  {
+    if (savesInProgress.contains(file))
+    {
+      // remove all occurrences
+      while (savesInProgress.remove(file))
+      {
+      }
+      return true;
+    }
+    else
+    {
+      return false;
+    }
+  }
+
   public BackupFiles(String filename)
   {
     this(new File(filename));
@@ -116,6 +147,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 +854,9 @@ public class BackupFiles
       tidyUpFiles();
     }
 
+    // remove this file from the save in progress stack
+    removeSaveInProgress();
+
     return rename;
   }
 
diff --git a/src/jalview/jbgui/QuitHandler.java b/src/jalview/jbgui/QuitHandler.java
new file mode 100644 (file)
index 0000000..56e23aa
--- /dev/null
@@ -0,0 +1,57 @@
+package jalview.jbgui;
+
+import javax.swing.JFrame;
+import javax.swing.JOptionPane;
+
+import com.formdev.flatlaf.extras.FlatDesktop;
+
+import jalview.util.MessageManager;
+
+public class QuitHandler
+{
+  public static void setQuitHandler()
+  {
+    FlatDesktop.setQuitHandler(response -> {
+      boolean confirmQuit = jalview.bin.Cache
+              .getDefault(jalview.gui.Desktop.CONFIRM_KEYBOARD_QUIT, true);
+      boolean canQuit = !confirmQuit;
+      int n;
+      if (confirmQuit)
+      {
+        // ensure Jalview window is brought to front for Quit confirmation
+        // window to be visible
+
+        // this method of raising the Jalview window is broken in java
+        // jalviewDesktop.setVisible(true);
+        // jalviewDesktop.toFront();
+
+        // a better hack which works instead
+        JFrame dialogParent = new JFrame();
+        dialogParent.setAlwaysOnTop(true);
+
+        n = JOptionPane.showConfirmDialog(dialogParent,
+                MessageManager.getString("label.quit_jalview"),
+                MessageManager.getString("action.quit"),
+                JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
+                null);
+
+        dialogParent.setAlwaysOnTop(false);
+        dialogParent.dispose();
+      }
+      else
+      {
+        n = JOptionPane.OK_OPTION;
+      }
+      canQuit = (n == JOptionPane.OK_OPTION);
+      if (canQuit)
+      {
+        response.performQuit();
+      }
+      else
+      {
+        response.cancelQuit();
+      }
+    });
+  }
+
+}