JAL-1988 Added a saves in progress check for quit
[jalview.git] / src / jalview / jbgui / QuitHandler.java
index 56e23aa..15cc37a 100644 (file)
@@ -1,10 +1,13 @@
 package jalview.jbgui;
 
+import java.io.File;
+
 import javax.swing.JFrame;
 import javax.swing.JOptionPane;
 
 import com.formdev.flatlaf.extras.FlatDesktop;
 
+import jalview.io.BackupFiles;
 import jalview.util.MessageManager;
 
 public class QuitHandler
@@ -12,37 +15,44 @@ public class QuitHandler
   public static void setQuitHandler()
   {
     FlatDesktop.setQuitHandler(response -> {
+      // confirm quit if needed and wanted
       boolean confirmQuit = jalview.bin.Cache
               .getDefault(jalview.gui.Desktop.CONFIRM_KEYBOARD_QUIT, true);
-      boolean canQuit = !confirmQuit;
-      int n;
+      /* 
+         if undostack is empty
+           confirmQuit = false
+       */
+      int n = confirmQuit ? JOptionPane.CANCEL_OPTION
+              : JOptionPane.OK_OPTION;
+
+      // if going to confirm, do it before the save in progress check to give
+      // the save time to finish!
       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"),
+        n = frameOnTop(MessageManager.getString("label.quit_jalview"),
                 MessageManager.getString("action.quit"),
-                JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
-                null);
+                JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
 
-        dialogParent.setAlwaysOnTop(false);
-        dialogParent.dispose();
       }
-      else
+
+      if (BackupFiles.hasSavesInProgress())
       {
-        n = JOptionPane.OK_OPTION;
+        // sleep 1
+        // ...
+
+        StringBuilder messageSB = new StringBuilder(
+                MessageManager.getString("label.save_in_progress"));
+        for (File file : BackupFiles.savesInProgressFiles())
+        {
+          messageSB.append("\n");
+          messageSB.append(file.getName());
+        }
+        n = frameOnTop(messageSB.toString(),
+                MessageManager.getString("action.force_quit"),
+                JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
       }
-      canQuit = (n == JOptionPane.OK_OPTION);
+
+      boolean canQuit = (n == JOptionPane.OK_OPTION);
       if (canQuit)
       {
         response.performQuit();
@@ -54,4 +64,28 @@ public class QuitHandler
     });
   }
 
+  public static int frameOnTop(String label, String actionString,
+          int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE)
+  {
+    // 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);
+
+    int n = JOptionPane.showConfirmDialog(dialogParent, label, actionString,
+            JOPTIONPANE_OPTION, JOPTIONPANE_MESSAGETYPE);
+
+    dialogParent.setAlwaysOnTop(false);
+    dialogParent.dispose();
+
+    return n;
+  }
+
 }