src/Pure/GUI/jfx_thread.scala
changeset 53783 f5e9d182f645
parent 49066 1067a639d42a
child 53853 e8430d668f44
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Pure/GUI/jfx_thread.scala	Sun Sep 22 14:30:34 2013 +0200
@@ -0,0 +1,37 @@
+/*  Title:      Pure/GUI/jfx_thread.scala
+    Author:     Makarius
+
+Evaluation within the Java FX application thread.
+*/
+
+package isabelle
+
+import javafx.application.{Platform => JFX_Platform}
+
+
+object JFX_Thread
+{
+  /* checks */
+
+  def assert() = Predef.assert(JFX_Platform.isFxApplicationThread())
+  def require() = Predef.require(JFX_Platform.isFxApplicationThread())
+
+
+  /* asynchronous context switch */
+
+  def later(body: => Unit)
+  {
+    if (JFX_Platform.isFxApplicationThread()) body
+    else JFX_Platform.runLater(new Runnable { def run = body })
+  }
+
+  def future[A](body: => A): Future[A] =
+  {
+    if (JFX_Platform.isFxApplicationThread()) Future.value(body)
+    else {
+      val promise = Future.promise[A]
+      later { promise.fulfill_result(Exn.capture(body)) }
+      promise
+    }
+  }
+}