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}
|
59392
|
14 |
import javax.swing.JTree
|
59396
|
15 |
import javax.swing.tree.{DefaultMutableTreeNode, TreeSelectionModel, TreePath}
|
|
16 |
import javax.swing.event.{TreeSelectionEvent, TreeSelectionListener, DocumentListener, DocumentEvent}
|
59392
|
17 |
|
|
18 |
import scala.util.matching.Regex
|
59396
|
19 |
import scala.swing.{Component, ScrollPane, BorderPanel, Label, TextField, Button, CheckBox, Action}
|
59392
|
20 |
|
|
21 |
|
59396
|
22 |
class Tree_Panel(val visualizer: Visualizer, repaint_all: () => Unit) extends BorderPanel
|
59392
|
23 |
{
|
59396
|
24 |
/* main actions */
|
|
25 |
|
|
26 |
private def selection_action()
|
|
27 |
{
|
|
28 |
if (tree != null) {
|
|
29 |
visualizer.current_node = None
|
|
30 |
visualizer.Selection.clear()
|
|
31 |
val paths = tree.getSelectionPaths
|
|
32 |
if (paths != null) {
|
|
33 |
for (path <- paths if path != null) {
|
|
34 |
path.getLastPathComponent match {
|
|
35 |
case tree_node: DefaultMutableTreeNode =>
|
|
36 |
tree_node.getUserObject match {
|
|
37 |
case node: Graph_Display.Node => visualizer.Selection.add(node)
|
|
38 |
case _ =>
|
|
39 |
}
|
|
40 |
case _ =>
|
|
41 |
}
|
|
42 |
}
|
|
43 |
}
|
|
44 |
repaint_all()
|
|
45 |
}
|
|
46 |
}
|
|
47 |
|
|
48 |
private def point_action(path: TreePath)
|
|
49 |
{
|
|
50 |
if (tree_pane != null && path != null) {
|
|
51 |
val action_node =
|
|
52 |
path.getLastPathComponent match {
|
|
53 |
case tree_node: DefaultMutableTreeNode =>
|
|
54 |
tree_node.getUserObject match {
|
|
55 |
case node: Graph_Display.Node => Some(node)
|
|
56 |
case _ => None
|
|
57 |
}
|
|
58 |
case _ => None
|
|
59 |
}
|
|
60 |
action_node match {
|
|
61 |
case Some(node) =>
|
|
62 |
val info = visualizer.layout.get_node(node)
|
|
63 |
tree_pane.peer.scrollRectToVisible(
|
|
64 |
new Rectangle(
|
|
65 |
(info.x - info.width2).toInt, (info.y - info.height2).toInt,
|
|
66 |
info.width.toInt, info.height.toInt))
|
|
67 |
case None =>
|
|
68 |
}
|
|
69 |
visualizer.current_node = action_node
|
|
70 |
repaint_all()
|
|
71 |
}
|
|
72 |
}
|
|
73 |
|
|
74 |
|
59392
|
75 |
/* tree */
|
|
76 |
|
|
77 |
private var nodes = List.empty[Graph_Display.Node]
|
|
78 |
private val root = new DefaultMutableTreeNode("Nodes")
|
|
79 |
|
59396
|
80 |
val tree = new JTree(root)
|
|
81 |
|
|
82 |
tree.addKeyListener(new KeyAdapter {
|
|
83 |
override def keyPressed(e: KeyEvent): Unit =
|
|
84 |
if (e.getKeyCode == KeyEvent.VK_ENTER) {
|
|
85 |
e.consume
|
|
86 |
selection_action()
|
|
87 |
}
|
|
88 |
})
|
59392
|
89 |
tree.addMouseListener(new MouseAdapter {
|
59396
|
90 |
override def mousePressed(e: MouseEvent): Unit =
|
|
91 |
if (e.getClickCount == 2)
|
|
92 |
point_action(tree.getPathForLocation(e.getX, e.getY))
|
59392
|
93 |
})
|
|
94 |
|
|
95 |
private val tree_pane = new ScrollPane(Component.wrap(tree))
|
|
96 |
tree_pane.horizontalScrollBarPolicy = ScrollPane.BarPolicy.Always
|
|
97 |
tree_pane.verticalScrollBarPolicy = ScrollPane.BarPolicy.Always
|
|
98 |
tree_pane.minimumSize = new Dimension(100, 50)
|
|
99 |
tree_pane.peer.getVerticalScrollBar.setUnitIncrement(10)
|
|
100 |
|
|
101 |
|
|
102 |
/* controls */
|
|
103 |
|
|
104 |
private val alphabetic = new CheckBox {
|
|
105 |
tooltip = "Alphabetic order instead of topological order"
|
|
106 |
selected = visualizer.alphabetic_order
|
|
107 |
action = Action("Alphabetic order") {
|
|
108 |
visualizer.alphabetic_order = selected
|
|
109 |
refresh()
|
|
110 |
}
|
|
111 |
}
|
|
112 |
|
|
113 |
private var selection_pattern: Option[Regex] = None
|
|
114 |
|
|
115 |
private def selection_filter(node: Graph_Display.Node): Boolean =
|
|
116 |
selection_pattern match {
|
|
117 |
case None => true
|
|
118 |
case Some(re) => re.pattern.matcher(node.toString).find
|
|
119 |
}
|
|
120 |
|
|
121 |
private val selection_label = new Label("Selection:") {
|
|
122 |
tooltip = "Selection of nodes via regular expression"
|
|
123 |
}
|
|
124 |
|
|
125 |
private val selection_field = new TextField(10) {
|
|
126 |
tooltip = selection_label.tooltip
|
|
127 |
}
|
|
128 |
private val selection_field_foreground = selection_field.foreground
|
|
129 |
|
|
130 |
private val selection_delay =
|
59395
|
131 |
GUI_Thread.delay_last(visualizer.options.seconds("editor_input_delay")) {
|
59392
|
132 |
val (pattern, ok) =
|
|
133 |
selection_field.text match {
|
|
134 |
case null | "" => (None, true)
|
|
135 |
case s =>
|
|
136 |
val pattern = Library.make_regex(s)
|
|
137 |
(pattern, pattern.isDefined)
|
|
138 |
}
|
|
139 |
if (selection_pattern != pattern) {
|
|
140 |
selection_pattern = pattern
|
|
141 |
// FIXME
|
|
142 |
System.console.writer.println(pattern)
|
|
143 |
}
|
|
144 |
selection_field.foreground =
|
|
145 |
if (ok) selection_field_foreground else visualizer.error_color
|
|
146 |
}
|
|
147 |
|
|
148 |
selection_field.peer.getDocument.addDocumentListener(new DocumentListener {
|
|
149 |
def changedUpdate(e: DocumentEvent) { selection_delay.invoke() }
|
|
150 |
def insertUpdate(e: DocumentEvent) { selection_delay.invoke() }
|
|
151 |
def removeUpdate(e: DocumentEvent) { selection_delay.invoke() }
|
|
152 |
})
|
|
153 |
|
|
154 |
private val selection_apply = new Button {
|
|
155 |
tooltip = "Apply tree selection to graph"
|
59396
|
156 |
action = Action("<html><b>Apply</b></html>") { selection_action () }
|
59392
|
157 |
}
|
|
158 |
|
|
159 |
private val controls = new Wrap_Panel(Wrap_Panel.Alignment.Right)(
|
|
160 |
alphabetic, selection_label, selection_field, selection_apply)
|
|
161 |
|
|
162 |
|
|
163 |
/* main layout */
|
|
164 |
|
|
165 |
def refresh()
|
|
166 |
{
|
|
167 |
val new_nodes =
|
|
168 |
if (visualizer.alphabetic_order)
|
|
169 |
visualizer.visible_graph.keys_iterator.toList
|
|
170 |
else
|
|
171 |
visualizer.visible_graph.topological_order
|
|
172 |
|
|
173 |
if (new_nodes != nodes) {
|
|
174 |
nodes = new_nodes
|
|
175 |
|
|
176 |
root.removeAllChildren
|
|
177 |
for (node <- nodes) root.add(new DefaultMutableTreeNode(node))
|
|
178 |
|
|
179 |
tree.clearSelection
|
|
180 |
for (i <- 0 until tree.getRowCount) tree.expandRow(i)
|
|
181 |
tree.revalidate()
|
|
182 |
}
|
|
183 |
revalidate()
|
|
184 |
repaint()
|
|
185 |
}
|
|
186 |
|
|
187 |
layout(tree_pane) = BorderPanel.Position.Center
|
|
188 |
layout(controls) = BorderPanel.Position.North
|
|
189 |
refresh()
|
|
190 |
}
|