author | wenzelm |
Tue, 19 Nov 2024 22:48:18 +0100 | |
changeset 81495 | 57dc2c8ba7c6 |
parent 81494 | b30dc7db521f |
child 81496 | 76f360c63dfe |
permissions | -rw-r--r-- |
81323 | 1 |
/* Title: Pure/GUI/tree_view.scala |
2 |
Author: Makarius |
|
3 |
||
81330 | 4 |
Tree view with sensible defaults. |
81323 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
import javax.swing.JTree |
|
81329 | 10 |
import javax.swing.tree.{MutableTreeNode, DefaultMutableTreeNode, DefaultTreeModel, |
81494 | 11 |
TreeSelectionModel, TreeCellRenderer, DefaultTreeCellRenderer} |
81323 | 12 |
|
13 |
||
81329 | 14 |
object Tree_View { |
15 |
type Node = DefaultMutableTreeNode |
|
16 |
||
17 |
object Node { |
|
18 |
def apply(obj: AnyRef = null): Node = |
|
19 |
if (obj == null) new DefaultMutableTreeNode else new DefaultMutableTreeNode(obj) |
|
20 |
||
21 |
def unapply(tree_node: MutableTreeNode): Option[AnyRef] = |
|
22 |
tree_node match { |
|
81380
83012fe97282
revert 1206400b9b48: proper Node.unapply for Node.apply(null);
wenzelm
parents:
81377
diff
changeset
|
23 |
case node: Node => Some(node.getUserObject) |
81329 | 24 |
case _ => None |
25 |
} |
|
26 |
} |
|
27 |
} |
|
28 |
||
81325 | 29 |
class Tree_View( |
81329 | 30 |
val root: Tree_View.Node = Tree_View.Node(), |
81325 | 31 |
single_selection_mode: Boolean = false |
32 |
) extends JTree(root) { |
|
33 |
def get_selection[A](which: PartialFunction[AnyRef, A]): Option[A] = |
|
34 |
getLastSelectedPathComponent match { |
|
81380
83012fe97282
revert 1206400b9b48: proper Node.unapply for Node.apply(null);
wenzelm
parents:
81377
diff
changeset
|
35 |
case Tree_View.Node(obj) if obj != null && which.isDefinedAt(obj) => Some(which(obj)) |
81325 | 36 |
case _ => None |
37 |
} |
|
81323 | 38 |
|
81483 | 39 |
def init_model(body: => Unit): Unit = { |
81325 | 40 |
clearSelection() |
41 |
root.removeAllChildren() |
|
81483 | 42 |
body |
43 |
expandRow(0) |
|
44 |
reload_model() |
|
81325 | 45 |
} |
46 |
||
47 |
def reload_model(): Unit = |
|
81326 | 48 |
getModel match { |
49 |
case model: DefaultTreeModel => model.reload(root) |
|
50 |
case _ => |
|
51 |
} |
|
81325 | 52 |
|
53 |
||
81494 | 54 |
/* cell renderer */ |
55 |
||
81495 | 56 |
def render_tree_cell(renderer: DefaultTreeCellRenderer): Unit = { |
57 |
renderer.setIcon(null) |
|
58 |
} |
|
81494 | 59 |
|
60 |
private val tree_cell_renderer: TreeCellRenderer = new DefaultTreeCellRenderer { |
|
61 |
override def getTreeCellRendererComponent( |
|
62 |
tree: JTree, |
|
63 |
value: AnyRef, |
|
64 |
selected: Boolean, |
|
65 |
expanded: Boolean, |
|
66 |
leaf: Boolean, |
|
67 |
row: Int, |
|
68 |
hasFocus: Boolean |
|
69 |
): java.awt.Component = { |
|
70 |
super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus) |
|
71 |
render_tree_cell(this) |
|
72 |
this |
|
73 |
} |
|
74 |
} |
|
75 |
||
76 |
||
81325 | 77 |
/* init */ |
78 |
||
81494 | 79 |
setCellRenderer(tree_cell_renderer) |
80 |
||
81325 | 81 |
setRowHeight(0) |
81323 | 82 |
|
83 |
if (single_selection_mode) { |
|
81325 | 84 |
getSelectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION) |
81323 | 85 |
} |
86 |
||
87 |
// follow jEdit |
|
88 |
if (!GUI.is_macos_laf) { |
|
81325 | 89 |
putClientProperty("JTree.lineStyle", "Angled") |
81323 | 90 |
} |
91 |
} |