jdk/test/javax/swing/regtesthelpers/Util.java
changeset 12405 3a06b392a805
parent 11096 a39b7342177f
child 14167 e5ddc84321f2
--- a/jdk/test/javax/swing/regtesthelpers/Util.java	Tue Apr 10 18:53:15 2012 +0300
+++ b/jdk/test/javax/swing/regtesthelpers/Util.java	Tue Apr 10 19:09:48 2012 +0300
@@ -24,8 +24,10 @@
 import javax.swing.*;
 import java.awt.*;
 import java.awt.image.BufferedImage;
+import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.concurrent.Callable;
 
 /**
  * <p>This class contains utilities useful for regression testing.
@@ -153,4 +155,31 @@
             robot.keyRelease(keys[i]);
         }
     }
+
+    /**
+     * Invokes the <code>task</code> on the EDT thread.
+     *
+     * @return result of the <code>task</code>
+     */
+    public static <T> T invokeOnEDT(final Callable<T> task) throws Exception {
+        final List<T> result = new ArrayList<>(1);
+        final Exception[] exception = new Exception[1];
+
+        SwingUtilities.invokeAndWait(new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    result.add(task.call());
+                } catch (Exception e) {
+                    exception[0] = e;
+                }
+            }
+        });
+
+        if (exception[0] != null) {
+            throw exception[0];
+        }
+
+        return result.get(0);
+    }
 }