private Map<Object, Runnable> callbacks = new HashMap<>();
+ private int timeout = -1;
+
+ public void setTimeout(int i)
+ {
+ timeout = i;
+ }
+
/*
* JalviewJS reports user choice in the dialog as the selected option (text);
* this list allows conversion to index (int)
private static void outputMessage(Object message)
{
- jalview.bin.Console.outPrintln(">>> JOption Message : " + message.toString());
+ jalview.bin.Console
+ .outPrintln(">>> JOption Message : " + message.toString());
}
public static Object getMockResponse()
"Supplied buttons array not the same length as supplied options array.");
break NOTNULL;
}
+
+ // run through buttons for initialValue first so we can set (and start)
+ // a final timeoutThreadF to include (and interrupt) in the button
+ // actions
+ Thread timeoutThread = null;
+ for (int i = 0; i < options.length; i++)
+ {
+ Object o = options[i];
+ JButton jb = buttons[i];
+ if (o.equals(initialValue))
+ {
+ if (timeout > 0 && jb != null && jb instanceof JButton)
+ {
+ // after timeout ms click the default button
+ timeoutThread = new Thread(() -> {
+ try
+ {
+ Thread.sleep(timeout);
+ } catch (InterruptedException e)
+ {
+ Console.debug(
+ "Dialog timeout interrupted. Probably a button pressed.");
+ }
+ jb.doClick();
+ });
+ }
+ initialValueButton = jb;
+ break;
+ }
+ }
+ final Thread timeoutThreadF = timeoutThread;
+ if (timeoutThreadF != null)
+ {
+ timeoutThreadF.start();
+ }
+
int[] buttonActions = { JOptionPane.YES_OPTION,
JOptionPane.NO_OPTION, JOptionPane.CANCEL_OPTION };
for (int i = 0; i < options.length; i++)
"Setting button " + i + " to '" + o.toString() + "'");
JButton jb = buttons[i];
- if (o.equals(initialValue))
- initialValueButton = jb;
-
int buttonAction = buttonActions[i];
Runnable action = callbacks.get(buttonAction);
jb.setText((String) o);
@Override
public void actionPerformed(ActionEvent e)
{
+ if (timeoutThreadF != null)
+ {
+ timeoutThreadF.interrupt();
+ }
Object obj = e.getSource();
if (obj == null || !(obj instanceof Component))
int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options,
Object initialValue, boolean modal, JButton[] buttons)
{
+ showDialogOnTopAsync(dialogParent, label, actionString,
+ JOPTIONPANE_OPTION, JOPTIONPANE_MESSAGETYPE, icon, options,
+ initialValue, modal, buttons, true);
+ }
+
+ public void showDialogOnTopAsync(JFrame dialogParent, Object label,
+ String actionString, int JOPTIONPANE_OPTION,
+ int JOPTIONPANE_MESSAGETYPE, Icon icon, Object[] options,
+ Object initialValue, boolean modal, JButton[] buttons,
+ boolean dispose)
+ {
if (!isInteractiveMode())
{
handleResponse(getMockResponse());
// A better hack which works is to create a new JFrame parent with
// setAlwaysOnTop(true)
+ boolean parentOnTop = dialogParent.isAlwaysOnTop();
dialogParent.setAlwaysOnTop(true);
parentComponent = dialogParent;
JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal,
buttons);
- dialogParent.setAlwaysOnTop(false);
- dialogParent.dispose();
+ dialogParent.setAlwaysOnTop(parentOnTop);
+
+ if (dispose)
+ {
+ dialogParent.setAlwaysOnTop(false);
+ dialogParent.dispose();
+ }
}
/**