author | wenzelm |
Fri, 18 Dec 2009 12:28:50 +0100 | |
changeset 34119 | ae92efb48784 |
parent 34026 | 0cb44ac299f8 |
child 34217 | 67e1ac2d3b2c |
permissions | -rw-r--r-- |
31862
53acb8ec6c51
renamed Swing to Swing_Thread, to avoid overlap with scala.swing.Swing;
wenzelm
parents:
29777
diff
changeset
|
1 |
/* Title: Pure/General/swing_thread.scala |
29202 | 2 |
Author: Makarius |
31942
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
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:
29777
diff
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:
31862
diff
changeset
|
10 |
import javax.swing.{SwingUtilities, Timer} |
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
11 |
import java.awt.event.{ActionListener, ActionEvent} |
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
12 |
|
34026 | 13 |
import scala.actors.{Future, Futures} |
14 |
||
29202 | 15 |
|
31862
53acb8ec6c51
renamed Swing to Swing_Thread, to avoid overlap with scala.swing.Swing;
wenzelm
parents:
29777
diff
changeset
|
16 |
object Swing_Thread |
29202 | 17 |
{ |
32494 | 18 |
/* checks */ |
19 |
||
20 |
def assert() = Predef.assert(SwingUtilities.isEventDispatchThread()) |
|
21 |
def require() = Predef.require(SwingUtilities.isEventDispatchThread()) |
|
22 |
||
23 |
||
31942
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
24 |
/* main dispatch queue */ |
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
25 |
|
34026 | 26 |
def now[A](body: => A): A = |
27 |
{ |
|
29777 | 28 |
var result: Option[A] = None |
32494 | 29 |
if (SwingUtilities.isEventDispatchThread()) { result = Some(body) } |
29777 | 30 |
else SwingUtilities.invokeAndWait(new Runnable { def run = { result = Some(body) } }) |
31 |
result.get |
|
29649
8b0c1397868e
more robust treatment of SwingUtilities.isEventDispatchThread;
wenzelm
parents:
29202
diff
changeset
|
32 |
} |
29202 | 33 |
|
34026 | 34 |
def future[A](body: => A): Future[A] = |
35 |
Futures.future(now(body)) |
|
36 |
||
29649
8b0c1397868e
more robust treatment of SwingUtilities.isEventDispatchThread;
wenzelm
parents:
29202
diff
changeset
|
37 |
def later(body: => Unit) { |
32494 | 38 |
if (SwingUtilities.isEventDispatchThread()) body |
29649
8b0c1397868e
more robust treatment of SwingUtilities.isEventDispatchThread;
wenzelm
parents:
29202
diff
changeset
|
39 |
else SwingUtilities.invokeLater(new Runnable { def run = body }) |
8b0c1397868e
more robust treatment of SwingUtilities.isEventDispatchThread;
wenzelm
parents:
29202
diff
changeset
|
40 |
} |
31942
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
41 |
|
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
42 |
|
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
43 |
/* delayed actions */ |
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
44 |
|
32501 | 45 |
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:
31862
diff
changeset
|
46 |
{ |
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
47 |
val listener = |
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
48 |
new ActionListener { override def actionPerformed(e: ActionEvent) { action } } |
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
49 |
val timer = new Timer(time_span, listener) |
32501 | 50 |
timer.setRepeats(false) |
51 |
||
52 |
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:
31862
diff
changeset
|
53 |
invoke _ |
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
54 |
} |
32501 | 55 |
|
56 |
// delayed action after first invocation |
|
57 |
def delay_first = delayed_action(true) _ |
|
58 |
||
59 |
// delayed action after last invocation |
|
60 |
def delay_last = delayed_action(false) _ |
|
29202 | 61 |
} |