author | wenzelm |
Sun, 25 Jan 2015 20:16:27 +0100 | |
changeset 59443 | 5b552b4f63a5 |
parent 59441 | ab2c3597f1d3 |
child 59459 | 985fc55e9f27 |
permissions | -rw-r--r-- |
59202 | 1 |
/* Title: Tools/Graphview/graph_panel.scala |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
2 |
Author: Markus Kaiser, TU Muenchen |
59240 | 3 |
Author: Makarius |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
4 |
|
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
5 |
Graphview Java2D drawing panel. |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
6 |
*/ |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
7 |
|
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
8 |
package isabelle.graphview |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
9 |
|
55618 | 10 |
|
49558 | 11 |
import isabelle._ |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
12 |
|
59441 | 13 |
import java.awt.{Dimension, Graphics2D, Point, Rectangle} |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
14 |
import java.awt.geom.{AffineTransform, Point2D} |
59408 | 15 |
import javax.imageio.ImageIO |
59225 | 16 |
import javax.swing.{JScrollPane, JComponent, SwingUtilities} |
59408 | 17 |
import javax.swing.border.EmptyBorder |
49729 | 18 |
|
59408 | 19 |
import scala.swing.{BorderPanel, Button, CheckBox, Action, FileChooser, Panel, ScrollPane} |
59253 | 20 |
import scala.swing.event.{Event, Key, MousePressed, MouseDragged, MouseClicked, MouseEvent} |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
21 |
|
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
22 |
|
59408 | 23 |
class Graph_Panel(val visualizer: Visualizer) extends BorderPanel |
49729 | 24 |
{ |
59408 | 25 |
graph_panel => |
26 |
||
49729 | 27 |
|
59398 | 28 |
|
59408 | 29 |
/** graph **/ |
59398 | 30 |
|
59408 | 31 |
/* painter */ |
59243
21ef04bd4e17
recovered tooltip from 6e77ddb1e3fb: non-null default is required as prerequisite;
wenzelm
parents:
59241
diff
changeset
|
32 |
|
59408 | 33 |
private val painter = new Panel |
34 |
{ |
|
35 |
override def paint(gfx: Graphics2D) |
|
36 |
{ |
|
37 |
super.paintComponent(gfx) |
|
38 |
||
39 |
gfx.setColor(visualizer.background_color) |
|
40 |
gfx.fillRect(0, 0, peer.getWidth, peer.getHeight) |
|
41 |
||
42 |
gfx.transform(Transform()) |
|
43 |
visualizer.paint_all_visible(gfx) |
|
44 |
} |
|
49729 | 45 |
} |
46 |
||
59408 | 47 |
def set_preferred_size() |
48 |
{ |
|
49 |
val box = visualizer.bounding_box() |
|
50 |
val s = Transform.scale_discrete |
|
51 |
painter.preferredSize = new Dimension((box.width * s).ceil.toInt, (box.height * s).ceil.toInt) |
|
52 |
painter.revalidate() |
|
53 |
} |
|
59398 | 54 |
|
59408 | 55 |
def refresh() |
56 |
{ |
|
57 |
if (painter != null) { |
|
58 |
set_preferred_size() |
|
59 |
painter.repaint() |
|
60 |
} |
|
61 |
} |
|
59237
ac135eff1ffb
clarified mouse wheel: conventional scrolling, not scaling;
wenzelm
parents:
59234
diff
changeset
|
62 |
|
59398 | 63 |
def scroll_to_node(node: Graph_Display.Node) |
49735
30e2f3f1c623
more precise repaint and revalidate -- the latter is important to keep in sync with content update;
wenzelm
parents:
49733
diff
changeset
|
64 |
{ |
59398 | 65 |
val gap = visualizer.metrics.gap |
66 |
val info = visualizer.layout.get_node(node) |
|
67 |
||
68 |
val t = Transform() |
|
69 |
val p = |
|
70 |
t.transform(new Point2D.Double(info.x - info.width2 - gap, info.y - info.height2 - gap), null) |
|
71 |
val q = |
|
72 |
t.transform(new Point2D.Double(info.x + info.width2 + gap, info.y + info.height2 + gap), null) |
|
73 |
||
59408 | 74 |
painter.peer.scrollRectToVisible( |
59398 | 75 |
new Rectangle(p.getX.toInt, p.getY.toInt, |
76 |
(q.getX - p.getX).ceil.toInt, (q.getY - p.getY).ceil.toInt)) |
|
49735
30e2f3f1c623
more precise repaint and revalidate -- the latter is important to keep in sync with content update;
wenzelm
parents:
49733
diff
changeset
|
77 |
} |
30e2f3f1c623
more precise repaint and revalidate -- the latter is important to keep in sync with content update;
wenzelm
parents:
49733
diff
changeset
|
78 |
|
50470 | 79 |
|
59408 | 80 |
/* scrollable graph pane */ |
81 |
||
82 |
private val graph_pane: ScrollPane = new ScrollPane(painter) { |
|
83 |
tooltip = "" |
|
50478 | 84 |
|
59408 | 85 |
override lazy val peer: JScrollPane = new JScrollPane with SuperMixin { |
86 |
override def getToolTipText(event: java.awt.event.MouseEvent): String = |
|
87 |
visualizer.find_node(Transform.pane_to_graph_coordinates(event.getPoint)) match { |
|
88 |
case Some(node) => |
|
89 |
visualizer.model.full_graph.get_node(node) match { |
|
90 |
case Nil => null |
|
91 |
case content => |
|
92 |
visualizer.make_tooltip(graph_pane.peer, event.getX, event.getY, content) |
|
93 |
} |
|
94 |
case None => null |
|
95 |
} |
|
59218 | 96 |
} |
50470 | 97 |
|
59408 | 98 |
horizontalScrollBarPolicy = ScrollPane.BarPolicy.Always |
99 |
verticalScrollBarPolicy = ScrollPane.BarPolicy.Always |
|
100 |
||
101 |
listenTo(mouse.moves) |
|
102 |
listenTo(mouse.clicks) |
|
103 |
reactions += |
|
59218 | 104 |
{ |
59408 | 105 |
case MousePressed(_, p, _, _, _) => |
106 |
Mouse_Interaction.pressed(p) |
|
107 |
painter.repaint() |
|
108 |
case MouseDragged(_, to, _) => |
|
109 |
Mouse_Interaction.dragged(to) |
|
110 |
painter.repaint() |
|
111 |
case e @ MouseClicked(_, p, m, n, _) => |
|
112 |
Mouse_Interaction.clicked(p, m, n, SwingUtilities.isRightMouseButton(e.peer)) |
|
113 |
painter.repaint() |
|
114 |
} |
|
50470 | 115 |
|
59408 | 116 |
contents = painter |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
117 |
} |
59408 | 118 |
graph_pane.peer.getVerticalScrollBar.setUnitIncrement(10) |
50470 | 119 |
|
59408 | 120 |
|
121 |
/* transform */ |
|
49733
38a68e6593be
prefer synchronous Mutator_Event.Bus on Swing_Thread;
wenzelm
parents:
49732
diff
changeset
|
122 |
|
59398 | 123 |
def rescale(s: Double) |
124 |
{ |
|
125 |
Transform.scale = s |
|
126 |
if (zoom != null) zoom.set_item((Transform.scale_discrete * 100).floor.toInt) |
|
127 |
refresh() |
|
128 |
} |
|
50470 | 129 |
|
59399 | 130 |
def fit_to_window() |
131 |
{ |
|
132 |
Transform.fit_to_window() |
|
133 |
refresh() |
|
134 |
} |
|
135 |
||
50469 | 136 |
private object Transform |
137 |
{ |
|
50474
6ee044e2d1a7
initial layout coordinates more like old browser;
wenzelm
parents:
50470
diff
changeset
|
138 |
private var _scale: Double = 1.0 |
50477 | 139 |
def scale: Double = _scale |
140 |
def scale_=(s: Double) |
|
50468 | 141 |
{ |
59241
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
142 |
_scale = (s min 10.0) max 0.1 |
50468 | 143 |
} |
59255
db265648139c
clarified fit_to_window: floor scale within window bounds;
wenzelm
parents:
59253
diff
changeset
|
144 |
|
50477 | 145 |
def scale_discrete: Double = |
59286
ac74eedb910a
GUI.imitate_font: more explicit result size, e.g. relevant for caching;
wenzelm
parents:
59262
diff
changeset
|
146 |
{ |
59290 | 147 |
val font_height = GUI.line_metrics(visualizer.metrics.font).getHeight.toInt |
59286
ac74eedb910a
GUI.imitate_font: more explicit result size, e.g. relevant for caching;
wenzelm
parents:
59262
diff
changeset
|
148 |
(scale * font_height).floor / font_height |
ac74eedb910a
GUI.imitate_font: more explicit result size, e.g. relevant for caching;
wenzelm
parents:
59262
diff
changeset
|
149 |
} |
50470 | 150 |
|
59218 | 151 |
def apply() = |
152 |
{ |
|
59302
4d985afc0565
explict layout graph structure, with dummies and coordinates;
wenzelm
parents:
59294
diff
changeset
|
153 |
val box = visualizer.bounding_box() |
59397 | 154 |
val t = AffineTransform.getScaleInstance(scale_discrete, scale_discrete) |
155 |
t.translate(- box.x, - box.y) |
|
156 |
t |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
157 |
} |
50470 | 158 |
|
59218 | 159 |
def fit_to_window() |
160 |
{ |
|
59290 | 161 |
if (visualizer.visible_graph.is_empty) |
50491 | 162 |
rescale(1.0) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
163 |
else { |
59302
4d985afc0565
explict layout graph structure, with dummies and coordinates;
wenzelm
parents:
59294
diff
changeset
|
164 |
val box = visualizer.bounding_box() |
59241
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
165 |
rescale((size.width / box.width) min (size.height / box.height)) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
166 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
167 |
} |
50470 | 168 |
|
59218 | 169 |
def pane_to_graph_coordinates(at: Point2D): Point2D = |
170 |
{ |
|
50477 | 171 |
val s = Transform.scale_discrete |
59408 | 172 |
val p = Transform().inverseTransform(graph_pane.peer.getViewport.getViewPosition, null) |
50470 | 173 |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
174 |
p.setLocation(p.getX + at.getX / s, p.getY + at.getY / s) |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
175 |
p |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
176 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
177 |
} |
50470 | 178 |
|
59398 | 179 |
|
180 |
/* interaction */ |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
181 |
|
59408 | 182 |
visualizer.model.Colors.events += { case _ => painter.repaint() } |
183 |
visualizer.model.Mutators.events += { case _ => painter.repaint() } |
|
59398 | 184 |
|
185 |
private object Mouse_Interaction |
|
186 |
{ |
|
187 |
private var draginfo: (Point, List[Graph_Display.Node], List[Layout.Dummy]) = |
|
188 |
(new Point(0, 0), Nil, Nil) |
|
59253 | 189 |
|
190 |
def pressed(at: Point) |
|
59218 | 191 |
{ |
59253 | 192 |
val c = Transform.pane_to_graph_coordinates(at) |
193 |
val l = |
|
59305 | 194 |
visualizer.find_node(c) match { |
59253 | 195 |
case Some(node) => |
196 |
if (visualizer.Selection.contains(node)) visualizer.Selection.get() |
|
197 |
else List(node) |
|
198 |
case None => Nil |
|
199 |
} |
|
200 |
val d = |
|
201 |
l match { |
|
59305 | 202 |
case Nil => visualizer.find_dummy(c).toList |
59253 | 203 |
case _ => Nil |
204 |
} |
|
205 |
draginfo = (at, l, d) |
|
206 |
} |
|
50470 | 207 |
|
59398 | 208 |
def dragged(to: Point) |
59253 | 209 |
{ |
59398 | 210 |
val (from, p, d) = draginfo |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
211 |
|
59253 | 212 |
val s = Transform.scale_discrete |
59398 | 213 |
val dx = to.x - from.x |
214 |
val dy = to.y - from.y |
|
215 |
||
59253 | 216 |
(p, d) match { |
217 |
case (Nil, Nil) => |
|
59408 | 218 |
val r = graph_pane.peer.getViewport.getViewRect |
59302
4d985afc0565
explict layout graph structure, with dummies and coordinates;
wenzelm
parents:
59294
diff
changeset
|
219 |
r.translate(- dx, - dy) |
59408 | 220 |
painter.peer.scrollRectToVisible(r) |
50470 | 221 |
|
59253 | 222 |
case (Nil, ds) => |
59302
4d985afc0565
explict layout graph structure, with dummies and coordinates;
wenzelm
parents:
59294
diff
changeset
|
223 |
ds.foreach(d => visualizer.translate_vertex(d, dx / s, dy / s)) |
59253 | 224 |
|
225 |
case (ls, _) => |
|
59302
4d985afc0565
explict layout graph structure, with dummies and coordinates;
wenzelm
parents:
59294
diff
changeset
|
226 |
ls.foreach(l => visualizer.translate_vertex(Layout.Node(l), dx / s, dy / s)) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
227 |
} |
59398 | 228 |
|
229 |
draginfo = (to, p, d) |
|
230 |
} |
|
231 |
||
232 |
def clicked(at: Point, m: Key.Modifiers, clicks: Int, right_click: Boolean) |
|
233 |
{ |
|
234 |
val c = Transform.pane_to_graph_coordinates(at) |
|
235 |
||
236 |
if (clicks < 2) { |
|
237 |
if (right_click) { |
|
59403 | 238 |
// FIXME |
239 |
if (false) { |
|
59408 | 240 |
val menu = Popups(graph_panel, visualizer.find_node(c), visualizer.Selection.get()) |
241 |
menu.show(graph_pane.peer, at.x, at.y) |
|
59403 | 242 |
} |
59398 | 243 |
} |
244 |
else { |
|
245 |
(visualizer.find_node(c), m) match { |
|
246 |
case (Some(node), Key.Modifier.Control) => visualizer.Selection.add(node) |
|
247 |
case (None, Key.Modifier.Control) => |
|
248 |
||
249 |
case (Some(node), Key.Modifier.Shift) => visualizer.Selection.add(node) |
|
250 |
case (None, Key.Modifier.Shift) => |
|
251 |
||
252 |
case (Some(node), _) => |
|
253 |
visualizer.Selection.clear() |
|
254 |
visualizer.Selection.add(node) |
|
255 |
case (None, _) => |
|
256 |
visualizer.Selection.clear() |
|
257 |
} |
|
258 |
} |
|
259 |
} |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
260 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
261 |
} |
59408 | 262 |
|
263 |
||
264 |
||
265 |
/** controls **/ |
|
266 |
||
267 |
private val mutator_dialog = |
|
268 |
new Mutator_Dialog(visualizer, visualizer.model.Mutators, "Filters", "Hide", false) |
|
269 |
private val color_dialog = |
|
270 |
new Mutator_Dialog(visualizer, visualizer.model.Colors, "Colorations") |
|
271 |
||
272 |
private val chooser = new FileChooser { |
|
273 |
fileSelectionMode = FileChooser.SelectionMode.FilesOnly |
|
274 |
title = "Save image (.png or .pdf)" |
|
275 |
} |
|
276 |
||
59409 | 277 |
private val show_content = new CheckBox() { |
278 |
selected = visualizer.show_content |
|
279 |
action = Action("Show content") { |
|
280 |
visualizer.show_content = selected |
|
281 |
visualizer.update_layout() |
|
282 |
refresh() |
|
283 |
} |
|
284 |
tooltip = "Show full node content within graph layout" |
|
285 |
} |
|
286 |
||
287 |
private val show_arrow_heads = new CheckBox() { |
|
288 |
selected = visualizer.show_arrow_heads |
|
289 |
action = Action("Show arrow heads") { |
|
290 |
visualizer.show_arrow_heads = selected |
|
291 |
painter.repaint() |
|
292 |
} |
|
293 |
tooltip = "Draw edges with explicit arrow heads" |
|
294 |
} |
|
295 |
||
296 |
private val show_dummies = new CheckBox() { |
|
297 |
selected = visualizer.show_dummies |
|
298 |
action = Action("Show dummies") { |
|
299 |
visualizer.show_dummies = selected |
|
300 |
painter.repaint() |
|
301 |
} |
|
302 |
tooltip = "Draw dummy nodes within graph layout, for easier mouse dragging" |
|
303 |
} |
|
304 |
||
305 |
private val save_image = new Button { |
|
306 |
action = Action("Save image") { |
|
307 |
chooser.showSaveDialog(this) match { |
|
59441 | 308 |
case FileChooser.Result.Approve => |
59443
5b552b4f63a5
support for off-line graph output, without GUI thread;
wenzelm
parents:
59441
diff
changeset
|
309 |
try { Graph_File.write(chooser.selectedFile, visualizer) } |
59441 | 310 |
catch { |
311 |
case ERROR(msg) => GUI.error_dialog(this.peer, "Error", GUI.scrollable_text(msg)) |
|
312 |
} |
|
59409 | 313 |
case _ => |
314 |
} |
|
315 |
} |
|
316 |
tooltip = "Save current graph layout as PNG or PDF" |
|
317 |
} |
|
318 |
||
319 |
private val zoom = new GUI.Zoom_Box { def changed = rescale(0.01 * factor) } |
|
320 |
||
321 |
private val fit_window = new Button { |
|
322 |
action = Action("Fit to window") { fit_to_window() } |
|
323 |
tooltip = "Zoom to fit window width and height" |
|
324 |
} |
|
325 |
||
326 |
private val update_layout = new Button { |
|
327 |
action = Action("Update layout") { |
|
328 |
visualizer.update_layout() |
|
329 |
refresh() |
|
330 |
} |
|
331 |
tooltip = "Regenerate graph layout according to built-in algorithm" |
|
332 |
} |
|
333 |
||
334 |
private val colorations = new Button { action = Action("Colorations") { color_dialog.open } } |
|
335 |
private val filters = new Button { action = Action("Filters") { mutator_dialog.open } } |
|
336 |
||
59408 | 337 |
private val controls = |
59409 | 338 |
new Wrap_Panel(Wrap_Panel.Alignment.Right)(show_content, show_arrow_heads, show_dummies, |
59411 | 339 |
save_image, zoom, fit_window, update_layout) // FIXME colorations, filters |
59408 | 340 |
|
341 |
||
342 |
||
343 |
/** main layout **/ |
|
344 |
||
345 |
layout(graph_pane) = BorderPanel.Position.Center |
|
346 |
layout(controls) = BorderPanel.Position.North |
|
347 |
||
348 |
rescale(1.0) |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
349 |
} |