Package swingtree

Class TreeSelectionDelegate<I,N>

java.lang.Object
swingtree.TreeSelectionDelegate<I,N>
Type Parameters:
I - The identity type of the nodes, which selection paths are made of.
N - The common node type of the tree.

public final class TreeSelectionDelegate<I,N> extends Object
Everything a selection change in a bound JTree has to say, handed to the Action registered through UIForTree.onSelection(sprouts.Action).

It speaks entirely in the user's own node values. The handles a bound tree keeps inside its TreePaths are an internal matter and never surface here, which is also why JTree.getLastSelectedPathComponent() is not a useful thing to call on a bound tree.

Note that for the common case of mirroring the selection into a view model there is no need for this delegate at all: bind a property with UIForTree.withSelection(Var) instead and the two stay in sync by themselves.

  • Method Summary

    Modifier and Type
    Method
    Description
    The node the user just moved the selection onto, which is empty when the selection was cleared rather than moved.
    sprouts.Tuple<I>
    The identity of the selected position: the ids leading from the top of the tree down to the lead() node, the root's own id first — or, in a forest, the id of the top level node it sits under.
    sprouts.Tuple<N>
    The chain of nodes from the top of the tree down to (and including) the lead() node, which is how you learn where in the structure the selection landed.
    Optional<sprouts.Var<N>>
    A writable property focused on the selected node, so that an action reacting to a selection can go straight on to edit what was selected, and the edit lands in the one property the tree is bound to.
    sprouts.Tuple<N>
    Every currently selected node, in the order the tree reports them.
    sprouts.Tuple<sprouts.Tuple<I>>
    The identity of every selected position, in the order the tree reports them.
     
    The tree whose selection changed, in case you need to reach past this delegate.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Method Details

    • tree

      public JTree tree()
      The tree whose selection changed, in case you need to reach past this delegate.
      Returns:
      The JTree this selection belongs to.
    • lead

      public Optional<N> lead()
      The node the user just moved the selection onto, which is empty when the selection was cleared rather than moved.
      Returns:
      The node the selection now leads with.
    • selection

      public sprouts.Tuple<N> selection()
      Every currently selected node, in the order the tree reports them. For a single selection tree this holds at most one node, and it is empty when nothing is selected.
      Returns:
      All selected nodes.
    • pathToLead

      public sprouts.Tuple<N> pathToLead()
      The chain of nodes from the top of the tree down to (and including) the lead() node, which is how you learn where in the structure the selection landed. It is empty when the selection was cleared. A forest has no root, so its trails begin at the top level node the selection sits under.
      Returns:
      The nodes leading from the top of the tree to the selected node.
    • leadPath

      public sprouts.Tuple<I> leadPath()
      The identity of the selected position: the ids leading from the top of the tree down to the lead() node, the root's own id first — or, in a forest, the id of the top level node it sits under. This is the same value a property bound with UIForTree.withSelection(sprouts.Var) receives, and it is empty when the selection was cleared rather than moved.

      Where pathToLead() answers "what is selected", this answers "which position is selected" — the question a node value cannot answer on its own, because the same value may sit in several places at once.

      Returns:
      The ids leading to the selected node.
    • selectionPaths

      public sprouts.Tuple<sprouts.Tuple<I>> selectionPaths()
      The identity of every selected position, in the order the tree reports them. This is what a property bound with UIForTree.withSelectionPaths(sprouts.Var) receives.
      Returns:
      One path of ids per selected node.
    • property

      public Optional<sprouts.Var<N>> property()
      A writable property focused on the selected node, so that an action reacting to a selection can go straight on to edit what was selected, and the edit lands in the one property the tree is bound to.

      It is empty when nothing is selected, or when the tree was bound to a read only Val, in which case there is nothing to write into.

      Returns:
      A lens property onto the selected node.
    • toString

      public String toString()
      Overrides:
      toString in class Object