author | wenzelm |
Thu, 01 Sep 2022 10:54:12 +0200 | |
changeset 76035 | 97060c904a08 |
parent 76034 | dda3c117f13c |
child 76036 | 181bf8567f41 |
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} |
16 |
||
75816 | 17 |
import org.gjt.sp.jedit.{jEdit, View} |
18 |
||
19 |
||
76023 | 20 |
object Document_Dockable { |
21 |
def document_output(): Path = |
|
22 |
Path.explode("$ISABELLE_HOME_USER/document/root.pdf") |
|
23 |
||
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 { |
|
35 |
val empty: State = State() |
|
76026 | 36 |
def finish(result: Result): State = State(output = result.output) |
76023 | 37 |
} |
38 |
||
39 |
sealed case class State( |
|
40 |
progress: Progress = new Progress, |
|
41 |
process: Future[Unit] = Future.value(()), |
|
42 |
output: List[XML.Tree] = Nil, |
|
43 |
status: Status.Value = Status.FINISHED |
|
44 |
) |
|
45 |
} |
|
46 |
||
75816 | 47 |
class Document_Dockable(view: View, position: String) extends Dockable(view, position) { |
48 |
GUI_Thread.require {} |
|
49 |
||
50 |
||
76023 | 51 |
/* component state -- owned by GUI thread */ |
52 |
||
53 |
private val current_state = Synchronized(Document_Dockable.State.empty) |
|
54 |
||
55 |
private val process_indicator = new Process_Indicator |
|
56 |
private val pretty_text_area = new Pretty_Text_Area(view) |
|
57 |
private val message_pane = new TabbedPane |
|
58 |
||
76034 | 59 |
private def show_state(): Unit = GUI_Thread.later { |
76023 | 60 |
val st = current_state.value |
61 |
||
62 |
pretty_text_area.update(Document.Snapshot.init, Command.Results.empty, st.output) |
|
75816 | 63 |
|
76023 | 64 |
st.status match { |
65 |
case Document_Dockable.Status.WAITING => |
|
66 |
process_indicator.update("Waiting for PIDE document content ...", 5) |
|
67 |
case Document_Dockable.Status.RUNNING => |
|
68 |
process_indicator.update("Running document build process ...", 15) |
|
69 |
case Document_Dockable.Status.FINISHED => |
|
70 |
process_indicator.update(null, 0) |
|
71 |
} |
|
76034 | 72 |
} |
76023 | 73 |
|
76034 | 74 |
private def show_page(page: TabbedPane.Page): Unit = GUI_Thread.later { |
75 |
message_pane.selection.page = page |
|
76023 | 76 |
} |
77 |
||
78 |
||
79 |
/* text area with zoom/resize */ |
|
75816 | 80 |
|
81 |
override def detach_operation: Option[() => Unit] = pretty_text_area.detach_operation |
|
82 |
||
76021
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
83 |
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
|
84 |
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
|
85 |
|
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
86 |
private val delay_resize: Delay = |
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
87 |
Delay.first(PIDE.options.seconds("editor_update_delay"), gui = true) { handle_resize() } |
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
88 |
|
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
89 |
addComponentListener(new ComponentAdapter { |
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
90 |
override def componentResized(e: ComponentEvent): Unit = delay_resize.invoke() |
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
91 |
override def componentShown(e: ComponentEvent): Unit = delay_resize.invoke() |
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 |
|
76023 | 94 |
|
95 |
/* progress log */ |
|
96 |
||
97 |
private val log_area = new TextArea { |
|
98 |
editable = false |
|
99 |
columns = 60 |
|
100 |
rows = 24 |
|
101 |
} |
|
102 |
log_area.font = GUI.copy_font((new Label).font) |
|
103 |
||
104 |
private val scroll_log_area = new ScrollPane(log_area) |
|
105 |
||
106 |
private def init_progress() = { |
|
107 |
GUI_Thread.later { log_area.text = "" } |
|
108 |
new Progress { |
|
109 |
override def echo(txt: String): Unit = |
|
110 |
GUI_Thread.later { |
|
111 |
log_area.append(txt + "\n") |
|
112 |
val vertical = scroll_log_area.peer.getVerticalScrollBar |
|
113 |
vertical.setValue(vertical.getMaximum) |
|
114 |
} |
|
115 |
||
116 |
override def theory(theory: Progress.Theory): Unit = echo(theory.message) |
|
117 |
} |
|
118 |
} |
|
76021
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
119 |
|
75816 | 120 |
|
121 |
/* document build process */ |
|
122 |
||
76023 | 123 |
private def cancel(): Unit = |
124 |
current_state.change { st => st.process.cancel(); st } |
|
125 |
||
126 |
private def build_document(): Unit = { |
|
127 |
current_state.change { st => |
|
128 |
if (st.process.is_finished) { |
|
129 |
val progress = init_progress() |
|
130 |
val process = |
|
131 |
Future.thread[Unit](name = "document_build") { |
|
76034 | 132 |
show_page(log_page) |
76023 | 133 |
val res = |
134 |
Exn.capture { |
|
135 |
progress.echo("Start " + Date.now()) |
|
136 |
Time.seconds(2.0).sleep() |
|
137 |
progress.echo("Stop " + Date.now()) |
|
138 |
} |
|
76026 | 139 |
val msg = |
76023 | 140 |
res match { |
76026 | 141 |
case Exn.Res(_) => Protocol.make_message(XML.string("OK")) |
142 |
case Exn.Exn(exn) => Protocol.error_message(XML.string(Exn.message(exn))) |
|
76023 | 143 |
} |
76026 | 144 |
val result = Document_Dockable.Result(output = List(msg)) |
76035 | 145 |
current_state.change(_ => Document_Dockable.State.finish(result)) |
146 |
show_state() |
|
147 |
show_page(output_page) |
|
76023 | 148 |
} |
149 |
st.copy(progress = progress, process = process, status = Document_Dockable.Status.RUNNING) |
|
150 |
} |
|
151 |
else st |
|
152 |
} |
|
153 |
show_state() |
|
154 |
} |
|
155 |
||
156 |
private def view_document(): Unit = { |
|
157 |
val path = Document_Dockable.document_output() |
|
158 |
if (path.is_file) Isabelle_System.pdf_viewer(path) |
|
159 |
} |
|
75816 | 160 |
|
161 |
||
162 |
/* controls */ |
|
163 |
||
76021
752425c69577
clarified component structure, concerning initialization order;
wenzelm
parents:
75853
diff
changeset
|
164 |
private val document_session: GUI.Selector[String] = |
75839 | 165 |
new GUI.Selector(JEdit_Sessions.sessions_structure().build_topological_order.sorted) { |
75830 | 166 |
val title = "Session" |
167 |
} |
|
168 |
||
75853 | 169 |
private val build_button = |
170 |
new GUI.Button("<html><b>Build</b></html>") { |
|
75816 | 171 |
tooltip = "Build document" |
76023 | 172 |
override def clicked(): Unit = build_document() |
173 |
} |
|
174 |
||
175 |
private val cancel_button = |
|
176 |
new GUI.Button("Cancel") { |
|
177 |
tooltip = "Cancel build process" |
|
178 |
override def clicked(): Unit = cancel() |
|
179 |
} |
|
180 |
||
181 |
private val view_button = |
|
182 |
new GUI.Button("View") { |
|
183 |
tooltip = "View document" |
|
184 |
override def clicked(): Unit = view_document() |
|
75816 | 185 |
} |
186 |
||
187 |
private val controls = |
|
75830 | 188 |
Wrap_Panel(List(document_session, process_indicator.component, build_button, |
76023 | 189 |
view_button, cancel_button)) |
75816 | 190 |
|
191 |
add(controls.peer, BorderLayout.NORTH) |
|
192 |
||
193 |
override def focusOnDefaultComponent(): Unit = build_button.requestFocus() |
|
194 |
||
195 |
||
76023 | 196 |
/* message pane with pages */ |
197 |
||
198 |
private val output_controls = |
|
199 |
Wrap_Panel(List(pretty_text_area.search_label, pretty_text_area.search_field, zoom)) |
|
200 |
||
201 |
private val output_page = |
|
202 |
new TabbedPane.Page("Output", new BorderPanel { |
|
203 |
layout(output_controls) = BorderPanel.Position.North |
|
204 |
layout(Component.wrap(pretty_text_area)) = BorderPanel.Position.Center |
|
205 |
}, "Output from build process") |
|
206 |
||
207 |
private val log_page = |
|
208 |
new TabbedPane.Page("Log", new BorderPanel { |
|
209 |
layout(log_area) = BorderPanel.Position.Center |
|
210 |
}, "Raw log of build process") |
|
211 |
||
212 |
message_pane.pages ++= List(output_page, log_page) |
|
213 |
||
214 |
set_content(message_pane) |
|
215 |
||
216 |
||
75816 | 217 |
/* main */ |
218 |
||
219 |
private val main = |
|
220 |
Session.Consumer[Session.Global_Options](getClass.getName) { |
|
221 |
case _: Session.Global_Options => |
|
222 |
GUI_Thread.later { handle_resize() } |
|
223 |
} |
|
224 |
||
225 |
override def init(): Unit = { |
|
226 |
PIDE.session.global_options += main |
|
227 |
handle_resize() |
|
228 |
} |
|
229 |
||
230 |
override def exit(): Unit = { |
|
231 |
PIDE.session.global_options -= main |
|
232 |
delay_resize.revoke() |
|
233 |
} |
|
234 |
} |