<commit_before>/*  Title:      Pure/General/swing_thread.scala
    Author:     Makarius
    Author:     <NAME>, TU Munich

Evaluation within the AWT/Swing thread.
*/

package isabelle

import javax.swing.{SwingUtilities, Timer}
import java.awt.event.{ActionListener, ActionEvent}


object Swing_Thread
{
  /* main dispatch queue */

  def now[A](body: => A): A = {
    var result: Option[A] = None
    if (SwingUtilities.isEventDispatchThread) { result = Some(body) }
    else SwingUtilities.invokeAndWait(new Runnable { def run = { result = Some(body) } })
    result.get
  }

  def later(body: => Unit) {
    if (SwingUtilities.isEventDispatchThread) body
    else SwingUtilities.invokeLater(new Runnable { def run = body })
  }


  /* delayed actions */

  // turn multiple invocations into single action within time span
  def delay(time_span: Int)(action: => Unit): () => Unit =
  {
    val listener =
      new ActionListener { override def actionPerformed(e: ActionEvent) { action } }
    val timer = new Timer(time_span, listener)
    def invoke()<commit_msg>explicit checks;<commit_after>/*  Title:      Pure/General/swing_thread.scala
    Author:     Makarius
    Author:     <NAME>, TU Munich

Evaluation within the AWT/Swing thread.
*/

package isabelle

import javax.swing.{SwingUtilities, Timer}
import java.awt.event.{ActionListener, ActionEvent}


object Swing_Thread
{
  /* checks */

  def assert() = Predef.assert(SwingUtilities.isEventDispatchThread())
  def require() = Predef.require(SwingUtilities.isEventDispatchThread())


  /* main dispatch queue */

  def now[A](body: => A): A = {
    var result: Option[A] = None
    if (SwingUtilities.isEventDispatchThread()) { result = Some(body) }
    else SwingUtilities.invokeAndWait(new Runnable { def run = { result = Some(body) } })
    result.get
  }

  def later(body: => Unit) {
    if (SwingUtilities.isEventDispatchThread()) body
    else SwingUtilities.invokeLater(new Runnable { def run = body })
  }


  /* delayed actions */

  // turn multiple invocations into single action within time span
  def delay(time_span: Int)(action: => Unit): () => Unit =
  {
    val listener =
      new ActionListener { override def actionPerformed(e: ActionEvent) { action } }
    val timer = new Timer(time_span, listener)
    def invoke()
{'fraction_non_alphanumeric': 0.11593573599652628, 'fraction_numerical': 0.0, 'mean_word_length': 3.482490272373541, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2630449', 'n_tokens_mistral': 726, 'n_tokens_neox': 705, 'n_words': 189}