Merge branch 'jims/bug/JAL-4298_java_console_causing_hang_at_startup' into develop
[jalview.git] / src / jalview / gui / JvOptionPane.java
index 3f5de0a..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)
@@ -851,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++)
@@ -860,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);
@@ -871,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))
@@ -1290,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;
 
@@ -1297,6 +1342,8 @@ public class JvOptionPane extends JOptionPane
             JOPTIONPANE_MESSAGETYPE, icon, options, initialValue, modal,
             buttons);
 
+    dialogParent.setAlwaysOnTop(parentOnTop);
+
     if (dispose)
     {
       dialogParent.setAlwaysOnTop(false);