author | wenzelm |
Mon, 18 Nov 2024 15:05:31 +0100 | |
changeset 81483 | 7d4df25af572 |
parent 81443 | 7f3416f35b5d |
child 81485 | 6ca7c8f56396 |
permissions | -rw-r--r-- |
59392 | 1 |
/* Title: Tools/Graphview/tree_panel.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Tree view on graph nodes. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle.graphview |
|
8 |
||
9 |
||
10 |
import isabelle._ |
|
11 |
||
59396 | 12 |
import java.awt.{Dimension, Rectangle} |
13 |
import java.awt.event.{KeyEvent, KeyAdapter, MouseEvent, MouseAdapter} |
|
81329 | 14 |
import javax.swing.tree.TreePath |
59396 | 15 |
import javax.swing.event.{TreeSelectionEvent, TreeSelectionListener, DocumentListener, DocumentEvent} |
59392 | 16 |
|
17 |
import scala.util.matching.Regex |
|
75852 | 18 |
import scala.swing.{Component, ScrollPane, BorderPanel, Label, TextField, Button, Action} |
59392 | 19 |
|
20 |
||
75393 | 21 |
class Tree_Panel(val graphview: Graphview, graph_panel: Graph_Panel) |
22 |
extends BorderPanel { |
|
59396 | 23 |
/* main actions */ |
24 |
||
75393 | 25 |
private def selection_action(): Unit = { |
59396 | 26 |
if (tree != null) { |
59459 | 27 |
graphview.current_node = None |
28 |
graphview.Selection.clear() |
|
59396 | 29 |
val paths = tree.getSelectionPaths |
30 |
if (paths != null) { |
|
31 |
for (path <- paths if path != null) { |
|
32 |
path.getLastPathComponent match { |
|
81329 | 33 |
case Tree_View.Node(node: Graph_Display.Node) => graphview.Selection.add(node) |
59396 | 34 |
case _ => |
35 |
} |
|
36 |
} |
|
37 |
} |
|
59397 | 38 |
graph_panel.repaint() |
59396 | 39 |
} |
40 |
} |
|
41 |
||
75393 | 42 |
private def point_action(path: TreePath): Unit = { |
59396 | 43 |
if (tree_pane != null && path != null) { |
44 |
val action_node = |
|
45 |
path.getLastPathComponent match { |
|
81329 | 46 |
case Tree_View.Node(node: Graph_Display.Node) => Some(node) |
59396 | 47 |
case _ => None |
48 |
} |
|
59397 | 49 |
action_node.foreach(graph_panel.scroll_to_node(_)) |
59459 | 50 |
graphview.current_node = action_node |
59397 | 51 |
graph_panel.repaint() |
59396 | 52 |
} |
53 |
} |
|
54 |
||
55 |
||
59392 | 56 |
/* tree */ |
57 |
||
58 |
private var nodes = List.empty[Graph_Display.Node] |
|
59 |
||
81329 | 60 |
val tree: Tree_View = new Tree_View(root = Tree_View.Node("Nodes")) |
59396 | 61 |
|
62 |
tree.addKeyListener(new KeyAdapter { |
|
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 | 66 |
selection_action() |
67 |
} |
|
68 |
}) |
|
59392 | 69 |
tree.addMouseListener(new MouseAdapter { |
59396 | 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 | 73 |
point_action(tree.getPathForLocation(e.getX, e.getY)) |
81321 | 74 |
} |
59392 | 75 |
}) |
76 |
||
77 |
private val tree_pane = new ScrollPane(Component.wrap(tree)) |
|
78 |
tree_pane.horizontalScrollBarPolicy = ScrollPane.BarPolicy.Always |
|
79 |
tree_pane.verticalScrollBarPolicy = ScrollPane.BarPolicy.Always |
|
60849 | 80 |
tree_pane.minimumSize = new Dimension(200, 100) |
59392 | 81 |
tree_pane.peer.getVerticalScrollBar.setUnitIncrement(10) |
82 |
||
83 |
||
84 |
/* controls */ |
|
85 |
||
86 |
private var selection_pattern: Option[Regex] = None |
|
87 |
||
88 |
private def selection_filter(node: Graph_Display.Node): Boolean = |
|
89 |
selection_pattern match { |
|
59402
4de91de47a6c
proper selection of nodes via regular expression;
wenzelm
parents:
59400
diff
changeset
|
90 |
case None => false |
76098 | 91 |
case Some(re) => re.findFirstIn(node.toString).isDefined |
59392 | 92 |
} |
93 |
||
94 |
private val selection_label = new Label("Selection:") { |
|
95 |
tooltip = "Selection of nodes via regular expression" |
|
96 |
} |
|
97 |
||
98 |
private val selection_field = new TextField(10) { |
|
99 |
tooltip = selection_label.tooltip |
|
100 |
} |
|
101 |
private val selection_field_foreground = selection_field.foreground |
|
102 |
||
103 |
private val selection_delay = |
|
71704 | 104 |
Delay.last(graphview.options.seconds("editor_input_delay"), gui = true) { |
59392 | 105 |
val (pattern, ok) = |
106 |
selection_field.text match { |
|
107 |
case null | "" => (None, true) |
|
108 |
case s => |
|
109 |
val pattern = Library.make_regex(s) |
|
110 |
(pattern, pattern.isDefined) |
|
111 |
} |
|
112 |
if (selection_pattern != pattern) { |
|
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 | 118 |
} |
119 |
selection_field.foreground = |
|
59459 | 120 |
if (ok) selection_field_foreground else graphview.error_color |
59392 | 121 |
} |
122 |
||
73340 | 123 |
selection_field.peer.getDocument.addDocumentListener( |
124 |
new DocumentListener { |
|
125 |
def changedUpdate(e: DocumentEvent): Unit = selection_delay.invoke() |
|
126 |
def insertUpdate(e: DocumentEvent): Unit = selection_delay.invoke() |
|
127 |
def removeUpdate(e: DocumentEvent): Unit = selection_delay.invoke() |
|
128 |
}) |
|
59392 | 129 |
|
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 | 132 |
tooltip = "Apply tree selection to graph" |
133 |
} |
|
134 |
||
59409 | 135 |
private val controls = |
66206 | 136 |
Wrap_Panel(List(selection_label, selection_field, selection_apply)) |
59392 | 137 |
|
138 |
||
139 |
/* main layout */ |
|
140 |
||
75393 | 141 |
def refresh(): Unit = { |
59459 | 142 |
val new_nodes = graphview.visible_graph.topological_order |
81483 | 143 |
if (nodes != new_nodes) { |
59392 | 144 |
nodes = new_nodes |
81483 | 145 |
tree.init_model { for (node <- nodes) tree.root.add(Tree_View.Node(node)) } |
59392 | 146 |
} |
147 |
revalidate() |
|
148 |
repaint() |
|
149 |
} |
|
150 |
||
151 |
layout(tree_pane) = BorderPanel.Position.Center |
|
152 |
layout(controls) = BorderPanel.Position.North |
|
153 |
refresh() |
|
154 |
} |