From 5db57ea2e44da7e8f3d5ff0866b0b81cd4458aeb Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Mon, 24 Oct 2022 18:08:00 +0100 Subject: [PATCH] JAL-1988 JAL-3772 wait timer working first time but blocking desktop gui updates --- src/jalview/gui/JvOptionPane.java | 11 +++++++- src/jalview/io/BackupFiles.java | 7 ++++- src/jalview/jbgui/QuitHandler.java | 53 ++++++++++++++++++------------------ 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/src/jalview/gui/JvOptionPane.java b/src/jalview/gui/JvOptionPane.java index c5427cb..346bbf9 100644 --- a/src/jalview/gui/JvOptionPane.java +++ b/src/jalview/gui/JvOptionPane.java @@ -929,6 +929,16 @@ public class JvOptionPane extends JOptionPane int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options, Object initialValue, boolean modal) { + showDialogOnTopAsync(new JFrame(), label, actionString, + JOPTIONPANE_OPTION, JOPTIONPANE_MESSAGETYPE, icon, options, + initialValue, modal); + } + + public void showDialogOnTopAsync(JFrame dialogParent, String label, + String actionString, int JOPTIONPANE_OPTION, + int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options, + Object initialValue, boolean modal) + { // Ensure Jalview window is brought to front (primarily for Quit // confirmation window to be visible) @@ -938,7 +948,6 @@ public class JvOptionPane extends JOptionPane // A better hack which works is to create a new JFrame parent with // setAlwaysOnTop(true) - JFrame dialogParent = new JFrame(); dialogParent.setAlwaysOnTop(true); parentComponent = dialogParent; diff --git a/src/jalview/io/BackupFiles.java b/src/jalview/io/BackupFiles.java index 88524d1..3024edf 100644 --- a/src/jalview/io/BackupFiles.java +++ b/src/jalview/io/BackupFiles.java @@ -166,7 +166,12 @@ public class BackupFiles public static boolean hasSavesInProgress() { - return savesInProgress.size() > 0; + boolean has = false; + for (CompletableFuture cf : savesInProgressCompletableFutures()) + { + has |= !cf.isDone(); + } + return has; } public static List savesInProgressFiles() diff --git a/src/jalview/jbgui/QuitHandler.java b/src/jalview/jbgui/QuitHandler.java index 6369b1a..db8b500 100644 --- a/src/jalview/jbgui/QuitHandler.java +++ b/src/jalview/jbgui/QuitHandler.java @@ -260,6 +260,8 @@ public class QuitHandler waitTime = Math.max(waitTime, size / 2); Console.debug("Set waitForSave to " + waitTime); } + final int waitTimeFinal = waitTime; + QResponse waitResponse = QResponse.NULL; // future that returns a Boolean when all files are saved CompletableFuture filesAllSaved = new CompletableFuture<>(); @@ -277,26 +279,21 @@ public class QuitHandler }); } - final int waitTimeFinal = waitTime; // timeout the wait -- will result in another wait button when looped CompletableFuture waitTimeout = CompletableFuture .supplyAsync(() -> { - executor.submit(() -> { - try - { - Thread.sleep(waitTimeFinal); - } catch (InterruptedException e) - { - // probably interrupted by all files saving - } - }); + Console.debug("################# STARTING WAIT SLEEP"); + try + { + Thread.sleep(waitTimeFinal); + } catch (InterruptedException e) + { + // probably interrupted by all files saving + } return true; }); CompletableFuture waitForSave = CompletableFuture .anyOf(waitTimeout, filesAllSaved); - Console.debug("##### WAITFORSAVE RUNNING"); - - QResponse waitResponse = QResponse.NULL; int iteration = 0; boolean doIterations = true; @@ -305,16 +302,17 @@ public class QuitHandler { try { - waitForSave.get(); + waitForSave.copy().get(); } catch (InterruptedException | ExecutionException e1) { Console.debug( - "Exception whilst waiting for files to save before quitting", + "Exception whilst waiting for files to save before quit", e1); } - if (interactive) + if (interactive && BackupFiles.hasSavesInProgress()) { Console.debug("********************About to make waitDialog"); + JFrame parent = new JFrame(); JvOptionPane waitDialog = JvOptionPane.newOptionDialog() .setResponseHandler(JOptionPane.YES_OPTION, defaultOkQuit) .setResponseHandler(JOptionPane.NO_OPTION, forceQuit) @@ -324,23 +322,24 @@ public class QuitHandler for (CompletableFuture cf : BackupFiles .savesInProgressCompletableFutures()) { - // update the list of saving files as they save too - cf.thenRun(() -> { - waitDialog.setMessage(waitingForSaveMessage()); - }); - // if this is the last one then close the dialog cf.whenComplete((ret, e) -> { + Console.debug("############# A FILE SAVED!"); + // update the list of saving files as they save too + waitDialog.setMessage(waitingForSaveMessage()); + waitDialog.setName("AAARGH!"); + // if this is the last one then close the dialog if (!BackupFiles.hasSavesInProgress()) { - // like a click on Wait button + // like a click on Wait button ??? Console.debug( "***** TRYING TO MAKE THE WAIT FOR SAVE DIALOG DISAPPEAR!"); waitDialog.setValue(JOptionPane.YES_OPTION); + parent.dispose(); } }); } - waitDialog.showDialogOnTopAsync(waitingForSaveMessage(), + waitDialog.showDialogOnTopAsync(parent, waitingForSaveMessage(), MessageManager.getString("action.wait"), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, new Object[] @@ -350,9 +349,9 @@ public class QuitHandler MessageManager.getString("action.wait"), true); Console.debug("********************Finished waitDialog"); - waitResponse = gotQuitResponse(); - Console.debug("####### WAITFORSAVE SET: " + waitResponse); - switch (waitResponse) + final QResponse thisWaitResponse = gotQuitResponse(); + Console.debug("####### WAITFORSAVE SET: " + thisWaitResponse); + switch (thisWaitResponse) { case QUIT: // wait -- do another iteration break; @@ -369,7 +368,7 @@ public class QuitHandler } } // end if interactive - } + } // end while wait iteration loop waitResponse = gotQuitResponse(); Console.debug("####### WAITFORSAVE RETURNING: " + waitResponse); -- 1.7.10.2