| author | huffman |
| Sun, 04 Dec 2011 18:30:57 +0100 | |
| changeset 45749 | 92c6ddca552e |
| parent 45673 | cd41e3903fbf |
| child 46574 | 41701fce8ac7 |
| 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:
45667
diff
changeset
|
2 |
Module: PIDE |
| 29202 | 3 |
Author: Makarius |
|
31942
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
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:
29777
diff
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:
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 |
||
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:
31862
diff
changeset
|
23 |
/* main dispatch queue */ |
|
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
24 |
|
| 34026 | 25 |
def now[A](body: => A): A = |
26 |
{
|
|
|
38847
57043221eb43
Swing_Thread.now: volatile result to make double-sure;
wenzelm
parents:
38223
diff
changeset
|
27 |
@volatile var result: Option[A] = None |
| 32494 | 28 |
if (SwingUtilities.isEventDispatchThread()) { result = Some(body) }
|
| 29777 | 29 |
else SwingUtilities.invokeAndWait(new Runnable { def run = { result = Some(body) } })
|
30 |
result.get |
|
|
29649
8b0c1397868e
more robust treatment of SwingUtilities.isEventDispatchThread;
wenzelm
parents:
29202
diff
changeset
|
31 |
} |
| 29202 | 32 |
|
|
34299
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
wenzelm
parents:
34217
diff
changeset
|
33 |
def future[A](body: => A): Future[A] = |
|
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
wenzelm
parents:
34217
diff
changeset
|
34 |
{
|
|
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
wenzelm
parents:
34217
diff
changeset
|
35 |
if (SwingUtilities.isEventDispatchThread()) Future.value(body) |
|
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
wenzelm
parents:
34217
diff
changeset
|
36 |
else Future.fork { now(body) }
|
|
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
wenzelm
parents:
34217
diff
changeset
|
37 |
} |
| 34026 | 38 |
|
|
34299
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
wenzelm
parents:
34217
diff
changeset
|
39 |
def later(body: => Unit) |
|
68716caa7745
Swing_Thread.future: plain Future.value if this is already Swing;
wenzelm
parents:
34217
diff
changeset
|
40 |
{
|
| 32494 | 41 |
if (SwingUtilities.isEventDispatchThread()) body |
|
29649
8b0c1397868e
more robust treatment of SwingUtilities.isEventDispatchThread;
wenzelm
parents:
29202
diff
changeset
|
42 |
else SwingUtilities.invokeLater(new Runnable { def run = body })
|
|
8b0c1397868e
more robust treatment of SwingUtilities.isEventDispatchThread;
wenzelm
parents:
29202
diff
changeset
|
43 |
} |
|
31942
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
44 |
|
|
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
45 |
|
|
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
46 |
/* delayed actions */ |
|
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
47 |
|
|
40848
8662b9b1f123
more abstract/uniform handling of time, preferring seconds as Double;
wenzelm
parents:
38847
diff
changeset
|
48 |
private def delayed_action(first: Boolean)(time: Time)(action: => Unit): () => Unit = |
|
31942
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
49 |
{
|
| 38223 | 50 |
val listener = new ActionListener {
|
51 |
override def actionPerformed(e: ActionEvent) { Swing_Thread.assert(); action }
|
|
52 |
} |
|
|
40848
8662b9b1f123
more abstract/uniform handling of time, preferring seconds as Double;
wenzelm
parents:
38847
diff
changeset
|
53 |
val timer = new Timer(time.ms.toInt, listener) |
| 32501 | 54 |
timer.setRepeats(false) |
55 |
||
| 44572 | 56 |
def invoke() { now { 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
|
57 |
invoke _ |
|
63466160ff27
renamed Delay to Swing_Thread.delay (action is executed within AWT thread!);
wenzelm
parents:
31862
diff
changeset
|
58 |
} |
| 32501 | 59 |
|
60 |
// delayed action after first invocation |
|
61 |
def delay_first = delayed_action(true) _ |
|
62 |
||
63 |
// delayed action after last invocation |
|
64 |
def delay_last = delayed_action(false) _ |
|
| 29202 | 65 |
} |