Merge branch 'releases/Release_2_11_3_Branch'
[jalview.git] / src / jalview / gui / QuitHandler.java
index 79723cf..1b48c6d 100644 (file)
@@ -1,8 +1,28 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.gui;
 
 import java.io.File;
 import java.util.List;
-import java.util.concurrent.Callable;
+import java.util.Locale;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
@@ -17,6 +37,7 @@ import javax.swing.JOptionPane;
 import javax.swing.JTextPane;
 
 import com.formdev.flatlaf.extras.FlatDesktop;
+import com.formdev.flatlaf.extras.FlatDesktop.QuitResponse;
 
 import jalview.api.AlignmentViewPanel;
 import jalview.bin.Cache;
@@ -36,6 +57,8 @@ public class QuitHandler
 
   private static boolean interactive = true;
 
+  private static QuitResponse flatlafResponse = null;
+
   public static enum QResponse
   {
     NULL, QUIT, CANCEL_QUIT, FORCE_QUIT
@@ -55,29 +78,17 @@ public class QuitHandler
 
   private static ExecutorService executor = Executors.newFixedThreadPool(3);
 
-  public static QResponse setQuitHandler()
+  public static void setQuitHandler()
   {
     FlatDesktop.setQuitHandler(response -> {
-      Callable<Void> performQuit = () -> {
-        response.performQuit();
-        setResponse(QResponse.QUIT);
-        return null;
-      };
-      Callable<Void> performForceQuit = () -> {
-        response.performQuit();
-        setResponse(QResponse.FORCE_QUIT);
-        return null;
-      };
-      Callable<Void> cancelQuit = () -> {
-        response.cancelQuit();
-        // reset
-        setResponse(QResponse.NULL);
-        return null;
-      };
-      getQuitResponse(true, performQuit, performForceQuit, cancelQuit);
+      flatlafResponse = response;
+      Desktop.instance.desktopQuit();
     });
+  }
 
-    return gotQuitResponse();
+  public static void startForceQuit()
+  {
+    setResponse(QResponse.FORCE_QUIT);
   }
 
   private static QResponse gotQuitResponse = QResponse.NULL;
@@ -85,6 +96,11 @@ public class QuitHandler
   protected static QResponse setResponse(QResponse qresponse)
   {
     gotQuitResponse = qresponse;
+    if ((qresponse == QResponse.CANCEL_QUIT || qresponse == QResponse.NULL)
+            && flatlafResponse != null)
+    {
+      flatlafResponse.cancelQuit();
+    }
     return qresponse;
   }
 
@@ -93,25 +109,22 @@ public class QuitHandler
     return gotQuitResponse;
   }
 
-  public static final Callable<Void> defaultCancelQuit = () -> {
+  public static final Runnable defaultCancelQuit = () -> {
     Console.debug("QuitHandler: (default) Quit action CANCELLED by user");
     // reset
     setResponse(QResponse.CANCEL_QUIT);
-    return null;
   };
 
-  public static final Callable<Void> defaultOkQuit = () -> {
+  public static final Runnable defaultOkQuit = () -> {
     Console.debug("QuitHandler: (default) Quit action CONFIRMED by user");
     setResponse(QResponse.QUIT);
-    return null;
   };
 
-  public static final Callable<Void> defaultForceQuit = () -> {
+  public static final Runnable defaultForceQuit = () -> {
     Console.debug("QuitHandler: (default) Quit action FORCED by user");
     // note that shutdown hook will not be run
     Runtime.getRuntime().halt(0);
     setResponse(QResponse.FORCE_QUIT); // this line never reached!
-    return null;
   };
 
   public static QResponse getQuitResponse(boolean ui)
@@ -120,8 +133,8 @@ public class QuitHandler
             defaultCancelQuit);
   }
 
-  public static QResponse getQuitResponse(boolean ui, Callable<Void> okQuit,
-          Callable<Void> forceQuit, Callable<Void> cancelQuit)
+  public static QResponse getQuitResponse(boolean ui, Runnable okQuit,
+          Runnable forceQuit, Runnable cancelQuit)
   {
     QResponse got = gotQuitResponse();
     if (got != QResponse.NULL && got != QResponse.CANCEL_QUIT)
@@ -187,6 +200,8 @@ public class QuitHandler
       int count = Desktop.instance.structureViewersStillRunningCount();
       if (count > 0)
       {
+        String alwaysCloseExternalViewers = Cache
+                .getDefault("ALWAYS_CLOSE_EXTERNAL_VIEWERS", "ask");
         String prompt = MessageManager
                 .formatMessage(count == 1 ? "label.confirm_quit_viewer"
                         : "label.confirm_quit_viewers");
@@ -197,11 +212,22 @@ public class QuitHandler
         String[] buttonsText = { MessageManager.getString("action.yes"),
             MessageManager.getString("action.no"), cancelQuitText };
 
-        int confirmResponse = JvOptionPane.showOptionDialog(
-                Desktop.instance, prompt, title,
-                JvOptionPane.YES_NO_CANCEL_OPTION,
-                JvOptionPane.WARNING_MESSAGE, null, buttonsText,
-                cancelQuit);
+        int confirmResponse = -1;
+        if (alwaysCloseExternalViewers == null || "ask".equals(
+                alwaysCloseExternalViewers.toLowerCase(Locale.ROOT)))
+        {
+          confirmResponse = JvOptionPane.showOptionDialog(Desktop.instance,
+                  prompt, title, JvOptionPane.YES_NO_CANCEL_OPTION,
+                  JvOptionPane.WARNING_MESSAGE, null, buttonsText,
+                  cancelQuit);
+        }
+        else
+        {
+          confirmResponse = Cache
+                  .getDefault("ALWAYS_CLOSE_EXTERNAL_VIEWERS", false)
+                          ? JvOptionPane.YES_OPTION
+                          : JvOptionPane.NO_OPTION;
+        }
 
         if (confirmResponse == JvOptionPane.CANCEL_OPTION)
         {
@@ -240,7 +266,7 @@ public class QuitHandler
       }
     }
 
-    Callable<Void> next = null;
+    Runnable next = null;
     switch (gotQuitResponse())
     {
     case QUIT:
@@ -281,16 +307,15 @@ public class QuitHandler
     return gotQuitResponse();
   }
 
-  private static QResponse waitQuit(boolean interactive,
-          Callable<Void> okQuit, Callable<Void> forceQuit,
-          Callable<Void> cancelQuit)
+  private static QResponse waitQuit(boolean interactive, Runnable okQuit,
+          Runnable forceQuit, Runnable cancelQuit)
   {
     // check for saves in progress
     if (!BackupFiles.hasSavesInProgress())
       return QResponse.QUIT;
 
     int size = 0;
-    AlignFrame[] afArray = Desktop.getAlignFrames();
+    AlignFrame[] afArray = Desktop.getDesktopAlignFrames();
     if (!(afArray == null || afArray.length == 0))
     {
       for (int i = 0; i < afArray.length; i++)