src/Tools/Graphview/graph_panel.scala
author wenzelm
Sat, 17 Jan 2015 23:12:02 +0100
changeset 59386 32b162d1d9b5
parent 59305 b5e33012180e
child 59392 02bacfc31446
permissions -rw-r--r--
proper refresh after apply_layout, in order to update preferred size, which is required for scroll pane;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
59202
711c2446dc9d clarified source location;
wenzelm
parents: 57044
diff changeset
     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
e411afcfaa29 tuned headers;
wenzelm
parents: 59239
diff changeset
     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
995162143ef4 tuned imports;
wenzelm
parents: 51616
diff changeset
    10
49558
af7b652180d5 minimal component and build setup for graphview;
wenzelm
parents: 49557
diff changeset
    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
59241
541b95e94dc7 clarified bounding box, similar to old graph browser;
wenzelm
parents: 59240
diff changeset
    13
import java.awt.{Dimension, Graphics2D, Point}
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
d0edf67253d3 tuned imports;
wenzelm
parents: 59219
diff changeset
    15
import javax.swing.{JScrollPane, JComponent, SwingUtilities}
49729
f53a8f73b40f more basic tooltips;
wenzelm
parents: 49569
diff changeset
    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
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
    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
876a81f5788b tuned signature;
wenzelm
parents: 59231
diff changeset
    21
class Graph_Panel(val visualizer: Visualizer) extends ScrollPane
49729
f53a8f73b40f more basic tooltips;
wenzelm
parents: 49569
diff changeset
    22
{
49731
wenzelm
parents: 49730
diff changeset
    23
  panel =>
49729
f53a8f73b40f more basic tooltips;
wenzelm
parents: 49569
diff changeset
    24
59243
21ef04bd4e17 recovered tooltip from 6e77ddb1e3fb: non-null default is required as prerequisite;
wenzelm
parents: 59241
diff changeset
    25
  tooltip = ""
21ef04bd4e17 recovered tooltip from 6e77ddb1e3fb: non-null default is required as prerequisite;
wenzelm
parents: 59241
diff changeset
    26
49729
f53a8f73b40f more basic tooltips;
wenzelm
parents: 49569
diff changeset
    27
  override lazy val peer: JScrollPane = new JScrollPane with SuperMixin {
49730
e0d98ff3c0db use Pretty_Tooltip for Graphview_Panel;
wenzelm
parents: 49729
diff changeset
    28
    override def getToolTipText(event: java.awt.event.MouseEvent): String =
59305
b5e33012180e tuned signature;
wenzelm
parents: 59303
diff changeset
    29
      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
    30
        case Some(node) =>
59259
399506ee38a5 clarified static full_graph vs. dynamic visible_graph;
wenzelm
parents: 59255
diff changeset
    31
          visualizer.model.full_graph.get_node(node) match {
49732
ad362eec19c3 more direct tooltip content;
wenzelm
parents: 49731
diff changeset
    32
            case Nil => null
59233
876a81f5788b tuned signature;
wenzelm
parents: 59231
diff changeset
    33
            case content => visualizer.make_tooltip(panel.peer, event.getX, event.getY, content)
49732
ad362eec19c3 more direct tooltip content;
wenzelm
parents: 49731
diff changeset
    34
          }
ad362eec19c3 more direct tooltip content;
wenzelm
parents: 49731
diff changeset
    35
        case None => null
ad362eec19c3 more direct tooltip content;
wenzelm
parents: 49731
diff changeset
    36
      }
49729
f53a8f73b40f more basic tooltips;
wenzelm
parents: 49569
diff changeset
    37
  }
f53a8f73b40f more basic tooltips;
wenzelm
parents: 49569
diff changeset
    38
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    39
  focusable = true
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    40
  requestFocus()
50470
wenzelm
parents: 50469
diff changeset
    41
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    42
  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
    43
  verticalScrollBarPolicy = ScrollPane.BarPolicy.Always
49729
f53a8f73b40f more basic tooltips;
wenzelm
parents: 49569
diff changeset
    44
59237
ac135eff1ffb clarified mouse wheel: conventional scrolling, not scaling;
wenzelm
parents: 59234
diff changeset
    45
  peer.getVerticalScrollBar.setUnitIncrement(10)
ac135eff1ffb clarified mouse wheel: conventional scrolling, not scaling;
wenzelm
parents: 59234
diff changeset
    46
49735
30e2f3f1c623 more precise repaint and revalidate -- the latter is important to keep in sync with content update;
wenzelm
parents: 49733
diff changeset
    47
  def refresh()
30e2f3f1c623 more precise repaint and revalidate -- the latter is important to keep in sync with content update;
wenzelm
parents: 49733
diff changeset
    48
  {
50491
0faaa279faee improved coupling of zoom_box and scale;
wenzelm
parents: 50478
diff changeset
    49
    if (paint_panel != null) {
0faaa279faee improved coupling of zoom_box and scale;
wenzelm
parents: 50478
diff changeset
    50
      paint_panel.set_preferred_size()
0faaa279faee improved coupling of zoom_box and scale;
wenzelm
parents: 50478
diff changeset
    51
      paint_panel.repaint()
0faaa279faee improved coupling of zoom_box and scale;
wenzelm
parents: 50478
diff changeset
    52
    }
49735
30e2f3f1c623 more precise repaint and revalidate -- the latter is important to keep in sync with content update;
wenzelm
parents: 49733
diff changeset
    53
  }
30e2f3f1c623 more precise repaint and revalidate -- the latter is important to keep in sync with content update;
wenzelm
parents: 49733
diff changeset
    54
59386
32b162d1d9b5 proper refresh after apply_layout, in order to update preferred size, which is required for scroll pane;
wenzelm
parents: 59305
diff changeset
    55
  def fit_to_window()
32b162d1d9b5 proper refresh after apply_layout, in order to update preferred size, which is required for scroll pane;
wenzelm
parents: 59305
diff changeset
    56
  {
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    57
    Transform.fit_to_window()
49735
30e2f3f1c623 more precise repaint and revalidate -- the latter is important to keep in sync with content update;
wenzelm
parents: 49733
diff changeset
    58
    refresh()
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    59
  }
50470
wenzelm
parents: 50469
diff changeset
    60
57044
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56372
diff changeset
    61
  val zoom = new GUI.Zoom_Box { def changed = rescale(0.01 * factor) }
50491
0faaa279faee improved coupling of zoom_box and scale;
wenzelm
parents: 50478
diff changeset
    62
50478
ccfdd1f6cf10 added explicit zoom box;
wenzelm
parents: 50477
diff changeset
    63
  def rescale(s: Double)
ccfdd1f6cf10 added explicit zoom box;
wenzelm
parents: 50477
diff changeset
    64
  {
ccfdd1f6cf10 added explicit zoom box;
wenzelm
parents: 50477
diff changeset
    65
    Transform.scale = s
59255
db265648139c clarified fit_to_window: floor scale within window bounds;
wenzelm
parents: 59253
diff changeset
    66
    if (zoom != null) zoom.set_item((Transform.scale_discrete * 100).floor.toInt)
50478
ccfdd1f6cf10 added explicit zoom box;
wenzelm
parents: 50477
diff changeset
    67
    refresh()
ccfdd1f6cf10 added explicit zoom box;
wenzelm
parents: 50477
diff changeset
    68
  }
ccfdd1f6cf10 added explicit zoom box;
wenzelm
parents: 50477
diff changeset
    69
59253
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
    70
  def apply_layout()
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
    71
  {
59302
4d985afc0565 explict layout graph structure, with dummies and coordinates;
wenzelm
parents: 59294
diff changeset
    72
    visualizer.update_layout()
59386
32b162d1d9b5 proper refresh after apply_layout, in order to update preferred size, which is required for scroll pane;
wenzelm
parents: 59305
diff changeset
    73
    refresh()
59253
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
    74
  }
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    75
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
    76
  private class Paint_Panel extends Panel
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
    77
  {
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
    78
    def set_preferred_size()
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
    79
    {
59302
4d985afc0565 explict layout graph structure, with dummies and coordinates;
wenzelm
parents: 59294
diff changeset
    80
      val box = visualizer.bounding_box()
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
    81
      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
    82
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
    83
      preferredSize =
59241
541b95e94dc7 clarified bounding box, similar to old graph browser;
wenzelm
parents: 59240
diff changeset
    84
        new Dimension((box.width * s).ceil.toInt, (box.height * s).ceil.toInt)
50470
wenzelm
parents: 50469
diff changeset
    85
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
    86
      revalidate()
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
    87
    }
50470
wenzelm
parents: 50469
diff changeset
    88
59250
wenzelm
parents: 59245
diff changeset
    89
    override def paint(gfx: Graphics2D)
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
    90
    {
59250
wenzelm
parents: 59245
diff changeset
    91
      super.paintComponent(gfx)
wenzelm
parents: 59245
diff changeset
    92
      gfx.setColor(visualizer.background_color)
wenzelm
parents: 59245
diff changeset
    93
      gfx.fillRect(0, 0, peer.getWidth, peer.getHeight)
wenzelm
parents: 59245
diff changeset
    94
      gfx.transform(Transform())
50470
wenzelm
parents: 50469
diff changeset
    95
59294
126293918a37 clarified visualizer parameters;
wenzelm
parents: 59291
diff changeset
    96
      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
    97
    }
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    98
  }
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
    99
  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
   100
  contents = paint_panel
50470
wenzelm
parents: 50469
diff changeset
   101
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   102
  listenTo(mouse.moves)
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   103
  listenTo(mouse.clicks)
59253
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   104
  reactions += Mouse_Interaction.react
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   105
  reactions +=
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   106
  {
59253
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   107
    case MousePressed(_, _, _, _, _) => repaint()
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   108
    case MouseDragged(_, _, _) => repaint()
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   109
    case MouseClicked(_, _, _, _, _) => repaint()
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   110
  }
49733
38a68e6593be prefer synchronous Mutator_Event.Bus on Swing_Thread;
wenzelm
parents: 49732
diff changeset
   111
38a68e6593be prefer synchronous Mutator_Event.Bus on Swing_Thread;
wenzelm
parents: 49732
diff changeset
   112
  visualizer.model.Colors.events += { case _ => repaint() }
38a68e6593be prefer synchronous Mutator_Event.Bus on Swing_Thread;
wenzelm
parents: 49732
diff changeset
   113
  visualizer.model.Mutators.events += { case _ => repaint() }
50470
wenzelm
parents: 50469
diff changeset
   114
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   115
  apply_layout()
50491
0faaa279faee improved coupling of zoom_box and scale;
wenzelm
parents: 50478
diff changeset
   116
  rescale(1.0)
50470
wenzelm
parents: 50469
diff changeset
   117
50469
wenzelm
parents: 50468
diff changeset
   118
  private object Transform
wenzelm
parents: 50468
diff changeset
   119
  {
50474
6ee044e2d1a7 initial layout coordinates more like old browser;
wenzelm
parents: 50470
diff changeset
   120
    private var _scale: Double = 1.0
50477
ffa18243a4e3 some attempts at more discrete scale factor;
wenzelm
parents: 50476
diff changeset
   121
    def scale: Double = _scale
ffa18243a4e3 some attempts at more discrete scale factor;
wenzelm
parents: 50476
diff changeset
   122
    def scale_=(s: Double)
50468
7a2a4b84c5ee tuned min/max;
wenzelm
parents: 50465
diff changeset
   123
    {
59241
541b95e94dc7 clarified bounding box, similar to old graph browser;
wenzelm
parents: 59240
diff changeset
   124
      _scale = (s min 10.0) max 0.1
50468
7a2a4b84c5ee tuned min/max;
wenzelm
parents: 50465
diff changeset
   125
    }
59255
db265648139c clarified fit_to_window: floor scale within window bounds;
wenzelm
parents: 59253
diff changeset
   126
50477
ffa18243a4e3 some attempts at more discrete scale factor;
wenzelm
parents: 50476
diff changeset
   127
    def scale_discrete: Double =
59286
ac74eedb910a GUI.imitate_font: more explicit result size, e.g. relevant for caching;
wenzelm
parents: 59262
diff changeset
   128
    {
59290
569a8109eeb2 separate module Metrics;
wenzelm
parents: 59287
diff changeset
   129
      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
   130
      (scale * font_height).floor / font_height
ac74eedb910a GUI.imitate_font: more explicit result size, e.g. relevant for caching;
wenzelm
parents: 59262
diff changeset
   131
    }
50470
wenzelm
parents: 50469
diff changeset
   132
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   133
    def apply() =
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   134
    {
59302
4d985afc0565 explict layout graph structure, with dummies and coordinates;
wenzelm
parents: 59294
diff changeset
   135
      val box = visualizer.bounding_box()
50477
ffa18243a4e3 some attempts at more discrete scale factor;
wenzelm
parents: 50476
diff changeset
   136
      val at = AffineTransform.getScaleInstance(scale_discrete, scale_discrete)
59241
541b95e94dc7 clarified bounding box, similar to old graph browser;
wenzelm
parents: 59240
diff changeset
   137
      at.translate(- box.x, - box.y)
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   138
      at
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   139
    }
50470
wenzelm
parents: 50469
diff changeset
   140
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   141
    def fit_to_window()
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   142
    {
59290
569a8109eeb2 separate module Metrics;
wenzelm
parents: 59287
diff changeset
   143
      if (visualizer.visible_graph.is_empty)
50491
0faaa279faee improved coupling of zoom_box and scale;
wenzelm
parents: 50478
diff changeset
   144
        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
   145
      else {
59302
4d985afc0565 explict layout graph structure, with dummies and coordinates;
wenzelm
parents: 59294
diff changeset
   146
        val box = visualizer.bounding_box()
59241
541b95e94dc7 clarified bounding box, similar to old graph browser;
wenzelm
parents: 59240
diff changeset
   147
        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
   148
      }
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   149
    }
50470
wenzelm
parents: 50469
diff changeset
   150
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   151
    def pane_to_graph_coordinates(at: Point2D): Point2D =
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   152
    {
50477
ffa18243a4e3 some attempts at more discrete scale factor;
wenzelm
parents: 50476
diff changeset
   153
      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
   154
      val p = Transform().inverseTransform(peer.getViewport.getViewPosition, null)
50470
wenzelm
parents: 50469
diff changeset
   155
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   156
      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
   157
      p
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   158
    }
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   159
  }
50470
wenzelm
parents: 50469
diff changeset
   160
59253
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   161
  object Mouse_Interaction
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   162
  {
59302
4d985afc0565 explict layout graph structure, with dummies and coordinates;
wenzelm
parents: 59294
diff changeset
   163
    private var draginfo: (Point, List[Graph_Display.Node], List[Layout.Dummy]) = null
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   164
59253
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   165
    val react: PartialFunction[Event, Unit] =
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   166
    {
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   167
      case MousePressed(_, p, _, _, _) => pressed(p)
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   168
      case MouseDragged(_, to, _) =>
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   169
        drag(draginfo, to)
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   170
        val (_, p, d) = draginfo
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   171
        draginfo = (to, p, d)
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   172
      case e @ MouseClicked(_, p, m, n, _) => click(p, m, n, e)
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   173
    }
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   174
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   175
    def pressed(at: Point)
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   176
    {
59253
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   177
      val c = Transform.pane_to_graph_coordinates(at)
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   178
      val l =
59305
b5e33012180e tuned signature;
wenzelm
parents: 59303
diff changeset
   179
        visualizer.find_node(c) match {
59253
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   180
          case Some(node) =>
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   181
            if (visualizer.Selection.contains(node)) visualizer.Selection.get()
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   182
            else List(node)
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   183
          case None => Nil
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   184
        }
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   185
      val d =
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   186
        l match {
59305
b5e33012180e tuned signature;
wenzelm
parents: 59303
diff changeset
   187
          case Nil => visualizer.find_dummy(c).toList
59253
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   188
          case _ => Nil
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   189
        }
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   190
      draginfo = (at, l, d)
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   191
    }
50470
wenzelm
parents: 50469
diff changeset
   192
59253
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   193
    def click(at: Point, m: Key.Modifiers, clicks: Int, e: MouseEvent)
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   194
    {
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   195
      val c = Transform.pane_to_graph_coordinates(at)
50470
wenzelm
parents: 50469
diff changeset
   196
59253
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   197
      def left_click()
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   198
      {
59305
b5e33012180e tuned signature;
wenzelm
parents: 59303
diff changeset
   199
        (visualizer.find_node(c), m) match {
59253
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   200
          case (Some(node), Key.Modifier.Control) => visualizer.Selection.add(node)
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   201
          case (None, Key.Modifier.Control) =>
50470
wenzelm
parents: 50469
diff changeset
   202
59253
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   203
          case (Some(node), Key.Modifier.Shift) => visualizer.Selection.add(node)
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   204
          case (None, Key.Modifier.Shift) =>
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   205
59253
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   206
          case (Some(node), _) =>
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   207
            visualizer.Selection.clear()
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   208
            visualizer.Selection.add(node)
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   209
          case (None, _) =>
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   210
            visualizer.Selection.clear()
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   211
        }
50470
wenzelm
parents: 50469
diff changeset
   212
      }
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   213
59253
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   214
      def right_click()
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   215
      {
59305
b5e33012180e tuned signature;
wenzelm
parents: 59303
diff changeset
   216
        val menu = Popups(panel, visualizer.find_node(c), visualizer.Selection.get())
59253
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   217
        menu.show(panel.peer, at.x, at.y)
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   218
      }
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   219
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   220
      if (clicks < 2) {
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   221
        if (SwingUtilities.isRightMouseButton(e.peer)) right_click()
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   222
        else left_click()
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   223
      }
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   224
    }
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   225
59302
4d985afc0565 explict layout graph structure, with dummies and coordinates;
wenzelm
parents: 59294
diff changeset
   226
    def drag(info: (Point, List[Graph_Display.Node], List[Layout.Dummy]), to: Point)
59253
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   227
    {
59262
5cd92c743958 explicit Layout.Point;
wenzelm
parents: 59259
diff changeset
   228
      val (from, p, d) = info
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   229
59253
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   230
      val s = Transform.scale_discrete
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   231
      val (dx, dy) = (to.x - from.x, to.y - from.y)
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   232
      (p, d) match {
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   233
        case (Nil, Nil) =>
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   234
          val r = panel.peer.getViewport.getViewRect
59302
4d985afc0565 explict layout graph structure, with dummies and coordinates;
wenzelm
parents: 59294
diff changeset
   235
          r.translate(- dx, - dy)
59253
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   236
          paint_panel.peer.scrollRectToVisible(r)
50470
wenzelm
parents: 50469
diff changeset
   237
59253
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   238
        case (Nil, ds) =>
59302
4d985afc0565 explict layout graph structure, with dummies and coordinates;
wenzelm
parents: 59294
diff changeset
   239
          ds.foreach(d => visualizer.translate_vertex(d, dx / s, dy / s))
59253
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   240
9448f4fc95e0 apply_layout: proper repaint;
wenzelm
parents: 59250
diff changeset
   241
        case (ls, _) =>
59302
4d985afc0565 explict layout graph structure, with dummies and coordinates;
wenzelm
parents: 59294
diff changeset
   242
          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
   243
      }
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   244
    }
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   245
  }
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   246
}