de.caff.gimmicks.swing

## Class TreeHelper

- java.lang.Object
- - de.caff.gimmicks.swing.TreeHelper

-     public class TreeHelper
      extends java.lang.Object

  Some useful methods for JTree.
  Author:
    Rammi

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `TreeHelper()`

  - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static java.util.Collection<javax.swing.tree.TreeNode>` | `getMatches(javax.swing.tree.TreeModel model, java.util.function.Predicate<javax.swing.tree.TreeNode> checker)` Traverse the tree and get all nodes fulfilling a check.
    `static javax.swing.tree.TreeNode` | `getNextMatch(javax.swing.tree.TreeModel model, javax.swing.tree.TreeNode currentNode, java.util.function.Predicate<javax.swing.tree.TreeNode> checker)` Traverse the tree and get the first following node which fulfills a check.
    `static javax.swing.tree.TreeNode` | `getNextMatchCycling(javax.swing.tree.TreeModel model, javax.swing.tree.TreeNode currentNode, java.util.function.Predicate<javax.swing.tree.TreeNode> checker)` Traverse the tree and get the first node which fulfills a check.
    `static boolean` | `isDisplayed(javax.swing.JTree tree, javax.swing.tree.TreePath path)` Is a node currently visible for the user?
    `static javax.swing.tree.TreeNode` | `nextNode(javax.swing.tree.TreeNode node)` Get the next node when traversing the tree depth first.
    `static javax.swing.tree.TreePath` | `nodeToPath(javax.swing.tree.TreeNode node)` Get the path of a tree node.
    `static boolean` | `pathStartsWith(javax.swing.tree.TreePath path, javax.swing.tree.TreePath prefix)` Does a tree path start with a given prefix?
    `static void` | `traverse(javax.swing.tree.TreeModel model, java.util.function.Consumer<javax.swing.tree.TreePath> consumer)` Traverse over the tree and call a consumer for each node.
    `static void` | `traverse(javax.swing.tree.TreePath path, java.util.function.Consumer<javax.swing.tree.TreePath> consumer)` Traverse over a (sub) tree and call a consumer for each node.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### TreeHelper

          public TreeHelper()

  - ### Method Detail

    - #### nodeToPath

          @NotNull
          public static javax.swing.tree.TreePath nodeToPath(@NotNull
                                                                      javax.swing.tree.TreeNode node)

      Get the path of a tree node.
      Parameters:
        `node` - tree node for which the path is requested
      Returns:
        tree path of the given node

    - #### getNextMatch

          @Nullable
          public static javax.swing.tree.TreeNode getNextMatch(@NotNull
                                                                         javax.swing.tree.TreeModel model,
                                                                         @Nullable
                                                                         javax.swing.tree.TreeNode currentNode,
                                                                         @NotNull
                                                                         java.util.function.Predicate<javax.swing.tree.TreeNode> checker)

      Traverse the tree and get the first following node which fulfills a check. This method performs a depth-first traversal.
      Parameters:
        `model` - tree model
        `currentNode` - current node, if `null` the search starts with the root node, if not the first possible find is the node following this node
        `checker` - checker which tells whether a node fulfills the search criteria (e.g. if you are only interested in leafs, just check for that)
      Returns:
        the first node found which fulfills the search criteria, `null` if no following node fulfills the criteria
      See Also:
        `getNextMatchCycling(TreeModel, TreeNode, Predicate)`

    - #### getNextMatchCycling

          @Nullable
          public static javax.swing.tree.TreeNode getNextMatchCycling(@NotNull
                                                                                javax.swing.tree.TreeModel model,
                                                                                @Nullable
                                                                                javax.swing.tree.TreeNode currentNode,
                                                                                @NotNull
                                                                                java.util.function.Predicate<javax.swing.tree.TreeNode> checker)

      Traverse the tree and get the first node which fulfills a check. This method performs a depth-first traversal. This will cycle through nodes before the given `currentNode` if nothing is found following it.
      Parameters:
        `model` - tree model
        `currentNode` - current node, if `null` the search starts with the root node, if not the first possible find is the node following this node
        `checker` - checker which tells whether a node fulfills the search criteria (e.g. if you are only interested in leafs, just check for that)
      Returns:
        the first node found which fulfills the search criteria, maybe `currentNode` if it's the only node fulfilling it, or `null` if no node (including `currentNode`) fulfills the criteria
      See Also:
        `getNextMatch(TreeModel, TreeNode, Predicate)`

    - #### nextNode

          @Nullable
          public static javax.swing.tree.TreeNode nextNode(@NotNull
                                                                     javax.swing.tree.TreeNode node)

      Get the next node when traversing the tree depth first.
      Parameters:
        `node` - current node
      Returns:
        the node following the given node

    - #### getMatches

          @NotNull
          public static java.util.Collection<javax.swing.tree.TreeNode> getMatches(@NotNull
                                                                                            javax.swing.tree.TreeModel model,
                                                                                            @NotNull
                                                                                            java.util.function.Predicate<javax.swing.tree.TreeNode> checker)

      Traverse the tree and get all nodes fulfilling a check.
      Parameters:
        `model` - tree model
        `checker` - node checker
      Returns:
        all nodes for which the node checker considered a match

    - #### traverse

          public static void traverse(@NotNull
                                      javax.swing.tree.TreeModel model,
                                      @NotNull
                                      java.util.function.Consumer<javax.swing.tree.TreePath> consumer)

      Traverse over the tree and call a consumer for each node.
      Parameters:
        `model` - tree model to traverse over
        `consumer` - consumer to be called for node

    - #### traverse

          public static void traverse(@NotNull
                                      javax.swing.tree.TreePath path,
                                      @NotNull
                                      java.util.function.Consumer<javax.swing.tree.TreePath> consumer)

      Traverse over a (sub) tree and call a consumer for each node.
      Parameters:
        `path` - tree path to traverse
        `consumer` - consumer called for each node

    - #### isDisplayed

          public static boolean isDisplayed(@NotNull
                                            javax.swing.JTree tree,
                                            javax.swing.tree.TreePath path)

      Is a node currently visible for the user?
      Parameters:
        `tree` - tree for which the node is defined
        `path` - path to node
      Returns:
        `true` if the node is expanded and displayed in the viewport
        `false` otherwise

    - #### pathStartsWith

          public static boolean pathStartsWith(@NotNull
                                               javax.swing.tree.TreePath path,
                                               @NotNull
                                               javax.swing.tree.TreePath prefix)

      Does a tree path start with a given prefix? I.e. is the tree node defined by `path` a (grand*)child of `prefix` (or the same)?
      Parameters:
        `path` - path to check
        `prefix` - prefix path to check
      Returns:
        `true` if `path` itself or one of its successive parents equals `prefix`,
        `false` otherwise