1 /* Title: Pure/library.scala
10 import java.lang.System
11 import java.awt.Component
12 import javax.swing.JOptionPane
14 import scala.swing.ComboBox
15 import scala.swing.event.SelectionChanged
16 import scala.collection.mutable
25 def apply(message: String): Throwable = new RuntimeException(message)
26 def unapply(exn: Throwable): Option[String] =
28 case e: RuntimeException => Some(Exn.message(e))
33 def error(message: String): Nothing = throw ERROR(message)
35 def cat_error(msg1: String, msg2: String): Nothing =
36 if (msg1 == "") error(msg1)
37 else error(msg1 + "\n" + msg2)
42 def separate[A](s: A, list: List[A]): List[A] =
44 case x :: xs if !xs.isEmpty => x :: s :: separate(s, xs)
48 def space_explode(sep: Char, str: String): List[String] =
51 val result = new mutable.ListBuffer[String]
55 val i = str.indexOf(sep, start)
56 if (i == -1) { result += str.substring(start); finished = true }
57 else { result += str.substring(start, i); start = i + 1 }
62 def split_lines(str: String): List[String] = space_explode('\n', str)
65 /* iterate over chunks (cf. space_explode) */
67 def chunks(source: CharSequence, sep: Char = '\n') = new Iterator[CharSequence]
69 private val end = source.length
70 private def next_chunk(i: Int): Option[(CharSequence, Int)] =
73 var j = i; do j += 1 while (j < end && source.charAt(j) != sep)
74 Some((source.subSequence(i + 1, j), j))
78 private var state: Option[(CharSequence, Int)] = if (end == 0) None else next_chunk(-1)
80 def hasNext(): Boolean = state.isDefined
81 def next(): CharSequence =
83 case Some((s, i)) => { state = next_chunk(i); s }
84 case None => Iterator.empty.next()
88 def first_line(source: CharSequence): String =
90 val lines = chunks(source)
91 if (lines.hasNext) lines.next.toString
98 def quote(s: String): String = "\"" + s + "\""
99 def commas(ss: Iterable[String]): String = ss.iterator.mkString(", ")
100 def commas_quote(ss: Iterable[String]): String = ss.iterator.mkString("\"", ", ", "\"")
103 /* reverse CharSequence */
105 class Reverse(text: CharSequence, start: Int, end: Int) extends CharSequence
107 require(0 <= start && start <= end && end <= text.length)
109 def this(text: CharSequence) = this(text, 0, text.length)
111 def length: Int = end - start
112 def charAt(i: Int): Char = text.charAt(end - i - 1)
114 def subSequence(i: Int, j: Int): CharSequence =
115 if (0 <= i && i <= j && j <= length) new Reverse(text, end - j, end - i)
116 else throw new IndexOutOfBoundsException
118 override def toString: String =
120 val buf = new StringBuilder(length)
121 for (i <- 0 until length)
122 buf.append(charAt(i))
130 private def simple_dialog(kind: Int, default_title: String)
131 (parent: Component, title: String, message: Any*)
134 val java_message = message map { case x: scala.swing.Component => x.peer case x => x }
135 JOptionPane.showMessageDialog(parent,
136 java_message.toArray.asInstanceOf[Array[AnyRef]],
137 if (title == null) default_title else title, kind)
141 def dialog = simple_dialog(JOptionPane.PLAIN_MESSAGE, null) _
142 def warning_dialog = simple_dialog(JOptionPane.WARNING_MESSAGE, "Warning") _
143 def error_dialog = simple_dialog(JOptionPane.ERROR_MESSAGE, "Error") _
145 def confirm_dialog(parent: Component, title: String, option_type: Int, message: Any*): Int =
147 val java_message = message map { case x: scala.swing.Component => x.peer case x => x }
148 JOptionPane.showConfirmDialog(parent,
149 java_message.toArray.asInstanceOf[Array[AnyRef]], title,
150 option_type, JOptionPane.QUESTION_MESSAGE)
156 class Zoom_Box(apply_factor: Int => Unit) extends ComboBox[String](
157 List("50%", "70%", "85%", "100%", "125%", "150%", "175%", "200%", "300%", "400%"))
159 val Factor = "([0-9]+)%?"r
160 def parse(text: String): Int =
163 val i = Integer.parseInt(s)
164 if (10 <= i && i <= 1000) i else 100
167 def print(i: Int): String = i.toString + "%"
169 makeEditable()(c => new ComboBox.BuiltInEditor(c)(text => print(parse(text)), x => x))
171 case SelectionChanged(_) => apply_factor(parse(selection.item))
175 prototypeDisplayValue = Some("00000%")
181 def timeit[A](message: String)(e: => A) =
183 val start = System.currentTimeMillis()
184 val result = Exn.capture(e)
185 val stop = System.currentTimeMillis()
187 (if (message == null || message.isEmpty) "" else message + ": ") +
188 new Time(stop - start).message + " elapsed time")
196 val ERROR = Library.ERROR
197 val error = Library.error _
198 val cat_error = Library.cat_error _
200 val space_explode = Library.space_explode _
201 val split_lines = Library.split_lines _
203 val quote = Library.quote _
204 val commas = Library.commas _
205 val commas_quote = Library.commas_quote _