src/Tools/Graphview/graph_panel.scala
author wenzelm
Thu, 01 Jan 2015 14:37:25 +0100
changeset 59225 d0edf67253d3
parent 59219 6e77ddb1e3fb
child 59226 7b8c50be8d42
permissions -rw-r--r--
tuned imports;
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
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
     3
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
     4
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
     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
55618
995162143ef4 tuned imports;
wenzelm
parents: 51616
diff changeset
     9
49558
af7b652180d5 minimal component and build setup for graphview;
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
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    12
import java.awt.{Dimension, Graphics2D, Point, Rectangle}
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    13
import java.awt.geom.{AffineTransform, Point2D}
49732
ad362eec19c3 more direct tooltip content;
wenzelm
parents: 49731
diff changeset
    14
import java.awt.image.BufferedImage
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}
59225
d0edf67253d3 tuned imports;
wenzelm
parents: 59219
diff changeset
    18
import scala.swing.event.{Event, Key, KeyTyped, MousePressed, MouseDragged,
d0edf67253d3 tuned imports;
wenzelm
parents: 59219
diff changeset
    19
  MouseMoved, MouseClicked, MouseWheelMoved, MouseEvent}
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    20
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    21
49731
wenzelm
parents: 49730
diff changeset
    22
class Graph_Panel(
wenzelm
parents: 49730
diff changeset
    23
    val visualizer: Visualizer,
wenzelm
parents: 49730
diff changeset
    24
    make_tooltip: (JComponent, Int, Int, XML.Body) => String)
49729
f53a8f73b40f more basic tooltips;
wenzelm
parents: 49569
diff changeset
    25
  extends ScrollPane
f53a8f73b40f more basic tooltips;
wenzelm
parents: 49569
diff changeset
    26
{
49731
wenzelm
parents: 49730
diff changeset
    27
  panel =>
49729
f53a8f73b40f more basic tooltips;
wenzelm
parents: 49569
diff changeset
    28
f53a8f73b40f more basic tooltips;
wenzelm
parents: 49569
diff changeset
    29
  override lazy val peer: JScrollPane = new JScrollPane with SuperMixin {
49730
e0d98ff3c0db use Pretty_Tooltip for Graphview_Panel;
wenzelm
parents: 49729
diff changeset
    30
    override def getToolTipText(event: java.awt.event.MouseEvent): String =
49732
ad362eec19c3 more direct tooltip content;
wenzelm
parents: 49731
diff changeset
    31
      node(Transform.pane_to_graph_coordinates(event.getPoint)) match {
ad362eec19c3 more direct tooltip content;
wenzelm
parents: 49731
diff changeset
    32
        case Some(name) =>
ad362eec19c3 more direct tooltip content;
wenzelm
parents: 49731
diff changeset
    33
          visualizer.model.complete.get_node(name).content match {
ad362eec19c3 more direct tooltip content;
wenzelm
parents: 49731
diff changeset
    34
            case Nil => null
ad362eec19c3 more direct tooltip content;
wenzelm
parents: 49731
diff changeset
    35
            case content => make_tooltip(panel.peer, event.getX, event.getY, content)
ad362eec19c3 more direct tooltip content;
wenzelm
parents: 49731
diff changeset
    36
          }
ad362eec19c3 more direct tooltip content;
wenzelm
parents: 49731
diff changeset
    37
        case None => null
ad362eec19c3 more direct tooltip content;
wenzelm
parents: 49731
diff changeset
    38
      }
49729
f53a8f73b40f more basic tooltips;
wenzelm
parents: 49569
diff changeset
    39
  }
f53a8f73b40f more basic tooltips;
wenzelm
parents: 49569
diff changeset
    40
f53a8f73b40f more basic tooltips;
wenzelm
parents: 49569
diff changeset
    41
  peer.setWheelScrollingEnabled(false)
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    42
  focusable = true
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    43
  requestFocus()
50470
wenzelm
parents: 50469
diff changeset
    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
f53a8f73b40f more basic tooltips;
wenzelm
parents: 49569
diff changeset
    47
49732
ad362eec19c3 more direct tooltip content;
wenzelm
parents: 49731
diff changeset
    48
  def node(at: Point2D): Option[String] =
56372
fadb0fef09d7 more explicit iterator terminology, in accordance to Scala 2.8 library;
wenzelm
parents: 55618
diff changeset
    49
    visualizer.model.visible_nodes_iterator
50476
1cb983bccb5b more official graphics context with font metrics;
wenzelm
parents: 50474
diff changeset
    50
      .find(name => visualizer.Drawer.shape(visualizer.gfx, Some(name)).contains(at))
49732
ad362eec19c3 more direct tooltip content;
wenzelm
parents: 49731
diff changeset
    51
49735
30e2f3f1c623 more precise repaint and revalidate -- the latter is important to keep in sync with content update;
wenzelm
parents: 49733
diff changeset
    52
  def refresh()
30e2f3f1c623 more precise repaint and revalidate -- the latter is important to keep in sync with content update;
wenzelm
parents: 49733
diff changeset
    53
  {
50491
0faaa279faee improved coupling of zoom_box and scale;
wenzelm
parents: 50478
diff changeset
    54
    if (paint_panel != null) {
0faaa279faee improved coupling of zoom_box and scale;
wenzelm
parents: 50478
diff changeset
    55
      paint_panel.set_preferred_size()
0faaa279faee improved coupling of zoom_box and scale;
wenzelm
parents: 50478
diff changeset
    56
      paint_panel.repaint()
0faaa279faee improved coupling of zoom_box and scale;
wenzelm
parents: 50478
diff changeset
    57
    }
49735
30e2f3f1c623 more precise repaint and revalidate -- the latter is important to keep in sync with content update;
wenzelm
parents: 49733
diff changeset
    58
  }
30e2f3f1c623 more precise repaint and revalidate -- the latter is important to keep in sync with content update;
wenzelm
parents: 49733
diff changeset
    59
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    60
  def fit_to_window() = {
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    61
    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
    62
    refresh()
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    63
  }
50470
wenzelm
parents: 50469
diff changeset
    64
57044
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56372
diff changeset
    65
  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
    66
50478
ccfdd1f6cf10 added explicit zoom box;
wenzelm
parents: 50477
diff changeset
    67
  def rescale(s: Double)
ccfdd1f6cf10 added explicit zoom box;
wenzelm
parents: 50477
diff changeset
    68
  {
ccfdd1f6cf10 added explicit zoom box;
wenzelm
parents: 50477
diff changeset
    69
    Transform.scale = s
57044
042d6e58cb40 more uniform Font_Info.Zoom_Box;
wenzelm
parents: 56372
diff changeset
    70
    if (zoom != null) zoom.set_item((Transform.scale_discrete * 100).round.toInt)
50478
ccfdd1f6cf10 added explicit zoom box;
wenzelm
parents: 50477
diff changeset
    71
    refresh()
ccfdd1f6cf10 added explicit zoom box;
wenzelm
parents: 50477
diff changeset
    72
  }
ccfdd1f6cf10 added explicit zoom box;
wenzelm
parents: 50477
diff changeset
    73
50470
wenzelm
parents: 50469
diff changeset
    74
  def apply_layout() = visualizer.Coordinates.update_layout()
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
    {
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
    80
      val (minX, minY, maxX, maxY) = visualizer.Coordinates.bounds()
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
    81
      val s = Transform.scale_discrete
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
    82
      val (px, py) = Transform.padding
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    83
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
    84
      preferredSize =
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
    85
        new Dimension(
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
    86
          (math.abs(maxX - minX + px) * s).toInt,
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
    87
          (math.abs(maxY - minY + py) * s).toInt)
50470
wenzelm
parents: 50469
diff changeset
    88
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
    89
      revalidate()
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
    90
    }
50470
wenzelm
parents: 50469
diff changeset
    91
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
    92
    override def paint(g: Graphics2D)
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
    93
    {
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    94
      super.paintComponent(g)
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    95
      g.transform(Transform())
50470
wenzelm
parents: 50469
diff changeset
    96
49731
wenzelm
parents: 49730
diff changeset
    97
      visualizer.Drawer.paint_all_visible(g, true)
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    98
    }
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    99
  }
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
   100
  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
   101
  contents = paint_panel
50470
wenzelm
parents: 50469
diff changeset
   102
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   103
  listenTo(keys)
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   104
  listenTo(mouse.moves)
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   105
  listenTo(mouse.clicks)
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   106
  listenTo(mouse.wheel)
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   107
  reactions += Interaction.Mouse.react
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   108
  reactions += Interaction.Keyboard.react
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   109
  reactions +=
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   110
  {
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   111
    case KeyTyped(_, _, _, _) => repaint(); requestFocus()
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   112
    case MousePressed(_, _, _, _, _) => repaint(); requestFocus()
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   113
    case MouseDragged(_, _, _) => repaint(); requestFocus()
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   114
    case MouseWheelMoved(_, _, _, _) => repaint(); requestFocus()
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   115
    case MouseClicked(_, _, _, _, _) => repaint(); requestFocus()
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   116
    case MouseMoved(_, _, _) => repaint()
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   117
  }
49733
38a68e6593be prefer synchronous Mutator_Event.Bus on Swing_Thread;
wenzelm
parents: 49732
diff changeset
   118
38a68e6593be prefer synchronous Mutator_Event.Bus on Swing_Thread;
wenzelm
parents: 49732
diff changeset
   119
  visualizer.model.Colors.events += { case _ => repaint() }
38a68e6593be prefer synchronous Mutator_Event.Bus on Swing_Thread;
wenzelm
parents: 49732
diff changeset
   120
  visualizer.model.Mutators.events += { case _ => repaint() }
50470
wenzelm
parents: 50469
diff changeset
   121
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   122
  apply_layout()
50491
0faaa279faee improved coupling of zoom_box and scale;
wenzelm
parents: 50478
diff changeset
   123
  rescale(1.0)
50470
wenzelm
parents: 50469
diff changeset
   124
50469
wenzelm
parents: 50468
diff changeset
   125
  private object Transform
wenzelm
parents: 50468
diff changeset
   126
  {
50474
6ee044e2d1a7 initial layout coordinates more like old browser;
wenzelm
parents: 50470
diff changeset
   127
    val padding = (100, 40)
50470
wenzelm
parents: 50469
diff changeset
   128
50474
6ee044e2d1a7 initial layout coordinates more like old browser;
wenzelm
parents: 50470
diff changeset
   129
    private var _scale: Double = 1.0
50477
ffa18243a4e3 some attempts at more discrete scale factor;
wenzelm
parents: 50476
diff changeset
   130
    def scale: Double = _scale
ffa18243a4e3 some attempts at more discrete scale factor;
wenzelm
parents: 50476
diff changeset
   131
    def scale_=(s: Double)
50468
7a2a4b84c5ee tuned min/max;
wenzelm
parents: 50465
diff changeset
   132
    {
50474
6ee044e2d1a7 initial layout coordinates more like old browser;
wenzelm
parents: 50470
diff changeset
   133
      _scale = (s min 10) max 0.1
50468
7a2a4b84c5ee tuned min/max;
wenzelm
parents: 50465
diff changeset
   134
    }
50477
ffa18243a4e3 some attempts at more discrete scale factor;
wenzelm
parents: 50476
diff changeset
   135
    def scale_discrete: Double =
ffa18243a4e3 some attempts at more discrete scale factor;
wenzelm
parents: 50476
diff changeset
   136
      (_scale * visualizer.font_size).round.toDouble / visualizer.font_size
50470
wenzelm
parents: 50469
diff changeset
   137
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   138
    def apply() =
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   139
    {
49731
wenzelm
parents: 49730
diff changeset
   140
      val (minX, minY, _, _) = visualizer.Coordinates.bounds()
50470
wenzelm
parents: 50469
diff changeset
   141
50477
ffa18243a4e3 some attempts at more discrete scale factor;
wenzelm
parents: 50476
diff changeset
   142
      val at = AffineTransform.getScaleInstance(scale_discrete, scale_discrete)
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   143
      at.translate(-minX + padding._1 / 2, -minY + padding._2 / 2)
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   144
      at
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   145
    }
50470
wenzelm
parents: 50469
diff changeset
   146
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   147
    def fit_to_window()
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   148
    {
56372
fadb0fef09d7 more explicit iterator terminology, in accordance to Scala 2.8 library;
wenzelm
parents: 55618
diff changeset
   149
      if (visualizer.model.visible_nodes_iterator.isEmpty)
50491
0faaa279faee improved coupling of zoom_box and scale;
wenzelm
parents: 50478
diff changeset
   150
        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
   151
      else {
49731
wenzelm
parents: 49730
diff changeset
   152
        val (minX, minY, maxX, maxY) = visualizer.Coordinates.bounds()
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
        val (dx, dy) = (maxX - minX + padding._1, maxY - minY + padding._2)
49745
083accbfa77d more conventional Double constants;
wenzelm
parents: 49735
diff changeset
   155
        val (sx, sy) = (1.0 * size.width / dx, 1.0 * size.height / dy)
50491
0faaa279faee improved coupling of zoom_box and scale;
wenzelm
parents: 50478
diff changeset
   156
        rescale(sx min sy)
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   157
      }
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   158
    }
50470
wenzelm
parents: 50469
diff changeset
   159
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   160
    def pane_to_graph_coordinates(at: Point2D): Point2D =
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   161
    {
50477
ffa18243a4e3 some attempts at more discrete scale factor;
wenzelm
parents: 50476
diff changeset
   162
      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
   163
      val p = Transform().inverseTransform(peer.getViewport.getViewPosition, null)
50470
wenzelm
parents: 50469
diff changeset
   164
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   165
      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
   166
      p
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   167
    }
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   168
  }
50470
wenzelm
parents: 50469
diff changeset
   169
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   170
  object Interaction
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   171
  {
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   172
    object Keyboard
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   173
    {
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   174
      val react: PartialFunction[Event, Unit] =
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   175
      {
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   176
        case KeyTyped(_, c, m, _) => typed(c, m)
50470
wenzelm
parents: 50469
diff changeset
   177
      }
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   178
59225
d0edf67253d3 tuned imports;
wenzelm
parents: 59219
diff changeset
   179
      def typed(c: Char, m: Key.Modifiers) =
50470
wenzelm
parents: 50469
diff changeset
   180
        (c, m) match {
50491
0faaa279faee improved coupling of zoom_box and scale;
wenzelm
parents: 50478
diff changeset
   181
          case ('+', _) => rescale(Transform.scale * 5.0/4)
0faaa279faee improved coupling of zoom_box and scale;
wenzelm
parents: 50478
diff changeset
   182
          case ('-', _) => rescale(Transform.scale * 4.0/5)
50470
wenzelm
parents: 50469
diff changeset
   183
          case ('0', _) => Transform.fit_to_window()
wenzelm
parents: 50469
diff changeset
   184
          case ('1', _) => visualizer.Coordinates.update_layout()
wenzelm
parents: 50469
diff changeset
   185
          case ('2', _) => Transform.fit_to_window()
wenzelm
parents: 50469
diff changeset
   186
          case _ =>
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   187
        }
50470
wenzelm
parents: 50469
diff changeset
   188
    }
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   189
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   190
    object Mouse
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   191
    {
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   192
      type Dummy = ((String, String), Int)
50470
wenzelm
parents: 50469
diff changeset
   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 draginfo: (Point, Iterable[String], Iterable[Dummy]) = null
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   195
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   196
      val react: PartialFunction[Event, Unit] =
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   197
      {
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   198
        case MousePressed(_, p, _, _, _) => pressed(p)
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   199
        case MouseDragged(_, to, _) =>
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   200
          drag(draginfo, to)
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   201
          val (_, p, d) = draginfo
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   202
          draginfo = (to, p, d)
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   203
        case MouseWheelMoved(_, p, _, r) => wheel(r, p)
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   204
        case e @ MouseClicked(_, p, m, n, _) => click(p, m, n, e)
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   205
      }
50470
wenzelm
parents: 50469
diff changeset
   206
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   207
      def dummy(at: Point2D): Option[Dummy] =
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   208
        visualizer.model.visible_edges_iterator.map(
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   209
          i => visualizer.Coordinates(i).zipWithIndex.map((i, _))).flatten.find({
50470
wenzelm
parents: 50469
diff changeset
   210
            case (_, ((x, y), _)) =>
50476
1cb983bccb5b more official graphics context with font metrics;
wenzelm
parents: 50474
diff changeset
   211
              visualizer.Drawer.shape(visualizer.gfx, None).contains(at.getX() - x, at.getY() - y)
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   212
          }) match {
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   213
            case None => None
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   214
            case Some((name, (_, index))) => Some((name, index))
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   215
          }
50470
wenzelm
parents: 50469
diff changeset
   216
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   217
      def pressed(at: Point)
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   218
      {
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   219
        val c = Transform.pane_to_graph_coordinates(at)
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   220
        val l = node(c) match {
49731
wenzelm
parents: 49730
diff changeset
   221
          case Some(l) =>
wenzelm
parents: 49730
diff changeset
   222
            if (visualizer.Selection(l)) visualizer.Selection() else List(l)
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   223
          case None => Nil
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   224
        }
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   225
        val d =
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   226
          l match {
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   227
            case Nil =>
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   228
              dummy(c) match {
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   229
                case Some(d) => List(d)
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   230
                case None => Nil
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   231
              }
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   232
            case _ => Nil
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   233
          }
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   234
        draginfo = (at, l, d)
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   235
      }
50470
wenzelm
parents: 50469
diff changeset
   236
59225
d0edf67253d3 tuned imports;
wenzelm
parents: 59219
diff changeset
   237
      def click(at: Point, m: Key.Modifiers, clicks: Int, e: MouseEvent)
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   238
      {
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   239
        val c = Transform.pane_to_graph_coordinates(at)
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   240
        val p = node(c)
50470
wenzelm
parents: 50469
diff changeset
   241
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   242
        def left_click()
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   243
        {
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   244
          (p, m) match {
59225
d0edf67253d3 tuned imports;
wenzelm
parents: 59219
diff changeset
   245
            case (Some(l), Key.Modifier.Control) => visualizer.Selection.add(l)
d0edf67253d3 tuned imports;
wenzelm
parents: 59219
diff changeset
   246
            case (None, Key.Modifier.Control) =>
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   247
59225
d0edf67253d3 tuned imports;
wenzelm
parents: 59219
diff changeset
   248
            case (Some(l), Key.Modifier.Shift) => visualizer.Selection.add(l)
d0edf67253d3 tuned imports;
wenzelm
parents: 59219
diff changeset
   249
            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
   250
49731
wenzelm
parents: 49730
diff changeset
   251
            case (Some(l), _) => visualizer.Selection.set(List(l))
wenzelm
parents: 49730
diff changeset
   252
            case (None, _) => visualizer.Selection.clear
50470
wenzelm
parents: 50469
diff changeset
   253
          }
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   254
        }
50470
wenzelm
parents: 50469
diff changeset
   255
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   256
        def right_click()
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   257
        {
49731
wenzelm
parents: 49730
diff changeset
   258
          val menu = Popups(panel, p, visualizer.Selection())
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   259
          menu.show(panel.peer, at.x, at.y)
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   260
        }
50470
wenzelm
parents: 50469
diff changeset
   261
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   262
        if (clicks < 2) {
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   263
          if (SwingUtilities.isRightMouseButton(e.peer)) right_click()
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   264
          else left_click()
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   265
        }
50470
wenzelm
parents: 50469
diff changeset
   266
      }
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   267
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   268
      def drag(draginfo: (Point, Iterable[String], Iterable[Dummy]), to: Point)
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   269
      {
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   270
        val (from, p, d) = draginfo
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   271
50477
ffa18243a4e3 some attempts at more discrete scale factor;
wenzelm
parents: 50476
diff changeset
   272
        val s = Transform.scale_discrete
50470
wenzelm
parents: 50469
diff changeset
   273
        val (dx, dy) = (to.x - from.x, to.y - from.y)
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   274
        (p, d) match {
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   275
          case (Nil, Nil) =>
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   276
            val r = panel.peer.getViewport.getViewRect
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   277
            r.translate(-dx, -dy)
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   278
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   279
            paint_panel.peer.scrollRectToVisible(r)
50470
wenzelm
parents: 50469
diff changeset
   280
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   281
          case (Nil, ds) =>
49731
wenzelm
parents: 49730
diff changeset
   282
            ds.foreach(d => visualizer.Coordinates.translate(d, (dx / s, dy / s)))
50470
wenzelm
parents: 50469
diff changeset
   283
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   284
          case (ls, _) =>
49731
wenzelm
parents: 49730
diff changeset
   285
            ls.foreach(l => visualizer.Coordinates.translate(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
   286
        }
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   287
      }
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   288
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   289
      def wheel(rotation: Int, at: Point)
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   290
      {
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   291
        val at2 = Transform.pane_to_graph_coordinates(at)
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   292
        // > 0 -> up
50491
0faaa279faee improved coupling of zoom_box and scale;
wenzelm
parents: 50478
diff changeset
   293
        rescale(Transform.scale * (if (rotation > 0) 4.0/5 else 5.0/4))
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   294
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   295
        // move mouseposition to center
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   296
        Transform().transform(at2, at2)
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   297
        val r = panel.peer.getViewport.getViewRect
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   298
        val (width, height) = (r.getWidth, r.getHeight)
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   299
        paint_panel.peer.scrollRectToVisible(
59218
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   300
          new Rectangle(
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   301
            (at2.getX() - width / 2).toInt,
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   302
            (at2.getY() - height / 2).toInt,
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   303
            width.toInt,
eadd82d440b0 tuned whitespace;
wenzelm
parents: 59202
diff changeset
   304
            height.toInt))
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   305
      }
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   306
    }
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   307
  }
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   308
}