JAL-4125 Additions to JvOptionPane and QuitHandler with StructureViewerBase handling...
[jalview.git] / src / jalview / gui / QuitHandler.java
index 77eed81..ae12e7d 100644 (file)
@@ -7,6 +7,7 @@ import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.concurrent.RejectedExecutionException;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 
@@ -145,22 +146,23 @@ public class QuitHandler
 
     if (confirmQuit)
     {
-      JvOptionPane.newOptionDialog()
+      setQuitDialog(JvOptionPane.newOptionDialog()
               .setResponseHandler(JOptionPane.YES_OPTION, defaultOkQuit)
-              .setResponseHandler(JOptionPane.NO_OPTION, cancelQuit)
-              .showDialogOnTopAsync(
-                      new StringBuilder(MessageManager
-                              .getString("label.quit_jalview"))
-                              .append("\n")
-                              .append(MessageManager
-                                      .getString("label.unsaved_changes"))
-                              .toString(),
-                      MessageManager.getString("action.quit"),
-                      JOptionPane.YES_NO_OPTION,
-                      JOptionPane.QUESTION_MESSAGE, null, new Object[]
-                      { MessageManager.getString("action.quit"),
-                          MessageManager.getString("action.cancel") },
-                      MessageManager.getString("action.quit"), true);
+              .setResponseHandler(JOptionPane.NO_OPTION, cancelQuit));
+      JvOptionPane qd = getQuitDialog();
+      qd.showDialogOnTopAsync(
+              new StringBuilder(
+                      MessageManager.getString("label.quit_jalview"))
+                      .append("\n")
+                      .append(MessageManager
+                              .getString("label.unsaved_changes"))
+                      .toString(),
+              MessageManager.getString("action.quit"),
+              JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
+              new Object[]
+              { MessageManager.getString("action.quit"),
+                  MessageManager.getString("action.cancel") },
+              MessageManager.getString("action.quit"), true);
     }
 
     got = gotQuitResponse();
@@ -201,6 +203,13 @@ public class QuitHandler
     {
       executor.submit(next).get();
       got = gotQuitResponse();
+    } catch (RejectedExecutionException e)
+    {
+      // QuitHander.abortQuit() probably called
+      // CANCEL_QUIT test will reset QuitHandler
+      Console.info("Quit aborted!");
+      got = QResponse.NULL;
+      setResponse(QResponse.NULL);
     } catch (InterruptedException | ExecutionException e)
     {
       jalview.bin.Console
@@ -208,9 +217,10 @@ public class QuitHandler
     }
     setResponse(got);
 
-    if (gotQuitResponse() == QResponse.CANCEL_QUIT)
+    if (quitCancelled())
     {
       // reset if cancelled
+      Console.debug("Quit cancelled");
       setResponse(QResponse.NULL);
       return QResponse.CANCEL_QUIT;
     }
@@ -305,8 +315,7 @@ public class QuitHandler
             }
             else
             {
-              if (!(QuitHandler.gotQuitResponse() == QResponse.CANCEL_QUIT
-                      || QuitHandler.gotQuitResponse() == QResponse.NULL))
+              if (!(quitCancelled()))
               {
                 for (int i = 0; i < buttons.length; i++)
                 {
@@ -406,6 +415,31 @@ public class QuitHandler
 
   public static void abortQuit()
   {
-    setResponse(QResponse.CANCEL_QUIT);
+    setResponse(QResponse.NULL);
+    // executor.shutdownNow();
+  }
+
+  private static JvOptionPane quitDialog = null;
+
+  private static void setQuitDialog(JvOptionPane qd)
+  {
+    quitDialog = qd;
+  }
+
+  private static JvOptionPane getQuitDialog()
+  {
+    return quitDialog;
+  }
+
+  public static boolean quitCancelled()
+  {
+    return QuitHandler.gotQuitResponse() == QResponse.CANCEL_QUIT
+            || QuitHandler.gotQuitResponse() == QResponse.NULL;
+  }
+
+  public static boolean quitting()
+  {
+    return QuitHandler.gotQuitResponse() == QResponse.QUIT
+            || QuitHandler.gotQuitResponse() == QResponse.FORCE_QUIT;
   }
 }
\ No newline at end of file