author | wenzelm |
Tue, 29 Sep 2015 16:58:33 +0200 | |
changeset 61280 | 12f9ab87a06d |
parent 61279 | 8410015c3e82 |
child 61281 | 11c1bf92d61d |
permissions | -rw-r--r-- |
53783
f5e9d182f645
clarified location of GUI modules (which depend on Swing of JFX);
wenzelm
parents:
53460
diff
changeset
|
1 |
/* Title: Pure/GUI/system_dialog.scala |
53453
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
3 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
4 |
Dialog for system processes, with optional output window. |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
5 |
*/ |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
6 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
7 |
package isabelle |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
8 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
9 |
|
61278 | 10 |
import java.awt.event.{WindowEvent, WindowAdapter} |
11 |
import javax.swing.{WindowConstants, JFrame, JDialog} |
|
53460 | 12 |
import java.io.{File => JFile, BufferedReader, InputStreamReader} |
53453
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
13 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
14 |
import scala.swing.{ScrollPane, Button, CheckBox, FlowPanel, |
53458 | 15 |
BorderPanel, Frame, TextArea, Component, Label} |
53453
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
16 |
import scala.swing.event.ButtonClicked |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
17 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
18 |
|
61278 | 19 |
class System_Dialog(owner: JFrame = null) extends Progress |
53453
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
20 |
{ |
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
56662
diff
changeset
|
21 |
/* component state -- owned by GUI thread */ |
53453
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
22 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
23 |
private var _title = "Isabelle" |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
24 |
private var _window: Option[Window] = None |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
25 |
private var _return_code: Option[Int] = None |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
26 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
27 |
private def check_window(): Window = |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
28 |
{ |
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
56662
diff
changeset
|
29 |
GUI_Thread.require {} |
53453
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
30 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
31 |
_window match { |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
32 |
case Some(window) => window |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
33 |
case None => |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
34 |
val window = new Window |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
35 |
_window = Some(window) |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
36 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
37 |
window.pack() |
61280 | 38 |
window.setLocationRelativeTo(owner) |
61278 | 39 |
window.setVisible(true) |
53453
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
40 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
41 |
window |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
42 |
} |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
43 |
} |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
44 |
|
53456 | 45 |
private val result = Future.promise[Int] |
46 |
||
53454 | 47 |
private def conclude() |
53453
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
48 |
{ |
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
56662
diff
changeset
|
49 |
GUI_Thread.require {} |
53453
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
50 |
require(_return_code.isDefined) |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
51 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
52 |
_window match { |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
53 |
case None => |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
54 |
case Some(window) => |
61278 | 55 |
window.setVisible(false) |
53457 | 56 |
window.dispose |
53453
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
57 |
_window = None |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
58 |
} |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
59 |
|
53456 | 60 |
try { result.fulfill(_return_code.get) } |
61 |
catch { case ERROR(_) => } |
|
53453
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
62 |
} |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
63 |
|
53456 | 64 |
def join(): Int = result.join |
53460 | 65 |
def join_exit(): Nothing = sys.exit(join) |
53456 | 66 |
|
53453
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
67 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
68 |
/* window */ |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
69 |
|
61278 | 70 |
private class Window extends JDialog(owner, _title) |
53453
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
71 |
{ |
61278 | 72 |
setIconImages(GUI.isabelle_images()) |
53453
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
73 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
74 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
75 |
/* text */ |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
76 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
77 |
val text = new TextArea { |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
78 |
editable = false |
59387 | 79 |
columns = 65 |
80 |
rows = 24 |
|
53453
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
81 |
} |
61278 | 82 |
text.font = (new Label).font |
53453
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
83 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
84 |
val scroll_text = new ScrollPane(text) |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
85 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
86 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
87 |
/* layout panel with dynamic actions */ |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
88 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
89 |
val action_panel = new FlowPanel(FlowPanel.Alignment.Center)() |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
90 |
val layout_panel = new BorderPanel |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
91 |
layout_panel.layout(scroll_text) = BorderPanel.Position.Center |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
92 |
layout_panel.layout(action_panel) = BorderPanel.Position.South |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
93 |
|
61278 | 94 |
setContentPane(layout_panel.peer) |
53453
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
95 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
96 |
def set_actions(cs: Component*) |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
97 |
{ |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
98 |
action_panel.contents.clear |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
99 |
action_panel.contents ++= cs |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
100 |
layout_panel.revalidate |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
101 |
layout_panel.repaint |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
102 |
} |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
103 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
104 |
|
53455 | 105 |
/* close */ |
106 |
||
61278 | 107 |
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE) |
53455 | 108 |
|
61278 | 109 |
addWindowListener(new WindowAdapter { |
61279 | 110 |
override def windowClosing(e: WindowEvent) { |
61278 | 111 |
if (_return_code.isDefined) conclude() |
112 |
else stopping() |
|
113 |
} |
|
114 |
}) |
|
53455 | 115 |
|
116 |
def stopping() |
|
117 |
{ |
|
118 |
is_stopped = true |
|
119 |
set_actions(new Label("Stopping ...")) |
|
120 |
} |
|
121 |
||
122 |
val stop_button = new Button("Stop") { |
|
123 |
reactions += { case ButtonClicked(_) => stopping() } |
|
124 |
} |
|
53453
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
125 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
126 |
var do_auto_close = true |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
127 |
def can_auto_close: Boolean = do_auto_close && _return_code == Some(0) |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
128 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
129 |
val auto_close = new CheckBox("Auto close") { |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
130 |
reactions += { |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
131 |
case ButtonClicked(_) => do_auto_close = this.selected |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
132 |
if (can_auto_close) conclude() |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
133 |
} |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
134 |
} |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
135 |
auto_close.selected = do_auto_close |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
136 |
auto_close.tooltip = "Automatically close dialog when finished" |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
137 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
138 |
set_actions(stop_button, auto_close) |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
139 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
140 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
141 |
/* exit */ |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
142 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
143 |
val delay_exit = |
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
56662
diff
changeset
|
144 |
GUI_Thread.delay_first(Time.seconds(1.0)) |
53453
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
145 |
{ |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
146 |
if (can_auto_close) conclude() |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
147 |
else { |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
148 |
val button = |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
149 |
new Button(if (_return_code == Some(0)) "OK" else "Exit") { |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
150 |
reactions += { case ButtonClicked(_) => conclude() } |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
151 |
} |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
152 |
set_actions(button) |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
153 |
button.peer.getRootPane.setDefaultButton(button.peer) |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
154 |
} |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
155 |
} |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
156 |
} |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
157 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
158 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
159 |
/* progress operations */ |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
160 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
161 |
def title(txt: String): Unit = |
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
56662
diff
changeset
|
162 |
GUI_Thread.later { |
53453
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
163 |
_title = txt |
61278 | 164 |
_window.foreach(window => window.setTitle(txt)) |
53453
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
165 |
} |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
166 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
167 |
def return_code(rc: Int): Unit = |
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
56662
diff
changeset
|
168 |
GUI_Thread.later { |
53453
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
169 |
_return_code = Some(rc) |
53456 | 170 |
_window match { |
171 |
case None => conclude() |
|
172 |
case Some(window) => window.delay_exit.invoke |
|
173 |
} |
|
53453
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
174 |
} |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
175 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
176 |
override def echo(txt: String): Unit = |
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
56662
diff
changeset
|
177 |
GUI_Thread.later { |
53453
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
178 |
val window = check_window() |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
179 |
window.text.append(txt + "\n") |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
180 |
val vertical = window.scroll_text.peer.getVerticalScrollBar |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
181 |
vertical.setValue(vertical.getMaximum) |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
182 |
} |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
183 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
184 |
override def theory(session: String, theory: String): Unit = |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
185 |
echo(session + ": theory " + theory) |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
186 |
|
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
187 |
@volatile private var is_stopped = false |
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
188 |
override def stopped: Boolean = is_stopped |
53460 | 189 |
|
190 |
||
191 |
/* system operations */ |
|
192 |
||
193 |
def execute(cwd: JFile, env: Map[String, String], args: String*): Int = |
|
194 |
{ |
|
195 |
val proc = Isabelle_System.raw_execute(cwd, env, true, args: _*) |
|
196 |
proc.getOutputStream.close |
|
197 |
||
198 |
val stdout = new BufferedReader(new InputStreamReader(proc.getInputStream, UTF8.charset)) |
|
199 |
try { |
|
200 |
var line = stdout.readLine |
|
201 |
while (line != null) { |
|
202 |
echo(line) |
|
203 |
line = stdout.readLine |
|
204 |
} |
|
205 |
} |
|
206 |
finally { stdout.close } |
|
207 |
||
208 |
proc.waitFor |
|
209 |
} |
|
53453
20ff79162ff3
dialog for system processes, with optional output window;
wenzelm
parents:
diff
changeset
|
210 |
} |