src/Tools/jEdit/src/graphview_dockable.scala
author wenzelm
Mon, 25 Mar 2019 17:21:26 +0100
changeset 69981 3dced198b9ec
parent 69359 3b709d9074ec
child 71525 d7b0d078266d
permissions -rw-r--r--
more strict AFP properties;
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
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
     2
    Author:     Makarius
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
     3
50452
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
     4
Stateless 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
     5
*/
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
     6
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
     7
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
     8
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
import isabelle._
66591
6efa351190d0 more robust: provide docking framework via base plugin;
wenzelm
parents: 66114
diff changeset
    11
import isabelle.jedit_base.Dockable
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    12
53247
bd595338ca18 uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents: 53177
diff changeset
    13
import javax.swing.JComponent
59286
ac74eedb910a GUI.imitate_font: more explicit result size, e.g. relevant for caching;
wenzelm
parents: 59245
diff changeset
    14
import java.awt.{Point, Font}
50452
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    15
import java.awt.event.{WindowFocusListener, WindowEvent}
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    16
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    17
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
    18
50452
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    19
import scala.swing.TextArea
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    20
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    21
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    22
object Graphview_Dockable
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    23
{
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 56662
diff changeset
    24
  /* implicit arguments -- owned by GUI thread */
50452
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    25
52972
8fd8e1c14988 tuned signature;
wenzelm
parents: 52496
diff changeset
    26
  private var implicit_snapshot = Document.Snapshot.init
50452
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    27
59245
be4180f3c236 more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents: 59233
diff changeset
    28
  private val no_graph: Exn.Result[Graph_Display.Graph] = Exn.Exn(ERROR("No graph"))
50452
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    29
  private var implicit_graph = no_graph
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    30
59245
be4180f3c236 more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents: 59233
diff changeset
    31
  private def set_implicit(snapshot: Document.Snapshot, graph: Exn.Result[Graph_Display.Graph])
50452
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    32
  {
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 56662
diff changeset
    33
    GUI_Thread.require {}
50452
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    34
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    35
    implicit_snapshot = snapshot
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    36
    implicit_graph = graph
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    37
  }
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    38
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    39
  private def reset_implicit(): Unit =
52972
8fd8e1c14988 tuned signature;
wenzelm
parents: 52496
diff changeset
    40
    set_implicit(Document.Snapshot.init, no_graph)
50452
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    41
59245
be4180f3c236 more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents: 59233
diff changeset
    42
  def apply(view: View, snapshot: Document.Snapshot, graph: Exn.Result[Graph_Display.Graph])
50452
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    43
  {
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    44
    set_implicit(snapshot, graph)
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    45
    view.getDockableWindowManager.floatDockableWindow("isabelle-graphview")
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    46
  }
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    47
}
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    48
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
    49
49570
2265456f6131 more uniform graphview terminology;
wenzelm
parents: 49566
diff changeset
    50
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
    51
{
50452
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    52
  private val snapshot = Graphview_Dockable.implicit_snapshot
59228
56b34fc7a015 more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents: 57612
diff changeset
    53
  private val graph_result = Graphview_Dockable.implicit_graph
50446
8dc05db0bf69 always apply transitive_reduction_acyclic in imitation of old graph browser (essential to avoid slow layout and overcrowded display, e.g. class_deps);
wenzelm
parents: 50205
diff changeset
    54
50452
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    55
  private val window_focus_listener =
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    56
    new WindowFocusListener {
59228
56b34fc7a015 more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents: 57612
diff changeset
    57
      def windowGainedFocus(e: WindowEvent) {
56b34fc7a015 more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents: 57612
diff changeset
    58
        Graphview_Dockable.set_implicit(snapshot, graph_result) }
50452
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    59
      def windowLostFocus(e: WindowEvent) { Graphview_Dockable.reset_implicit() }
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    60
    }
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    61
50452
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    62
  val graphview =
59228
56b34fc7a015 more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents: 57612
diff changeset
    63
    graph_result match {
56b34fc7a015 more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents: 57612
diff changeset
    64
      case Exn.Res(graph) =>
59462
c7eff4356885 tuned signature;
wenzelm
parents: 59459
diff changeset
    65
        val graphview = new isabelle.graphview.Graphview(graph) {
59395
4c5396f52546 tuned signature;
wenzelm
parents: 59394
diff changeset
    66
          def options: Options = PIDE.options.value
4c5396f52546 tuned signature;
wenzelm
parents: 59394
diff changeset
    67
50452
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    68
          override def make_tooltip(parent: JComponent, x: Int, y: Int, body: XML.Body): String =
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    69
          {
52494
a1e09340c0f4 clarified tooltip timing of pending event and active state;
wenzelm
parents: 52483
diff changeset
    70
            Pretty_Tooltip.invoke(() =>
a1e09340c0f4 clarified tooltip timing of pending event and active state;
wenzelm
parents: 52483
diff changeset
    71
              {
66114
c137a9f038a6 clarified signature;
wenzelm
parents: 65131
diff changeset
    72
                val model = File_Model.empty(PIDE.session)
c137a9f038a6 clarified signature;
wenzelm
parents: 65131
diff changeset
    73
                val rendering = JEdit_Rendering(snapshot, model, options)
65131
5d35f4bccfa7 proper Text.Range.offside (NB: in Scala ~1 == -2);
wenzelm
parents: 64621
diff changeset
    74
                val info = Text.Info(Text.Range.offside, body)
53247
bd595338ca18 uniform use of isabelle.jEdit.Popup, based on generic screen location operations;
wenzelm
parents: 53177
diff changeset
    75
                Pretty_Tooltip(view, parent, new Point(x, y), rendering, Command.Results.empty, info)
52494
a1e09340c0f4 clarified tooltip timing of pending event and active state;
wenzelm
parents: 52483
diff changeset
    76
              })
50452
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
    77
            null
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
    78
          }
59395
4c5396f52546 tuned signature;
wenzelm
parents: 59394
diff changeset
    79
4c5396f52546 tuned signature;
wenzelm
parents: 59394
diff changeset
    80
          override def make_font(): Font =
69359
3b709d9074ec prefer Isabelle_Fonts.sans for GUI;
wenzelm
parents: 69358
diff changeset
    81
            if (editor_style) GUI.imitate_font(Font_Info.main().font)
61176
9791f631c20d avoid hardwired colors;
wenzelm
parents: 60215
diff changeset
    82
            else
9791f631c20d avoid hardwired colors;
wenzelm
parents: 60215
diff changeset
    83
              GUI.imitate_font(Font_Info.main().font,
69358
71ef6e6da3dc prefer Isabelle_Fonts.sans (not mono) as derived GUI font;
wenzelm
parents: 66591
diff changeset
    84
                family = options.string("graphview_font_family"),
71ef6e6da3dc prefer Isabelle_Fonts.sans (not mono) as derived GUI font;
wenzelm
parents: 66591
diff changeset
    85
                scale = options.real("graphview_font_scale"))
61176
9791f631c20d avoid hardwired colors;
wenzelm
parents: 60215
diff changeset
    86
9791f631c20d avoid hardwired colors;
wenzelm
parents: 60215
diff changeset
    87
          override def foreground_color =
9791f631c20d avoid hardwired colors;
wenzelm
parents: 60215
diff changeset
    88
            if (editor_style) view.getTextArea.getPainter.getForeground
9791f631c20d avoid hardwired colors;
wenzelm
parents: 60215
diff changeset
    89
            else super.foreground_color
59395
4c5396f52546 tuned signature;
wenzelm
parents: 59394
diff changeset
    90
61176
9791f631c20d avoid hardwired colors;
wenzelm
parents: 60215
diff changeset
    91
          override def background_color =
9791f631c20d avoid hardwired colors;
wenzelm
parents: 60215
diff changeset
    92
            if (editor_style) view.getTextArea.getPainter.getBackground
9791f631c20d avoid hardwired colors;
wenzelm
parents: 60215
diff changeset
    93
            else super.background_color
9791f631c20d avoid hardwired colors;
wenzelm
parents: 60215
diff changeset
    94
9791f631c20d avoid hardwired colors;
wenzelm
parents: 60215
diff changeset
    95
          override def selection_color =
9791f631c20d avoid hardwired colors;
wenzelm
parents: 60215
diff changeset
    96
            if (editor_style) view.getTextArea.getPainter.getSelectionColor
9791f631c20d avoid hardwired colors;
wenzelm
parents: 60215
diff changeset
    97
            else super.selection_color
9791f631c20d avoid hardwired colors;
wenzelm
parents: 60215
diff changeset
    98
9791f631c20d avoid hardwired colors;
wenzelm
parents: 60215
diff changeset
    99
          override def highlight_color =
9791f631c20d avoid hardwired colors;
wenzelm
parents: 60215
diff changeset
   100
            if (editor_style) view.getTextArea.getPainter.getLineHighlightColor
9791f631c20d avoid hardwired colors;
wenzelm
parents: 60215
diff changeset
   101
            else super.highlight_color
9791f631c20d avoid hardwired colors;
wenzelm
parents: 60215
diff changeset
   102
59233
876a81f5788b tuned signature;
wenzelm
parents: 59231
diff changeset
   103
          override def error_color = PIDE.options.color_value("error_color")
61176
9791f631c20d avoid hardwired colors;
wenzelm
parents: 60215
diff changeset
   104
9791f631c20d avoid hardwired colors;
wenzelm
parents: 60215
diff changeset
   105
          editor_style = true
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   106
        }
59459
985fc55e9f27 clarified module name;
wenzelm
parents: 59410
diff changeset
   107
        new isabelle.graphview.Main_Panel(graphview)
50452
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
   108
      case Exn.Exn(exn) => new TextArea(Exn.message(exn))
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   109
    }
50452
bfb5964e3041 stateless dockable window for graphview, which is triggered by the active area of the corresponding diagnostic command;
wenzelm
parents: 50446
diff changeset
   110
  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
   111
59288
b1086f3e4590 apply layout on change of options;
wenzelm
parents: 59286
diff changeset
   112
59396
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
   113
  override def focusOnDefaultComponent
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
   114
  {
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
   115
    graphview match {
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
   116
      case main_panel: isabelle.graphview.Main_Panel =>
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
   117
        main_panel.tree_panel.tree.requestFocusInWindow
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
   118
      case _ =>
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
   119
    }
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
   120
  }
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
   121
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
   122
59288
b1086f3e4590 apply layout on change of options;
wenzelm
parents: 59286
diff changeset
   123
  /* main */
b1086f3e4590 apply layout on change of options;
wenzelm
parents: 59286
diff changeset
   124
b1086f3e4590 apply layout on change of options;
wenzelm
parents: 59286
diff changeset
   125
  private val main =
b1086f3e4590 apply layout on change of options;
wenzelm
parents: 59286
diff changeset
   126
    Session.Consumer[Session.Global_Options](getClass.getName) {
b1086f3e4590 apply layout on change of options;
wenzelm
parents: 59286
diff changeset
   127
      case _: Session.Global_Options =>
b1086f3e4590 apply layout on change of options;
wenzelm
parents: 59286
diff changeset
   128
        GUI_Thread.later {
b1086f3e4590 apply layout on change of options;
wenzelm
parents: 59286
diff changeset
   129
          graphview match {
b1086f3e4590 apply layout on change of options;
wenzelm
parents: 59286
diff changeset
   130
            case main_panel: isabelle.graphview.Main_Panel =>
59392
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents: 59290
diff changeset
   131
              main_panel.update_layout()
59288
b1086f3e4590 apply layout on change of options;
wenzelm
parents: 59286
diff changeset
   132
            case _ =>
b1086f3e4590 apply layout on change of options;
wenzelm
parents: 59286
diff changeset
   133
          }
b1086f3e4590 apply layout on change of options;
wenzelm
parents: 59286
diff changeset
   134
        }
b1086f3e4590 apply layout on change of options;
wenzelm
parents: 59286
diff changeset
   135
    }
b1086f3e4590 apply layout on change of options;
wenzelm
parents: 59286
diff changeset
   136
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   137
  override def init()
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   138
  {
60215
5fb4990dfc73 misc tuning, based on warnings by IntelliJ IDEA;
wenzelm
parents: 59462
diff changeset
   139
    GUI.parent_window(this).foreach(_.addWindowFocusListener(window_focus_listener))
59288
b1086f3e4590 apply layout on change of options;
wenzelm
parents: 59286
diff changeset
   140
    PIDE.session.global_options += main
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   141
  }
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   142
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   143
  override def exit()
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   144
  {
60215
5fb4990dfc73 misc tuning, based on warnings by IntelliJ IDEA;
wenzelm
parents: 59462
diff changeset
   145
    GUI.parent_window(this).foreach(_.removeWindowFocusListener(window_focus_listener))
59288
b1086f3e4590 apply layout on change of options;
wenzelm
parents: 59286
diff changeset
   146
    PIDE.session.global_options -= main
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49557
diff changeset
   147
  }
49557
61988f9df94d added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff changeset
   148
}