JAL-1551 spotlessApply
[jalview.git] / src / jalview / gui / QuitHandler.java
index ae12e7d..79723cf 100644 (file)
@@ -41,6 +41,18 @@ public class QuitHandler
     NULL, QUIT, CANCEL_QUIT, FORCE_QUIT
   };
 
+  public static enum Message
+  {
+    UNSAVED_CHANGES, UNSAVED_ALIGNMENTS
+  };
+
+  protected static Message message = Message.UNSAVED_CHANGES;
+
+  public static void setMessage(Message m)
+  {
+    message = m;
+  }
+
   private static ExecutorService executor = Executors.newFixedThreadPool(3);
 
   public static QResponse setQuitHandler()
@@ -146,6 +158,10 @@ public class QuitHandler
 
     if (confirmQuit)
     {
+      String messageString = MessageManager
+              .getString(message == Message.UNSAVED_ALIGNMENTS
+                      ? "label.unsaved_alignments"
+                      : "label.unsaved_changes");
       setQuitDialog(JvOptionPane.newOptionDialog()
               .setResponseHandler(JOptionPane.YES_OPTION, defaultOkQuit)
               .setResponseHandler(JOptionPane.NO_OPTION, cancelQuit));
@@ -153,10 +169,8 @@ public class QuitHandler
       qd.showDialogOnTopAsync(
               new StringBuilder(
                       MessageManager.getString("label.quit_jalview"))
-                      .append("\n")
-                      .append(MessageManager
-                              .getString("label.unsaved_changes"))
-                      .toString(),
+                              .append("\n").append(messageString)
+                              .toString(),
               MessageManager.getString("action.quit"),
               JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
               new Object[]
@@ -166,6 +180,46 @@ public class QuitHandler
     }
 
     got = gotQuitResponse();
+
+    // check for external viewer frames
+    if (got != QResponse.CANCEL_QUIT)
+    {
+      int count = Desktop.instance.structureViewersStillRunningCount();
+      if (count > 0)
+      {
+        String prompt = MessageManager
+                .formatMessage(count == 1 ? "label.confirm_quit_viewer"
+                        : "label.confirm_quit_viewers");
+        String title = MessageManager.getString(
+                count == 1 ? "label.close_viewer" : "label.close_viewers");
+        String cancelQuitText = MessageManager
+                .getString("action.cancel_quit");
+        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);
+
+        if (confirmResponse == JvOptionPane.CANCEL_OPTION)
+        {
+          // Cancel Quit
+          QuitHandler.setResponse(QResponse.CANCEL_QUIT);
+        }
+        else
+        {
+          // Close viewers/Leave viewers open
+          StructureViewerBase
+                  .setQuitClose(confirmResponse == JvOptionPane.YES_OPTION);
+        }
+      }
+
+    }
+
+    got = gotQuitResponse();
+
     boolean wait = false;
     if (got == QResponse.CANCEL_QUIT)
     {
@@ -442,4 +496,4 @@ public class QuitHandler
     return QuitHandler.gotQuitResponse() == QResponse.QUIT
             || QuitHandler.gotQuitResponse() == QResponse.FORCE_QUIT;
   }
-}
\ No newline at end of file
+}