| author | huffman | 
| Wed, 17 Nov 2010 16:13:33 -0800 | |
| changeset 40623 | dafba3a1dc5b | 
| parent 38847 | 57043221eb43 | 
| child 40848 | 8662b9b1f123 | 
| permissions | -rw-r--r-- | 
| 36676 | 1 | /* Title: Pure/System/swing_thread.scala | 
| 29202 | 2 | Author: Makarius | 
| 31942 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 3 | Author: Fabian Immler, TU Munich | 
| 29202 | 4 | |
| 31862 
53acb8ec6c51
renamed Swing to Swing_Thread, to avoid overlap with scala.swing.Swing;
 wenzelm parents: 
29777diff
changeset | 5 | Evaluation within the AWT/Swing thread. | 
| 29202 | 6 | */ | 
| 7 | ||
| 8 | package isabelle | |
| 9 | ||
| 31942 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 10 | import javax.swing.{SwingUtilities, Timer}
 | 
| 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 11 | import java.awt.event.{ActionListener, ActionEvent}
 | 
| 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 12 | |
| 29202 | 13 | |
| 31862 
53acb8ec6c51
renamed Swing to Swing_Thread, to avoid overlap with scala.swing.Swing;
 wenzelm parents: 
29777diff
changeset | 14 | object Swing_Thread | 
| 29202 | 15 | {
 | 
| 32494 | 16 | /* checks */ | 
| 17 | ||
| 18 | def assert() = Predef.assert(SwingUtilities.isEventDispatchThread()) | |
| 19 | def require() = Predef.require(SwingUtilities.isEventDispatchThread()) | |
| 20 | ||
| 21 | ||
| 31942 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 22 | /* main dispatch queue */ | 
| 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 23 | |
| 34026 | 24 | def now[A](body: => A): A = | 
| 25 |   {
 | |
| 38847 
57043221eb43
Swing_Thread.now: volatile result to make double-sure;
 wenzelm parents: 
38223diff
changeset | 26 | @volatile var result: Option[A] = None | 
| 32494 | 27 |     if (SwingUtilities.isEventDispatchThread()) { result = Some(body) }
 | 
| 29777 | 28 |     else SwingUtilities.invokeAndWait(new Runnable { def run = { result = Some(body) } })
 | 
| 29 | result.get | |
| 29649 
8b0c1397868e
more robust treatment of SwingUtilities.isEventDispatchThread;
 wenzelm parents: 
29202diff
changeset | 30 | } | 
| 29202 | 31 | |
| 34299 
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
 wenzelm parents: 
34217diff
changeset | 32 | def future[A](body: => A): Future[A] = | 
| 
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
 wenzelm parents: 
34217diff
changeset | 33 |   {
 | 
| 
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
 wenzelm parents: 
34217diff
changeset | 34 | if (SwingUtilities.isEventDispatchThread()) Future.value(body) | 
| 
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
 wenzelm parents: 
34217diff
changeset | 35 |     else Future.fork { now(body) }
 | 
| 
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
 wenzelm parents: 
34217diff
changeset | 36 | } | 
| 34026 | 37 | |
| 34299 
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
 wenzelm parents: 
34217diff
changeset | 38 | def later(body: => Unit) | 
| 
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
 wenzelm parents: 
34217diff
changeset | 39 |   {
 | 
| 32494 | 40 | if (SwingUtilities.isEventDispatchThread()) body | 
| 29649 
8b0c1397868e
more robust treatment of SwingUtilities.isEventDispatchThread;
 wenzelm parents: 
29202diff
changeset | 41 |     else SwingUtilities.invokeLater(new Runnable { def run = body })
 | 
| 
8b0c1397868e
more robust treatment of SwingUtilities.isEventDispatchThread;
 wenzelm parents: 
29202diff
changeset | 42 | } | 
| 31942 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 43 | |
| 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 44 | |
| 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 45 | /* delayed actions */ | 
| 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 46 | |
| 32501 | 47 | private def delayed_action(first: Boolean)(time_span: Int)(action: => Unit): () => Unit = | 
| 31942 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 48 |   {
 | 
| 38223 | 49 |     val listener = new ActionListener {
 | 
| 50 |       override def actionPerformed(e: ActionEvent) { Swing_Thread.assert(); action }
 | |
| 51 | } | |
| 31942 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 52 | val timer = new Timer(time_span, listener) | 
| 32501 | 53 | timer.setRepeats(false) | 
| 54 | ||
| 55 |     def invoke() { if (first) timer.start() else timer.restart() }
 | |
| 31942 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 56 | invoke _ | 
| 
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
 wenzelm parents: 
31862diff
changeset | 57 | } | 
| 32501 | 58 | |
| 59 | // delayed action after first invocation | |
| 60 | def delay_first = delayed_action(true) _ | |
| 61 | ||
| 62 | // delayed action after last invocation | |
| 63 | def delay_last = delayed_action(false) _ | |
| 29202 | 64 | } |