JAL-1988 JAL-3772 linked undostack to saved changes flags. No confirmation if all...
[jalview.git] / src / jalview / jbgui / QuitHandler.java
index f7a30f3..6a95db7 100644 (file)
@@ -2,14 +2,21 @@ package jalview.jbgui;
 
 import java.io.File;
 import java.util.Date;
+import java.util.List;
 
 import javax.swing.JFrame;
 import javax.swing.JOptionPane;
 
 import com.formdev.flatlaf.extras.FlatDesktop;
 
+import jalview.api.AlignmentViewPanel;
 import jalview.bin.Console;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.SequenceI;
+import jalview.gui.AlignFrame;
+import jalview.gui.Desktop;
 import jalview.io.BackupFiles;
+import jalview.project.Jalview2XML;
 import jalview.util.MessageManager;
 import jalview.util.Platform;
 
@@ -75,12 +82,11 @@ public class QuitHandler
       Console.debug("Non interactive quit -- not confirming");
       confirmQuit = false;
     }
-    /* 
-    else if (undostack is empty) {
+    else if (Jalview2XML.allSavedUpToDate())
+    {
       Console.debug("Nothing changed -- not confirming quit");
-      confirmQuit = false
+      confirmQuit = false;
     }
-    */
     else
     {
       confirmQuit = jalview.bin.Cache
@@ -97,7 +103,13 @@ public class QuitHandler
     // the save time to finish!
     if (confirmQuit)
     {
-      answer = frameOnTop(MessageManager.getString("label.quit_jalview"),
+      answer = frameOnTop(
+              new StringBuilder(
+                      MessageManager.getString("label.quit_jalview"))
+                              .append(" ")
+                              .append(MessageManager
+                                      .getString("label.unsaved_changes"))
+                              .toString(),
               MessageManager.getString("action.quit"),
               JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
     }
@@ -109,8 +121,34 @@ public class QuitHandler
     }
 
     // check for saves in progress
-    int waitForSave = 5000; // MAKE THIS BETTER
-    int waitIncrement = 2000;
+    int waitForSave = 1000; // MAKE THIS BETTER
+    AlignFrame[] afArray = Desktop.getAlignFrames();
+    if (afArray == null || afArray.length == 0)
+    {
+      // no change
+    }
+    else
+    {
+      int size = 0;
+      for (int i = 0; i < afArray.length; i++)
+      {
+        AlignFrame af = afArray[i];
+        List<AlignmentViewPanel> avpList = (List<AlignmentViewPanel>) af
+                .getAlignPanels();
+        for (AlignmentViewPanel avp : avpList)
+        {
+          AlignmentI a = avp.getAlignment();
+          List<SequenceI> sList = a.getSequences();
+          for (SequenceI s : sList)
+          {
+            size += s.getLength();
+          }
+        }
+      }
+      waitForSave = size;
+      Console.debug("Set waitForSave to " + waitForSave);
+    }
+    int waitIncrement = 3000;
     long startTime = new Date().getTime();
     boolean saving = BackupFiles.hasSavesInProgress();
     if (saving)
@@ -126,10 +164,18 @@ public class QuitHandler
       {
         StringBuilder messageSB = new StringBuilder(
                 MessageManager.getString("label.save_in_progress"));
+        messageSB.append(":");
+        boolean any = false;
         for (File file : BackupFiles.savesInProgressFiles())
         {
-          messageSB.append("\n");
+          messageSB.append("\n- ");
           messageSB.append(file.getName());
+          any = true;
+        }
+        if (!any)
+        {
+          messageSB.append("\n");
+          messageSB.append(MessageManager.getString("label.unknown"));
         }
         int waitLonger = interactive ? JOptionPane.YES_OPTION
                 : JOptionPane.NO_OPTION;
@@ -140,19 +186,16 @@ public class QuitHandler
                   MessageManager.getString("action.wait"));
           if (waitLonger == JOptionPane.YES_OPTION) // wait
           {
-            Console.debug("*** YES answer=" + waitLonger);
             // do wait stuff
             saving = !waitForSave(waitIncrement);
           }
           else if (waitLonger == JOptionPane.NO_OPTION) // force quit
           {
-            Console.debug("*** NO answer=" + waitLonger);
             // do a force quit
             return returnResponse(QResponse.FORCE_QUIT); // shouldn't reach this
           }
           else if (waitLonger == JOptionPane.CANCEL_OPTION) // cancel quit
           {
-            Console.debug("*** CANCEL answer=" + waitLonger);
             return returnResponse(QResponse.CANCEL_QUIT);
           }
           else