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