src/Pure/GUI/tree_view.scala
author wenzelm
Tue, 19 Nov 2024 22:48:18 +0100
changeset 81495 57dc2c8ba7c6
parent 81494 b30dc7db521f
child 81496 76f360c63dfe
permissions -rw-r--r--
suppress odd icons for documents and folders;
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
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
     9
import javax.swing.JTree
81329
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    10
import javax.swing.tree.{MutableTreeNode, DefaultMutableTreeNode, DefaultTreeModel,
81494
b30dc7db521f support for modified tree cell renderer;
wenzelm
parents: 81483
diff changeset
    11
  TreeSelectionModel, TreeCellRenderer, DefaultTreeCellRenderer}
81323
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
    12
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
    13
81329
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    14
object Tree_View {
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    15
  type Node = DefaultMutableTreeNode
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    16
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    17
  object Node {
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    18
    def apply(obj: AnyRef = null): Node =
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    19
      if (obj == null) new DefaultMutableTreeNode else new DefaultMutableTreeNode(obj)
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    20
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    21
    def unapply(tree_node: MutableTreeNode): Option[AnyRef] =
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    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
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    24
        case _ => None
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    25
      }
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    26
  }
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    27
}
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    28
81325
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    29
class Tree_View(
81329
1775fdc7274e clarified signature;
wenzelm
parents: 81326
diff changeset
    30
  val root: Tree_View.Node = Tree_View.Node(),
81325
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    31
  single_selection_mode: Boolean = false
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    32
) extends JTree(root) {
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    33
  def get_selection[A](which: PartialFunction[AnyRef, A]): Option[A] =
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    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
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    36
      case _ => None
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    37
    }
81323
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
    38
81483
7d4df25af572 clarified Tree_View.init_model: more uniform;
wenzelm
parents: 81380
diff changeset
    39
  def init_model(body: => Unit): Unit = {
81325
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    40
    clearSelection()
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    41
    root.removeAllChildren()
81483
7d4df25af572 clarified Tree_View.init_model: more uniform;
wenzelm
parents: 81380
diff changeset
    42
    body
7d4df25af572 clarified Tree_View.init_model: more uniform;
wenzelm
parents: 81380
diff changeset
    43
    expandRow(0)
7d4df25af572 clarified Tree_View.init_model: more uniform;
wenzelm
parents: 81380
diff changeset
    44
    reload_model()
81325
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    45
  }
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    46
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    47
  def reload_model(): Unit =
81326
403203217493 more robust;
wenzelm
parents: 81325
diff changeset
    48
    getModel match {
403203217493 more robust;
wenzelm
parents: 81325
diff changeset
    49
      case model: DefaultTreeModel => model.reload(root)
403203217493 more robust;
wenzelm
parents: 81325
diff changeset
    50
      case _ =>
403203217493 more robust;
wenzelm
parents: 81325
diff changeset
    51
    }
81325
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    52
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    53
81494
b30dc7db521f support for modified tree cell renderer;
wenzelm
parents: 81483
diff changeset
    54
  /* cell renderer */
b30dc7db521f support for modified tree cell renderer;
wenzelm
parents: 81483
diff changeset
    55
81495
57dc2c8ba7c6 suppress odd icons for documents and folders;
wenzelm
parents: 81494
diff changeset
    56
  def render_tree_cell(renderer: DefaultTreeCellRenderer): Unit = {
57dc2c8ba7c6 suppress odd icons for documents and folders;
wenzelm
parents: 81494
diff changeset
    57
    renderer.setIcon(null)
57dc2c8ba7c6 suppress odd icons for documents and folders;
wenzelm
parents: 81494
diff changeset
    58
  }
81494
b30dc7db521f support for modified tree cell renderer;
wenzelm
parents: 81483
diff changeset
    59
b30dc7db521f support for modified tree cell renderer;
wenzelm
parents: 81483
diff changeset
    60
  private val tree_cell_renderer: TreeCellRenderer = new DefaultTreeCellRenderer {
b30dc7db521f support for modified tree cell renderer;
wenzelm
parents: 81483
diff changeset
    61
    override def getTreeCellRendererComponent(
b30dc7db521f support for modified tree cell renderer;
wenzelm
parents: 81483
diff changeset
    62
      tree: JTree,
b30dc7db521f support for modified tree cell renderer;
wenzelm
parents: 81483
diff changeset
    63
      value: AnyRef,
b30dc7db521f support for modified tree cell renderer;
wenzelm
parents: 81483
diff changeset
    64
      selected: Boolean,
b30dc7db521f support for modified tree cell renderer;
wenzelm
parents: 81483
diff changeset
    65
      expanded: Boolean,
b30dc7db521f support for modified tree cell renderer;
wenzelm
parents: 81483
diff changeset
    66
      leaf: Boolean,
b30dc7db521f support for modified tree cell renderer;
wenzelm
parents: 81483
diff changeset
    67
      row: Int,
b30dc7db521f support for modified tree cell renderer;
wenzelm
parents: 81483
diff changeset
    68
      hasFocus: Boolean
b30dc7db521f support for modified tree cell renderer;
wenzelm
parents: 81483
diff changeset
    69
    ): java.awt.Component = {
b30dc7db521f support for modified tree cell renderer;
wenzelm
parents: 81483
diff changeset
    70
      super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus)
b30dc7db521f support for modified tree cell renderer;
wenzelm
parents: 81483
diff changeset
    71
      render_tree_cell(this)
b30dc7db521f support for modified tree cell renderer;
wenzelm
parents: 81483
diff changeset
    72
      this
b30dc7db521f support for modified tree cell renderer;
wenzelm
parents: 81483
diff changeset
    73
    }
b30dc7db521f support for modified tree cell renderer;
wenzelm
parents: 81483
diff changeset
    74
  }
b30dc7db521f support for modified tree cell renderer;
wenzelm
parents: 81483
diff changeset
    75
b30dc7db521f support for modified tree cell renderer;
wenzelm
parents: 81483
diff changeset
    76
81325
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    77
  /* init */
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    78
81494
b30dc7db521f support for modified tree cell renderer;
wenzelm
parents: 81483
diff changeset
    79
  setCellRenderer(tree_cell_renderer)
b30dc7db521f support for modified tree cell renderer;
wenzelm
parents: 81483
diff changeset
    80
81325
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    81
  setRowHeight(0)
81323
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
    82
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
    83
  if (single_selection_mode) {
81325
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    84
    getSelectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION)
81323
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
    85
  }
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
    86
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
    87
  // follow jEdit
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
    88
  if (!GUI.is_macos_laf) {
81325
458e9e3b356e clarified signature;
wenzelm
parents: 81323
diff changeset
    89
    putClientProperty("JTree.lineStyle", "Angled")
81323
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
    90
  }
33fbf90fbc1d clarified signature: more explicit types;
wenzelm
parents:
diff changeset
    91
}