51533
|
1 |
/* Title: Tools/jEdit/src/timing_dockable.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
Dockable window for timing information.
|
|
5 |
*/
|
|
6 |
|
|
7 |
package isabelle.jedit
|
|
8 |
|
|
9 |
|
|
10 |
import isabelle._
|
|
11 |
|
|
12 |
import scala.actors.Actor._
|
|
13 |
import scala.swing.{FlowPanel, Label, ListView, Alignment, ScrollPane, Component, TextField}
|
|
14 |
import scala.swing.event.{EditDone, MouseClicked}
|
|
15 |
|
|
16 |
import java.lang.System
|
51536
|
17 |
import java.awt.{BorderLayout, Graphics2D, Insets, Color}
|
51533
|
18 |
import javax.swing.{JList, BorderFactory}
|
|
19 |
import javax.swing.border.{BevelBorder, SoftBevelBorder}
|
|
20 |
|
|
21 |
import org.gjt.sp.jedit.{View, jEdit}
|
|
22 |
|
|
23 |
|
|
24 |
class Timing_Dockable(view: View, position: String) extends Dockable(view, position)
|
|
25 |
{
|
|
26 |
/* entry */
|
|
27 |
|
|
28 |
private object Entry
|
|
29 |
{
|
|
30 |
object Ordering extends scala.math.Ordering[Entry]
|
|
31 |
{
|
|
32 |
def compare(entry1: Entry, entry2: Entry): Int =
|
|
33 |
entry2.timing compare entry1.timing
|
|
34 |
}
|
|
35 |
|
|
36 |
object Renderer_Component extends Label
|
|
37 |
{
|
|
38 |
opaque = false
|
|
39 |
xAlignment = Alignment.Leading
|
|
40 |
border = BorderFactory.createEmptyBorder(2, 2, 2, 2)
|
51536
|
41 |
|
|
42 |
var entry: Entry = null
|
|
43 |
override def paintComponent(gfx: Graphics2D)
|
|
44 |
{
|
|
45 |
def paint_rectangle(color: Color)
|
|
46 |
{
|
|
47 |
val size = peer.getSize()
|
|
48 |
val insets = border.getBorderInsets(peer)
|
|
49 |
val x = insets.left
|
|
50 |
val y = insets.top
|
|
51 |
val w = size.width - x - insets.right
|
|
52 |
val h = size.height - y - insets.bottom
|
|
53 |
gfx.setColor(color)
|
|
54 |
gfx.fillRect(x, y, w, h)
|
|
55 |
}
|
|
56 |
|
|
57 |
entry match {
|
|
58 |
case theory_entry: Theory_Entry if theory_entry.current =>
|
|
59 |
paint_rectangle(view.getTextArea.getPainter.getSelectionColor)
|
|
60 |
case _: Command_Entry =>
|
|
61 |
paint_rectangle(view.getTextArea.getPainter.getMultipleSelectionColor)
|
|
62 |
case _ =>
|
|
63 |
}
|
|
64 |
super.paintComponent(gfx)
|
|
65 |
}
|
51533
|
66 |
}
|
|
67 |
|
|
68 |
class Renderer extends ListView.Renderer[Entry]
|
|
69 |
{
|
|
70 |
def componentFor(list: ListView[_], isSelected: Boolean, focused: Boolean,
|
|
71 |
entry: Entry, index: Int): Component =
|
|
72 |
{
|
|
73 |
val component = Renderer_Component
|
51536
|
74 |
component.entry = entry
|
51534
|
75 |
component.text = entry.print
|
51533
|
76 |
component
|
|
77 |
}
|
|
78 |
}
|
|
79 |
}
|
|
80 |
|
51534
|
81 |
private abstract class Entry
|
|
82 |
{
|
|
83 |
def timing: Double
|
|
84 |
def print: String
|
|
85 |
def follow(snapshot: Document.Snapshot)
|
|
86 |
}
|
|
87 |
|
51536
|
88 |
private case class Theory_Entry(name: Document.Node.Name, timing: Double, current: Boolean)
|
|
89 |
extends Entry
|
51533
|
90 |
{
|
51534
|
91 |
def print: String = Time.print_seconds(timing) + "s theory " + quote(name.theory)
|
|
92 |
def follow(snapshot: Document.Snapshot): Unit = Hyperlink(name.node).follow(view)
|
|
93 |
}
|
|
94 |
|
|
95 |
private case class Command_Entry(command: Command, timing: Double) extends Entry
|
|
96 |
{
|
|
97 |
def print: String = " " + Time.print_seconds(timing) + "s command " + quote(command.name)
|
|
98 |
|
51533
|
99 |
def follow(snapshot: Document.Snapshot)
|
|
100 |
{
|
|
101 |
val node = snapshot.version.nodes(command.node_name)
|
|
102 |
if (node.commands.contains(command)) {
|
|
103 |
val sources = node.commands.iterator.takeWhile(_ != command).map(_.source)
|
|
104 |
val (line, column) = ((1, 1) /: sources)(Symbol.advance_line_column)
|
|
105 |
Hyperlink(command.node_name.node, line, column).follow(view)
|
|
106 |
}
|
|
107 |
}
|
|
108 |
}
|
|
109 |
|
|
110 |
|
|
111 |
/* timing view */
|
|
112 |
|
|
113 |
private val timing_view = new ListView(Nil: List[Entry]) {
|
|
114 |
listenTo(mouse.clicks)
|
|
115 |
reactions += {
|
|
116 |
case MouseClicked(_, point, _, clicks, _) if clicks == 2 =>
|
|
117 |
val index = peer.locationToIndex(point)
|
|
118 |
if (index >= 0) listData(index).follow(PIDE.session.snapshot())
|
|
119 |
}
|
|
120 |
}
|
|
121 |
timing_view.peer.setLayoutOrientation(JList.VERTICAL_WRAP)
|
|
122 |
timing_view.peer.setVisibleRowCount(0)
|
|
123 |
timing_view.selection.intervalMode = ListView.IntervalMode.Single
|
|
124 |
timing_view.renderer = new Entry.Renderer
|
|
125 |
|
|
126 |
set_content(new ScrollPane(timing_view))
|
|
127 |
|
|
128 |
|
|
129 |
/* timing threshold */
|
|
130 |
|
|
131 |
private var timing_threshold = PIDE.options.real("jedit_timing_threshold")
|
|
132 |
|
51534
|
133 |
private val threshold_label = new Label("Threshold: ")
|
51533
|
134 |
|
|
135 |
private val threshold_value = new TextField(Time.print_seconds(timing_threshold)) {
|
|
136 |
reactions += {
|
|
137 |
case EditDone(_) =>
|
|
138 |
text match {
|
|
139 |
case Properties.Value.Double(x) if x >= 0.0 => timing_threshold = x
|
|
140 |
case _ =>
|
|
141 |
}
|
|
142 |
text = Time.print_seconds(timing_threshold)
|
|
143 |
handle_update()
|
|
144 |
}
|
|
145 |
tooltip = "Threshold for timing display (seconds)"
|
|
146 |
}
|
|
147 |
|
|
148 |
private val controls = new FlowPanel(FlowPanel.Alignment.Right)(threshold_label, threshold_value)
|
|
149 |
add(controls.peer, BorderLayout.NORTH)
|
|
150 |
|
|
151 |
|
|
152 |
/* component state -- owned by Swing thread */
|
|
153 |
|
|
154 |
private var nodes_timing = Map.empty[Document.Node.Name, Protocol.Node_Timing]
|
51534
|
155 |
|
|
156 |
private def make_entries(): List[Entry] =
|
|
157 |
{
|
|
158 |
Swing_Thread.require()
|
|
159 |
|
|
160 |
val name =
|
|
161 |
Document_View(view.getTextArea) match {
|
|
162 |
case None => Document.Node.Name.empty
|
|
163 |
case Some(doc_view) => doc_view.model.name
|
|
164 |
}
|
|
165 |
val timing = nodes_timing.get(name) getOrElse Protocol.empty_node_timing
|
|
166 |
|
|
167 |
val theories =
|
51536
|
168 |
(for ((node_name, node_timing) <- nodes_timing.toList if !node_timing.commands.isEmpty)
|
|
169 |
yield Theory_Entry(node_name, node_timing.total, false)).sorted(Entry.Ordering)
|
51534
|
170 |
val commands =
|
|
171 |
(for ((command, command_timing) <- timing.commands.toList)
|
|
172 |
yield Command_Entry(command, command_timing)).sorted(Entry.Ordering)
|
|
173 |
|
51536
|
174 |
theories.flatMap(entry =>
|
|
175 |
if (entry.name == name) entry.copy(current = true) :: commands
|
|
176 |
else List(entry))
|
51534
|
177 |
}
|
51533
|
178 |
|
|
179 |
private def handle_update(restriction: Option[Set[Document.Node.Name]] = None)
|
|
180 |
{
|
|
181 |
Swing_Thread.now {
|
|
182 |
val snapshot = PIDE.session.snapshot()
|
|
183 |
|
|
184 |
val iterator =
|
|
185 |
restriction match {
|
|
186 |
case Some(names) => names.iterator.map(name => (name, snapshot.version.nodes(name)))
|
|
187 |
case None => snapshot.version.nodes.entries
|
|
188 |
}
|
|
189 |
val nodes_timing1 =
|
|
190 |
(nodes_timing /: iterator)({ case (timing1, (name, node)) =>
|
|
191 |
if (PIDE.thy_load.loaded_theories(name.theory)) timing1
|
|
192 |
else {
|
|
193 |
val node_timing =
|
|
194 |
Protocol.node_timing(snapshot.state, snapshot.version, node, timing_threshold)
|
|
195 |
timing1 + (name -> node_timing)
|
|
196 |
}
|
|
197 |
})
|
|
198 |
nodes_timing = nodes_timing1
|
|
199 |
|
51534
|
200 |
val entries = make_entries()
|
|
201 |
if (timing_view.listData.toList != entries) timing_view.listData = entries
|
51533
|
202 |
}
|
|
203 |
}
|
|
204 |
|
|
205 |
|
|
206 |
/* main actor */
|
|
207 |
|
|
208 |
private val main_actor = actor {
|
|
209 |
loop {
|
|
210 |
react {
|
|
211 |
case changed: Session.Commands_Changed => handle_update(Some(changed.nodes))
|
|
212 |
|
|
213 |
case bad => System.err.println("Timing_Dockable: ignoring bad message " + bad)
|
|
214 |
}
|
|
215 |
}
|
|
216 |
}
|
|
217 |
|
|
218 |
override def init()
|
|
219 |
{
|
|
220 |
PIDE.session.commands_changed += main_actor
|
|
221 |
handle_update()
|
|
222 |
}
|
|
223 |
|
|
224 |
override def exit()
|
|
225 |
{
|
|
226 |
PIDE.session.commands_changed -= main_actor
|
|
227 |
}
|
|
228 |
}
|