| author | wenzelm |
| Sun, 22 Sep 2013 14:30:34 +0200 | |
| changeset 53783 | f5e9d182f645 |
| parent 52859 | src/Pure/System/swing_thread.scala@f31624cc4467 |
| child 53853 | e8430d668f44 |
| permissions | -rw-r--r-- |
|
53783
f5e9d182f645
clarified location of GUI modules (which depend on Swing of JFX);
wenzelm
parents:
52859
diff
changeset
|
1 |
/* Title: Pure/GUI/swing_thread.scala |
| 29202 | 2 |
Author: Makarius |
3 |
||
|
31862
53acb8ec6c51
renamed Swing to Swing_Thread, to avoid overlap with scala.swing.Swing;
wenzelm
parents:
29777
diff
changeset
|
4 |
Evaluation within the AWT/Swing thread. |
| 29202 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
|
31942
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
9 |
import javax.swing.{SwingUtilities, Timer}
|
|
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
10 |
import java.awt.event.{ActionListener, ActionEvent}
|
|
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
11 |
|
| 29202 | 12 |
|
|
31862
53acb8ec6c51
renamed Swing to Swing_Thread, to avoid overlap with scala.swing.Swing;
wenzelm
parents:
29777
diff
changeset
|
13 |
object Swing_Thread |
| 29202 | 14 |
{
|
| 32494 | 15 |
/* checks */ |
16 |
||
| 52859 | 17 |
def assert[A](body: => A) = |
18 |
{
|
|
19 |
Predef.assert(SwingUtilities.isEventDispatchThread()) |
|
20 |
body |
|
21 |
} |
|
| 32494 | 22 |
|
| 52859 | 23 |
def require[A](body: => A) = |
24 |
{
|
|
25 |
Predef.require(SwingUtilities.isEventDispatchThread()) |
|
26 |
body |
|
27 |
} |
|
| 52477 | 28 |
|
| 32494 | 29 |
|
|
31942
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
30 |
/* main dispatch queue */ |
|
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
31 |
|
| 34026 | 32 |
def now[A](body: => A): A = |
33 |
{
|
|
|
48000
880f1693299a
further attempts to simplify/robustify Swing_Thread.now, to avoid spurious physical race conditions on Java 6 / Mac OS X;
wenzelm
parents:
47989
diff
changeset
|
34 |
if (SwingUtilities.isEventDispatchThread()) body |
|
880f1693299a
further attempts to simplify/robustify Swing_Thread.now, to avoid spurious physical race conditions on Java 6 / Mac OS X;
wenzelm
parents:
47989
diff
changeset
|
35 |
else {
|
|
880f1693299a
further attempts to simplify/robustify Swing_Thread.now, to avoid spurious physical race conditions on Java 6 / Mac OS X;
wenzelm
parents:
47989
diff
changeset
|
36 |
lazy val result = { assert(); Exn.capture(body) }
|
|
880f1693299a
further attempts to simplify/robustify Swing_Thread.now, to avoid spurious physical race conditions on Java 6 / Mac OS X;
wenzelm
parents:
47989
diff
changeset
|
37 |
SwingUtilities.invokeAndWait(new Runnable { def run = result })
|
|
880f1693299a
further attempts to simplify/robustify Swing_Thread.now, to avoid spurious physical race conditions on Java 6 / Mac OS X;
wenzelm
parents:
47989
diff
changeset
|
38 |
Exn.release(result) |
|
880f1693299a
further attempts to simplify/robustify Swing_Thread.now, to avoid spurious physical race conditions on Java 6 / Mac OS X;
wenzelm
parents:
47989
diff
changeset
|
39 |
} |
|
29649
8b0c1397868e
more robust treatment of SwingUtilities.isEventDispatchThread;
wenzelm
parents:
29202
diff
changeset
|
40 |
} |
| 29202 | 41 |
|
|
34299
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
wenzelm
parents:
34217
diff
changeset
|
42 |
def future[A](body: => A): Future[A] = |
|
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
wenzelm
parents:
34217
diff
changeset
|
43 |
{
|
|
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
wenzelm
parents:
34217
diff
changeset
|
44 |
if (SwingUtilities.isEventDispatchThread()) Future.value(body) |
|
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
wenzelm
parents:
34217
diff
changeset
|
45 |
else Future.fork { now(body) }
|
|
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
wenzelm
parents:
34217
diff
changeset
|
46 |
} |
| 34026 | 47 |
|
|
34299
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
wenzelm
parents:
34217
diff
changeset
|
48 |
def later(body: => Unit) |
|
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
wenzelm
parents:
34217
diff
changeset
|
49 |
{
|
| 32494 | 50 |
if (SwingUtilities.isEventDispatchThread()) body |
|
29649
8b0c1397868e
more robust treatment of SwingUtilities.isEventDispatchThread;
wenzelm
parents:
29202
diff
changeset
|
51 |
else SwingUtilities.invokeLater(new Runnable { def run = body })
|
|
8b0c1397868e
more robust treatment of SwingUtilities.isEventDispatchThread;
wenzelm
parents:
29202
diff
changeset
|
52 |
} |
|
31942
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
53 |
|
|
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
54 |
|
|
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
55 |
/* delayed actions */ |
|
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
56 |
|
| 49195 | 57 |
abstract class Delay extends |
|
31942
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
58 |
{
|
| 49195 | 59 |
def invoke(): Unit |
60 |
def revoke(): Unit |
|
|
49196
1d63ceb0d177
postpone update of text overview panel after incoming session edits, to improve reactivity of editing massive theories like src/HOL/Multivariate_Analysis;
wenzelm
parents:
49195
diff
changeset
|
61 |
def postpone(time: Time): Unit |
| 49195 | 62 |
} |
63 |
||
| 49249 | 64 |
private def delayed_action(first: Boolean)(time: => Time)(action: => Unit): Delay = |
| 49195 | 65 |
new Delay {
|
66 |
private val timer = new Timer(time.ms.toInt, null) |
|
67 |
timer.setRepeats(false) |
|
68 |
timer.addActionListener(new ActionListener {
|
|
69 |
override def actionPerformed(e: ActionEvent) {
|
|
70 |
assert() |
|
| 49249 | 71 |
timer.setInitialDelay(time.ms.toInt) |
| 49195 | 72 |
action |
73 |
} |
|
74 |
}) |
|
75 |
||
76 |
def invoke() |
|
77 |
{
|
|
78 |
require() |
|
79 |
if (first) timer.start() else timer.restart() |
|
80 |
} |
|
81 |
||
82 |
def revoke() |
|
83 |
{
|
|
84 |
require() |
|
85 |
timer.stop() |
|
| 49249 | 86 |
timer.setInitialDelay(time.ms.toInt) |
| 49195 | 87 |
} |
|
49196
1d63ceb0d177
postpone update of text overview panel after incoming session edits, to improve reactivity of editing massive theories like src/HOL/Multivariate_Analysis;
wenzelm
parents:
49195
diff
changeset
|
88 |
|
|
1d63ceb0d177
postpone update of text overview panel after incoming session edits, to improve reactivity of editing massive theories like src/HOL/Multivariate_Analysis;
wenzelm
parents:
49195
diff
changeset
|
89 |
def postpone(alt_time: Time) |
|
1d63ceb0d177
postpone update of text overview panel after incoming session edits, to improve reactivity of editing massive theories like src/HOL/Multivariate_Analysis;
wenzelm
parents:
49195
diff
changeset
|
90 |
{
|
|
1d63ceb0d177
postpone update of text overview panel after incoming session edits, to improve reactivity of editing massive theories like src/HOL/Multivariate_Analysis;
wenzelm
parents:
49195
diff
changeset
|
91 |
require() |
|
1d63ceb0d177
postpone update of text overview panel after incoming session edits, to improve reactivity of editing massive theories like src/HOL/Multivariate_Analysis;
wenzelm
parents:
49195
diff
changeset
|
92 |
if (timer.isRunning) {
|
| 49251 | 93 |
timer.setInitialDelay(timer.getInitialDelay max alt_time.ms.toInt) |
|
49196
1d63ceb0d177
postpone update of text overview panel after incoming session edits, to improve reactivity of editing massive theories like src/HOL/Multivariate_Analysis;
wenzelm
parents:
49195
diff
changeset
|
94 |
timer.restart() |
|
1d63ceb0d177
postpone update of text overview panel after incoming session edits, to improve reactivity of editing massive theories like src/HOL/Multivariate_Analysis;
wenzelm
parents:
49195
diff
changeset
|
95 |
} |
|
1d63ceb0d177
postpone update of text overview panel after incoming session edits, to improve reactivity of editing massive theories like src/HOL/Multivariate_Analysis;
wenzelm
parents:
49195
diff
changeset
|
96 |
} |
| 38223 | 97 |
} |
| 32501 | 98 |
|
99 |
// delayed action after first invocation |
|
100 |
def delay_first = delayed_action(true) _ |
|
101 |
||
102 |
// delayed action after last invocation |
|
103 |
def delay_last = delayed_action(false) _ |
|
| 29202 | 104 |
} |