author | wenzelm |
Mon, 19 Jan 2015 20:39:01 +0100 | |
changeset 59409 | b7cfe12acf2e |
parent 59406 | 283aa6225d98 |
child 59412 | 0426b53a5d54 |
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} |
|
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 |
||
59397 | 22 |
class Tree_Panel(val visualizer: Visualizer, graph_panel: Graph_Panel) 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 |
} |
|
59397 | 44 |
graph_panel.repaint() |
59396 | 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 |
} |
|
59397 | 60 |
action_node.foreach(graph_panel.scroll_to_node(_)) |
59396 | 61 |
visualizer.current_node = action_node |
59397 | 62 |
graph_panel.repaint() |
59396 | 63 |
} |
64 |
} |
|
65 |
||
66 |
||
59392 | 67 |
/* tree */ |
68 |
||
69 |
private var nodes = List.empty[Graph_Display.Node] |
|
70 |
private val root = new DefaultMutableTreeNode("Nodes") |
|
71 |
||
59396 | 72 |
val tree = new JTree(root) |
73 |
||
74 |
tree.addKeyListener(new KeyAdapter { |
|
75 |
override def keyPressed(e: KeyEvent): Unit = |
|
76 |
if (e.getKeyCode == KeyEvent.VK_ENTER) { |
|
77 |
e.consume |
|
78 |
selection_action() |
|
79 |
} |
|
80 |
}) |
|
59392 | 81 |
tree.addMouseListener(new MouseAdapter { |
59396 | 82 |
override def mousePressed(e: MouseEvent): Unit = |
83 |
if (e.getClickCount == 2) |
|
84 |
point_action(tree.getPathForLocation(e.getX, e.getY)) |
|
59392 | 85 |
}) |
86 |
||
87 |
private val tree_pane = new ScrollPane(Component.wrap(tree)) |
|
88 |
tree_pane.horizontalScrollBarPolicy = ScrollPane.BarPolicy.Always |
|
89 |
tree_pane.verticalScrollBarPolicy = ScrollPane.BarPolicy.Always |
|
90 |
tree_pane.minimumSize = new Dimension(100, 50) |
|
91 |
tree_pane.peer.getVerticalScrollBar.setUnitIncrement(10) |
|
92 |
||
93 |
||
94 |
/* controls */ |
|
95 |
||
96 |
private var selection_pattern: Option[Regex] = None |
|
97 |
||
98 |
private def selection_filter(node: Graph_Display.Node): Boolean = |
|
99 |
selection_pattern match { |
|
59402
4de91de47a6c
proper selection of nodes via regular expression;
wenzelm
parents:
59400
diff
changeset
|
100 |
case None => false |
59392 | 101 |
case Some(re) => re.pattern.matcher(node.toString).find |
102 |
} |
|
103 |
||
104 |
private val selection_label = new Label("Selection:") { |
|
105 |
tooltip = "Selection of nodes via regular expression" |
|
106 |
} |
|
107 |
||
108 |
private val selection_field = new TextField(10) { |
|
109 |
tooltip = selection_label.tooltip |
|
110 |
} |
|
111 |
private val selection_field_foreground = selection_field.foreground |
|
112 |
||
113 |
private val selection_delay = |
|
59395 | 114 |
GUI_Thread.delay_last(visualizer.options.seconds("editor_input_delay")) { |
59392 | 115 |
val (pattern, ok) = |
116 |
selection_field.text match { |
|
117 |
case null | "" => (None, true) |
|
118 |
case s => |
|
119 |
val pattern = Library.make_regex(s) |
|
120 |
(pattern, pattern.isDefined) |
|
121 |
} |
|
122 |
if (selection_pattern != pattern) { |
|
123 |
selection_pattern = pattern |
|
59402
4de91de47a6c
proper selection of nodes via regular expression;
wenzelm
parents:
59400
diff
changeset
|
124 |
tree.setSelectionRows( |
4de91de47a6c
proper selection of nodes via regular expression;
wenzelm
parents:
59400
diff
changeset
|
125 |
(for { (node, i) <- nodes.iterator.zipWithIndex if selection_filter(node) } |
4de91de47a6c
proper selection of nodes via regular expression;
wenzelm
parents:
59400
diff
changeset
|
126 |
yield i + 1).toArray) |
4de91de47a6c
proper selection of nodes via regular expression;
wenzelm
parents:
59400
diff
changeset
|
127 |
tree.repaint() |
59392 | 128 |
} |
129 |
selection_field.foreground = |
|
130 |
if (ok) selection_field_foreground else visualizer.error_color |
|
131 |
} |
|
132 |
||
133 |
selection_field.peer.getDocument.addDocumentListener(new DocumentListener { |
|
134 |
def changedUpdate(e: DocumentEvent) { selection_delay.invoke() } |
|
135 |
def insertUpdate(e: DocumentEvent) { selection_delay.invoke() } |
|
136 |
def removeUpdate(e: DocumentEvent) { selection_delay.invoke() } |
|
137 |
}) |
|
138 |
||
139 |
private val selection_apply = new Button { |
|
59406
283aa6225d98
proper tooltips -- override action toolTip which is empty here;
wenzelm
parents:
59404
diff
changeset
|
140 |
action = Action("<html><b>Apply</b></html>") { selection_action () } |
59392 | 141 |
tooltip = "Apply tree selection to graph" |
142 |
} |
|
143 |
||
59409 | 144 |
private val controls = |
145 |
new Wrap_Panel(Wrap_Panel.Alignment.Right)(selection_label, selection_field, selection_apply) |
|
59392 | 146 |
|
147 |
||
148 |
/* main layout */ |
|
149 |
||
150 |
def refresh() |
|
151 |
{ |
|
59404
5d08b2332b76
discontinued attempt at alphabetic_order -- selection via regex should be sufficient;
wenzelm
parents:
59402
diff
changeset
|
152 |
val new_nodes = visualizer.visible_graph.topological_order |
59392 | 153 |
if (new_nodes != nodes) { |
154 |
nodes = new_nodes |
|
155 |
||
156 |
root.removeAllChildren |
|
157 |
for (node <- nodes) root.add(new DefaultMutableTreeNode(node)) |
|
158 |
||
159 |
tree.clearSelection |
|
160 |
for (i <- 0 until tree.getRowCount) tree.expandRow(i) |
|
161 |
tree.revalidate() |
|
162 |
} |
|
163 |
revalidate() |
|
164 |
repaint() |
|
165 |
} |
|
166 |
||
167 |
layout(tree_pane) = BorderPanel.Position.Center |
|
168 |
layout(controls) = BorderPanel.Position.North |
|
169 |
refresh() |
|
170 |
} |