X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FQuitHandler.java;fp=src%2Fjalview%2Fgui%2FQuitHandler.java;h=9a44306b75f5bcc8de9283a894dce45ba2b2ba54;hb=a939b4c646235d460df3b7b9377ef8d8569caaff;hp=77eed81514408ccccc1f81f19de4a0d4065a71a3;hpb=8135460a52c0551e4a40777d077843fd4e19c049;p=jalview.git diff --git a/src/jalview/gui/QuitHandler.java b/src/jalview/gui/QuitHandler.java index 77eed81..9a44306 100644 --- a/src/jalview/gui/QuitHandler.java +++ b/src/jalview/gui/QuitHandler.java @@ -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; @@ -40,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() @@ -145,25 +158,67 @@ public class QuitHandler if (confirmQuit) { - JvOptionPane.newOptionDialog() + 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) - .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(messageString).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(); + + // 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) { @@ -201,6 +256,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 +270,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 +368,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 +468,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 +}