author | wenzelm |
Sat, 12 Jan 2013 17:28:07 +0100 | |
changeset 50847 | 78c40f1cc9b3 |
parent 50845 | 477ca927676f |
child 50850 | 4cd2d090be8f |
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 |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48996
diff
changeset
|
12 |
import java.util.Locale |
50414
e17a1f179bb0
explore theory_body_files via future, for improved performance;
wenzelm
parents:
50299
diff
changeset
|
13 |
import java.util.concurrent.{Future => JFuture, TimeUnit} |
34216 | 14 |
import java.awt.Component |
15 |
import javax.swing.JOptionPane |
|
34136 | 16 |
|
47867
dd9cbe708e6b
some attempts to make critical errors fit on screen;
wenzelm
parents:
46997
diff
changeset
|
17 |
import scala.swing.{ComboBox, TextArea, ScrollPane} |
37018 | 18 |
import scala.swing.event.SelectionChanged |
43598 | 19 |
import scala.collection.mutable |
37018 | 20 |
|
21 |
||
34136 | 22 |
object Library |
23 |
{ |
|
43652 | 24 |
/* user errors */ |
25 |
||
26 |
object ERROR |
|
27 |
{ |
|
28 |
def apply(message: String): Throwable = new RuntimeException(message) |
|
48479
819f7a5f3e7f
more general notion of user ERROR (cf. 44f56fe01528);
wenzelm
parents:
48425
diff
changeset
|
29 |
def unapply(exn: Throwable): Option[String] = Exn.user_message(exn) |
43652 | 30 |
} |
31 |
||
32 |
def error(message: String): Nothing = throw ERROR(message) |
|
33 |
||
34 |
def cat_error(msg1: String, msg2: String): Nothing = |
|
35 |
if (msg1 == "") error(msg1) |
|
36 |
else error(msg1 + "\n" + msg2) |
|
37 |
||
38 |
||
48996 | 39 |
/* separated chunks */ |
36688 | 40 |
|
41 |
def separate[A](s: A, list: List[A]): List[A] = |
|
42 |
list match { |
|
43 |
case x :: xs if !xs.isEmpty => x :: s :: separate(s, xs) |
|
44 |
case _ => list |
|
45 |
} |
|
46 |
||
48996 | 47 |
def separated_chunks(sep: Char, source: CharSequence): Iterator[CharSequence] = |
48 |
new Iterator[CharSequence] { |
|
49 |
private val end = source.length |
|
50 |
private def next_chunk(i: Int): Option[(CharSequence, Int)] = |
|
51 |
{ |
|
52 |
if (i < end) { |
|
53 |
var j = i; do j += 1 while (j < end && source.charAt(j) != sep) |
|
54 |
Some((source.subSequence(i + 1, j), j)) |
|
55 |
} |
|
56 |
else None |
|
43598 | 57 |
} |
48996 | 58 |
private var state: Option[(CharSequence, Int)] = if (end == 0) None else next_chunk(-1) |
59 |
||
60 |
def hasNext(): Boolean = state.isDefined |
|
61 |
def next(): CharSequence = |
|
62 |
state match { |
|
63 |
case Some((s, i)) => { state = next_chunk(i); s } |
|
64 |
case None => Iterator.empty.next() |
|
65 |
} |
|
43598 | 66 |
} |
67 |
||
48996 | 68 |
def space_explode(sep: Char, str: String): List[String] = |
69 |
separated_chunks(sep, str).map(_.toString).toList |
|
70 |
||
71 |
||
72 |
/* lines */ |
|
73 |
||
74 |
def cat_lines(lines: TraversableOnce[String]): String = lines.mkString("\n") |
|
75 |
||
43670
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
76 |
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
|
77 |
|
48996 | 78 |
def first_line(source: CharSequence): String = |
79 |
{ |
|
80 |
val lines = separated_chunks('\n', source) |
|
81 |
if (lines.hasNext) lines.next.toString |
|
82 |
else "" |
|
83 |
} |
|
84 |
||
50847 | 85 |
|
86 |
/* strings */ |
|
87 |
||
50299 | 88 |
def lowercase(str: String): String = str.toLowerCase(Locale.ENGLISH) |
89 |
def uppercase(str: String): String = str.toUpperCase(Locale.ENGLISH) |
|
90 |
||
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48996
diff
changeset
|
91 |
def capitalize(str: String): String = |
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48996
diff
changeset
|
92 |
if (str.length == 0) str |
50299 | 93 |
else uppercase(str.substring(0, 1)) + str.substring(1) |
49245
cb70157293c0
manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents:
48996
diff
changeset
|
94 |
|
50847 | 95 |
def try_unprefix(prfx: String, s: String): Option[String] = |
96 |
if (s.startsWith(prfx)) Some(s.substring(prfx.length)) else None |
|
97 |
||
43598 | 98 |
|
48996 | 99 |
/* quote */ |
46196 | 100 |
|
43598 | 101 |
def quote(s: String): String = "\"" + s + "\"" |
102 |
def commas(ss: Iterable[String]): String = ss.iterator.mkString(", ") |
|
48362 | 103 |
def commas_quote(ss: Iterable[String]): String = ss.iterator.map(quote).mkString(", ") |
43598 | 104 |
|
36688 | 105 |
|
34141 | 106 |
/* reverse CharSequence */ |
107 |
||
108 |
class Reverse(text: CharSequence, start: Int, end: Int) extends CharSequence |
|
109 |
{ |
|
110 |
require(0 <= start && start <= end && end <= text.length) |
|
111 |
||
112 |
def this(text: CharSequence) = this(text, 0, text.length) |
|
113 |
||
114 |
def length: Int = end - start |
|
115 |
def charAt(i: Int): Char = text.charAt(end - i - 1) |
|
116 |
||
117 |
def subSequence(i: Int, j: Int): CharSequence = |
|
118 |
if (0 <= i && i <= j && j <= length) new Reverse(text, end - j, end - i) |
|
119 |
else throw new IndexOutOfBoundsException |
|
120 |
||
121 |
override def toString: String = |
|
122 |
{ |
|
123 |
val buf = new StringBuilder(length) |
|
124 |
for (i <- 0 until length) |
|
125 |
buf.append(charAt(i)) |
|
126 |
buf.toString |
|
127 |
} |
|
128 |
} |
|
129 |
||
130 |
||
34216 | 131 |
/* simple dialogs */ |
132 |
||
50539 | 133 |
def scrollable_text(txt: String, width: Int = 60, editable: Boolean = false): ScrollPane = |
47867
dd9cbe708e6b
some attempts to make critical errors fit on screen;
wenzelm
parents:
46997
diff
changeset
|
134 |
{ |
dd9cbe708e6b
some attempts to make critical errors fit on screen;
wenzelm
parents:
46997
diff
changeset
|
135 |
val text = new TextArea(txt) |
dd9cbe708e6b
some attempts to make critical errors fit on screen;
wenzelm
parents:
46997
diff
changeset
|
136 |
if (width > 0) text.columns = width |
dd9cbe708e6b
some attempts to make critical errors fit on screen;
wenzelm
parents:
46997
diff
changeset
|
137 |
text.editable = editable |
dd9cbe708e6b
some attempts to make critical errors fit on screen;
wenzelm
parents:
46997
diff
changeset
|
138 |
new ScrollPane(text) |
dd9cbe708e6b
some attempts to make critical errors fit on screen;
wenzelm
parents:
46997
diff
changeset
|
139 |
} |
dd9cbe708e6b
some attempts to make critical errors fit on screen;
wenzelm
parents:
46997
diff
changeset
|
140 |
|
46997 | 141 |
private def simple_dialog(kind: Int, default_title: String, |
142 |
parent: Component, title: String, message: Seq[Any]) |
|
34216 | 143 |
{ |
36791 | 144 |
Swing_Thread.now { |
38232
00b72526dc64
simple_dialog: allow scala.swing.Component as well;
wenzelm
parents:
37686
diff
changeset
|
145 |
val java_message = message map { case x: scala.swing.Component => x.peer case x => x } |
36791 | 146 |
JOptionPane.showMessageDialog(parent, |
38232
00b72526dc64
simple_dialog: allow scala.swing.Component as well;
wenzelm
parents:
37686
diff
changeset
|
147 |
java_message.toArray.asInstanceOf[Array[AnyRef]], |
36791 | 148 |
if (title == null) default_title else title, kind) |
149 |
} |
|
34216 | 150 |
} |
151 |
||
46997 | 152 |
def dialog(parent: Component, title: String, message: Any*) = |
153 |
simple_dialog(JOptionPane.PLAIN_MESSAGE, null, parent, title, message) |
|
154 |
||
155 |
def warning_dialog(parent: Component, title: String, message: Any*) = |
|
156 |
simple_dialog(JOptionPane.WARNING_MESSAGE, "Warning", parent, title, message) |
|
157 |
||
158 |
def error_dialog(parent: Component, title: String, message: Any*) = |
|
159 |
simple_dialog(JOptionPane.ERROR_MESSAGE, "Error", parent, title, message) |
|
34216 | 160 |
|
44573
51f8895b9ad9
some dialog for auto loading of required files (still inactive);
wenzelm
parents:
44158
diff
changeset
|
161 |
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
|
162 |
Swing_Thread.now { |
51f8895b9ad9
some dialog for auto loading of required files (still inactive);
wenzelm
parents:
44158
diff
changeset
|
163 |
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
|
164 |
JOptionPane.showConfirmDialog(parent, |
51f8895b9ad9
some dialog for auto loading of required files (still inactive);
wenzelm
parents:
44158
diff
changeset
|
165 |
java_message.toArray.asInstanceOf[Array[AnyRef]], title, |
51f8895b9ad9
some dialog for auto loading of required files (still inactive);
wenzelm
parents:
44158
diff
changeset
|
166 |
option_type, JOptionPane.QUESTION_MESSAGE) |
51f8895b9ad9
some dialog for auto loading of required files (still inactive);
wenzelm
parents:
44158
diff
changeset
|
167 |
} |
51f8895b9ad9
some dialog for auto loading of required files (still inactive);
wenzelm
parents:
44158
diff
changeset
|
168 |
|
34216 | 169 |
|
37018 | 170 |
/* zoom box */ |
171 |
||
37048 | 172 |
class Zoom_Box(apply_factor: Int => Unit) extends ComboBox[String]( |
173 |
List("50%", "70%", "85%", "100%", "125%", "150%", "175%", "200%", "300%", "400%")) |
|
174 |
{ |
|
47993 | 175 |
val Factor = "([0-9]+)%?".r |
37048 | 176 |
def parse(text: String): Int = |
177 |
text match { |
|
178 |
case Factor(s) => |
|
179 |
val i = Integer.parseInt(s) |
|
180 |
if (10 <= i && i <= 1000) i else 100 |
|
181 |
case _ => 100 |
|
182 |
} |
|
50491 | 183 |
|
37048 | 184 |
def print(i: Int): String = i.toString + "%" |
37018 | 185 |
|
50491 | 186 |
def set_item(i: Int) { |
187 |
peer.getEditor match { |
|
188 |
case null => |
|
189 |
case editor => editor.setItem(print(i)) |
|
190 |
} |
|
191 |
} |
|
192 |
||
37048 | 193 |
makeEditable()(c => new ComboBox.BuiltInEditor(c)(text => print(parse(text)), x => x)) |
194 |
reactions += { |
|
195 |
case SelectionChanged(_) => apply_factor(parse(selection.item)) |
|
37018 | 196 |
} |
37048 | 197 |
listenTo(selection) |
198 |
selection.index = 3 |
|
199 |
prototypeDisplayValue = Some("00000%") |
|
200 |
} |
|
50414
e17a1f179bb0
explore theory_body_files via future, for improved performance;
wenzelm
parents:
50299
diff
changeset
|
201 |
|
e17a1f179bb0
explore theory_body_files via future, for improved performance;
wenzelm
parents:
50299
diff
changeset
|
202 |
|
e17a1f179bb0
explore theory_body_files via future, for improved performance;
wenzelm
parents:
50299
diff
changeset
|
203 |
/* Java futures */ |
e17a1f179bb0
explore theory_body_files via future, for improved performance;
wenzelm
parents:
50299
diff
changeset
|
204 |
|
e17a1f179bb0
explore theory_body_files via future, for improved performance;
wenzelm
parents:
50299
diff
changeset
|
205 |
def future_value[A](x: A) = new JFuture[A] |
e17a1f179bb0
explore theory_body_files via future, for improved performance;
wenzelm
parents:
50299
diff
changeset
|
206 |
{ |
e17a1f179bb0
explore theory_body_files via future, for improved performance;
wenzelm
parents:
50299
diff
changeset
|
207 |
def cancel(may_interrupt: Boolean): Boolean = false |
e17a1f179bb0
explore theory_body_files via future, for improved performance;
wenzelm
parents:
50299
diff
changeset
|
208 |
def isCancelled(): Boolean = false |
e17a1f179bb0
explore theory_body_files via future, for improved performance;
wenzelm
parents:
50299
diff
changeset
|
209 |
def isDone(): Boolean = true |
e17a1f179bb0
explore theory_body_files via future, for improved performance;
wenzelm
parents:
50299
diff
changeset
|
210 |
def get(): A = x |
e17a1f179bb0
explore theory_body_files via future, for improved performance;
wenzelm
parents:
50299
diff
changeset
|
211 |
def get(timeout: Long, time_unit: TimeUnit): A = x |
e17a1f179bb0
explore theory_body_files via future, for improved performance;
wenzelm
parents:
50299
diff
changeset
|
212 |
} |
34136 | 213 |
} |
43652 | 214 |
|
215 |
||
216 |
class Basic_Library |
|
217 |
{ |
|
43670
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
218 |
val ERROR = Library.ERROR |
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
219 |
val error = Library.error _ |
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
220 |
val cat_error = Library.cat_error _ |
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
221 |
|
43652 | 222 |
val space_explode = Library.space_explode _ |
43670
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
223 |
val split_lines = Library.split_lines _ |
46196 | 224 |
val cat_lines = Library.cat_lines _ |
43652 | 225 |
val quote = Library.quote _ |
226 |
val commas = Library.commas _ |
|
227 |
val commas_quote = Library.commas_quote _ |
|
49470 | 228 |
|
229 |
||
230 |
/* parallel tasks */ |
|
231 |
||
232 |
implicit def function_as_callable[A](f: () => A) = |
|
233 |
new java.util.concurrent.Callable[A] { def call = f() } |
|
234 |
||
235 |
val default_thread_pool = |
|
236 |
scala.collection.parallel.ThreadPoolTasks.defaultThreadPool |
|
43652 | 237 |
} |