JAL-4125 Additions to JvOptionPane and QuitHandler with StructureViewerBase handling...
[jalview.git] / src / jalview / gui / StructureViewerBase.java
index ec5579c..9a575ff 100644 (file)
@@ -26,6 +26,7 @@ import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.ItemEvent;
 import java.awt.event.ItemListener;
+import java.beans.PropertyVetoException;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileOutputStream;
@@ -36,6 +37,9 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Random;
 import java.util.Vector;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.TimeUnit;
 
 import javax.swing.ButtonGroup;
 import javax.swing.JCheckBoxMenuItem;
@@ -135,6 +139,7 @@ public abstract class StructureViewerBase extends GStructureViewer
   public StructureViewerBase()
   {
     super();
+    setFrameIcon(null);
   }
 
   /**
@@ -1267,19 +1272,33 @@ public abstract class StructureViewerBase extends GStructureViewer
       if (!forceClose)
       {
         String viewerName = getViewerName();
+
+        int confirm = JvOptionPane.CANCEL_OPTION;
         String prompt = MessageManager
                 .formatMessage("label.confirm_close_viewer", new Object[]
                 { binding.getViewerTitle(viewerName, false), viewerName });
         prompt = JvSwingUtils.wrapTooltip(true, prompt);
-        int confirm = JvOptionPane.showConfirmDialog(this, prompt,
-                MessageManager.getString("label.close_viewer"),
-                JvOptionPane.YES_NO_CANCEL_OPTION);
+        String title = MessageManager.getString("label.close_viewer");
+        confirm = showCloseDialog(title, prompt);
+
         /*
          * abort closure if user hits escape or Cancel
          */
         if (confirm == JvOptionPane.CANCEL_OPTION
                 || confirm == JvOptionPane.CLOSED_OPTION)
         {
+          // abort possible quit handling if CANCEL chosen
+          if (confirm == JvOptionPane.CANCEL_OPTION)
+          {
+            try
+            {
+              // this is a bit futile
+              this.setClosed(false);
+            } catch (PropertyVetoException e)
+            {
+            }
+            QuitHandler.abortQuit();
+          }
           return;
         }
         forceClose = confirm == JvOptionPane.YES_OPTION;
@@ -1299,6 +1318,59 @@ public abstract class StructureViewerBase extends GStructureViewer
     dispose();
   }
 
+  private int showCloseDialog(final String title, final String prompt)
+  {
+    confirmResponse = JvOptionPane.CANCEL_OPTION;
+
+    if (QuitHandler.quitting())
+    {
+
+      Callable<Void> yesCall = () -> {
+        Console.debug("YES");
+        confirmResponse = JvOptionPane.YES_OPTION;
+        return null;
+      };
+      Callable<Void> noCall = () -> {
+        Console.debug("NO");
+        confirmResponse = JvOptionPane.NO_OPTION;
+        return null;
+      };
+      Callable<Void> cancelCall = () -> {
+        Console.debug("CANCEL");
+        confirmResponse = JvOptionPane.CANCEL_OPTION;
+        return null;
+      };
+      Callable<Void>[] calls = new Callable[] { yesCall, noCall,
+          cancelCall };
+      String cancelQuit = MessageManager.getString("action.cancel_quit");
+      String[] buttonsText = { MessageManager.getString("action.yes"),
+          MessageManager.getString("action.no"), cancelQuit };
+      JvOptionPane dialog = JvOptionPane.frameDialog(prompt,
+              MessageManager.getString("label.close_viewer"),
+              JvOptionPane.WARNING_MESSAGE, buttonsText, cancelQuit, calls,
+              false);
+      // wait for response
+      ExecutorService executor = dialog.getExecutor();
+      executor.shutdown();
+      try
+      {
+        Console.debug("### executor.awaitTermination() starting");
+        executor.awaitTermination(60, TimeUnit.SECONDS);
+        Console.debug("### executor.awaitTermination() finished");
+      } catch (InterruptedException e)
+      {
+      }
+    }
+    else
+    {
+      confirmResponse = JvOptionPane.showConfirmDialog(this, prompt,
+              MessageManager.getString("label.close_viewer"),
+              JvOptionPane.YES_NO_CANCEL_OPTION,
+              JvOptionPane.WARNING_MESSAGE);
+    }
+    return confirmResponse;
+  }
+
   @Override
   public void showHelp_actionPerformed()
   {
@@ -1329,4 +1401,6 @@ public abstract class StructureViewerBase extends GStructureViewer
             && viewerActionMenu.getItemCount() > 0
             && viewerActionMenu.isVisible();
   }
+
+  private static int confirmResponse = 0;
 }