author | wenzelm |
Sat, 01 Sep 2012 19:27:28 +0200 | |
changeset 49061 | 7449b804073b |
parent 48996 | a8bad1369ada |
child 49245 | cb70157293c0 |
permissions | -rw-r--r-- |
34136 | 1 |
/* Title: Pure/library.scala |
45673
cd41e3903fbf
separate compilation of PIDE vs. Pure sources, which enables independent Scala library;
wenzelm
parents:
45667
diff
changeset
|
2 |
Module: PIDE |
34136 | 3 |
Author: Makarius |
4 |
||
5 |
Basic library. |
|
6 |
*/ |
|
7 |
||
8 |
package isabelle |
|
9 |
||
38258 | 10 |
|
38636
b7647ca7de5a
module for simplified thread operations (Scala version);
wenzelm
parents:
38583
diff
changeset
|
11 |
import java.lang.System |
34216 | 12 |
import java.awt.Component |
13 |
import javax.swing.JOptionPane |
|
34136 | 14 |
|
47867
dd9cbe708e6b
some attempts to make critical errors fit on screen;
wenzelm
parents:
46997
diff
changeset
|
15 |
import scala.swing.{ComboBox, TextArea, ScrollPane} |
37018 | 16 |
import scala.swing.event.SelectionChanged |
43598 | 17 |
import scala.collection.mutable |
37018 | 18 |
|
19 |
||
34136 | 20 |
object Library |
21 |
{ |
|
43652 | 22 |
/* user errors */ |
23 |
||
24 |
object ERROR |
|
25 |
{ |
|
26 |
def apply(message: String): Throwable = new RuntimeException(message) |
|
48479
819f7a5f3e7f
more general notion of user ERROR (cf. 44f56fe01528);
wenzelm
parents:
48425
diff
changeset
|
27 |
def unapply(exn: Throwable): Option[String] = Exn.user_message(exn) |
43652 | 28 |
} |
29 |
||
30 |
def error(message: String): Nothing = throw ERROR(message) |
|
31 |
||
32 |
def cat_error(msg1: String, msg2: String): Nothing = |
|
33 |
if (msg1 == "") error(msg1) |
|
34 |
else error(msg1 + "\n" + msg2) |
|
35 |
||
36 |
||
48996 | 37 |
/* separated chunks */ |
36688 | 38 |
|
39 |
def separate[A](s: A, list: List[A]): List[A] = |
|
40 |
list match { |
|
41 |
case x :: xs if !xs.isEmpty => x :: s :: separate(s, xs) |
|
42 |
case _ => list |
|
43 |
} |
|
44 |
||
48996 | 45 |
def separated_chunks(sep: Char, source: CharSequence): Iterator[CharSequence] = |
46 |
new Iterator[CharSequence] { |
|
47 |
private val end = source.length |
|
48 |
private def next_chunk(i: Int): Option[(CharSequence, Int)] = |
|
49 |
{ |
|
50 |
if (i < end) { |
|
51 |
var j = i; do j += 1 while (j < end && source.charAt(j) != sep) |
|
52 |
Some((source.subSequence(i + 1, j), j)) |
|
53 |
} |
|
54 |
else None |
|
43598 | 55 |
} |
48996 | 56 |
private var state: Option[(CharSequence, Int)] = if (end == 0) None else next_chunk(-1) |
57 |
||
58 |
def hasNext(): Boolean = state.isDefined |
|
59 |
def next(): CharSequence = |
|
60 |
state match { |
|
61 |
case Some((s, i)) => { state = next_chunk(i); s } |
|
62 |
case None => Iterator.empty.next() |
|
63 |
} |
|
43598 | 64 |
} |
65 |
||
48996 | 66 |
def space_explode(sep: Char, str: String): List[String] = |
67 |
separated_chunks(sep, str).map(_.toString).toList |
|
68 |
||
69 |
||
70 |
/* lines */ |
|
71 |
||
72 |
def cat_lines(lines: TraversableOnce[String]): String = lines.mkString("\n") |
|
73 |
||
43670
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
74 |
def split_lines(str: String): List[String] = space_explode('\n', str) |
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
75 |
|
48996 | 76 |
def first_line(source: CharSequence): String = |
77 |
{ |
|
78 |
val lines = separated_chunks('\n', source) |
|
79 |
if (lines.hasNext) lines.next.toString |
|
80 |
else "" |
|
81 |
} |
|
82 |
||
48425 | 83 |
def trim_line(str: String): String = |
84 |
if (str.endsWith("\n")) str.substring(0, str.length - 1) |
|
85 |
else str |
|
86 |
||
43598 | 87 |
|
48996 | 88 |
/* quote */ |
46196 | 89 |
|
43598 | 90 |
def quote(s: String): String = "\"" + s + "\"" |
91 |
def commas(ss: Iterable[String]): String = ss.iterator.mkString(", ") |
|
48362 | 92 |
def commas_quote(ss: Iterable[String]): String = ss.iterator.map(quote).mkString(", ") |
43598 | 93 |
|
36688 | 94 |
|
34141 | 95 |
/* reverse CharSequence */ |
96 |
||
97 |
class Reverse(text: CharSequence, start: Int, end: Int) extends CharSequence |
|
98 |
{ |
|
99 |
require(0 <= start && start <= end && end <= text.length) |
|
100 |
||
101 |
def this(text: CharSequence) = this(text, 0, text.length) |
|
102 |
||
103 |
def length: Int = end - start |
|
104 |
def charAt(i: Int): Char = text.charAt(end - i - 1) |
|
105 |
||
106 |
def subSequence(i: Int, j: Int): CharSequence = |
|
107 |
if (0 <= i && i <= j && j <= length) new Reverse(text, end - j, end - i) |
|
108 |
else throw new IndexOutOfBoundsException |
|
109 |
||
110 |
override def toString: String = |
|
111 |
{ |
|
112 |
val buf = new StringBuilder(length) |
|
113 |
for (i <- 0 until length) |
|
114 |
buf.append(charAt(i)) |
|
115 |
buf.toString |
|
116 |
} |
|
117 |
} |
|
118 |
||
119 |
||
34216 | 120 |
/* simple dialogs */ |
121 |
||
48345 | 122 |
def scrollable_text(txt: String, width: Int = 80, editable: Boolean = false): ScrollPane = |
47867
dd9cbe708e6b
some attempts to make critical errors fit on screen;
wenzelm
parents:
46997
diff
changeset
|
123 |
{ |
dd9cbe708e6b
some attempts to make critical errors fit on screen;
wenzelm
parents:
46997
diff
changeset
|
124 |
val text = new TextArea(txt) |
dd9cbe708e6b
some attempts to make critical errors fit on screen;
wenzelm
parents:
46997
diff
changeset
|
125 |
if (width > 0) text.columns = width |
dd9cbe708e6b
some attempts to make critical errors fit on screen;
wenzelm
parents:
46997
diff
changeset
|
126 |
text.editable = editable |
dd9cbe708e6b
some attempts to make critical errors fit on screen;
wenzelm
parents:
46997
diff
changeset
|
127 |
new ScrollPane(text) |
dd9cbe708e6b
some attempts to make critical errors fit on screen;
wenzelm
parents:
46997
diff
changeset
|
128 |
} |
dd9cbe708e6b
some attempts to make critical errors fit on screen;
wenzelm
parents:
46997
diff
changeset
|
129 |
|
46997 | 130 |
private def simple_dialog(kind: Int, default_title: String, |
131 |
parent: Component, title: String, message: Seq[Any]) |
|
34216 | 132 |
{ |
36791 | 133 |
Swing_Thread.now { |
38232
00b72526dc64
simple_dialog: allow scala.swing.Component as well;
wenzelm
parents:
37686
diff
changeset
|
134 |
val java_message = message map { case x: scala.swing.Component => x.peer case x => x } |
36791 | 135 |
JOptionPane.showMessageDialog(parent, |
38232
00b72526dc64
simple_dialog: allow scala.swing.Component as well;
wenzelm
parents:
37686
diff
changeset
|
136 |
java_message.toArray.asInstanceOf[Array[AnyRef]], |
36791 | 137 |
if (title == null) default_title else title, kind) |
138 |
} |
|
34216 | 139 |
} |
140 |
||
46997 | 141 |
def dialog(parent: Component, title: String, message: Any*) = |
142 |
simple_dialog(JOptionPane.PLAIN_MESSAGE, null, parent, title, message) |
|
143 |
||
144 |
def warning_dialog(parent: Component, title: String, message: Any*) = |
|
145 |
simple_dialog(JOptionPane.WARNING_MESSAGE, "Warning", parent, title, message) |
|
146 |
||
147 |
def error_dialog(parent: Component, title: String, message: Any*) = |
|
148 |
simple_dialog(JOptionPane.ERROR_MESSAGE, "Error", parent, title, message) |
|
34216 | 149 |
|
44573
51f8895b9ad9
some dialog for auto loading of required files (still inactive);
wenzelm
parents:
44158
diff
changeset
|
150 |
def confirm_dialog(parent: Component, title: String, option_type: Int, message: Any*): Int = |
51f8895b9ad9
some dialog for auto loading of required files (still inactive);
wenzelm
parents:
44158
diff
changeset
|
151 |
Swing_Thread.now { |
51f8895b9ad9
some dialog for auto loading of required files (still inactive);
wenzelm
parents:
44158
diff
changeset
|
152 |
val java_message = message map { case x: scala.swing.Component => x.peer case x => x } |
51f8895b9ad9
some dialog for auto loading of required files (still inactive);
wenzelm
parents:
44158
diff
changeset
|
153 |
JOptionPane.showConfirmDialog(parent, |
51f8895b9ad9
some dialog for auto loading of required files (still inactive);
wenzelm
parents:
44158
diff
changeset
|
154 |
java_message.toArray.asInstanceOf[Array[AnyRef]], title, |
51f8895b9ad9
some dialog for auto loading of required files (still inactive);
wenzelm
parents:
44158
diff
changeset
|
155 |
option_type, JOptionPane.QUESTION_MESSAGE) |
51f8895b9ad9
some dialog for auto loading of required files (still inactive);
wenzelm
parents:
44158
diff
changeset
|
156 |
} |
51f8895b9ad9
some dialog for auto loading of required files (still inactive);
wenzelm
parents:
44158
diff
changeset
|
157 |
|
34216 | 158 |
|
37018 | 159 |
/* zoom box */ |
160 |
||
37048 | 161 |
class Zoom_Box(apply_factor: Int => Unit) extends ComboBox[String]( |
162 |
List("50%", "70%", "85%", "100%", "125%", "150%", "175%", "200%", "300%", "400%")) |
|
163 |
{ |
|
47993 | 164 |
val Factor = "([0-9]+)%?".r |
37048 | 165 |
def parse(text: String): Int = |
166 |
text match { |
|
167 |
case Factor(s) => |
|
168 |
val i = Integer.parseInt(s) |
|
169 |
if (10 <= i && i <= 1000) i else 100 |
|
170 |
case _ => 100 |
|
171 |
} |
|
172 |
def print(i: Int): String = i.toString + "%" |
|
37018 | 173 |
|
37048 | 174 |
makeEditable()(c => new ComboBox.BuiltInEditor(c)(text => print(parse(text)), x => x)) |
175 |
reactions += { |
|
176 |
case SelectionChanged(_) => apply_factor(parse(selection.item)) |
|
37018 | 177 |
} |
37048 | 178 |
listenTo(selection) |
179 |
selection.index = 3 |
|
180 |
prototypeDisplayValue = Some("00000%") |
|
181 |
} |
|
34136 | 182 |
} |
43652 | 183 |
|
184 |
||
185 |
class Basic_Library |
|
186 |
{ |
|
43670
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
187 |
val ERROR = Library.ERROR |
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
188 |
val error = Library.error _ |
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
189 |
val cat_error = Library.cat_error _ |
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
190 |
|
43652 | 191 |
val space_explode = Library.space_explode _ |
43670
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
192 |
val split_lines = Library.split_lines _ |
46196 | 193 |
val cat_lines = Library.cat_lines _ |
43652 | 194 |
val quote = Library.quote _ |
195 |
val commas = Library.commas _ |
|
196 |
val commas_quote = Library.commas_quote _ |
|
197 |
} |