src/Tools/jEdit/src/graphview_dockable.scala
author wenzelm
Mon, 08 Oct 2012 20:39:57 +0200
changeset 49735 30e2f3f1c623
parent 49730 e0d98ff3c0db
child 50117 32755e357a51
permissions -rw-r--r--
more precise repaint and revalidate -- the latter is important to keep in sync with content update;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49570
2265456f6131 more uniform graphview terminology;
wenzelm
parents: 49566
diff changeset
     1
/*  Title:      Tools/jEdit/src/graphview_dockable.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
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
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
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
     5
Dockable window for graphview.
49557
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
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
     8
package isabelle.jedit
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
     9
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    10
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    11
import isabelle._
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    12
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    13
import java.awt.BorderLayout
49730
e0d98ff3c0db use Pretty_Tooltip for Graphview_Panel;
wenzelm
parents: 49570
diff changeset
    14
import javax.swing.{JPanel, 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
import scala.actors.Actor._
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    17
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    18
import org.gjt.sp.jedit.View
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
49570
2265456f6131 more uniform graphview terminology;
wenzelm
parents: 49566
diff changeset
    21
class Graphview_Dockable(view: View, position: String) extends Dockable(view, position)
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    22
{
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    23
  Swing_Thread.require()
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    24
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    25
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    26
  /* component state -- owned by Swing thread */
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    27
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    28
  private val do_update = true  // FIXME
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    29
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    30
  private var current_snapshot = Document.State.init.snapshot()
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    31
  private var current_state = Command.empty.init_state
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    32
  private var current_graph: XML.Body = Nil
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    33
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    34
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    35
  /* GUI components */
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    36
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    37
  private val graphview = new JPanel
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    38
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    39
  // FIXME mutable GUI content
49735
30e2f3f1c623 more precise repaint and revalidate -- the latter is important to keep in sync with content update;
wenzelm
parents: 49730
diff changeset
    40
  private def set_graphview(snapshot: Document.Snapshot, graph: XML.Body)
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    41
  {
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    42
    graphview.removeAll()
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    43
    graphview.setLayout(new BorderLayout)
49730
e0d98ff3c0db use Pretty_Tooltip for Graphview_Panel;
wenzelm
parents: 49570
diff changeset
    44
    val panel =
e0d98ff3c0db use Pretty_Tooltip for Graphview_Panel;
wenzelm
parents: 49570
diff changeset
    45
      new isabelle.graphview.Main_Panel(isabelle.graphview.Model.decode_graph(graph)) {
e0d98ff3c0db use Pretty_Tooltip for Graphview_Panel;
wenzelm
parents: 49570
diff changeset
    46
        override def make_tooltip(parent: JComponent, x: Int, y: Int, body: XML.Body): String =
e0d98ff3c0db use Pretty_Tooltip for Graphview_Panel;
wenzelm
parents: 49570
diff changeset
    47
        {
49735
30e2f3f1c623 more precise repaint and revalidate -- the latter is important to keep in sync with content update;
wenzelm
parents: 49730
diff changeset
    48
          val rendering = Isabelle_Rendering(snapshot, Isabelle.options.value)
49730
e0d98ff3c0db use Pretty_Tooltip for Graphview_Panel;
wenzelm
parents: 49570
diff changeset
    49
          new Pretty_Tooltip(view, parent, rendering, x, y, body)
e0d98ff3c0db use Pretty_Tooltip for Graphview_Panel;
wenzelm
parents: 49570
diff changeset
    50
          null
e0d98ff3c0db use Pretty_Tooltip for Graphview_Panel;
wenzelm
parents: 49570
diff changeset
    51
        }
e0d98ff3c0db use Pretty_Tooltip for Graphview_Panel;
wenzelm
parents: 49570
diff changeset
    52
      }
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    53
    graphview.add(panel.peer, BorderLayout.CENTER)
49735
30e2f3f1c623 more precise repaint and revalidate -- the latter is important to keep in sync with content update;
wenzelm
parents: 49730
diff changeset
    54
    graphview.revalidate()
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    55
  }
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    56
49735
30e2f3f1c623 more precise repaint and revalidate -- the latter is important to keep in sync with content update;
wenzelm
parents: 49730
diff changeset
    57
  set_graphview(current_snapshot, current_graph)
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    58
  set_content(graphview)
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    59
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    60
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    61
  private def handle_update(follow: Boolean, restriction: Option[Set[Command]])
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    62
  {
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    63
    Swing_Thread.require()
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    64
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    65
    val (new_snapshot, new_state) =
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    66
      Document_View(view.getTextArea) match {
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    67
        case Some(doc_view) =>
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    68
          val snapshot = doc_view.model.snapshot()
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    69
          if (follow && !snapshot.is_outdated) {
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    70
            snapshot.node.command_at(doc_view.text_area.getCaretPosition).map(_._1) match {
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    71
              case Some(cmd) =>
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    72
                (snapshot, snapshot.state.command_state(snapshot.version, cmd))
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    73
              case None =>
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    74
                (Document.State.init.snapshot(), Command.empty.init_state)
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    75
            }
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    76
          }
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    77
          else (current_snapshot, current_state)
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    78
        case None => (current_snapshot, current_state)
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    79
      }
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    80
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    81
    val new_graph =
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    82
      if (!restriction.isDefined || restriction.get.contains(new_state.command)) {
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    83
        new_state.markup.cumulate[Option[XML.Body]](
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    84
          new_state.command.range, None, Some(Set(Isabelle_Markup.GRAPHVIEW)),
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    85
        {
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    86
          case (_, Text.Info(_, XML.Elem(Markup(Isabelle_Markup.GRAPHVIEW, _), graph))) =>
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    87
            Some(graph)
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    88
        }).filter(_.info.isDefined) match {
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    89
          case Text.Info(_, Some(graph)) #:: _ => graph
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    90
          case _ => Nil
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    91
        }
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    92
      }
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    93
      else current_graph
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    94
49735
30e2f3f1c623 more precise repaint and revalidate -- the latter is important to keep in sync with content update;
wenzelm
parents: 49730
diff changeset
    95
    if (new_graph != current_graph) set_graphview(new_snapshot, new_graph)
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    96
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    97
    current_snapshot = new_snapshot
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    98
    current_state = new_state
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    99
    current_graph = new_graph
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   100
  }
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
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
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   103
  /* main actor */
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   104
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   105
  private val main_actor = actor {
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   106
    loop {
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   107
      react {
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   108
        case Session.Global_Options =>  // FIXME
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   109
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   110
        case changed: Session.Commands_Changed =>
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   111
          Swing_Thread.later { handle_update(do_update, Some(changed.commands)) }
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   112
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   113
        case Session.Caret_Focus =>
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   114
          Swing_Thread.later { handle_update(do_update, None) }
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   115
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   116
        case bad =>
49570
2265456f6131 more uniform graphview terminology;
wenzelm
parents: 49566
diff changeset
   117
          java.lang.System.err.println("Graphview_Dockable: ignoring bad message " + bad)
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   118
      }
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   119
    }
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   120
  }
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   121
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   122
  override def init()
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   123
  {
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   124
    Swing_Thread.require()
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   125
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   126
    Isabelle.session.global_options += main_actor
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   127
    Isabelle.session.commands_changed += main_actor
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   128
    Isabelle.session.caret_focus += main_actor
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   129
    handle_update(do_update, None)
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   130
  }
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   131
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   132
  override def exit()
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   133
  {
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   134
    Swing_Thread.require()
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   135
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   136
    Isabelle.session.global_options -= main_actor
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   137
    Isabelle.session.commands_changed -= main_actor
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   138
    Isabelle.session.caret_focus -= main_actor
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   139
  }
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   140
}