From d90302c57e61e425f6a8fc52f6ab59ac28d32310 Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Thu, 3 Nov 2022 09:10:24 +0000 Subject: [PATCH] JAL-1988 JAL-3772 Tidying of unspecified Callable declarations. Use SwingUtilities.getAncestorOfClass() --- src/jalview/gui/Desktop.java | 4 +- src/jalview/gui/JvOptionPane.java | 51 ++++++----------- src/jalview/gui/QuitHandler.java | 64 +++++++++++----------- src/jalview/util/dialogrunner/DialogRunnerI.java | 2 +- 4 files changed, 53 insertions(+), 68 deletions(-) diff --git a/src/jalview/gui/Desktop.java b/src/jalview/gui/Desktop.java index 94145bd..9e3eb55 100644 --- a/src/jalview/gui/Desktop.java +++ b/src/jalview/gui/Desktop.java @@ -1349,7 +1349,7 @@ public class Desktop extends jalview.jbgui.GDesktop public QuitHandler.QResponse desktopQuit(boolean ui, boolean disposeFlag) { - final Callable doDesktopQuit = () -> { + final Callable doDesktopQuit = () -> { Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); Cache.setProperty("SCREENGEOMETRY_WIDTH", screen.width + ""); Cache.setProperty("SCREENGEOMETRY_HEIGHT", screen.height + ""); @@ -1397,7 +1397,7 @@ public class Desktop extends jalview.jbgui.GDesktop } instance.quit(); - return QuitHandler.gotQuitResponse(); + return null; // Void }; return QuitHandler.getQuitResponse(ui, doDesktopQuit, doDesktopQuit, diff --git a/src/jalview/gui/JvOptionPane.java b/src/jalview/gui/JvOptionPane.java index 10dbfcf..b58ea41 100644 --- a/src/jalview/gui/JvOptionPane.java +++ b/src/jalview/gui/JvOptionPane.java @@ -22,7 +22,6 @@ package jalview.gui; import java.awt.Component; -import java.awt.Container; import java.awt.Dialog.ModalityType; import java.awt.HeadlessException; import java.awt.Window; @@ -45,6 +44,7 @@ import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; +import javax.swing.SwingUtilities; import javax.swing.UIManager; import jalview.util.Platform; @@ -61,7 +61,7 @@ public class JvOptionPane extends JOptionPane private Component parentComponent; - private Map callbacks = new HashMap<>(); + private Map> callbacks = new HashMap<>(); /* * JalviewJS reports user choice in the dialog as the selected @@ -836,7 +836,7 @@ public class JvOptionPane extends JOptionPane initialValueButton = jb; int buttonAction = buttonActions[i]; - Callable action = callbacks.get(buttonAction); + Callable action = callbacks.get(buttonAction); jb.setText((String) o); jb.addActionListener(new ActionListener() { @@ -845,8 +845,15 @@ public class JvOptionPane extends JOptionPane { Object obj = e.getSource(); - Object joptionpaneObject = getAncestorClass(obj, - JOptionPane.class); + if (obj == null || !(obj instanceof Component)) + { + jalview.bin.Console.debug( + "Could not find Component source of event object " + + obj); + return; + } + Object joptionpaneObject = SwingUtilities.getAncestorOfClass( + JOptionPane.class, (Component) obj); if (joptionpaneObject == null || !(joptionpaneObject instanceof JOptionPane)) { @@ -859,11 +866,10 @@ public class JvOptionPane extends JOptionPane joptionpane.setValue(buttonAction); if (action != null) Executors.newSingleThreadExecutor().submit(action); - // joptionpane.transferFocusBackward(); joptionpane.transferFocusBackward(); joptionpane.setVisible(false); - // put focus and raise parent window if possible, unless cancel - // button pressed + // put focus and raise parent window if possible, unless cancel or + // no button pressed boolean raiseParent = (parentComponent != null); if (buttonAction == JOptionPane.CANCEL_OPTION) raiseParent = false; @@ -982,7 +988,8 @@ public class JvOptionPane extends JOptionPane } */ @Override - public JvOptionPane setResponseHandler(Object response, Callable action) + public JvOptionPane setResponseHandler(Object response, + Callable action) { callbacks.put(response, action); return this; @@ -1171,7 +1178,7 @@ public class JvOptionPane extends JOptionPane { Object o = options[i]; int buttonAction = buttonActions[i]; - Callable action = callbacks.get(buttonAction); + Callable action = callbacks.get(buttonAction); JButton jb; if (buttons != null && buttons.length > i && buttons[i] != null) { @@ -1251,28 +1258,4 @@ public class JvOptionPane extends JOptionPane return false; } - - /** - * Get parent JOptionPane if there is one - */ - public static Container getAncestorClass(Object o, Class cl) - { - Container c; - if (o instanceof Container) - c = (Container) o; - else - return null; - Container p = c.getParent(); - if (p == null) - return null; - if (p.getClass() == cl) - { - return p; - } - else if (p instanceof Component) - { - return getAncestorClass((Component) p, cl); - } - return null; - } } diff --git a/src/jalview/gui/QuitHandler.java b/src/jalview/gui/QuitHandler.java index 75ff0b0..429de55 100644 --- a/src/jalview/gui/QuitHandler.java +++ b/src/jalview/gui/QuitHandler.java @@ -33,8 +33,6 @@ public class QuitHandler private static final int MAX_WAIT_FOR_SAVE = 20000; - private static final int NON_INTERACTIVE_WAIT_CYCLES = 2; - public static enum QResponse { NULL, QUIT, CANCEL_QUIT, FORCE_QUIT @@ -45,23 +43,23 @@ public class QuitHandler public static QResponse setQuitHandler() { FlatDesktop.setQuitHandler(response -> { - Callable performQuit = () -> { + Callable performQuit = () -> { response.performQuit(); - return setResponse(QResponse.QUIT); + setResponse(QResponse.QUIT); + return null; }; - Callable performForceQuit = () -> { + Callable performForceQuit = () -> { response.performQuit(); - return setResponse(QResponse.FORCE_QUIT); + setResponse(QResponse.FORCE_QUIT); + return null; }; - Callable cancelQuit = () -> { + Callable cancelQuit = () -> { response.cancelQuit(); // reset setResponse(QResponse.NULL); - // but return cancel - return QResponse.CANCEL_QUIT; + return null; }; - QResponse qresponse = getQuitResponse(true, performQuit, - performForceQuit, cancelQuit); + getQuitResponse(true, performQuit, performForceQuit, cancelQuit); }); return gotQuitResponse(); @@ -80,24 +78,25 @@ public class QuitHandler return gotQuitResponse; } - public static final Callable defaultCancelQuit = () -> { + public static final Callable defaultCancelQuit = () -> { Console.debug("QuitHandler: (default) Quit action CANCELLED by user"); // reset setResponse(QResponse.NULL); - // and return cancel - return QResponse.CANCEL_QUIT; + return null; }; - public static final Callable defaultOkQuit = () -> { + public static final Callable defaultOkQuit = () -> { Console.debug("QuitHandler: (default) Quit action CONFIRMED by user"); - return setResponse(QResponse.QUIT); + setResponse(QResponse.QUIT); + return null; }; - public static final Callable defaultForceQuit = () -> { + public static final Callable defaultForceQuit = () -> { Console.debug("QuitHandler: (default) Quit action FORCED by user"); // note that shutdown hook will not be run Runtime.getRuntime().halt(0); - return setResponse(QResponse.FORCE_QUIT); // this line never reached! + setResponse(QResponse.FORCE_QUIT); // this line never reached! + return null; }; public static QResponse getQuitResponse(boolean ui) @@ -108,9 +107,8 @@ public class QuitHandler private static boolean interactive = true; - public static QResponse getQuitResponse(boolean ui, - Callable okQuit, Callable forceQuit, - Callable cancelQuit) + public static QResponse getQuitResponse(boolean ui, Callable okQuit, + Callable forceQuit, Callable cancelQuit) { QResponse got = gotQuitResponse(); if (got != QResponse.NULL && got != QResponse.CANCEL_QUIT) @@ -179,13 +177,13 @@ public class QuitHandler if (Cache.getDefault("WAIT_FOR_SAVE", true) && BackupFiles.hasSavesInProgress()) { - QResponse waitResponse = waitQuit(interactive, okQuit, forceQuit, - cancelQuit); + waitQuit(interactive, okQuit, forceQuit, cancelQuit); + QResponse waitResponse = gotQuitResponse(); wait = waitResponse == QResponse.QUIT; } } - Callable next = null; + Callable next = null; switch (gotQuitResponse()) { case QUIT: @@ -200,7 +198,8 @@ public class QuitHandler } try { - got = executor.submit(next).get(); + executor.submit(next).get(); + got = gotQuitResponse(); } catch (InterruptedException | ExecutionException e) { jalview.bin.Console @@ -208,12 +207,18 @@ public class QuitHandler } setResponse(got); + if (gotQuitResponse() == QResponse.CANCEL_QUIT) + { + // reset if cancelled + setResponse(QResponse.NULL); + return QResponse.CANCEL_QUIT; + } return gotQuitResponse(); } private static QResponse waitQuit(boolean interactive, - Callable okQuit, Callable forceQuit, - Callable cancelQuit) + Callable okQuit, Callable forceQuit, + Callable cancelQuit) { // check for saves in progress if (!BackupFiles.hasSavesInProgress()) @@ -241,7 +246,6 @@ public class QuitHandler int waitTime = Math.min(MAX_WAIT_FOR_SAVE, Math.max(MIN_WAIT_FOR_SAVE, size / 2)); Console.debug("Set waitForSave to " + waitTime); - QResponse waitResponse = QResponse.NULL; int iteration = 0; boolean doIterations = true; // note iterations not used in the gui now, @@ -373,9 +377,7 @@ public class QuitHandler } // end if interactive } // end while wait iteration loop - waitResponse = gotQuitResponse(); - - return waitResponse; + return gotQuitResponse(); }; private static String waitingForSaveMessage() diff --git a/src/jalview/util/dialogrunner/DialogRunnerI.java b/src/jalview/util/dialogrunner/DialogRunnerI.java index 9be64c6..1fc41e7 100644 --- a/src/jalview/util/dialogrunner/DialogRunnerI.java +++ b/src/jalview/util/dialogrunner/DialogRunnerI.java @@ -43,7 +43,7 @@ public interface DialogRunnerI * @param action * @return */ - DialogRunnerI setResponseHandler(Object response, Callable action); + DialogRunnerI setResponseHandler(Object response, Callable action); // DialogRunnerI setResponseHandler(Object response, Runnable action); -- 1.7.10.2