author | wenzelm |
Thu, 01 Jan 2015 15:58:30 +0100 | |
changeset 59228 | 56b34fc7a015 |
parent 59220 | 261ec482cd40 |
child 59231 | 6dea47cf6c6b |
permissions | -rw-r--r-- |
59202 | 1 |
/* Title: Tools/Graphview/visualizer.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 |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
3 |
|
50475 | 4 |
Graph visualization parameters and interface state. |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
5 |
*/ |
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 |
package isabelle.graphview |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
8 |
|
50464 | 9 |
|
49565
ea4308b7ef0f
ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents:
49557
diff
changeset
|
10 |
import isabelle._ |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
11 |
|
59220 | 12 |
import java.awt.{Font, FontMetrics, Color, Shape, RenderingHints, Graphics2D} |
50476
1cb983bccb5b
more official graphics context with font metrics;
wenzelm
parents:
50475
diff
changeset
|
13 |
import java.awt.image.BufferedImage |
49729 | 14 |
import javax.swing.JComponent |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
15 |
|
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
16 |
|
50465 | 17 |
class Visualizer(val model: Model) |
18 |
{ |
|
50472
bad1a1ca61e1
separate instance of class Parameters for each Main_Panel -- avoid global program state;
wenzelm
parents:
50471
diff
changeset
|
19 |
visualizer => |
bad1a1ca61e1
separate instance of class Parameters for each Main_Panel -- avoid global program state;
wenzelm
parents:
50471
diff
changeset
|
20 |
|
50476
1cb983bccb5b
more official graphics context with font metrics;
wenzelm
parents:
50475
diff
changeset
|
21 |
|
59228
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
22 |
/* main colors */ |
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
23 |
|
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
24 |
def foreground_color: Color = Color.BLACK |
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
25 |
def foreground1_color: Color = Color.GRAY |
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
26 |
def background_color: Color = Color.WHITE |
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
27 |
def selection_color: Color = Color.GREEN |
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
28 |
def error_color: Color = Color.RED |
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
29 |
|
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
30 |
|
50475 | 31 |
/* font rendering information */ |
32 |
||
33 |
val font_family: String = "IsabelleText" |
|
58940 | 34 |
val font_size: Int = 14 |
50476
1cb983bccb5b
more official graphics context with font metrics;
wenzelm
parents:
50475
diff
changeset
|
35 |
val font = new Font(font_family, Font.BOLD, font_size) |
50475 | 36 |
|
37 |
val rendering_hints = |
|
38 |
new RenderingHints( |
|
39 |
RenderingHints.KEY_ANTIALIASING, |
|
40 |
RenderingHints.VALUE_ANTIALIAS_ON) |
|
41 |
||
50476
1cb983bccb5b
more official graphics context with font metrics;
wenzelm
parents:
50475
diff
changeset
|
42 |
val gfx = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB).createGraphics |
1cb983bccb5b
more official graphics context with font metrics;
wenzelm
parents:
50475
diff
changeset
|
43 |
gfx.setFont(font) |
1cb983bccb5b
more official graphics context with font metrics;
wenzelm
parents:
50475
diff
changeset
|
44 |
gfx.setRenderingHints(rendering_hints) |
1cb983bccb5b
more official graphics context with font metrics;
wenzelm
parents:
50475
diff
changeset
|
45 |
|
1cb983bccb5b
more official graphics context with font metrics;
wenzelm
parents:
50475
diff
changeset
|
46 |
val font_metrics: FontMetrics = gfx.getFontMetrics(font) |
1cb983bccb5b
more official graphics context with font metrics;
wenzelm
parents:
50475
diff
changeset
|
47 |
|
50475 | 48 |
val tooltip_font_size: Int = 10 |
49 |
||
50 |
||
51 |
/* rendering parameters */ |
|
52 |
||
58940 | 53 |
val gap_x = 20 |
50475 | 54 |
val pad_x = 8 |
55 |
val pad_y = 5 |
|
56 |
||
57 |
var arrow_heads = false |
|
58 |
||
59 |
object Colors |
|
60 |
{ |
|
61 |
private val filter_colors = List( |
|
59220 | 62 |
new Color(0xD9, 0xF2, 0xE2), // blue |
63 |
new Color(0xFF, 0xE7, 0xD8), // orange |
|
64 |
new Color(0xFF, 0xFF, 0xE5), // yellow |
|
65 |
new Color(0xDE, 0xCE, 0xFF), // lilac |
|
66 |
new Color(0xCC, 0xEB, 0xFF), // turquoise |
|
67 |
new Color(0xFF, 0xE5, 0xE5), // red |
|
68 |
new Color(0xE5, 0xE5, 0xD9) // green |
|
50475 | 69 |
) |
70 |
||
71 |
private var curr : Int = -1 |
|
59220 | 72 |
def next(): Color = |
50475 | 73 |
{ |
74 |
curr = (curr + 1) % filter_colors.length |
|
75 |
filter_colors(curr) |
|
76 |
} |
|
77 |
} |
|
78 |
||
50472
bad1a1ca61e1
separate instance of class Parameters for each Main_Panel -- avoid global program state;
wenzelm
parents:
50471
diff
changeset
|
79 |
|
50465 | 80 |
object Coordinates |
81 |
{ |
|
50470 | 82 |
private var layout = Layout_Pendulum.empty_layout |
50465 | 83 |
|
84 |
def apply(k: String): (Double, Double) = |
|
50470 | 85 |
layout.nodes.get(k) match { |
50465 | 86 |
case Some(c) => c |
87 |
case None => (0, 0) |
|
88 |
} |
|
89 |
||
90 |
def apply(e: (String, String)): List[(Double, Double)] = |
|
50470 | 91 |
layout.dummies.get(e) match { |
50465 | 92 |
case Some(ds) => ds |
93 |
case None => Nil |
|
94 |
} |
|
95 |
||
96 |
def reposition(k: String, to: (Double, Double)) |
|
97 |
{ |
|
50470 | 98 |
layout = layout.copy(nodes = layout.nodes + (k -> to)) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
99 |
} |
50465 | 100 |
|
101 |
def reposition(d: ((String, String), Int), to: (Double, Double)) |
|
102 |
{ |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
103 |
val (e, index) = d |
50470 | 104 |
layout.dummies.get(e) match { |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
105 |
case None => |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
106 |
case Some(ds) => |
50470 | 107 |
layout = layout.copy(dummies = |
108 |
layout.dummies + (e -> |
|
109 |
(ds.zipWithIndex :\ List.empty[(Double, Double)]) { |
|
110 |
case ((t, i), n) => if (index == i) to :: n else t :: n |
|
111 |
})) |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
112 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
113 |
} |
50465 | 114 |
|
115 |
def translate(k: String, by: (Double, Double)) |
|
116 |
{ |
|
117 |
val ((x, y), (dx, dy)) = (Coordinates(k), by) |
|
118 |
reposition(k, (x + dx, y + dy)) |
|
119 |
} |
|
120 |
||
121 |
def translate(d: ((String, String), Int), by: (Double, Double)) |
|
122 |
{ |
|
123 |
val ((e, i),(dx, dy)) = (d, by) |
|
124 |
val (x, y) = apply(e)(i) |
|
125 |
reposition(d, (x + dx, y + dy)) |
|
126 |
} |
|
127 |
||
50470 | 128 |
def update_layout() |
50465 | 129 |
{ |
50474
6ee044e2d1a7
initial layout coordinates more like old browser;
wenzelm
parents:
50472
diff
changeset
|
130 |
layout = |
59228
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
131 |
if (model.current_graph.is_empty) Layout_Pendulum.empty_layout |
50474
6ee044e2d1a7
initial layout coordinates more like old browser;
wenzelm
parents:
50472
diff
changeset
|
132 |
else { |
6ee044e2d1a7
initial layout coordinates more like old browser;
wenzelm
parents:
50472
diff
changeset
|
133 |
val max_width = |
59228
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
134 |
model.current_graph.iterator.map({ case (_, (info, _)) => |
50474
6ee044e2d1a7
initial layout coordinates more like old browser;
wenzelm
parents:
50472
diff
changeset
|
135 |
font_metrics.stringWidth(info.name).toDouble }).max |
50476
1cb983bccb5b
more official graphics context with font metrics;
wenzelm
parents:
50475
diff
changeset
|
136 |
val box_distance = max_width + pad_x + gap_x |
50474
6ee044e2d1a7
initial layout coordinates more like old browser;
wenzelm
parents:
50472
diff
changeset
|
137 |
def box_height(n: Int): Double = |
50475 | 138 |
((font_metrics.getAscent + font_metrics.getDescent + pad_y) * (5 max n)).toDouble |
59228
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
139 |
Layout_Pendulum(model.current_graph, box_distance, box_height) |
50474
6ee044e2d1a7
initial layout coordinates more like old browser;
wenzelm
parents:
50472
diff
changeset
|
140 |
} |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
141 |
} |
50465 | 142 |
|
143 |
def bounds(): (Double, Double, Double, Double) = |
|
56372
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
wenzelm
parents:
55618
diff
changeset
|
144 |
model.visible_nodes_iterator.toList match { |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
145 |
case Nil => (0, 0, 0, 0) |
50465 | 146 |
case nodes => |
147 |
val X: (String => Double) = (n => apply(n)._1) |
|
148 |
val Y: (String => Double) = (n => apply(n)._2) |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
149 |
|
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
150 |
(X(nodes.minBy(X)), Y(nodes.minBy(Y)), |
50465 | 151 |
X(nodes.maxBy(X)), Y(nodes.maxBy(Y))) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
152 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
153 |
} |
50465 | 154 |
|
50464 | 155 |
object Drawer |
156 |
{ |
|
50465 | 157 |
def apply(g: Graphics2D, n: Option[String]) |
158 |
{ |
|
159 |
n match { |
|
160 |
case None => |
|
161 |
case Some(_) => Shapes.Growing_Node.paint(g, visualizer, n) |
|
162 |
} |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
163 |
} |
50465 | 164 |
|
165 |
def apply(g: Graphics2D, e: (String, String), head: Boolean, dummies: Boolean) |
|
166 |
{ |
|
167 |
Shapes.Cardinal_Spline_Edge.paint(g, visualizer, e, head, dummies) |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
168 |
} |
50465 | 169 |
|
50464 | 170 |
def paint_all_visible(g: Graphics2D, dummies: Boolean) |
171 |
{ |
|
50465 | 172 |
g.setFont(font) |
50464 | 173 |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
174 |
g.setRenderingHints(rendering_hints) |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
175 |
|
56372
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
wenzelm
parents:
55618
diff
changeset
|
176 |
model.visible_edges_iterator.foreach(e => { |
50475 | 177 |
apply(g, e, arrow_heads, dummies) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
178 |
}) |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
179 |
|
56372
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
wenzelm
parents:
55618
diff
changeset
|
180 |
model.visible_nodes_iterator.foreach(l => { |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
181 |
apply(g, Some(l)) |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
182 |
}) |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
183 |
} |
50465 | 184 |
|
185 |
def shape(g: Graphics2D, n: Option[String]): Shape = |
|
186 |
n match { |
|
187 |
case None => Shapes.Dummy.shape(g, visualizer, None) |
|
188 |
case Some(_) => Shapes.Growing_Node.shape(g, visualizer, n) |
|
189 |
} |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
190 |
} |
50465 | 191 |
|
192 |
object Selection |
|
193 |
{ |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
194 |
private var selected: List[String] = Nil |
50465 | 195 |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
196 |
def apply() = selected |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
197 |
def apply(s: String) = selected.contains(s) |
50465 | 198 |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
199 |
def add(s: String) { selected = s :: selected } |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
200 |
def set(ss: List[String]) { selected = ss } |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
201 |
def clear() { selected = Nil } |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
202 |
} |
50465 | 203 |
|
59220 | 204 |
sealed case class Node_Color(border: Color, background: Color, foreground: Color) |
50464 | 205 |
|
59220 | 206 |
def node_color(l: Option[String]): Node_Color = |
207 |
l match { |
|
59228
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
208 |
case None => |
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
209 |
Node_Color(foreground1_color, background_color, foreground_color) |
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
210 |
case Some(c) if Selection(c) => |
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
211 |
Node_Color(foreground_color, selection_color, foreground_color) |
59220 | 212 |
case Some(c) => |
59228
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
213 |
Node_Color(foreground_color, model.colors.getOrElse(c, background_color), foreground_color) |
59220 | 214 |
} |
215 |
||
59228
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
216 |
def edge_color(e: (String, String)): Color = foreground_color |
50465 | 217 |
|
218 |
object Caption |
|
50464 | 219 |
{ |
59228
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59220
diff
changeset
|
220 |
def apply(key: String) = model.complete_graph.get_node(key).name |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
221 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
222 |
} |