author | wenzelm |
Sun, 18 Jan 2015 22:46:38 +0100 | |
changeset 59403 | db65d45b6740 |
parent 59399 | 47fb85ccfce8 |
child 59408 | 63cb603b5114 |
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 |
|
59397 | 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} |
59225 | 15 |
import javax.swing.{JScrollPane, JComponent, SwingUtilities} |
49729 | 16 |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
17 |
import scala.swing.{Panel, ScrollPane} |
59253 | 18 |
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
|
19 |
|
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
20 |
|
59233 | 21 |
class Graph_Panel(val visualizer: Visualizer) extends ScrollPane |
49729 | 22 |
{ |
49731 | 23 |
panel => |
49729 | 24 |
|
59398 | 25 |
|
26 |
/* tooltips */ |
|
27 |
||
59243
21ef04bd4e17
recovered tooltip from 6e77ddb1e3fb: non-null default is required as prerequisite;
wenzelm
parents:
59241
diff
changeset
|
28 |
tooltip = "" |
21ef04bd4e17
recovered tooltip from 6e77ddb1e3fb: non-null default is required as prerequisite;
wenzelm
parents:
59241
diff
changeset
|
29 |
|
49729 | 30 |
override lazy val peer: JScrollPane = new JScrollPane with SuperMixin { |
49730 | 31 |
override def getToolTipText(event: java.awt.event.MouseEvent): String = |
59305 | 32 |
visualizer.find_node(Transform.pane_to_graph_coordinates(event.getPoint)) match { |
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59243
diff
changeset
|
33 |
case Some(node) => |
59259
399506ee38a5
clarified static full_graph vs. dynamic visible_graph;
wenzelm
parents:
59255
diff
changeset
|
34 |
visualizer.model.full_graph.get_node(node) match { |
49732 | 35 |
case Nil => null |
59233 | 36 |
case content => visualizer.make_tooltip(panel.peer, event.getX, event.getY, content) |
49732 | 37 |
} |
38 |
case None => null |
|
39 |
} |
|
49729 | 40 |
} |
41 |
||
59398 | 42 |
|
43 |
/* scrolling */ |
|
44 |
||
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
45 |
horizontalScrollBarPolicy = ScrollPane.BarPolicy.Always |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
46 |
verticalScrollBarPolicy = ScrollPane.BarPolicy.Always |
49729 | 47 |
|
59237
ac135eff1ffb
clarified mouse wheel: conventional scrolling, not scaling;
wenzelm
parents:
59234
diff
changeset
|
48 |
peer.getVerticalScrollBar.setUnitIncrement(10) |
ac135eff1ffb
clarified mouse wheel: conventional scrolling, not scaling;
wenzelm
parents:
59234
diff
changeset
|
49 |
|
59398 | 50 |
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
|
51 |
{ |
59398 | 52 |
val gap = visualizer.metrics.gap |
53 |
val info = visualizer.layout.get_node(node) |
|
54 |
||
55 |
val t = Transform() |
|
56 |
val p = |
|
57 |
t.transform(new Point2D.Double(info.x - info.width2 - gap, info.y - info.height2 - gap), null) |
|
58 |
val q = |
|
59 |
t.transform(new Point2D.Double(info.x + info.width2 + gap, info.y + info.height2 + gap), null) |
|
60 |
||
61 |
paint_panel.peer.scrollRectToVisible( |
|
62 |
new Rectangle(p.getX.toInt, p.getY.toInt, |
|
63 |
(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
|
64 |
} |
30e2f3f1c623
more precise repaint and revalidate -- the latter is important to keep in sync with content update;
wenzelm
parents:
49733
diff
changeset
|
65 |
|
50470 | 66 |
|
59398 | 67 |
/* painting */ |
50478 | 68 |
|
59218 | 69 |
private class Paint_Panel extends Panel |
70 |
{ |
|
71 |
def set_preferred_size() |
|
72 |
{ |
|
59302
4d985afc0565
explict layout graph structure, with dummies and coordinates;
wenzelm
parents:
59294
diff
changeset
|
73 |
val box = visualizer.bounding_box() |
59218 | 74 |
val s = Transform.scale_discrete |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
75 |
|
59218 | 76 |
preferredSize = |
59241
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
77 |
new Dimension((box.width * s).ceil.toInt, (box.height * s).ceil.toInt) |
50470 | 78 |
|
59218 | 79 |
revalidate() |
80 |
} |
|
50470 | 81 |
|
59250 | 82 |
override def paint(gfx: Graphics2D) |
59218 | 83 |
{ |
59250 | 84 |
super.paintComponent(gfx) |
85 |
gfx.setColor(visualizer.background_color) |
|
86 |
gfx.fillRect(0, 0, peer.getWidth, peer.getHeight) |
|
87 |
gfx.transform(Transform()) |
|
50470 | 88 |
|
59294 | 89 |
visualizer.paint_all_visible(gfx) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
90 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
91 |
} |
49954
44658062d822
more explicit auxiliary classes to avoid warning "reflective access of structural type member method" of scala-2.10.0-RC1;
wenzelm
parents:
49745
diff
changeset
|
92 |
private val paint_panel = new Paint_Panel |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
93 |
contents = paint_panel |
50470 | 94 |
|
59398 | 95 |
def refresh() |
59218 | 96 |
{ |
59398 | 97 |
if (paint_panel != null) { |
98 |
paint_panel.set_preferred_size() |
|
99 |
paint_panel.repaint() |
|
100 |
} |
|
59218 | 101 |
} |
49733
38a68e6593be
prefer synchronous Mutator_Event.Bus on Swing_Thread;
wenzelm
parents:
49732
diff
changeset
|
102 |
|
59398 | 103 |
val zoom = new GUI.Zoom_Box { def changed = rescale(0.01 * factor) } |
104 |
||
105 |
def rescale(s: Double) |
|
106 |
{ |
|
107 |
Transform.scale = s |
|
108 |
if (zoom != null) zoom.set_item((Transform.scale_discrete * 100).floor.toInt) |
|
109 |
refresh() |
|
110 |
} |
|
50470 | 111 |
|
50491 | 112 |
rescale(1.0) |
50470 | 113 |
|
59397 | 114 |
|
59398 | 115 |
/* transform */ |
59397 | 116 |
|
59399 | 117 |
def fit_to_window() |
118 |
{ |
|
119 |
Transform.fit_to_window() |
|
120 |
refresh() |
|
121 |
} |
|
122 |
||
50469 | 123 |
private object Transform |
124 |
{ |
|
50474
6ee044e2d1a7
initial layout coordinates more like old browser;
wenzelm
parents:
50470
diff
changeset
|
125 |
private var _scale: Double = 1.0 |
50477 | 126 |
def scale: Double = _scale |
127 |
def scale_=(s: Double) |
|
50468 | 128 |
{ |
59241
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
129 |
_scale = (s min 10.0) max 0.1 |
50468 | 130 |
} |
59255
db265648139c
clarified fit_to_window: floor scale within window bounds;
wenzelm
parents:
59253
diff
changeset
|
131 |
|
50477 | 132 |
def scale_discrete: Double = |
59286
ac74eedb910a
GUI.imitate_font: more explicit result size, e.g. relevant for caching;
wenzelm
parents:
59262
diff
changeset
|
133 |
{ |
59290 | 134 |
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
|
135 |
(scale * font_height).floor / font_height |
ac74eedb910a
GUI.imitate_font: more explicit result size, e.g. relevant for caching;
wenzelm
parents:
59262
diff
changeset
|
136 |
} |
50470 | 137 |
|
59218 | 138 |
def apply() = |
139 |
{ |
|
59302
4d985afc0565
explict layout graph structure, with dummies and coordinates;
wenzelm
parents:
59294
diff
changeset
|
140 |
val box = visualizer.bounding_box() |
59397 | 141 |
val t = AffineTransform.getScaleInstance(scale_discrete, scale_discrete) |
142 |
t.translate(- box.x, - box.y) |
|
143 |
t |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
144 |
} |
50470 | 145 |
|
59218 | 146 |
def fit_to_window() |
147 |
{ |
|
59290 | 148 |
if (visualizer.visible_graph.is_empty) |
50491 | 149 |
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
|
150 |
else { |
59302
4d985afc0565
explict layout graph structure, with dummies and coordinates;
wenzelm
parents:
59294
diff
changeset
|
151 |
val box = visualizer.bounding_box() |
59241
541b95e94dc7
clarified bounding box, similar to old graph browser;
wenzelm
parents:
59240
diff
changeset
|
152 |
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
|
153 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
154 |
} |
50470 | 155 |
|
59218 | 156 |
def pane_to_graph_coordinates(at: Point2D): Point2D = |
157 |
{ |
|
50477 | 158 |
val s = Transform.scale_discrete |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
159 |
val p = Transform().inverseTransform(peer.getViewport.getViewPosition, null) |
50470 | 160 |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
161 |
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
|
162 |
p |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
163 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
164 |
} |
50470 | 165 |
|
59398 | 166 |
|
167 |
/* interaction */ |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
168 |
|
59399 | 169 |
visualizer.model.Colors.events += { case _ => repaint() } |
170 |
visualizer.model.Mutators.events += { case _ => repaint() } |
|
171 |
||
59398 | 172 |
listenTo(mouse.moves) |
173 |
listenTo(mouse.clicks) |
|
174 |
reactions += |
|
175 |
{ |
|
176 |
case MousePressed(_, p, _, _, _) => |
|
177 |
Mouse_Interaction.pressed(p) |
|
178 |
repaint() |
|
179 |
case MouseDragged(_, to, _) => |
|
180 |
Mouse_Interaction.dragged(to) |
|
181 |
repaint() |
|
182 |
case e @ MouseClicked(_, p, m, n, _) => |
|
183 |
Mouse_Interaction.clicked(p, m, n, SwingUtilities.isRightMouseButton(e.peer)) |
|
184 |
repaint() |
|
185 |
} |
|
186 |
||
187 |
private object Mouse_Interaction |
|
188 |
{ |
|
189 |
private var draginfo: (Point, List[Graph_Display.Node], List[Layout.Dummy]) = |
|
190 |
(new Point(0, 0), Nil, Nil) |
|
59253 | 191 |
|
192 |
def pressed(at: Point) |
|
59218 | 193 |
{ |
59253 | 194 |
val c = Transform.pane_to_graph_coordinates(at) |
195 |
val l = |
|
59305 | 196 |
visualizer.find_node(c) match { |
59253 | 197 |
case Some(node) => |
198 |
if (visualizer.Selection.contains(node)) visualizer.Selection.get() |
|
199 |
else List(node) |
|
200 |
case None => Nil |
|
201 |
} |
|
202 |
val d = |
|
203 |
l match { |
|
59305 | 204 |
case Nil => visualizer.find_dummy(c).toList |
59253 | 205 |
case _ => Nil |
206 |
} |
|
207 |
draginfo = (at, l, d) |
|
208 |
} |
|
50470 | 209 |
|
59398 | 210 |
def dragged(to: Point) |
59253 | 211 |
{ |
59398 | 212 |
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
|
213 |
|
59253 | 214 |
val s = Transform.scale_discrete |
59398 | 215 |
val dx = to.x - from.x |
216 |
val dy = to.y - from.y |
|
217 |
||
59253 | 218 |
(p, d) match { |
219 |
case (Nil, Nil) => |
|
220 |
val r = panel.peer.getViewport.getViewRect |
|
59302
4d985afc0565
explict layout graph structure, with dummies and coordinates;
wenzelm
parents:
59294
diff
changeset
|
221 |
r.translate(- dx, - dy) |
59253 | 222 |
paint_panel.peer.scrollRectToVisible(r) |
50470 | 223 |
|
59253 | 224 |
case (Nil, ds) => |
59302
4d985afc0565
explict layout graph structure, with dummies and coordinates;
wenzelm
parents:
59294
diff
changeset
|
225 |
ds.foreach(d => visualizer.translate_vertex(d, dx / s, dy / s)) |
59253 | 226 |
|
227 |
case (ls, _) => |
|
59302
4d985afc0565
explict layout graph structure, with dummies and coordinates;
wenzelm
parents:
59294
diff
changeset
|
228 |
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
|
229 |
} |
59398 | 230 |
|
231 |
draginfo = (to, p, d) |
|
232 |
} |
|
233 |
||
234 |
def clicked(at: Point, m: Key.Modifiers, clicks: Int, right_click: Boolean) |
|
235 |
{ |
|
236 |
val c = Transform.pane_to_graph_coordinates(at) |
|
237 |
||
238 |
if (clicks < 2) { |
|
239 |
if (right_click) { |
|
59403 | 240 |
// FIXME |
241 |
if (false) { |
|
242 |
val menu = Popups(panel, visualizer.find_node(c), visualizer.Selection.get()) |
|
243 |
menu.show(panel.peer, at.x, at.y) |
|
244 |
} |
|
59398 | 245 |
} |
246 |
else { |
|
247 |
(visualizer.find_node(c), m) match { |
|
248 |
case (Some(node), Key.Modifier.Control) => visualizer.Selection.add(node) |
|
249 |
case (None, Key.Modifier.Control) => |
|
250 |
||
251 |
case (Some(node), Key.Modifier.Shift) => visualizer.Selection.add(node) |
|
252 |
case (None, Key.Modifier.Shift) => |
|
253 |
||
254 |
case (Some(node), _) => |
|
255 |
visualizer.Selection.clear() |
|
256 |
visualizer.Selection.add(node) |
|
257 |
case (None, _) => |
|
258 |
visualizer.Selection.clear() |
|
259 |
} |
|
260 |
} |
|
261 |
} |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
262 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
263 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
264 |
} |