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