src/Pure/General/delay.scala
author wenzelm
Sat, 04 Jul 2009 22:22:34 +0200
changeset 31941 d3a94ae9936f
permissions -rw-r--r--
Delayed action.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31941
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/General/symbol.scala
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
     2
    Author:     Fabian Immler, TU Munich
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
     4
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
     5
Delayed action: executed once after specified time span, even if
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
     6
prodded frequently.
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
     7
*/
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
     8
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
     9
package isabelle
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
    10
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
    11
import javax.swing.Timer
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
    12
import java.awt.event.{ActionListener, ActionEvent}
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
    13
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
    14
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
    15
object Delay
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
    16
{
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
    17
  def apply(amount: Int)(action: => Unit) = new Delay(amount)(action)
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
    18
}
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
    19
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
    20
class Delay(amount: Int)(action: => Unit)
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
    21
{
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
    22
  private val timer = new Timer(amount,
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
    23
    new ActionListener { override def actionPerformed(e: ActionEvent) { action } })
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
    24
  def prod()
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
    25
  {
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
    26
    if (!timer.isRunning()) {
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
    27
      timer.setRepeats(false)
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
    28
      timer.start()
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
    29
    }
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
    30
  }
d3a94ae9936f Delayed action.
wenzelm
parents:
diff changeset
    31
}