author | wenzelm |
Sun, 22 Aug 2010 16:43:20 +0200 | |
changeset 38576 | ce3eed2b16f7 |
parent 38258 | dd7dcb9b2637 |
child 38583 | ff7f9510b0d6 |
permissions | -rw-r--r-- |
34136 | 1 |
/* Title: Pure/library.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Basic library. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
38258 | 9 |
|
10 |
import java.lang.{System, Thread} |
|
34216 | 11 |
import java.awt.Component |
12 |
import javax.swing.JOptionPane |
|
34136 | 13 |
|
38258 | 14 |
import scala.actors.Actor |
37018 | 15 |
import scala.swing.ComboBox |
16 |
import scala.swing.event.SelectionChanged |
|
17 |
||
18 |
||
34136 | 19 |
object Library |
20 |
{ |
|
37035 | 21 |
/* partial functions */ |
22 |
||
23 |
def undefined[A, B] = new PartialFunction[A, B] { |
|
24 |
def apply(x: A): B = throw new NoSuchElementException("undefined") |
|
25 |
def isDefinedAt(x: A) = false |
|
26 |
} |
|
27 |
||
28 |
||
36688 | 29 |
/* separate */ |
30 |
||
31 |
def separate[A](s: A, list: List[A]): List[A] = |
|
32 |
list match { |
|
33 |
case x :: xs if !xs.isEmpty => x :: s :: separate(s, xs) |
|
34 |
case _ => list |
|
35 |
} |
|
36 |
||
37 |
||
34141 | 38 |
/* reverse CharSequence */ |
39 |
||
40 |
class Reverse(text: CharSequence, start: Int, end: Int) extends CharSequence |
|
41 |
{ |
|
42 |
require(0 <= start && start <= end && end <= text.length) |
|
43 |
||
44 |
def this(text: CharSequence) = this(text, 0, text.length) |
|
45 |
||
46 |
def length: Int = end - start |
|
47 |
def charAt(i: Int): Char = text.charAt(end - i - 1) |
|
48 |
||
49 |
def subSequence(i: Int, j: Int): CharSequence = |
|
50 |
if (0 <= i && i <= j && j <= length) new Reverse(text, end - j, end - i) |
|
51 |
else throw new IndexOutOfBoundsException |
|
52 |
||
53 |
override def toString: String = |
|
54 |
{ |
|
55 |
val buf = new StringBuilder(length) |
|
56 |
for (i <- 0 until length) |
|
57 |
buf.append(charAt(i)) |
|
58 |
buf.toString |
|
59 |
} |
|
60 |
} |
|
61 |
||
62 |
||
36685 | 63 |
/* iterate over chunks (cf. space_explode/split_lines in ML) */ |
64 |
||
65 |
def chunks(source: CharSequence, sep: Char = '\n') = new Iterator[CharSequence] |
|
66 |
{ |
|
67 |
private val end = source.length |
|
68 |
private def next_chunk(i: Int): Option[(CharSequence, Int)] = |
|
69 |
{ |
|
70 |
if (i < end) { |
|
71 |
var j = i; do j += 1 while (j < end && source.charAt(j) != sep) |
|
72 |
Some((source.subSequence(i + 1, j), j)) |
|
73 |
} |
|
74 |
else None |
|
75 |
} |
|
76 |
private var state: Option[(CharSequence, Int)] = if (end == 0) None else next_chunk(-1) |
|
77 |
||
78 |
def hasNext(): Boolean = state.isDefined |
|
79 |
def next(): CharSequence = |
|
80 |
state match { |
|
81 |
case Some((s, i)) => { state = next_chunk(i); s } |
|
82 |
case None => throw new NoSuchElementException("next on empty iterator") |
|
83 |
} |
|
84 |
} |
|
85 |
||
86 |
||
34216 | 87 |
/* simple dialogs */ |
88 |
||
89 |
private def simple_dialog(kind: Int, default_title: String) |
|
90 |
(parent: Component, title: String, message: Any*) |
|
91 |
{ |
|
36791 | 92 |
Swing_Thread.now { |
38232
00b72526dc64
simple_dialog: allow scala.swing.Component as well;
wenzelm
parents:
37686
diff
changeset
|
93 |
val java_message = message map { case x: scala.swing.Component => x.peer case x => x } |
36791 | 94 |
JOptionPane.showMessageDialog(parent, |
38232
00b72526dc64
simple_dialog: allow scala.swing.Component as well;
wenzelm
parents:
37686
diff
changeset
|
95 |
java_message.toArray.asInstanceOf[Array[AnyRef]], |
36791 | 96 |
if (title == null) default_title else title, kind) |
97 |
} |
|
34216 | 98 |
} |
99 |
||
100 |
def dialog = simple_dialog(JOptionPane.PLAIN_MESSAGE, null) _ |
|
101 |
def warning_dialog = simple_dialog(JOptionPane.WARNING_MESSAGE, "Warning") _ |
|
102 |
def error_dialog = simple_dialog(JOptionPane.ERROR_MESSAGE, "Error") _ |
|
103 |
||
104 |
||
37018 | 105 |
/* zoom box */ |
106 |
||
37048 | 107 |
class Zoom_Box(apply_factor: Int => Unit) extends ComboBox[String]( |
108 |
List("50%", "70%", "85%", "100%", "125%", "150%", "175%", "200%", "300%", "400%")) |
|
109 |
{ |
|
110 |
val Factor = "([0-9]+)%?"r |
|
111 |
def parse(text: String): Int = |
|
112 |
text match { |
|
113 |
case Factor(s) => |
|
114 |
val i = Integer.parseInt(s) |
|
115 |
if (10 <= i && i <= 1000) i else 100 |
|
116 |
case _ => 100 |
|
117 |
} |
|
118 |
def print(i: Int): String = i.toString + "%" |
|
37018 | 119 |
|
37048 | 120 |
makeEditable()(c => new ComboBox.BuiltInEditor(c)(text => print(parse(text)), x => x)) |
121 |
reactions += { |
|
122 |
case SelectionChanged(_) => apply_factor(parse(selection.item)) |
|
37018 | 123 |
} |
37048 | 124 |
listenTo(selection) |
125 |
selection.index = 3 |
|
126 |
prototypeDisplayValue = Some("00000%") |
|
127 |
} |
|
37018 | 128 |
|
129 |
||
34136 | 130 |
/* timing */ |
131 |
||
34314 | 132 |
def timeit[A](message: String)(e: => A) = |
34136 | 133 |
{ |
37686 | 134 |
val start = System.nanoTime() |
34136 | 135 |
val result = Exn.capture(e) |
37686 | 136 |
val stop = System.nanoTime() |
34314 | 137 |
System.err.println( |
37686 | 138 |
(if (message == null || message.isEmpty) "" else message + ": ") + |
139 |
((stop - start).toDouble / 1000000) + "ms elapsed time") |
|
34136 | 140 |
Exn.release(result) |
141 |
} |
|
38258 | 142 |
|
143 |
||
144 |
/* thread as actor */ |
|
145 |
||
146 |
def thread_actor(name: String)(body: => Unit): Actor = |
|
147 |
{ |
|
148 |
val actor = Future.promise[Actor] |
|
149 |
val thread = new Thread(name) { override def run() = { actor.fulfill(Actor.self); body } } |
|
150 |
thread.start |
|
151 |
actor.join |
|
152 |
} |
|
34136 | 153 |
} |