author | wenzelm |
Sun, 18 Dec 2022 16:01:37 +0100 | |
changeset 76680 | e95b9c9e17ff |
parent 76610 | 6e2383488a55 |
child 76681 | 8ad17c4669da |
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 { |
|
76491 | 35 |
def empty(): State = State() |
76026 | 36 |
def finish(result: Result): State = State(output = result.output) |
76023 | 37 |
} |
38 |
||
39 |
sealed case class State( |
|
76606 | 40 |
progress: Progress = new Progress, |
76023 | 41 |
process: Future[Unit] = Future.value(()), |
42 |
output: List[XML.Tree] = Nil, |
|
76491 | 43 |
status: Status.Value = Status.FINISHED) |
76023 | 44 |
} |
45 |
||
75816 | 46 |
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
|
47 |
dockable => |
cc9ddf373bd2
maintain global state of document editor views, notably for is_active operation;
wenzelm
parents:
76606
diff
changeset
|
48 |
|
75816 | 49 |
GUI_Thread.require {} |
50 |
||
51 |
||
76023 | 52 |
/* component state -- owned by GUI thread */ |
53 |
||
76491 | 54 |
private val current_state = Synchronized(Document_Dockable.State.empty()) |
76023 | 55 |
|
56 |
private val process_indicator = new Process_Indicator |
|
57 |
private val pretty_text_area = new Pretty_Text_Area(view) |
|
58 |
private val message_pane = new TabbedPane |
|
59 |
||
76034 | 60 |
private def show_state(): Unit = GUI_Thread.later { |
76023 | 61 |
val st = current_state.value |
62 |
||
63 |
pretty_text_area.update(Document.Snapshot.init, Command.Results.empty, st.output) |
|
75816 | 64 |
|
76023 | 65 |
st.status match { |
66 |
case Document_Dockable.Status.WAITING => |
|
67 |
process_indicator.update("Waiting for PIDE document content ...", 5) |
|
68 |
case Document_Dockable.Status.RUNNING => |
|
69 |
process_indicator.update("Running document build process ...", 15) |
|
70 |
case Document_Dockable.Status.FINISHED => |
|
71 |
process_indicator.update(null, 0) |
|
72 |
} |
|
76034 | 73 |
} |
76023 | 74 |
|
76034 | 75 |
private def show_page(page: TabbedPane.Page): Unit = GUI_Thread.later { |
76 |
message_pane.selection.page = page |
|
76023 | 77 |
} |
78 |
||
79 |
||
80 |
/* text area with zoom/resize */ |
|
75816 | 81 |
|
82 |
override def detach_operation: Option[() => Unit] = pretty_text_area.detach_operation |
|
83 |
||
76021
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
84 |
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
|
85 |
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
|
86 |
|
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
87 |
private val delay_resize: Delay = |
76610 | 88 |
Delay.first(PIDE.session.update_delay, gui = true) { handle_resize() } |
76021
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
89 |
|
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
90 |
addComponentListener(new ComponentAdapter { |
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
91 |
override def componentResized(e: ComponentEvent): Unit = delay_resize.invoke() |
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
92 |
override def componentShown(e: ComponentEvent): Unit = delay_resize.invoke() |
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
93 |
}) |
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
94 |
|
76023 | 95 |
|
96 |
/* progress log */ |
|
97 |
||
76489
8c9830109ab2
clarified Log_Progress vs. GUI: more like Syslog_Dockable;
wenzelm
parents:
76036
diff
changeset
|
98 |
private val log_area = |
8c9830109ab2
clarified Log_Progress vs. GUI: more like Syslog_Dockable;
wenzelm
parents:
76036
diff
changeset
|
99 |
new TextArea { |
8c9830109ab2
clarified Log_Progress vs. GUI: more like Syslog_Dockable;
wenzelm
parents:
76036
diff
changeset
|
100 |
editable = false |
8c9830109ab2
clarified Log_Progress vs. GUI: more like Syslog_Dockable;
wenzelm
parents:
76036
diff
changeset
|
101 |
font = GUI.copy_font((new Label).font) |
8c9830109ab2
clarified Log_Progress vs. GUI: more like Syslog_Dockable;
wenzelm
parents:
76036
diff
changeset
|
102 |
} |
76023 | 103 |
private val scroll_log_area = new ScrollPane(log_area) |
104 |
||
76606 | 105 |
def log_progress(): Document_Editor.Log_Progress = |
106 |
new Document_Editor.Log_Progress(PIDE.session) { |
|
76491 | 107 |
override def show(text: String): Unit = |
108 |
if (text != log_area.text) { |
|
109 |
log_area.text = text |
|
110 |
val vertical = scroll_log_area.peer.getVerticalScrollBar |
|
111 |
vertical.setValue(vertical.getMaximum) |
|
112 |
} |
|
76023 | 113 |
} |
76021
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
114 |
|
75816 | 115 |
|
116 |
/* document build process */ |
|
117 |
||
76023 | 118 |
private def cancel(): Unit = |
119 |
current_state.change { st => st.process.cancel(); st } |
|
120 |
||
76491 | 121 |
private def init_state(): Unit = |
76606 | 122 |
current_state.change { _ => Document_Dockable.State(progress = log_progress()) } |
76491 | 123 |
|
76023 | 124 |
private def build_document(): Unit = { |
125 |
current_state.change { st => |
|
126 |
if (st.process.is_finished) { |
|
76606 | 127 |
val progress = log_progress() |
76023 | 128 |
val process = |
129 |
Future.thread[Unit](name = "document_build") { |
|
76567 | 130 |
show_page(theories_page) |
131 |
Time.seconds(2.0).sleep() |
|
132 |
||
76034 | 133 |
show_page(log_page) |
76023 | 134 |
val res = |
135 |
Exn.capture { |
|
136 |
progress.echo("Start " + Date.now()) |
|
76489
8c9830109ab2
clarified Log_Progress vs. GUI: more like Syslog_Dockable;
wenzelm
parents:
76036
diff
changeset
|
137 |
for (i <- 1 to 200) { |
8c9830109ab2
clarified Log_Progress vs. GUI: more like Syslog_Dockable;
wenzelm
parents:
76036
diff
changeset
|
138 |
progress.echo("message " + i) |
8c9830109ab2
clarified Log_Progress vs. GUI: more like Syslog_Dockable;
wenzelm
parents:
76036
diff
changeset
|
139 |
Time.seconds(0.1).sleep() |
8c9830109ab2
clarified Log_Progress vs. GUI: more like Syslog_Dockable;
wenzelm
parents:
76036
diff
changeset
|
140 |
} |
76023 | 141 |
progress.echo("Stop " + Date.now()) |
142 |
} |
|
76026 | 143 |
val msg = |
76023 | 144 |
res match { |
76680 | 145 |
case Exn.Res(_) => Protocol.writeln_message("OK") |
146 |
case Exn.Exn(exn) => Protocol.error_message(Exn.message(exn)) |
|
76023 | 147 |
} |
76026 | 148 |
val result = Document_Dockable.Result(output = List(msg)) |
76035 | 149 |
current_state.change(_ => Document_Dockable.State.finish(result)) |
150 |
show_state() |
|
76568 | 151 |
|
152 |
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
|
153 |
GUI_Thread.later { progress.load() } |
76023 | 154 |
} |
155 |
st.copy(progress = progress, process = process, status = Document_Dockable.Status.RUNNING) |
|
156 |
} |
|
157 |
else st |
|
158 |
} |
|
159 |
show_state() |
|
160 |
} |
|
161 |
||
75816 | 162 |
|
163 |
/* controls */ |
|
164 |
||
76602 | 165 |
private lazy val delay_load: Delay = |
76610 | 166 |
Delay.last(PIDE.session.load_delay, gui = true) { |
76602 | 167 |
val models = Document_Model.get_models() |
168 |
val thy_files = PIDE.resources.resolve_dependencies(models, Nil) |
|
169 |
} |
|
170 |
||
76578
06b001094ddb
more uniform session selectors, with persistent options;
wenzelm
parents:
76577
diff
changeset
|
171 |
private val document_session = |
76580 | 172 |
JEdit_Sessions.document_selector(PIDE.options, standalone = true) |
75830 | 173 |
|
76602 | 174 |
document_session.reactions += { case SelectionChanged(_) => delay_load.invoke() } |
175 |
||
75853 | 176 |
private val build_button = |
177 |
new GUI.Button("<html><b>Build</b></html>") { |
|
75816 | 178 |
tooltip = "Build document" |
76023 | 179 |
override def clicked(): Unit = build_document() |
180 |
} |
|
181 |
||
182 |
private val cancel_button = |
|
183 |
new GUI.Button("Cancel") { |
|
184 |
tooltip = "Cancel build process" |
|
185 |
override def clicked(): Unit = cancel() |
|
186 |
} |
|
187 |
||
188 |
private val view_button = |
|
189 |
new GUI.Button("View") { |
|
190 |
tooltip = "View document" |
|
76606 | 191 |
override def clicked(): Unit = Document_Editor.view_document() |
75816 | 192 |
} |
193 |
||
194 |
private val controls = |
|
75830 | 195 |
Wrap_Panel(List(document_session, process_indicator.component, build_button, |
76023 | 196 |
view_button, cancel_button)) |
75816 | 197 |
|
198 |
add(controls.peer, BorderLayout.NORTH) |
|
199 |
||
200 |
override def focusOnDefaultComponent(): Unit = build_button.requestFocus() |
|
201 |
||
202 |
||
76023 | 203 |
/* message pane with pages */ |
204 |
||
76602 | 205 |
private val theories = new Theories_Status(view, document = true) |
76567 | 206 |
|
207 |
private val theories_page = |
|
208 |
new TabbedPane.Page("Theories", new BorderPanel { |
|
209 |
layout(theories.gui) = BorderPanel.Position.Center |
|
210 |
}, "Selection and status of document theories") |
|
211 |
||
76023 | 212 |
private val output_controls = |
213 |
Wrap_Panel(List(pretty_text_area.search_label, pretty_text_area.search_field, zoom)) |
|
214 |
||
215 |
private val output_page = |
|
216 |
new TabbedPane.Page("Output", new BorderPanel { |
|
217 |
layout(output_controls) = BorderPanel.Position.North |
|
218 |
layout(Component.wrap(pretty_text_area)) = BorderPanel.Position.Center |
|
219 |
}, "Output from build process") |
|
220 |
||
221 |
private val log_page = |
|
222 |
new TabbedPane.Page("Log", new BorderPanel { |
|
76489
8c9830109ab2
clarified Log_Progress vs. GUI: more like Syslog_Dockable;
wenzelm
parents:
76036
diff
changeset
|
223 |
layout(scroll_log_area) = BorderPanel.Position.Center |
76023 | 224 |
}, "Raw log of build process") |
225 |
||
76567 | 226 |
message_pane.pages ++= List(theories_page, log_page, output_page) |
76023 | 227 |
|
228 |
set_content(message_pane) |
|
229 |
||
230 |
||
75816 | 231 |
/* main */ |
232 |
||
233 |
private val main = |
|
76567 | 234 |
Session.Consumer[Any](getClass.getName) { |
75816 | 235 |
case _: Session.Global_Options => |
76567 | 236 |
GUI_Thread.later { |
76578
06b001094ddb
more uniform session selectors, with persistent options;
wenzelm
parents:
76577
diff
changeset
|
237 |
document_session.load() |
76567 | 238 |
handle_resize() |
239 |
theories.reinit() |
|
240 |
} |
|
241 |
case changed: Session.Commands_Changed => |
|
242 |
GUI_Thread.later { |
|
243 |
theories.update(domain = Some(changed.nodes), trim = changed.assignment) |
|
244 |
} |
|
75816 | 245 |
} |
246 |
||
247 |
override def init(): Unit = { |
|
76609
cc9ddf373bd2
maintain global state of document editor views, notably for is_active operation;
wenzelm
parents:
76606
diff
changeset
|
248 |
PIDE.editor.document_editor_init(dockable) |
76491 | 249 |
init_state() |
75816 | 250 |
PIDE.session.global_options += main |
76567 | 251 |
PIDE.session.commands_changed += main |
252 |
theories.update() |
|
75816 | 253 |
handle_resize() |
76602 | 254 |
delay_load.invoke() |
75816 | 255 |
} |
256 |
||
257 |
override def exit(): Unit = { |
|
258 |
PIDE.session.global_options -= main |
|
76567 | 259 |
PIDE.session.commands_changed -= main |
75816 | 260 |
delay_resize.revoke() |
76609
cc9ddf373bd2
maintain global state of document editor views, notably for is_active operation;
wenzelm
parents:
76606
diff
changeset
|
261 |
PIDE.editor.document_editor_exit(dockable) |
75816 | 262 |
} |
263 |
} |