| author | haftmann | 
| Thu, 04 Oct 2012 23:19:02 +0200 | |
| changeset 49716 | c55b39740529 | 
| parent 49251 | cd28155bb7d5 | 
| child 52477 | 025b3777e592 | 
| permissions | -rw-r--r-- | 
| 36676 | 1 | /* Title: Pure/System/swing_thread.scala | 
| 45673 
cd41e3903fbf
separate compilation of PIDE vs. Pure sources, which enables independent Scala library;
 wenzelm parents: 
45667diff
changeset | 2 | Module: PIDE | 
| 29202 | 3 | Author: Makarius | 
| 31942 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 4 | Author: Fabian Immler, TU Munich | 
| 29202 | 5 | |
| 31862 
53acb8ec6c51
renamed Swing to Swing_Thread, to avoid overlap with scala.swing.Swing;
 wenzelm parents: 
29777diff
changeset | 6 | Evaluation within the AWT/Swing thread. | 
| 29202 | 7 | */ | 
| 8 | ||
| 9 | package isabelle | |
| 10 | ||
| 31942 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 11 | import javax.swing.{SwingUtilities, Timer}
 | 
| 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 12 | import java.awt.event.{ActionListener, ActionEvent}
 | 
| 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 13 | |
| 29202 | 14 | |
| 31862 
53acb8ec6c51
renamed Swing to Swing_Thread, to avoid overlap with scala.swing.Swing;
 wenzelm parents: 
29777diff
changeset | 15 | object Swing_Thread | 
| 29202 | 16 | {
 | 
| 32494 | 17 | /* checks */ | 
| 18 | ||
| 19 | def assert() = Predef.assert(SwingUtilities.isEventDispatchThread()) | |
| 20 | def require() = Predef.require(SwingUtilities.isEventDispatchThread()) | |
| 21 | ||
| 22 | ||
| 31942 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 23 | /* main dispatch queue */ | 
| 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 24 | |
| 34026 | 25 | def now[A](body: => A): A = | 
| 26 |   {
 | |
| 48000 
880f1693299a
further attempts to simplify/robustify Swing_Thread.now, to avoid spurious physical race conditions on Java 6 / Mac OS X;
 wenzelm parents: 
47989diff
changeset | 27 | 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: 
47989diff
changeset | 28 |     else {
 | 
| 
880f1693299a
further attempts to simplify/robustify Swing_Thread.now, to avoid spurious physical race conditions on Java 6 / Mac OS X;
 wenzelm parents: 
47989diff
changeset | 29 |       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: 
47989diff
changeset | 30 |       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: 
47989diff
changeset | 31 | 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: 
47989diff
changeset | 32 | } | 
| 29649 
8b0c1397868e
more robust treatment of SwingUtilities.isEventDispatchThread;
 wenzelm parents: 
29202diff
changeset | 33 | } | 
| 29202 | 34 | |
| 34299 
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
 wenzelm parents: 
34217diff
changeset | 35 | def future[A](body: => A): Future[A] = | 
| 
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
 wenzelm parents: 
34217diff
changeset | 36 |   {
 | 
| 
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
 wenzelm parents: 
34217diff
changeset | 37 | if (SwingUtilities.isEventDispatchThread()) Future.value(body) | 
| 
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
 wenzelm parents: 
34217diff
changeset | 38 |     else Future.fork { now(body) }
 | 
| 
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
 wenzelm parents: 
34217diff
changeset | 39 | } | 
| 34026 | 40 | |
| 34299 
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
 wenzelm parents: 
34217diff
changeset | 41 | def later(body: => Unit) | 
| 
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
 wenzelm parents: 
34217diff
changeset | 42 |   {
 | 
| 32494 | 43 | if (SwingUtilities.isEventDispatchThread()) body | 
| 29649 
8b0c1397868e
more robust treatment of SwingUtilities.isEventDispatchThread;
 wenzelm parents: 
29202diff
changeset | 44 |     else SwingUtilities.invokeLater(new Runnable { def run = body })
 | 
| 
8b0c1397868e
more robust treatment of SwingUtilities.isEventDispatchThread;
 wenzelm parents: 
29202diff
changeset | 45 | } | 
| 31942 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 46 | |
| 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 47 | |
| 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 48 | /* delayed actions */ | 
| 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 49 | |
| 49195 | 50 | abstract class Delay extends | 
| 31942 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 51 |   {
 | 
| 49195 | 52 | def invoke(): Unit | 
| 53 | 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: 
49195diff
changeset | 54 | def postpone(time: Time): Unit | 
| 49195 | 55 | } | 
| 56 | ||
| 49249 | 57 | private def delayed_action(first: Boolean)(time: => Time)(action: => Unit): Delay = | 
| 49195 | 58 |     new Delay {
 | 
| 59 | private val timer = new Timer(time.ms.toInt, null) | |
| 60 | timer.setRepeats(false) | |
| 61 |       timer.addActionListener(new ActionListener {
 | |
| 62 |         override def actionPerformed(e: ActionEvent) {
 | |
| 63 | assert() | |
| 49249 | 64 | timer.setInitialDelay(time.ms.toInt) | 
| 49195 | 65 | action | 
| 66 | } | |
| 67 | }) | |
| 68 | ||
| 69 | def invoke() | |
| 70 |       {
 | |
| 71 | require() | |
| 72 | if (first) timer.start() else timer.restart() | |
| 73 | } | |
| 74 | ||
| 75 | def revoke() | |
| 76 |       {
 | |
| 77 | require() | |
| 78 | timer.stop() | |
| 49249 | 79 | timer.setInitialDelay(time.ms.toInt) | 
| 49195 | 80 | } | 
| 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: 
49195diff
changeset | 81 | |
| 
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: 
49195diff
changeset | 82 | 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: 
49195diff
changeset | 83 |       {
 | 
| 
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: 
49195diff
changeset | 84 | 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: 
49195diff
changeset | 85 |         if (timer.isRunning) {
 | 
| 49251 | 86 | 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: 
49195diff
changeset | 87 | 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: 
49195diff
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: 
49195diff
changeset | 89 | } | 
| 38223 | 90 | } | 
| 32501 | 91 | |
| 92 | // delayed action after first invocation | |
| 93 | def delay_first = delayed_action(true) _ | |
| 94 | ||
| 95 | // delayed action after last invocation | |
| 96 | def delay_last = delayed_action(false) _ | |
| 29202 | 97 | } |