Merge branch 'develop' into patch/JAL-4281_idwidthandannotHeight_in_project
[jalview.git] / src / jalview / gui / JvOptionPane.java
index 5b926c3..7a5daf7 100644 (file)
@@ -88,6 +88,13 @@ public class JvOptionPane extends JOptionPane
 
   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)
@@ -722,7 +729,8 @@ public class JvOptionPane extends JOptionPane
 
   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()
@@ -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))
@@ -1264,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());
@@ -1278,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;
 
@@ -1285,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();
+    }
   }
 
   /**