X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FJvOptionPane.java;h=7a5daf705eb1ba3f8856d8874fa46ca6a5255669;hb=f6d1305378fa256a42caf7d284b13d5a084bc471;hp=90d812ce23c72fc9297d9cafc21413b025bd612b;hpb=c764fbfdffe3a52349038c957d7a73b8e22e9985;p=jalview.git diff --git a/src/jalview/gui/JvOptionPane.java b/src/jalview/gui/JvOptionPane.java index 90d812c..7a5daf7 100644 --- a/src/jalview/gui/JvOptionPane.java +++ b/src/jalview/gui/JvOptionPane.java @@ -88,6 +88,13 @@ public class JvOptionPane extends JOptionPane private Map 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) @@ -722,7 +729,8 @@ public class JvOptionPane extends JOptionPane private static void outputMessage(Object message) { - System.out.println(">>> JOption Message : " + message.toString()); + jalview.bin.Console + .outPrintln(">>> JOption Message : " + message.toString()); } public static Object getMockResponse() @@ -850,6 +858,42 @@ public class JvOptionPane extends JOptionPane "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++) @@ -859,9 +903,6 @@ public class JvOptionPane extends JOptionPane "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); @@ -870,6 +911,10 @@ public class JvOptionPane extends JOptionPane @Override public void actionPerformed(ActionEvent e) { + if (timeoutThreadF != null) + { + timeoutThreadF.interrupt(); + } Object obj = e.getSource(); if (obj == null || !(obj instanceof Component)) @@ -1141,7 +1186,13 @@ public class JvOptionPane extends JOptionPane private void internalDialogHandleResponse() { - String responseString = (String) this.getValue(); + Object value = this.getValue(); + if (value == null + || (value instanceof Integer && (Integer) value == -1)) + { + return; + } + String responseString = value.toString(); int response = ourOptions.indexOf(responseString); if (!Platform.isJS()) @@ -1258,6 +1309,17 @@ public class JvOptionPane extends JOptionPane 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()); @@ -1272,6 +1334,7 @@ public class JvOptionPane extends JOptionPane // A better hack which works is to create a new JFrame parent with // setAlwaysOnTop(true) + boolean parentOnTop = dialogParent.isAlwaysOnTop(); dialogParent.setAlwaysOnTop(true); parentComponent = dialogParent; @@ -1279,8 +1342,13 @@ public class JvOptionPane extends JOptionPane JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal, buttons); - dialogParent.setAlwaysOnTop(false); - dialogParent.dispose(); + dialogParent.setAlwaysOnTop(parentOnTop); + + if (dispose) + { + dialogParent.setAlwaysOnTop(false); + dialogParent.dispose(); + } } /**