| 34136 |      1 | /*  Title:      Pure/library.scala
 | 
|  |      2 |     Author:     Makarius
 | 
|  |      3 | 
 | 
|  |      4 | Basic library.
 | 
|  |      5 | */
 | 
|  |      6 | 
 | 
|  |      7 | package isabelle
 | 
|  |      8 | 
 | 
|  |      9 | import java.lang.System
 | 
| 34216 |     10 | import java.awt.Component
 | 
|  |     11 | import javax.swing.JOptionPane
 | 
| 34136 |     12 | 
 | 
|  |     13 | 
 | 
|  |     14 | object Library
 | 
|  |     15 | {
 | 
| 34141 |     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 | 
 | 
| 34216 |     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 | 
 | 
| 34136 |     56 |   /* timing */
 | 
|  |     57 | 
 | 
| 34314 |     58 |   def timeit[A](message: String)(e: => A) =
 | 
| 34136 |     59 |   {
 | 
|  |     60 |     val start = System.currentTimeMillis()
 | 
|  |     61 |     val result = Exn.capture(e)
 | 
|  |     62 |     val stop = System.currentTimeMillis()
 | 
| 34314 |     63 |     System.err.println(
 | 
| 34317 |     64 |       (if (message.isEmpty) "" else message + ": ") + (stop - start) + "ms elapsed time")
 | 
| 34136 |     65 |     Exn.release(result)
 | 
|  |     66 |   }
 | 
|  |     67 | }
 |