author | wenzelm |
Sun, 06 Apr 2014 16:36:28 +0200 | |
changeset 56438 | 7f6b2634d853 |
parent 55618 | 995162143ef4 |
child 56662 | f373fb77e0a4 |
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 |
53853
e8430d668f44
more quasi-generic PIDE modules (NB: Swing/JFX needs to be kept separate from non-GUI material);
wenzelm
parents:
53783
diff
changeset
|
2 |
Module: PIDE-GUI |
29202 | 3 |
Author: Makarius |
4 |
||
31862
53acb8ec6c51
renamed Swing to Swing_Thread, to avoid overlap with scala.swing.Swing;
wenzelm
parents:
29777
diff
changeset
|
5 |
Evaluation within the AWT/Swing thread. |
29202 | 6 |
*/ |
7 |
||
8 |
package isabelle |
|
9 |
||
55618 | 10 |
|
31942
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
11 |
import javax.swing.{SwingUtilities, Timer} |
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
12 |
import java.awt.event.{ActionListener, ActionEvent} |
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
13 |
|
29202 | 14 |
|
31862
53acb8ec6c51
renamed Swing to Swing_Thread, to avoid overlap with scala.swing.Swing;
wenzelm
parents:
29777
diff
changeset
|
15 |
object Swing_Thread |
29202 | 16 |
{ |
32494 | 17 |
/* checks */ |
18 |
||
52859 | 19 |
def assert[A](body: => A) = |
20 |
{ |
|
21 |
Predef.assert(SwingUtilities.isEventDispatchThread()) |
|
22 |
body |
|
23 |
} |
|
32494 | 24 |
|
52859 | 25 |
def require[A](body: => A) = |
26 |
{ |
|
27 |
Predef.require(SwingUtilities.isEventDispatchThread()) |
|
28 |
body |
|
29 |
} |
|
52477 | 30 |
|
32494 | 31 |
|
31942
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
32 |
/* main dispatch queue */ |
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
33 |
|
34026 | 34 |
def now[A](body: => A): A = |
35 |
{ |
|
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
|
36 |
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
|
37 |
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
|
38 |
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
|
39 |
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
|
40 |
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
|
41 |
} |
29649
8b0c1397868e
more robust treatment of SwingUtilities.isEventDispatchThread;
wenzelm
parents:
29202
diff
changeset
|
42 |
} |
29202 | 43 |
|
34299
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
wenzelm
parents:
34217
diff
changeset
|
44 |
def future[A](body: => A): Future[A] = |
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
wenzelm
parents:
34217
diff
changeset
|
45 |
{ |
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
wenzelm
parents:
34217
diff
changeset
|
46 |
if (SwingUtilities.isEventDispatchThread()) Future.value(body) |
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
wenzelm
parents:
34217
diff
changeset
|
47 |
else Future.fork { now(body) } |
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
wenzelm
parents:
34217
diff
changeset
|
48 |
} |
34026 | 49 |
|
34299
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
wenzelm
parents:
34217
diff
changeset
|
50 |
def later(body: => Unit) |
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
wenzelm
parents:
34217
diff
changeset
|
51 |
{ |
32494 | 52 |
if (SwingUtilities.isEventDispatchThread()) body |
29649
8b0c1397868e
more robust treatment of SwingUtilities.isEventDispatchThread;
wenzelm
parents:
29202
diff
changeset
|
53 |
else SwingUtilities.invokeLater(new Runnable { def run = body }) |
8b0c1397868e
more robust treatment of SwingUtilities.isEventDispatchThread;
wenzelm
parents:
29202
diff
changeset
|
54 |
} |
31942
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
55 |
|
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
56 |
|
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
57 |
/* delayed actions */ |
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
58 |
|
49195 | 59 |
abstract class Delay extends |
31942
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
60 |
{ |
49195 | 61 |
def invoke(): Unit |
62 |
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
|
63 |
def postpone(time: Time): Unit |
49195 | 64 |
} |
65 |
||
49249 | 66 |
private def delayed_action(first: Boolean)(time: => Time)(action: => Unit): Delay = |
49195 | 67 |
new Delay { |
68 |
private val timer = new Timer(time.ms.toInt, null) |
|
69 |
timer.setRepeats(false) |
|
70 |
timer.addActionListener(new ActionListener { |
|
71 |
override def actionPerformed(e: ActionEvent) { |
|
72 |
assert() |
|
49249 | 73 |
timer.setInitialDelay(time.ms.toInt) |
49195 | 74 |
action |
75 |
} |
|
76 |
}) |
|
77 |
||
78 |
def invoke() |
|
79 |
{ |
|
80 |
require() |
|
81 |
if (first) timer.start() else timer.restart() |
|
82 |
} |
|
83 |
||
84 |
def revoke() |
|
85 |
{ |
|
86 |
require() |
|
87 |
timer.stop() |
|
49249 | 88 |
timer.setInitialDelay(time.ms.toInt) |
49195 | 89 |
} |
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
|
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 |
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
|
92 |
{ |
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
|
93 |
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
|
94 |
if (timer.isRunning) { |
49251 | 95 |
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
|
96 |
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
|
97 |
} |
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
|
98 |
} |
38223 | 99 |
} |
32501 | 100 |
|
101 |
// delayed action after first invocation |
|
102 |
def delay_first = delayed_action(true) _ |
|
103 |
||
104 |
// delayed action after last invocation |
|
105 |
def delay_last = delayed_action(false) _ |
|
29202 | 106 |
} |