package jalview.gui;
import java.awt.Component;
-import java.awt.Container;
import java.awt.Dialog.ModalityType;
import java.awt.HeadlessException;
import java.awt.Window;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
+import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import jalview.util.Platform;
private Component parentComponent;
- private Map<Object, Callable> callbacks = new HashMap<>();
+ private Map<Object, Callable<Void>> callbacks = new HashMap<>();
/*
* JalviewJS reports user choice in the dialog as the selected
initialValueButton = jb;
int buttonAction = buttonActions[i];
- Callable action = callbacks.get(buttonAction);
+ Callable<Void> action = callbacks.get(buttonAction);
jb.setText((String) o);
jb.addActionListener(new ActionListener()
{
{
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))
{
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;
}
*/
@Override
- public JvOptionPane setResponseHandler(Object response, Callable action)
+ public JvOptionPane setResponseHandler(Object response,
+ Callable<Void> action)
{
callbacks.put(response, action);
return this;
{
Object o = options[i];
int buttonAction = buttonActions[i];
- Callable action = callbacks.get(buttonAction);
+ Callable<Void> action = callbacks.get(buttonAction);
JButton jb;
if (buttons != null && buttons.length > i && buttons[i] != null)
{
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;
- }
}
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
public static QResponse setQuitHandler()
{
FlatDesktop.setQuitHandler(response -> {
- Callable<QResponse> performQuit = () -> {
+ Callable<Void> performQuit = () -> {
response.performQuit();
- return setResponse(QResponse.QUIT);
+ setResponse(QResponse.QUIT);
+ return null;
};
- Callable<QResponse> performForceQuit = () -> {
+ Callable<Void> performForceQuit = () -> {
response.performQuit();
- return setResponse(QResponse.FORCE_QUIT);
+ setResponse(QResponse.FORCE_QUIT);
+ return null;
};
- Callable<QResponse> cancelQuit = () -> {
+ Callable<Void> 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();
return gotQuitResponse;
}
- public static final Callable<QResponse> defaultCancelQuit = () -> {
+ public static final Callable<Void> 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<QResponse> defaultOkQuit = () -> {
+ public static final Callable<Void> defaultOkQuit = () -> {
Console.debug("QuitHandler: (default) Quit action CONFIRMED by user");
- return setResponse(QResponse.QUIT);
+ setResponse(QResponse.QUIT);
+ return null;
};
- public static final Callable<QResponse> defaultForceQuit = () -> {
+ public static final Callable<Void> 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)
private static boolean interactive = true;
- public static QResponse getQuitResponse(boolean ui,
- Callable<QResponse> okQuit, Callable<QResponse> forceQuit,
- Callable<QResponse> cancelQuit)
+ public static QResponse getQuitResponse(boolean ui, Callable<Void> okQuit,
+ Callable<Void> forceQuit, Callable<Void> cancelQuit)
{
QResponse got = gotQuitResponse();
if (got != QResponse.NULL && got != QResponse.CANCEL_QUIT)
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<QResponse> next = null;
+ Callable<Void> next = null;
switch (gotQuitResponse())
{
case QUIT:
}
try
{
- got = executor.submit(next).get();
+ executor.submit(next).get();
+ got = gotQuitResponse();
} catch (InterruptedException | ExecutionException e)
{
jalview.bin.Console
}
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<QResponse> okQuit, Callable<QResponse> forceQuit,
- Callable<QResponse> cancelQuit)
+ Callable<Void> okQuit, Callable<Void> forceQuit,
+ Callable<Void> cancelQuit)
{
// check for saves in progress
if (!BackupFiles.hasSavesInProgress())
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,
} // end if interactive
} // end while wait iteration loop
- waitResponse = gotQuitResponse();
-
- return waitResponse;
+ return gotQuitResponse();
};
private static String waitingForSaveMessage()