src/Pure/General/swing_thread.scala
changeset 31942 63466160ff27
parent 31862 53acb8ec6c51
child 32494 4ab2292e452a
equal deleted inserted replaced
31941:d3a94ae9936f 31942:63466160ff27
     1 /*  Title:      Pure/General/swing_thread.scala
     1 /*  Title:      Pure/General/swing_thread.scala
     2     Author:     Makarius
     2     Author:     Makarius
       
     3     Author:     Fabian Immler, TU Munich
     3 
     4 
     4 Evaluation within the AWT/Swing thread.
     5 Evaluation within the AWT/Swing thread.
     5 */
     6 */
     6 
     7 
     7 package isabelle
     8 package isabelle
     8 
     9 
     9 import javax.swing.SwingUtilities
    10 import javax.swing.{SwingUtilities, Timer}
       
    11 import java.awt.event.{ActionListener, ActionEvent}
       
    12 
    10 
    13 
    11 object Swing_Thread
    14 object Swing_Thread
    12 {
    15 {
       
    16   /* main dispatch queue */
       
    17 
    13   def now[A](body: => A): A = {
    18   def now[A](body: => A): A = {
    14     var result: Option[A] = None
    19     var result: Option[A] = None
    15     if (SwingUtilities.isEventDispatchThread) { result = Some(body) }
    20     if (SwingUtilities.isEventDispatchThread) { result = Some(body) }
    16     else SwingUtilities.invokeAndWait(new Runnable { def run = { result = Some(body) } })
    21     else SwingUtilities.invokeAndWait(new Runnable { def run = { result = Some(body) } })
    17     result.get
    22     result.get
    19 
    24 
    20   def later(body: => Unit) {
    25   def later(body: => Unit) {
    21     if (SwingUtilities.isEventDispatchThread) body
    26     if (SwingUtilities.isEventDispatchThread) body
    22     else SwingUtilities.invokeLater(new Runnable { def run = body })
    27     else SwingUtilities.invokeLater(new Runnable { def run = body })
    23   }
    28   }
       
    29 
       
    30 
       
    31   /* delayed actions */
       
    32 
       
    33   // turn multiple invocations into single action within time span
       
    34   def delay(time_span: Int)(action: => Unit): () => Unit =
       
    35   {
       
    36     val listener =
       
    37       new ActionListener { override def actionPerformed(e: ActionEvent) { action } }
       
    38     val timer = new Timer(time_span, listener)
       
    39     def invoke()
       
    40     {
       
    41       if (!timer.isRunning()) {
       
    42         timer.setRepeats(false)
       
    43         timer.start()
       
    44       }
       
    45     }
       
    46     invoke _
       
    47   }
    24 }
    48 }