src/Pure/library.scala
author wenzelm
Thu, 31 Dec 2009 23:47:09 +0100
changeset 34216 ada8eb23a08e
parent 34198 ff5486262cd6
child 34317 f799f3749596
permissions -rw-r--r--
added simple dialogs;
     1 /*  Title:      Pure/library.scala
     2     Author:     Makarius
     3 
     4 Basic library.
     5 */
     6 
     7 package isabelle
     8 
     9 import java.lang.System
    10 import java.awt.Component
    11 import javax.swing.JOptionPane
    12 
    13 
    14 object Library
    15 {
    16   /* reverse CharSequence */
    17 
    18   class Reverse(text: CharSequence, start: Int, end: Int) extends CharSequence
    19   {
    20     require(0 <= start && start <= end && end <= text.length)
    21 
    22     def this(text: CharSequence) = this(text, 0, text.length)
    23 
    24     def length: Int = end - start
    25     def charAt(i: Int): Char = text.charAt(end - i - 1)
    26 
    27     def subSequence(i: Int, j: Int): CharSequence =
    28       if (0 <= i && i <= j && j <= length) new Reverse(text, end - j, end - i)
    29       else throw new IndexOutOfBoundsException
    30 
    31     override def toString: String =
    32     {
    33       val buf = new StringBuilder(length)
    34       for (i <- 0 until length)
    35         buf.append(charAt(i))
    36       buf.toString
    37     }
    38   }
    39 
    40 
    41   /* simple dialogs */
    42 
    43   private def simple_dialog(kind: Int, default_title: String)
    44     (parent: Component, title: String, message: Any*)
    45   {
    46     JOptionPane.showMessageDialog(parent,
    47       message.toArray.asInstanceOf[Array[AnyRef]],
    48       if (title == null) default_title else title, kind)
    49   }
    50 
    51   def dialog = simple_dialog(JOptionPane.PLAIN_MESSAGE, null) _
    52   def warning_dialog = simple_dialog(JOptionPane.WARNING_MESSAGE, "Warning") _
    53   def error_dialog = simple_dialog(JOptionPane.ERROR_MESSAGE, "Error") _
    54 
    55 
    56   /* timing */
    57 
    58   def timeit[A](e: => A) =
    59   {
    60     val start = System.currentTimeMillis()
    61     val result = Exn.capture(e)
    62     val stop = System.currentTimeMillis()
    63     System.err.println((stop - start) + "ms elapsed time")
    64     Exn.release(result)
    65   }
    66 }