public class TreeHelper
extends java.lang.Object
Constructor and Description |
---|
TreeHelper() |
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.
|
@NotNull public static javax.swing.tree.TreePath nodeToPath(@NotNull javax.swing.tree.TreeNode node)
node
- tree node for which the path is requested@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)
model
- tree modelcurrentNode
- current node, if null
the search starts with the root node,
if not the first possible find is the node following this nodechecker
- checker which tells whether a node fulfills the search criteria
(e.g. if you are only interested in leafs, just check for that)null
if no following node fulfills the criteriagetNextMatchCycling(TreeModel, TreeNode, Predicate)
@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)
currentNode
if nothing
is found following it.model
- tree modelcurrentNode
- current node, if null
the search starts with the root node,
if not the first possible find is the node following this nodechecker
- checker which tells whether a node fulfills the search criteria
(e.g. if you are only interested in leafs, just check for that)currentNode
if it's the only node fulfilling it, or null
if no node (including
currentNode
) fulfills the criteriagetNextMatch(TreeModel, TreeNode, Predicate)
@Nullable public static javax.swing.tree.TreeNode nextNode(@NotNull javax.swing.tree.TreeNode node)
node
- current node@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)
model
- tree modelchecker
- node checkerpublic static void traverse(@NotNull javax.swing.tree.TreeModel model, @NotNull java.util.function.Consumer<javax.swing.tree.TreePath> consumer)
model
- tree model to traverse overconsumer
- consumer to be called for nodepublic static void traverse(@NotNull javax.swing.tree.TreePath path, @NotNull java.util.function.Consumer<javax.swing.tree.TreePath> consumer)
path
- tree path to traverseconsumer
- consumer called for each nodepublic static boolean isDisplayed(@NotNull javax.swing.JTree tree, javax.swing.tree.TreePath path)
tree
- tree for which the node is definedpath
- path to nodetrue
if the node is expanded and displayed in the viewportfalse
otherwisepublic static boolean pathStartsWith(@NotNull javax.swing.tree.TreePath path, @NotNull javax.swing.tree.TreePath prefix)
path
a (grand*)child of prefix
(or the same)?path
- path to checkprefix
- prefix path to checktrue
if path
itself or one of its successive parents equals prefix
,false
otherwise