src/Pure/GUI/tree_view.scala
author wenzelm
Wed, 22 Oct 2025 21:23:01 +0200
changeset 83341 b87ea73f8606
parent 83032 a29b383ad733
permissions -rw-r--r--
update to zstd-jni-1.5.7-6;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
81323
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/GUI/tree_view.scala
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
     3
81330
2239495a64f6 tuned comments;
wenzelm
parents: 81329
diff changeset
     4
Tree view with sensible defaults.
81323
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
     5
*/
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
     6
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
     7
package isabelle
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
     8
83032
a29b383ad733 more standard treatment of AccessibleContext, following existing Swing components;
wenzelm
parents: 83025
diff changeset
     9
import javax.accessibility.AccessibleContext
a29b383ad733 more standard treatment of AccessibleContext, following existing Swing components;
wenzelm
parents: 83025
diff changeset
    10
81323
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
    11
import javax.swing.JTree
81329
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    12
import javax.swing.tree.{MutableTreeNode, DefaultMutableTreeNode, DefaultTreeModel,
82142
508a673c87ac removed unused imports;
wenzelm
parents: 81497
diff changeset
    13
  TreeSelectionModel, DefaultTreeCellRenderer}
81323
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
    14
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
    15
81329
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    16
object Tree_View {
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    17
  type Node = DefaultMutableTreeNode
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    18
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    19
  object Node {
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    20
    def apply(obj: AnyRef = null): Node =
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    21
      if (obj == null) new DefaultMutableTreeNode else new DefaultMutableTreeNode(obj)
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    22
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    23
    def unapply(tree_node: MutableTreeNode): Option[AnyRef] =
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    24
      tree_node match {
81380
83012fe97282 revert 1206400b9b48: proper Node.unapply for Node.apply(null);
wenzelm
parents: 81377
diff changeset
    25
        case node: Node => Some(node.getUserObject)
81329
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    26
        case _ => None
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    27
      }
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    28
  }
81496
76f360c63dfe clarified default: avoid copies;
wenzelm
parents: 81495
diff changeset
    29
81497
da807cf1e461 clarified signature and object initialization;
wenzelm
parents: 81496
diff changeset
    30
  class Cell_Renderer extends DefaultTreeCellRenderer {
da807cf1e461 clarified signature and object initialization;
wenzelm
parents: 81496
diff changeset
    31
    def setup(value: AnyRef): Unit = setIcon(null)
da807cf1e461 clarified signature and object initialization;
wenzelm
parents: 81496
diff changeset
    32
da807cf1e461 clarified signature and object initialization;
wenzelm
parents: 81496
diff changeset
    33
    override def getTreeCellRendererComponent(
da807cf1e461 clarified signature and object initialization;
wenzelm
parents: 81496
diff changeset
    34
      tree: JTree,
da807cf1e461 clarified signature and object initialization;
wenzelm
parents: 81496
diff changeset
    35
      value: AnyRef,
da807cf1e461 clarified signature and object initialization;
wenzelm
parents: 81496
diff changeset
    36
      selected: Boolean,
da807cf1e461 clarified signature and object initialization;
wenzelm
parents: 81496
diff changeset
    37
      expanded: Boolean,
da807cf1e461 clarified signature and object initialization;
wenzelm
parents: 81496
diff changeset
    38
      leaf: Boolean,
da807cf1e461 clarified signature and object initialization;
wenzelm
parents: 81496
diff changeset
    39
      row: Int,
da807cf1e461 clarified signature and object initialization;
wenzelm
parents: 81496
diff changeset
    40
      hasFocus: Boolean
da807cf1e461 clarified signature and object initialization;
wenzelm
parents: 81496
diff changeset
    41
    ): java.awt.Component = {
da807cf1e461 clarified signature and object initialization;
wenzelm
parents: 81496
diff changeset
    42
      super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus)
da807cf1e461 clarified signature and object initialization;
wenzelm
parents: 81496
diff changeset
    43
      setup(value)
da807cf1e461 clarified signature and object initialization;
wenzelm
parents: 81496
diff changeset
    44
      this
da807cf1e461 clarified signature and object initialization;
wenzelm
parents: 81496
diff changeset
    45
    }
81496
76f360c63dfe clarified default: avoid copies;
wenzelm
parents: 81495
diff changeset
    46
  }
81329
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    47
}
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    48
81325
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    49
class Tree_View(
81329
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    50
  val root: Tree_View.Node = Tree_View.Node(),
83032
a29b383ad733 more standard treatment of AccessibleContext, following existing Swing components;
wenzelm
parents: 83025
diff changeset
    51
  single_selection_mode: Boolean = false,
a29b383ad733 more standard treatment of AccessibleContext, following existing Swing components;
wenzelm
parents: 83025
diff changeset
    52
  accessible_name: String = ""
81325
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    53
) extends JTree(root) {
83032
a29b383ad733 more standard treatment of AccessibleContext, following existing Swing components;
wenzelm
parents: 83025
diff changeset
    54
a29b383ad733 more standard treatment of AccessibleContext, following existing Swing components;
wenzelm
parents: 83025
diff changeset
    55
  override def getAccessibleContext: AccessibleContext = {
a29b383ad733 more standard treatment of AccessibleContext, following existing Swing components;
wenzelm
parents: 83025
diff changeset
    56
    if (accessibleContext == null) { accessibleContext = new Accessible_Context }
a29b383ad733 more standard treatment of AccessibleContext, following existing Swing components;
wenzelm
parents: 83025
diff changeset
    57
    accessibleContext
a29b383ad733 more standard treatment of AccessibleContext, following existing Swing components;
wenzelm
parents: 83025
diff changeset
    58
  }
a29b383ad733 more standard treatment of AccessibleContext, following existing Swing components;
wenzelm
parents: 83025
diff changeset
    59
  class Accessible_Context extends AccessibleJTree {
a29b383ad733 more standard treatment of AccessibleContext, following existing Swing components;
wenzelm
parents: 83025
diff changeset
    60
    override def getAccessibleName: String =
a29b383ad733 more standard treatment of AccessibleContext, following existing Swing components;
wenzelm
parents: 83025
diff changeset
    61
      proper_string(accessible_name).getOrElse(proper_string(root.toString).orNull)
a29b383ad733 more standard treatment of AccessibleContext, following existing Swing components;
wenzelm
parents: 83025
diff changeset
    62
  }
a29b383ad733 more standard treatment of AccessibleContext, following existing Swing components;
wenzelm
parents: 83025
diff changeset
    63
81325
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    64
  def get_selection[A](which: PartialFunction[AnyRef, A]): Option[A] =
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    65
    getLastSelectedPathComponent match {
81380
83012fe97282 revert 1206400b9b48: proper Node.unapply for Node.apply(null);
wenzelm
parents: 81377
diff changeset
    66
      case Tree_View.Node(obj) if obj != null && which.isDefinedAt(obj) => Some(which(obj))
81325
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    67
      case _ => None
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    68
    }
81323
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
    69
81483
7d4df25af572 clarified Tree_View.init_model: more uniform;
wenzelm
parents: 81380
diff changeset
    70
  def init_model(body: => Unit): Unit = {
81325
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    71
    clearSelection()
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    72
    root.removeAllChildren()
81483
7d4df25af572 clarified Tree_View.init_model: more uniform;
wenzelm
parents: 81380
diff changeset
    73
    body
7d4df25af572 clarified Tree_View.init_model: more uniform;
wenzelm
parents: 81380
diff changeset
    74
    expandRow(0)
7d4df25af572 clarified Tree_View.init_model: more uniform;
wenzelm
parents: 81380
diff changeset
    75
    reload_model()
81325
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    76
  }
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    77
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    78
  def reload_model(): Unit =
81326
403203217493 more robust;
wenzelm
parents: 81325
diff changeset
    79
    getModel match {
403203217493 more robust;
wenzelm
parents: 81325
diff changeset
    80
      case model: DefaultTreeModel => model.reload(root)
403203217493 more robust;
wenzelm
parents: 81325
diff changeset
    81
      case _ =>
403203217493 more robust;
wenzelm
parents: 81325
diff changeset
    82
    }
81325
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    83
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    84
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    85
  /* init */
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    86
81497
da807cf1e461 clarified signature and object initialization;
wenzelm
parents: 81496
diff changeset
    87
  setCellRenderer(new Tree_View.Cell_Renderer)
81494
b30dc7db521f support for modified tree cell renderer;
wenzelm
parents: 81483
diff changeset
    88
81325
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    89
  setRowHeight(0)
81323
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
    90
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
    91
  if (single_selection_mode) {
81325
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    92
    getSelectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION)
81323
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
    93
  }
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
    94
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
    95
  // follow jEdit
82554
fa069e15c8da clarified signature;
wenzelm
parents: 82142
diff changeset
    96
  if (!GUI.is_macos_laf()) {
81325
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    97
    putClientProperty("JTree.lineStyle", "Angled")
81323
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
    98
  }
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
    99
}