src/Tools/Graphview/tree_panel.scala
author wenzelm
Mon, 18 Nov 2024 15:05:31 +0100
changeset 81483 7d4df25af572
parent 81443 7f3416f35b5d
child 81485 6ca7c8f56396
permissions -rw-r--r--
clarified Tree_View.init_model: more uniform;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
59392
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
     1
/*  Title:      Tools/Graphview/tree_panel.scala
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
     3
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
     4
Tree view on graph nodes.
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
     5
*/
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
     6
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
     7
package isabelle.graphview
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
     8
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
     9
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    10
import isabelle._
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    11
59396
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    12
import java.awt.{Dimension, Rectangle}
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    13
import java.awt.event.{KeyEvent, KeyAdapter, MouseEvent, MouseAdapter}
81329
1775fdc7274e clarified signature;
wenzelm
parents: 81328
diff changeset
    14
import javax.swing.tree.TreePath
59396
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    15
import javax.swing.event.{TreeSelectionEvent, TreeSelectionListener, DocumentListener, DocumentEvent}
59392
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    16
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    17
import scala.util.matching.Regex
75852
fcc25bb49def clarified signature;
wenzelm
parents: 75807
diff changeset
    18
import scala.swing.{Component, ScrollPane, BorderPanel, Label, TextField, Button, Action}
59392
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    19
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    20
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    21
class Tree_Panel(val graphview: Graphview, graph_panel: Graph_Panel)
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    22
extends BorderPanel {
59396
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    23
  /* main actions */
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    24
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    25
  private def selection_action(): Unit = {
59396
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    26
    if (tree != null) {
59459
985fc55e9f27 clarified module name;
wenzelm
parents: 59412
diff changeset
    27
      graphview.current_node = None
985fc55e9f27 clarified module name;
wenzelm
parents: 59412
diff changeset
    28
      graphview.Selection.clear()
59396
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    29
      val paths = tree.getSelectionPaths
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    30
      if (paths != null) {
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    31
        for (path <- paths if path != null) {
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    32
          path.getLastPathComponent match {
81329
1775fdc7274e clarified signature;
wenzelm
parents: 81328
diff changeset
    33
            case Tree_View.Node(node: Graph_Display.Node) => graphview.Selection.add(node)
59396
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    34
            case _ =>
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    35
          }
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    36
        }
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    37
      }
59397
fc909f7e7ce5 proper scrolling wrt. transform;
wenzelm
parents: 59396
diff changeset
    38
      graph_panel.repaint()
59396
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    39
    }
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    40
  }
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    41
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    42
  private def point_action(path: TreePath): Unit = {
59396
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    43
    if (tree_pane != null && path != null) {
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    44
      val action_node =
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    45
        path.getLastPathComponent match {
81329
1775fdc7274e clarified signature;
wenzelm
parents: 81328
diff changeset
    46
          case Tree_View.Node(node: Graph_Display.Node) => Some(node)
59396
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    47
          case _ => None
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    48
        }
59397
fc909f7e7ce5 proper scrolling wrt. transform;
wenzelm
parents: 59396
diff changeset
    49
      action_node.foreach(graph_panel.scroll_to_node(_))
59459
985fc55e9f27 clarified module name;
wenzelm
parents: 59412
diff changeset
    50
      graphview.current_node = action_node
59397
fc909f7e7ce5 proper scrolling wrt. transform;
wenzelm
parents: 59396
diff changeset
    51
      graph_panel.repaint()
59396
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    52
    }
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    53
  }
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    54
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    55
59392
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    56
  /* tree */
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    57
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    58
  private var nodes = List.empty[Graph_Display.Node]
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    59
81329
1775fdc7274e clarified signature;
wenzelm
parents: 81328
diff changeset
    60
  val tree: Tree_View = new Tree_View(root = Tree_View.Node("Nodes"))
59396
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    61
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    62
  tree.addKeyListener(new KeyAdapter {
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    63
    override def keyPressed(e: KeyEvent): Unit =
81443
7f3416f35b5d more careful isConsumed() / consume() for key and mouse events;
wenzelm
parents: 81329
diff changeset
    64
      if (!e.isConsumed() && e.getKeyCode == KeyEvent.VK_ENTER) {
75807
b0394e7d43ea tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents: 75393
diff changeset
    65
        e.consume()
59396
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    66
        selection_action()
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    67
      }
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    68
  })
59392
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    69
  tree.addMouseListener(new MouseAdapter {
59396
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    70
    override def mousePressed(e: MouseEvent): Unit =
81443
7f3416f35b5d more careful isConsumed() / consume() for key and mouse events;
wenzelm
parents: 81329
diff changeset
    71
      if (!e.isConsumed() && e.getClickCount == 2) {
7f3416f35b5d more careful isConsumed() / consume() for key and mouse events;
wenzelm
parents: 81329
diff changeset
    72
        e.consume()
59396
a2f4252c5489 clarified main actions and keyboard focus;
wenzelm
parents: 59395
diff changeset
    73
        point_action(tree.getPathForLocation(e.getX, e.getY))
81321
wenzelm
parents: 81320
diff changeset
    74
      }
59392
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    75
  })
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    76
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    77
  private val tree_pane = new ScrollPane(Component.wrap(tree))
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    78
  tree_pane.horizontalScrollBarPolicy = ScrollPane.BarPolicy.Always
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    79
  tree_pane.verticalScrollBarPolicy = ScrollPane.BarPolicy.Always
60849
wenzelm
parents: 60846
diff changeset
    80
  tree_pane.minimumSize = new Dimension(200, 100)
59392
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    81
  tree_pane.peer.getVerticalScrollBar.setUnitIncrement(10)
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    82
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    83
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    84
  /* controls */
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    85
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    86
  private var selection_pattern: Option[Regex] = None
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    87
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    88
  private def selection_filter(node: Graph_Display.Node): Boolean =
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    89
    selection_pattern match {
59402
4de91de47a6c proper selection of nodes via regular expression;
wenzelm
parents: 59400
diff changeset
    90
      case None => false
76098
bcca0fbb8a34 tuned: prefer Scala Regex operations;
wenzelm
parents: 75852
diff changeset
    91
      case Some(re) => re.findFirstIn(node.toString).isDefined
59392
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    92
    }
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    93
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    94
  private val selection_label = new Label("Selection:") {
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    95
    tooltip = "Selection of nodes via regular expression"
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    96
  }
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    97
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    98
  private val selection_field = new TextField(10) {
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
    99
    tooltip = selection_label.tooltip
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   100
  }
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   101
  private val selection_field_foreground = selection_field.foreground
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   102
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   103
  private val selection_delay =
71704
b9a5eb0f3b43 clarified modules;
wenzelm
parents: 66206
diff changeset
   104
    Delay.last(graphview.options.seconds("editor_input_delay"), gui = true) {
59392
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   105
      val (pattern, ok) =
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   106
        selection_field.text match {
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   107
          case null | "" => (None, true)
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   108
          case s =>
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   109
            val pattern = Library.make_regex(s)
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   110
            (pattern, pattern.isDefined)
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   111
        }
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   112
      if (selection_pattern != pattern) {
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   113
        selection_pattern = pattern
59402
4de91de47a6c proper selection of nodes via regular expression;
wenzelm
parents: 59400
diff changeset
   114
        tree.setSelectionRows(
4de91de47a6c proper selection of nodes via regular expression;
wenzelm
parents: 59400
diff changeset
   115
          (for { (node, i) <- nodes.iterator.zipWithIndex if selection_filter(node) }
4de91de47a6c proper selection of nodes via regular expression;
wenzelm
parents: 59400
diff changeset
   116
            yield i + 1).toArray)
4de91de47a6c proper selection of nodes via regular expression;
wenzelm
parents: 59400
diff changeset
   117
        tree.repaint()
59392
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   118
      }
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   119
      selection_field.foreground =
59459
985fc55e9f27 clarified module name;
wenzelm
parents: 59412
diff changeset
   120
        if (ok) selection_field_foreground else graphview.error_color
59392
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   121
    }
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   122
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 71704
diff changeset
   123
  selection_field.peer.getDocument.addDocumentListener(
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 71704
diff changeset
   124
    new DocumentListener {
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 71704
diff changeset
   125
      def changedUpdate(e: DocumentEvent): Unit = selection_delay.invoke()
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 71704
diff changeset
   126
      def insertUpdate(e: DocumentEvent): Unit = selection_delay.invoke()
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 71704
diff changeset
   127
      def removeUpdate(e: DocumentEvent): Unit = selection_delay.invoke()
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 71704
diff changeset
   128
    })
59392
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   129
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   130
  private val selection_apply = new Button {
59406
283aa6225d98 proper tooltips -- override action toolTip which is empty here;
wenzelm
parents: 59404
diff changeset
   131
    action = Action("<html><b>Apply</b></html>") { selection_action () }
59392
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   132
    tooltip = "Apply tree selection to graph"
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   133
  }
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   134
59409
wenzelm
parents: 59406
diff changeset
   135
  private val controls =
66206
2d2082db735a clarified defaults;
wenzelm
parents: 66205
diff changeset
   136
    Wrap_Panel(List(selection_label, selection_field, selection_apply))
59392
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   137
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   138
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   139
  /* main layout */
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   140
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
   141
  def refresh(): Unit = {
59459
985fc55e9f27 clarified module name;
wenzelm
parents: 59412
diff changeset
   142
    val new_nodes = graphview.visible_graph.topological_order
81483
7d4df25af572 clarified Tree_View.init_model: more uniform;
wenzelm
parents: 81443
diff changeset
   143
    if (nodes != new_nodes) {
59392
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   144
      nodes = new_nodes
81483
7d4df25af572 clarified Tree_View.init_model: more uniform;
wenzelm
parents: 81443
diff changeset
   145
      tree.init_model { for (node <- nodes) tree.root.add(Tree_View.Node(node)) }
59392
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   146
    }
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   147
    revalidate()
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   148
    repaint()
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   149
  }
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   150
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   151
  layout(tree_pane) = BorderPanel.Position.Center
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   152
  layout(controls) = BorderPanel.Position.North
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   153
  refresh()
02bacfc31446 support for tree view on graph nodes;
wenzelm
parents:
diff changeset
   154
}