author | wenzelm |
Mon, 19 Dec 2022 11:16:46 +0100 | |
changeset 76701 | 3543ecb4c97d |
parent 76681 | 8ad17c4669da |
child 76707 | 2e231c07b428 |
permissions | -rw-r--r-- |
75816 | 1 |
/* Title: Tools/jEdit/src/document_dockable.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Dockable window for document build support. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle.jedit |
|
8 |
||
9 |
||
10 |
import isabelle._ |
|
11 |
||
12 |
import java.awt.BorderLayout |
|
13 |
import java.awt.event.{ComponentEvent, ComponentAdapter} |
|
14 |
||
76023 | 15 |
import scala.swing.{ScrollPane, TextArea, Label, TabbedPane, BorderPanel, Component} |
76602 | 16 |
import scala.swing.event.SelectionChanged |
76023 | 17 |
|
75816 | 18 |
import org.gjt.sp.jedit.{jEdit, View} |
19 |
||
20 |
||
76023 | 21 |
object Document_Dockable { |
76491 | 22 |
/* state */ |
23 |
||
76023 | 24 |
object Status extends Enumeration { |
25 |
val WAITING = Value("waiting") |
|
26 |
val RUNNING = Value("running") |
|
27 |
val FINISHED = Value("finished") |
|
28 |
} |
|
29 |
||
76026 | 30 |
sealed case class Result(output: List[XML.Tree] = Nil) { |
31 |
def failed: Boolean = output.exists(Protocol.is_error) |
|
32 |
} |
|
76023 | 33 |
|
34 |
object State { |
|
76681 | 35 |
def init(): State = State() |
76023 | 36 |
} |
37 |
||
38 |
sealed case class State( |
|
76606 | 39 |
progress: Progress = new Progress, |
76023 | 40 |
process: Future[Unit] = Future.value(()), |
76681 | 41 |
status: Status.Value = Status.FINISHED, |
42 |
output: List[XML.Tree] = Nil |
|
43 |
) { |
|
44 |
def run(progress: Progress, process: Future[Unit]): State = |
|
45 |
copy(progress = progress, process = process, status = Status.RUNNING) |
|
46 |
||
47 |
def finish(result: Result): State = State(output = result.output) |
|
48 |
def finish(msg: XML.Tree): State = finish(Result(output = List(msg))) |
|
49 |
} |
|
76023 | 50 |
} |
51 |
||
75816 | 52 |
class Document_Dockable(view: View, position: String) extends Dockable(view, position) { |
76609
cc9ddf373bd2
maintain global state of document editor views, notably for is_active operation;
wenzelm
parents:
76606
diff
changeset
|
53 |
dockable => |
cc9ddf373bd2
maintain global state of document editor views, notably for is_active operation;
wenzelm
parents:
76606
diff
changeset
|
54 |
|
75816 | 55 |
GUI_Thread.require {} |
56 |
||
57 |
||
76023 | 58 |
/* component state -- owned by GUI thread */ |
59 |
||
76681 | 60 |
private val current_state = Synchronized(Document_Dockable.State.init()) |
76023 | 61 |
|
62 |
private val process_indicator = new Process_Indicator |
|
63 |
private val pretty_text_area = new Pretty_Text_Area(view) |
|
64 |
private val message_pane = new TabbedPane |
|
65 |
||
76034 | 66 |
private def show_state(): Unit = GUI_Thread.later { |
76023 | 67 |
val st = current_state.value |
68 |
||
69 |
pretty_text_area.update(Document.Snapshot.init, Command.Results.empty, st.output) |
|
75816 | 70 |
|
76023 | 71 |
st.status match { |
72 |
case Document_Dockable.Status.WAITING => |
|
73 |
process_indicator.update("Waiting for PIDE document content ...", 5) |
|
74 |
case Document_Dockable.Status.RUNNING => |
|
75 |
process_indicator.update("Running document build process ...", 15) |
|
76 |
case Document_Dockable.Status.FINISHED => |
|
77 |
process_indicator.update(null, 0) |
|
78 |
} |
|
76034 | 79 |
} |
76023 | 80 |
|
76034 | 81 |
private def show_page(page: TabbedPane.Page): Unit = GUI_Thread.later { |
82 |
message_pane.selection.page = page |
|
76023 | 83 |
} |
84 |
||
85 |
||
86 |
/* text area with zoom/resize */ |
|
75816 | 87 |
|
88 |
override def detach_operation: Option[() => Unit] = pretty_text_area.detach_operation |
|
89 |
||
76021
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
90 |
private val zoom = new Font_Info.Zoom { override def changed(): Unit = handle_resize() } |
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
91 |
private def handle_resize(): Unit = GUI_Thread.require { pretty_text_area.zoom(zoom) } |
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
92 |
|
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
93 |
private val delay_resize: Delay = |
76610 | 94 |
Delay.first(PIDE.session.update_delay, gui = true) { handle_resize() } |
76021
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
95 |
|
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
96 |
addComponentListener(new ComponentAdapter { |
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
97 |
override def componentResized(e: ComponentEvent): Unit = delay_resize.invoke() |
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
98 |
override def componentShown(e: ComponentEvent): Unit = delay_resize.invoke() |
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
99 |
}) |
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
100 |
|
76023 | 101 |
|
102 |
/* progress log */ |
|
103 |
||
76489
8c9830109ab2
clarified Log_Progress vs. GUI: more like Syslog_Dockable;
wenzelm
parents:
76036
diff
changeset
|
104 |
private val log_area = |
8c9830109ab2
clarified Log_Progress vs. GUI: more like Syslog_Dockable;
wenzelm
parents:
76036
diff
changeset
|
105 |
new TextArea { |
8c9830109ab2
clarified Log_Progress vs. GUI: more like Syslog_Dockable;
wenzelm
parents:
76036
diff
changeset
|
106 |
editable = false |
8c9830109ab2
clarified Log_Progress vs. GUI: more like Syslog_Dockable;
wenzelm
parents:
76036
diff
changeset
|
107 |
font = GUI.copy_font((new Label).font) |
8c9830109ab2
clarified Log_Progress vs. GUI: more like Syslog_Dockable;
wenzelm
parents:
76036
diff
changeset
|
108 |
} |
76023 | 109 |
private val scroll_log_area = new ScrollPane(log_area) |
110 |
||
76606 | 111 |
def log_progress(): Document_Editor.Log_Progress = |
112 |
new Document_Editor.Log_Progress(PIDE.session) { |
|
76491 | 113 |
override def show(text: String): Unit = |
114 |
if (text != log_area.text) { |
|
115 |
log_area.text = text |
|
116 |
val vertical = scroll_log_area.peer.getVerticalScrollBar |
|
117 |
vertical.setValue(vertical.getMaximum) |
|
118 |
} |
|
76023 | 119 |
} |
76021
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
120 |
|
75816 | 121 |
|
122 |
/* document build process */ |
|
123 |
||
76023 | 124 |
private def cancel(): Unit = |
125 |
current_state.change { st => st.process.cancel(); st } |
|
126 |
||
76491 | 127 |
private def init_state(): Unit = |
76606 | 128 |
current_state.change { _ => Document_Dockable.State(progress = log_progress()) } |
76491 | 129 |
|
76681 | 130 |
private def load_document(session: String): Boolean = { |
131 |
current_state.change_result { st => |
|
132 |
if (st.process.is_finished) { |
|
133 |
val options = PIDE.options.value |
|
134 |
val progress = log_progress() |
|
135 |
val process = |
|
136 |
Future.thread[Unit](name = "Document_Dockable.load_document") { |
|
137 |
try { |
|
138 |
val session_background = |
|
139 |
Document_Build.session_background( |
|
140 |
options, session, dirs = JEdit_Sessions.session_dirs) |
|
76701 | 141 |
PIDE.editor.document_setup(Some(session_background)) |
76681 | 142 |
GUI_Thread.later { show_state(); show_page(theories_page) } |
143 |
} |
|
144 |
catch { |
|
145 |
case exn: Throwable if !Exn.is_interrupt(exn) => |
|
146 |
current_state.change(_.finish(Protocol.error_message(Exn.message(exn)))) |
|
147 |
GUI_Thread.later { show_state(); show_page(output_page) } |
|
148 |
} |
|
149 |
} |
|
150 |
(true, st.run(progress, process)) |
|
151 |
} |
|
152 |
else (false, st) |
|
153 |
} |
|
154 |
} |
|
155 |
||
76023 | 156 |
private def build_document(): Unit = { |
157 |
current_state.change { st => |
|
158 |
if (st.process.is_finished) { |
|
76606 | 159 |
val progress = log_progress() |
76023 | 160 |
val process = |
76681 | 161 |
Future.thread[Unit](name = "Document_Dockable.build_document") { |
76567 | 162 |
show_page(theories_page) |
163 |
Time.seconds(2.0).sleep() |
|
164 |
||
76034 | 165 |
show_page(log_page) |
76023 | 166 |
val res = |
167 |
Exn.capture { |
|
168 |
progress.echo("Start " + Date.now()) |
|
76489
8c9830109ab2
clarified Log_Progress vs. GUI: more like Syslog_Dockable;
wenzelm
parents:
76036
diff
changeset
|
169 |
for (i <- 1 to 200) { |
8c9830109ab2
clarified Log_Progress vs. GUI: more like Syslog_Dockable;
wenzelm
parents:
76036
diff
changeset
|
170 |
progress.echo("message " + i) |
8c9830109ab2
clarified Log_Progress vs. GUI: more like Syslog_Dockable;
wenzelm
parents:
76036
diff
changeset
|
171 |
Time.seconds(0.1).sleep() |
8c9830109ab2
clarified Log_Progress vs. GUI: more like Syslog_Dockable;
wenzelm
parents:
76036
diff
changeset
|
172 |
} |
76023 | 173 |
progress.echo("Stop " + Date.now()) |
174 |
} |
|
76026 | 175 |
val msg = |
76023 | 176 |
res match { |
76680 | 177 |
case Exn.Res(_) => Protocol.writeln_message("OK") |
178 |
case Exn.Exn(exn) => Protocol.error_message(Exn.message(exn)) |
|
76023 | 179 |
} |
76681 | 180 |
current_state.change(_.finish(msg)) |
76035 | 181 |
show_state() |
76568 | 182 |
|
183 |
show_page(if (Exn.is_interrupt_exn(res)) theories_page else output_page) |
|
76565
6827dd0c3723
clarified process: implicit load() when finished;
wenzelm
parents:
76564
diff
changeset
|
184 |
GUI_Thread.later { progress.load() } |
76023 | 185 |
} |
76681 | 186 |
st.run(progress, process) |
76023 | 187 |
} |
188 |
else st |
|
189 |
} |
|
190 |
show_state() |
|
191 |
} |
|
192 |
||
75816 | 193 |
|
194 |
/* controls */ |
|
195 |
||
76681 | 196 |
private val document_session = |
197 |
JEdit_Sessions.document_selector(PIDE.options, standalone = true) |
|
198 |
||
76602 | 199 |
private lazy val delay_load: Delay = |
76610 | 200 |
Delay.last(PIDE.session.load_delay, gui = true) { |
76681 | 201 |
for (session <- document_session.selection_value) { |
202 |
if (!load_document(session)) delay_load.invoke() |
|
203 |
} |
|
76602 | 204 |
} |
205 |
||
206 |
document_session.reactions += { case SelectionChanged(_) => delay_load.invoke() } |
|
207 |
||
75853 | 208 |
private val build_button = |
209 |
new GUI.Button("<html><b>Build</b></html>") { |
|
75816 | 210 |
tooltip = "Build document" |
76023 | 211 |
override def clicked(): Unit = build_document() |
212 |
} |
|
213 |
||
214 |
private val cancel_button = |
|
215 |
new GUI.Button("Cancel") { |
|
216 |
tooltip = "Cancel build process" |
|
217 |
override def clicked(): Unit = cancel() |
|
218 |
} |
|
219 |
||
220 |
private val view_button = |
|
221 |
new GUI.Button("View") { |
|
222 |
tooltip = "View document" |
|
76606 | 223 |
override def clicked(): Unit = Document_Editor.view_document() |
75816 | 224 |
} |
225 |
||
226 |
private val controls = |
|
75830 | 227 |
Wrap_Panel(List(document_session, process_indicator.component, build_button, |
76023 | 228 |
view_button, cancel_button)) |
75816 | 229 |
|
230 |
add(controls.peer, BorderLayout.NORTH) |
|
231 |
||
232 |
override def focusOnDefaultComponent(): Unit = build_button.requestFocus() |
|
233 |
||
234 |
||
76023 | 235 |
/* message pane with pages */ |
236 |
||
76602 | 237 |
private val theories = new Theories_Status(view, document = true) |
76567 | 238 |
|
239 |
private val theories_page = |
|
240 |
new TabbedPane.Page("Theories", new BorderPanel { |
|
241 |
layout(theories.gui) = BorderPanel.Position.Center |
|
242 |
}, "Selection and status of document theories") |
|
243 |
||
76023 | 244 |
private val output_controls = |
245 |
Wrap_Panel(List(pretty_text_area.search_label, pretty_text_area.search_field, zoom)) |
|
246 |
||
247 |
private val output_page = |
|
248 |
new TabbedPane.Page("Output", new BorderPanel { |
|
249 |
layout(output_controls) = BorderPanel.Position.North |
|
250 |
layout(Component.wrap(pretty_text_area)) = BorderPanel.Position.Center |
|
251 |
}, "Output from build process") |
|
252 |
||
253 |
private val log_page = |
|
254 |
new TabbedPane.Page("Log", new BorderPanel { |
|
76489
8c9830109ab2
clarified Log_Progress vs. GUI: more like Syslog_Dockable;
wenzelm
parents:
76036
diff
changeset
|
255 |
layout(scroll_log_area) = BorderPanel.Position.Center |
76023 | 256 |
}, "Raw log of build process") |
257 |
||
76567 | 258 |
message_pane.pages ++= List(theories_page, log_page, output_page) |
76023 | 259 |
|
260 |
set_content(message_pane) |
|
261 |
||
262 |
||
75816 | 263 |
/* main */ |
264 |
||
265 |
private val main = |
|
76567 | 266 |
Session.Consumer[Any](getClass.getName) { |
75816 | 267 |
case _: Session.Global_Options => |
76567 | 268 |
GUI_Thread.later { |
76578
06b001094ddb
more uniform session selectors, with persistent options;
wenzelm
parents:
76577
diff
changeset
|
269 |
document_session.load() |
76567 | 270 |
handle_resize() |
271 |
theories.reinit() |
|
272 |
} |
|
273 |
case changed: Session.Commands_Changed => |
|
274 |
GUI_Thread.later { |
|
275 |
theories.update(domain = Some(changed.nodes), trim = changed.assignment) |
|
276 |
} |
|
75816 | 277 |
} |
278 |
||
279 |
override def init(): Unit = { |
|
76701 | 280 |
PIDE.editor.document_init(dockable) |
76491 | 281 |
init_state() |
75816 | 282 |
PIDE.session.global_options += main |
76567 | 283 |
PIDE.session.commands_changed += main |
284 |
theories.update() |
|
75816 | 285 |
handle_resize() |
76602 | 286 |
delay_load.invoke() |
75816 | 287 |
} |
288 |
||
289 |
override def exit(): Unit = { |
|
290 |
PIDE.session.global_options -= main |
|
76567 | 291 |
PIDE.session.commands_changed -= main |
75816 | 292 |
delay_resize.revoke() |
76701 | 293 |
PIDE.editor.document_exit(dockable) |
75816 | 294 |
} |
295 |
} |