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