From 772049d78841aff3a1d40386eb9536d8aef8b644 Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Thu, 27 Oct 2022 18:51:22 +0100 Subject: [PATCH] JAL-1988 Allow generic Container object in JvOptionPane showDialog. Fix dynamically saving files in the wait for save dialog --- resources/lang/Messages.properties | 2 + resources/lang/Messages_es.properties | 2 + src/jalview/gui/JvOptionPane.java | 6 +- src/jalview/io/BackupFiles.java | 13 ++-- src/jalview/jbgui/QuitHandler.java | 108 ++++++++------------------------- 5 files changed, 39 insertions(+), 92 deletions(-) diff --git a/resources/lang/Messages.properties b/resources/lang/Messages.properties index 73da48c..1f57096 100644 --- a/resources/lang/Messages.properties +++ b/resources/lang/Messages.properties @@ -37,6 +37,8 @@ label.quit_jalview = Are you sure you want to quit Jalview? label.unsaved_changes = There are unsaved changes. label.save_in_progress = Some files are still saving: label.unknown = Unknown +label.all_saved = All files saved. +label.quitting_bye = Quitting, bye! action.wait = Wait action.cancel_quit = Cancel quit action.expand_views = Expand Views diff --git a/resources/lang/Messages_es.properties b/resources/lang/Messages_es.properties index d9a64b3..d1b6b4e 100644 --- a/resources/lang/Messages_es.properties +++ b/resources/lang/Messages_es.properties @@ -37,6 +37,8 @@ label.quit_jalview = label.unsaved_changes = Hay cambios sin guardar. label.save_in_progress = Algunos archivos aún se están guardando: label.unknown = desconocido +label.all_saved = Todos los archivos guardados. +label.quitting_bye = Saliendo ¡chao! action.wait = Espere action.cancel_quit = Cancelar la salida action.expand_views = Expandir vistas diff --git a/src/jalview/gui/JvOptionPane.java b/src/jalview/gui/JvOptionPane.java index 346bbf9..02bca5a 100644 --- a/src/jalview/gui/JvOptionPane.java +++ b/src/jalview/gui/JvOptionPane.java @@ -777,7 +777,7 @@ public class JvOptionPane extends JOptionPane initialValue, true); } - public void showDialog(String message, String title, int optionType, + public void showDialog(Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue, boolean modal) { @@ -934,7 +934,7 @@ public class JvOptionPane extends JOptionPane initialValue, modal); } - public void showDialogOnTopAsync(JFrame dialogParent, String label, + public void showDialogOnTopAsync(JFrame dialogParent, Object label, String actionString, int JOPTIONPANE_OPTION, int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options, Object initialValue, boolean modal) @@ -1009,7 +1009,7 @@ public class JvOptionPane extends JOptionPane /** * Create a non-modal confirm dialog */ - public JDialog createDialog(Component parentComponent, String message, + public JDialog createDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue, boolean modal) { diff --git a/src/jalview/io/BackupFiles.java b/src/jalview/io/BackupFiles.java index 3024edf..9112042 100644 --- a/src/jalview/io/BackupFiles.java +++ b/src/jalview/io/BackupFiles.java @@ -167,29 +167,32 @@ public class BackupFiles public static boolean hasSavesInProgress() { boolean has = false; - for (CompletableFuture cf : savesInProgressCompletableFutures()) + for (CompletableFuture cf : savesInProgressCompletableFutures(true)) { has |= !cf.isDone(); } return has; } - public static List savesInProgressFiles() + public static List savesInProgressFiles(boolean all) { List files = new ArrayList<>(); for (BackupFiles bfile : savesInProgress) { - files.add(bfile.getFile()); + if (all || !bfile.getMyFuture().isDone()) + files.add(bfile.getFile()); } return files; } - public static List> savesInProgressCompletableFutures() + public static List> savesInProgressCompletableFutures( + boolean all) { List> cfs = new ArrayList<>(); for (BackupFiles bfile : savesInProgress) { - cfs.add(bfile.getMyFuture()); + if (all || !bfile.getMyFuture().isDone()) + cfs.add(bfile.getMyFuture()); } return cfs; } diff --git a/src/jalview/jbgui/QuitHandler.java b/src/jalview/jbgui/QuitHandler.java index db8b500..ac14a2f 100644 --- a/src/jalview/jbgui/QuitHandler.java +++ b/src/jalview/jbgui/QuitHandler.java @@ -10,6 +10,7 @@ import java.util.concurrent.Executors; import javax.swing.JFrame; import javax.swing.JOptionPane; +import javax.swing.JTextPane; import com.formdev.flatlaf.extras.FlatDesktop; @@ -182,17 +183,6 @@ public class QuitHandler if (Cache.getDefault("WAIT_FOR_SAVE", true) && BackupFiles.hasSavesInProgress()) { - /* - Future waitGot = executor.submit(waitQuitCall); - try - { - got = waitGot.get(); - } catch (InterruptedException | ExecutionException e) - { - jalview.bin.Console.debug( - "Exception during quit handling (wait for save)", e); - } - */ QResponse waitResponse = waitQuit(interactive, okQuit, forceQuit, cancelQuit); wait = waitResponse == QResponse.QUIT; @@ -268,7 +258,7 @@ public class QuitHandler // callback as each file finishes saving for (CompletableFuture cf : BackupFiles - .savesInProgressCompletableFutures()) + .savesInProgressCompletableFutures(false)) { // if this is the last one then complete filesAllSaved cf.whenComplete((ret, e) -> { @@ -318,28 +308,29 @@ public class QuitHandler .setResponseHandler(JOptionPane.NO_OPTION, forceQuit) .setResponseHandler(JOptionPane.CANCEL_OPTION, cancelQuit); + JTextPane messagePane = new JTextPane(); + messagePane.setBackground(waitDialog.getBackground()); + messagePane.setBorder(null); + messagePane.setText(waitingForSaveMessage()); // callback as each file finishes saving for (CompletableFuture cf : BackupFiles - .savesInProgressCompletableFutures()) + .savesInProgressCompletableFutures(false)) { 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!"); + messagePane.setText(waitingForSaveMessage()); // if this is the last one then close the dialog if (!BackupFiles.hasSavesInProgress()) { - // like a click on Wait button ??? - Console.debug( - "***** TRYING TO MAKE THE WAIT FOR SAVE DIALOG DISAPPEAR!"); + // like a click on Wait button waitDialog.setValue(JOptionPane.YES_OPTION); parent.dispose(); } }); } - waitDialog.showDialogOnTopAsync(parent, waitingForSaveMessage(), + waitDialog.showDialogOnTopAsync(parent, messagePane, MessageManager.getString("action.wait"), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, new Object[] @@ -375,56 +366,6 @@ public class QuitHandler return waitResponse; }; - public static void okk() - { - /* - if (false) - { - if (false) - { - - waitLonger = JOptionPane.showOptionDialog(dialogParent, - waitingForSaveMessage(), - MessageManager.getString("action.wait"), - JOptionPane.YES_NO_CANCEL_OPTION, - JOptionPane.WARNING_MESSAGE, null, options, wait); - } - else - { - // non-interactive - waitLonger = iteration < NON_INTERACTIVE_WAIT_CYCLES - ? JOptionPane.YES_OPTION - : JOptionPane.NO_OPTION; - } - - if (waitLonger == JOptionPane.YES_OPTION) // "wait" - { - saving = !waitForSave(waitIncrement); - } - else if (waitLonger == JOptionPane.NO_OPTION) // "force - // quit" - { - // do a force quit - return setResponse(QResponse.FORCE_QUIT); - } - else if (waitLonger == JOptionPane.CANCEL_OPTION) // cancel quit - { - return setResponse(QResponse.CANCEL_QUIT); - } - else - { - // Most likely got here by user dismissing the dialog with the - // 'x' - // -- treat as a "Cancel" - return setResponse(QResponse.CANCEL_QUIT); - } - } - - // not sure how we got here, best be safe - return QResponse.CANCEL_QUIT; - */ - }; - private static int waitForceQuitCancelQuitOptionDialog(Object message, String title) { @@ -435,34 +376,33 @@ public class QuitHandler MessageManager.getString("action.force_quit"), MessageManager.getString("action.cancel_quit") }; - // BackupFiles.setWaitForSaveDialog(dialogParent); - int answer = JOptionPane.showOptionDialog(dialogParent, message, title, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, options, wait); - // BackupFiles.clearWaitForSaveDialog(); - return answer; } private static String waitingForSaveMessage() { - StringBuilder messageSB = new StringBuilder( - MessageManager.getString("label.save_in_progress")); - boolean any = false; - for (File file : BackupFiles.savesInProgressFiles()) + StringBuilder messageSB = new StringBuilder(); + + List files = BackupFiles.savesInProgressFiles(false); + boolean any = files.size() > 0; + if (any) { - messageSB.append("\n- "); - messageSB.append(file.getName()); - any = true; + messageSB.append(MessageManager.getString("label.save_in_progress")); + for (File file : files) + { + messageSB.append("\n- ").append(file.getName()); + } } - if (!any) + else { - messageSB.append("\n"); - messageSB.append(MessageManager.getString("label.unknown")); + messageSB.append(MessageManager.getString("label.all_saved")) + .append("\n") + .append(MessageManager.getString("label.quitting_bye")); } - return messageSB.toString(); } -- 1.7.10.2