JAL-1988 JAL-3772 Removed 'Wait' button from waitDialog, no iterations for GUI. Added...
[jalview.git] / src / jalview / jbgui / QuitHandler.java
index ef8ec55..0d73006 100644 (file)
@@ -10,6 +10,7 @@ import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 
+import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.JOptionPane;
 import javax.swing.JTextPane;
@@ -31,7 +32,7 @@ import jalview.util.Platform;
 
 public class QuitHandler
 {
-  private static final int MIN_WAIT_FOR_SAVE = 5000;
+  private static final int MIN_WAIT_FOR_SAVE = 3000;
 
   private static final int MAX_WAIT_FOR_SAVE = 20000;
 
@@ -247,7 +248,8 @@ public class QuitHandler
     QResponse waitResponse = QResponse.NULL;
 
     int iteration = 0;
-    boolean doIterations = true;
+    boolean doIterations = true; // note iterations not used in the gui now,
+                                 // only one pass without the "Wait" button
     while (doIterations && BackupFiles.hasSavesInProgress()
             && iteration++ < (interactive ? 100 : 5))
     {
@@ -281,24 +283,11 @@ public class QuitHandler
 
       if (interactive && BackupFiles.hasSavesInProgress())
       {
-        boolean allowForceQuit = iteration > 0; // iteration > 1 to not show
-                                                // force quit the first time
+        boolean showForceQuit = iteration > 0; // iteration > 1 to not show
+                                               // force quit the first time
         JFrame parent = new JFrame();
+        JButton[] buttons = { new JButton(), new JButton() };
         JvOptionPane waitDialog = JvOptionPane.newOptionDialog();
-        if (allowForceQuit)
-        {
-          waitDialog
-                  .setResponseHandler(JOptionPane.YES_OPTION, defaultOkQuit)
-                  .setResponseHandler(JOptionPane.NO_OPTION, forceQuit)
-                  .setResponseHandler(JOptionPane.CANCEL_OPTION,
-                          cancelQuit);
-        }
-        else
-        {
-          waitDialog
-                  .setResponseHandler(JOptionPane.YES_OPTION, defaultOkQuit)
-                  .setResponseHandler(JOptionPane.NO_OPTION, cancelQuit);
-        }
         JTextPane messagePane = new JTextPane();
         messagePane.setBackground(waitDialog.getBackground());
         messagePane.setBorder(null);
@@ -309,48 +298,64 @@ public class QuitHandler
         {
           cf.whenComplete((ret, e) -> {
             if (BackupFiles.hasSavesInProgress())
+            {
               // update the list of saving files as they save too
               messagePane.setText(waitingForSaveMessage());
+            }
             else
             {
-              // if this is the last one then close the dialog
-              messagePane.setText(new StringBuilder()
-                      .append(MessageManager.getString("label.all_saved"))
-                      .append("\n").append(MessageManager
-                              .getString("label.quitting_bye"))
-                      .toString());
-              try
-              {
-                Console.debug("WAITING FOR MESSAGE");
-                Thread.sleep(500);
-              } catch (InterruptedException e1)
+              if (!(QuitHandler.gotQuitResponse() == QResponse.CANCEL_QUIT
+                      || QuitHandler.gotQuitResponse() == QResponse.NULL))
               {
+                for (int i = 0; i < buttons.length; i++)
+                {
+                  Console.debug("DISABLING BUTTON " + buttons[i].getText());
+                  buttons[i].setEnabled(false);
+                  buttons[i].setVisible(false);
+                }
+                // if this is the last one then close the dialog
+                messagePane.setText(new StringBuilder()
+                        .append(MessageManager.getString("label.all_saved"))
+                        .append("\n")
+                        .append(MessageManager
+                                .getString("label.quitting_bye"))
+                        .toString());
+                messagePane.setEditable(false);
+                try
+                {
+                  Thread.sleep(1500);
+                } catch (InterruptedException e1)
+                {
+                }
+                parent.dispose();
               }
-              // like a click on Wait button
-              waitDialog.setValue(JOptionPane.YES_OPTION);
-              parent.dispose();
             }
           });
         }
 
-        String[] options = new String[allowForceQuit ? 3 : 2];
-        if (allowForceQuit)
+        String[] options;
+        int dialogType = -1;
+        if (showForceQuit)
         {
-          options[0] = MessageManager.getString("action.wait");
-          options[1] = MessageManager.getString("action.force_quit");
-          options[2] = MessageManager.getString("action.cancel_quit");
+          options = new String[2];
+          options[0] = MessageManager.getString("action.force_quit");
+          options[1] = MessageManager.getString("action.cancel_quit");
+          dialogType = JOptionPane.YES_NO_OPTION;
+          waitDialog.setResponseHandler(JOptionPane.YES_OPTION, forceQuit)
+                  .setResponseHandler(JOptionPane.NO_OPTION, cancelQuit);
         }
         else
         {
-          options[0] = MessageManager.getString("action.wait");
-          options[1] = MessageManager.getString("action.cancel_quit");
+          options = new String[1];
+          options[0] = MessageManager.getString("action.cancel_quit");
+          dialogType = JOptionPane.YES_OPTION;
+          waitDialog.setResponseHandler(JOptionPane.YES_OPTION, cancelQuit);
         }
         waitDialog.showDialogOnTopAsync(parent, messagePane,
-                MessageManager.getString("action.wait"),
-                allowForceQuit ? JOptionPane.YES_NO_CANCEL_OPTION
-                        : JOptionPane.YES_NO_OPTION,
+                MessageManager.getString("label.wait_for_save"), dialogType,
                 JOptionPane.WARNING_MESSAGE, null, options,
-                MessageManager.getString("action.wait"), true);
+                MessageManager.getString("action.cancel_quit"), true,
+                buttons);
 
         parent.dispose();
         final QResponse thisWaitResponse = gotQuitResponse();
@@ -377,23 +382,6 @@ public class QuitHandler
     return waitResponse;
   };
 
-  private static int waitForceQuitCancelQuitOptionDialog(Object message,
-          String title)
-  {
-    JFrame dialogParent = new JFrame();
-    dialogParent.setAlwaysOnTop(true);
-    String wait = MessageManager.getString("action.wait");
-    Object[] options = { wait,
-        MessageManager.getString("action.force_quit"),
-        MessageManager.getString("action.cancel_quit") };
-
-    int answer = JOptionPane.showOptionDialog(dialogParent, message, title,
-            JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE,
-            null, options, wait);
-
-    return answer;
-  }
-
   private static String waitingForSaveMessage()
   {
     StringBuilder messageSB = new StringBuilder();
@@ -405,14 +393,15 @@ public class QuitHandler
     {
       for (File file : files)
       {
-        messageSB.append("\n- ").append(file.getName());
+        messageSB.append("\n\u2022 ").append(file.getName());
       }
     }
     else
     {
       messageSB.append(MessageManager.getString("label.unknown"));
     }
+    messageSB.append("\n\n")
+            .append(MessageManager.getString("label.quit_after_saving"));
     return messageSB.toString();
   }
-
 }
\ No newline at end of file