8 |
8 |
9 |
9 |
10 import javax.swing.SwingUtilities |
10 import javax.swing.SwingUtilities |
11 |
11 |
12 |
12 |
13 object GUI_Thread |
13 object GUI_Thread { |
14 { |
|
15 /* context check */ |
14 /* context check */ |
16 |
15 |
17 def assert[A](body: => A): A = |
16 def assert[A](body: => A): A = { |
18 { |
|
19 Predef.assert(SwingUtilities.isEventDispatchThread) |
17 Predef.assert(SwingUtilities.isEventDispatchThread) |
20 body |
18 body |
21 } |
19 } |
22 |
20 |
23 def require[A](body: => A): A = |
21 def require[A](body: => A): A = { |
24 { |
|
25 Predef.require(SwingUtilities.isEventDispatchThread, "GUI thread expected") |
22 Predef.require(SwingUtilities.isEventDispatchThread, "GUI thread expected") |
26 body |
23 body |
27 } |
24 } |
28 |
25 |
29 |
26 |
30 /* event dispatch queue */ |
27 /* event dispatch queue */ |
31 |
28 |
32 def now[A](body: => A): A = |
29 def now[A](body: => A): A = { |
33 { |
|
34 if (SwingUtilities.isEventDispatchThread) body |
30 if (SwingUtilities.isEventDispatchThread) body |
35 else { |
31 else { |
36 lazy val result = assert { Exn.capture(body) } |
32 lazy val result = assert { Exn.capture(body) } |
37 SwingUtilities.invokeAndWait(() => result) |
33 SwingUtilities.invokeAndWait(() => result) |
38 Exn.release(result) |
34 Exn.release(result) |
39 } |
35 } |
40 } |
36 } |
41 |
37 |
42 def later(body: => Unit): Unit = |
38 def later(body: => Unit): Unit = { |
43 { |
|
44 if (SwingUtilities.isEventDispatchThread) body |
39 if (SwingUtilities.isEventDispatchThread) body |
45 else SwingUtilities.invokeLater(() => body) |
40 else SwingUtilities.invokeLater(() => body) |
46 } |
41 } |
47 |
42 |
48 def future[A](body: => A): Future[A] = |
43 def future[A](body: => A): Future[A] = { |
49 { |
|
50 if (SwingUtilities.isEventDispatchThread) Future.value(body) |
44 if (SwingUtilities.isEventDispatchThread) Future.value(body) |
51 else { |
45 else { |
52 val promise = Future.promise[A] |
46 val promise = Future.promise[A] |
53 later { promise.fulfill_result(Exn.capture(body)) } |
47 later { promise.fulfill_result(Exn.capture(body)) } |
54 promise |
48 promise |