Class GTreeNode
- All Implemented Interfaces:
Cloneable
,Comparable<GTreeNode>
- Direct Known Subclasses:
DomainFileNode
,GTreeLazyNode
,InProgressGTreeNode
,InProgressGTreeRootNode
,NoProjectNode
addNode(GTreeNode)
or setChildren(List)
. For large trees, subclasses
should instead extend GTreeLazyNode
or GTreeSlowLoadingNode
All methods in this class that mutate the children node must perform that operation in the swing thread.
To create a simple GTreeNode where nodes will be added immediately using the addNode() methods, simply extend this class and implement the following methods:
- getName()
- getToolTip()
- isLeaf()
- getIcon()
- The
equals()
method: TheGTree
has the ability to remember expanded and selected states. This will only work if the nodes in the saved state can be matched with the nodes in theGTree
. Java will do this by using theequals()
method. There is a potential problem with this usage. If nodes within theGTree
get rebuilt ( i.e., new nodes are created), then, by default, the expanded and selected state feature will be unable to find the correct nodes, since the defaultequals()
method onGTreeNode
performs a comparison based upon instances. To fix this problem, theequals(Object)
method has been implemented such that nodes are considered equal if they have the same name (seegetName()
). ThehashCode()
method will return the hash of the name. The name attribute was chosen because it should be the most unique and descriptive piece of information available in a generic GTreeNode.
There are two situations where the
equals(Object)
andhashCode()
using the name are insufficient. One is if your tree implementation allows nodes with the same name with the same parent. The other possible situation is if your nodes can change their name, which may confuse the tree. If either of these situations apply, just override theequals(Object)
andhashCode()
methods to make them more robust.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds the given node at the given index as a child to this nodevoid
Adds the given node as a child to this node.void
Adds the given nodes as children to this nodechildren()
clone()
Creates a clone of this node.void
collapse()
Convenience method for collapsing (closing) this node in the tree.int
void
dispose()
protected void
Adds a node to this node's children at the given index and notifies the tree.protected void
Adds a node to this node's children.protected void
doAddNodes
(List<GTreeNode> nodes) Adds the given nodes to this node's children.protected void
doFireNodeAdded
(GTreeNode newNode) protected void
protected void
doFireNodeRemoved
(GTreeNode removedNode, int index) protected void
protected void
doRemoveNode
(GTreeNode node) Removes the node from this node's children and notifies the tree.protected void
doSetChildren
(List<GTreeNode> childList) Sets the children of this node to the given list of child nodes, but does not notify the tree.protected void
doSetChildrenAndFireEvent
(List<GTreeNode> childList) Sets the children of this node to the given list of child nodes and fires the appropriate tree event to kick the tree to update the display.boolean
void
expand()
Convenience method for expanding (opening) this node in the tree.filter
(GTreeFilter filter, TaskMonitor monitor) Generates a filtered copy of this node and its children.void
Notifies the tree that a node has changed, excluding its children.void
Notifies the tree that the node has different children.Subclasses implement this method to initially load the children.getChild
(int index) Returns the child node at the given index.Returns the child node of this node with the given name.Returns the child node of this node with the given name which satisfies predicate filter.int
Returns the number of visible children of this node.Returns all of the visible children of this node.Returns the display text for the node.abstract Icon
getIcon
(boolean expanded) Returns the Icon to be displayed for this node in the treeint
Returns the index of this node within its parent nodeint
getIndexOfChild
(GTreeNode node) Returns the index of the given node within this node.int
Returns the total number of leaf nodes in the subtree from this node.abstract String
getName()
Returns the name of the node.int
Returns the total number of nodes in the subtree rooted at this node.final GTreeNode
Returns the parent of this node.getRoot()
Returns the rootNode for this tree or null if there is no parent path to a root node.abstract String
Returns the string to be displayed as a tooltip when the user hovers the mouse on this node in the treegetTree()
Returns the GTree that this node is attached toReturns the TreePath for this nodeint
hashCode()
boolean
isAncestor
(GTreeNode node) Returns true if the given node is a child of this node or one of its children.boolean
Determine if this node may be auto-expanded.boolean
Returns true if this node is allowed to be edited in the tree.boolean
Convenience method determining if this node is expanded in a tree.final boolean
Returns true if the node is in the process of loading its children.abstract boolean
isLeaf()
Returns true if this node never has childrenboolean
isLoaded()
True if the children for this node have been loaded yet.final boolean
isRoot()
Returns true if this is a root node of a GTreeiterator
(boolean depthFirst) Returns an iterator of the GTree nodes in the subtree of this nodeint
loadAll
(TaskMonitor monitor) Causes any lazy or slow loading nodes in the tree to load their children so that the tree is fully loaded.void
Removes all children from this node.void
removeNode
(GTreeNode node) Remove the given node from this nodevoid
setChildren
(List<GTreeNode> childList) Sets the children on this node.stream
(boolean depthFirst) Returns a stream of the GTree nodes in the subtree of this nodetoString()
void
valueChanged
(Object newValue) Notification method called when a cell editor completes editing to notify this node that its value has changed.
-
Constructor Details
-
GTreeNode
public GTreeNode()
-
-
Method Details
-
generateChildren
Subclasses implement this method to initially load the children.- Returns:
- a list of the initial children for this node.
-
getDisplayText
Returns the display text for the node. By default, this is the same as the name of the node. The name of the node usually serves two purposes: 1) to uniquely identify the node (the identity) and 2) the display text (what you see in the tree). Sometimes, it is useful to display more information in the tree without affecting the nodes identity. In this case, you can override this method to return the "display" name, whilegetName()
will still return the name used to identify the node.- Returns:
- the display text for the node.
-
getName
Returns the name of the node. IfgetDisplayText()
is not overridden, then this is also the text that will be displayed in the tree for that node. In general, the name of a node should not change. If the text displayed in the tree changes over time, overridegetDisplayText()
.- Returns:
- the name of the node
-
getIcon
Returns the Icon to be displayed for this node in the tree- Parameters:
expanded
- true if the node is expanded- Returns:
- the icon to be displayed for this node in the tree
-
getToolTip
Returns the string to be displayed as a tooltip when the user hovers the mouse on this node in the tree- Returns:
- the tooltip to be displayed
-
isLeaf
public abstract boolean isLeaf()Returns true if this node never has children- Returns:
- true if this node is a leaf
-
compareTo
- Specified by:
compareTo
in interfaceComparable<GTreeNode>
-
addNode
Adds the given node as a child to this node. Note: this method may be inefficient so if you have many nodes to add, you should use eitheraddNodes(List)
orsetChildren(List)
- Parameters:
node
- the node to add as a child
-
addNodes
Adds the given nodes as children to this node- Parameters:
nodes
- the nodes to add
-
addNode
Adds the given node at the given index as a child to this node- Parameters:
index
- the index to place the nodenode
- the node to add as a child of this node
-
getChildren
Returns all of the visible children of this node. If there are filtered nodes, then they will not be returned.- Returns:
- all of the visible children of this node. If there are filtered nodes, then they will not be returned.
-
getChildCount
public int getChildCount()Returns the number of visible children of this node. Does not include nodes that are current filtered out- Returns:
- the number of visible children of this node
-
getChild
Returns the child node of this node with the given name.- Parameters:
name
- the name of the child to be returned- Returns:
- the child with the given name
-
getChild
Returns the child node of this node with the given name which satisfies predicate filter.- Parameters:
name
- the name of the child to be returnedfilter
- predicate filter- Returns:
- the child with the given name
-
getChild
Returns the child node at the given index. Returns null if the index is out of bounds.- Parameters:
index
- the index of the child to be returned- Returns:
- the child at the given index
-
getNodeCount
public int getNodeCount()Returns the total number of nodes in the subtree rooted at this node. Leaf nodes return 1.- Returns:
- the number of nodes from this node downward
-
getLeafCount
public int getLeafCount()Returns the total number of leaf nodes in the subtree from this node. Note that if any nodes are "lazy" (seeGTreeLazyNode
) and not currently loaded, then it will be considered as a leaf and return 1.- Returns:
- the total number of leaf nodes in the subtree from this node
-
getIndexInParent
public int getIndexInParent()Returns the index of this node within its parent node- Returns:
- the index of this node within its parent node
-
getIndexOfChild
Returns the index of the given node within this node. -1 is returned if the node is not a child of this node.- Parameters:
node
- whose index we want- Returns:
- the index of the given node within this node
-
getTreePath
Returns the TreePath for this node- Returns:
- the TreePath for this node
-
removeAll
public void removeAll()Removes all children from this node. The children nodes will be disposed. -
removeNode
Remove the given node from this node- Parameters:
node
- the to be removed
-
setChildren
Sets the children on this node. Any existing current children will be dispose.- Parameters:
childList
- this list of nodes to be set as children of this node
-
isAncestor
Returns true if the given node is a child of this node or one of its children.- Parameters:
node
- the potential descendant node to check- Returns:
- true if the given node is a child of this node or one of its children
-
valueChanged
Notification method called when a cell editor completes editing to notify this node that its value has changed. If you override this method you must also overrideisEditable()
.- Parameters:
newValue
- the new value provided by the cell editor- See Also:
-
isEditable
public boolean isEditable()Returns true if this node is allowed to be edited in the tree. You must override this method to allow a node to be edited. You must also overridevalueChanged(Object)
to handle the result of the edit.- Returns:
- true if this node is allowed to be edited in the tree
- See Also:
-
getRoot
Returns the rootNode for this tree or null if there is no parent path to a root node.- Returns:
- the rootNode for a tree of nodes in a
GTree
-
filter
public GTreeNode filter(GTreeFilter filter, TaskMonitor monitor) throws CancelledException, CloneNotSupportedException Generates a filtered copy of this node and its children.A node will be included if it or any of its descendants are accepted by the filter. NOTE: the filter will only be applied to a nodes children if they are loaded. So to perform a filter on all the nodes in the tree, the
loadAll(TaskMonitor)
should be called before the filter call.- Parameters:
filter
- the filter being appliedmonitor
- a TaskMonitor for tracking the progress and cancelling- Returns:
- A copy of this node and its children that matches the filter or null if this node and none of its children match the filter.
- Throws:
CancelledException
- if the operation is cancelled via the TaskMonitorCloneNotSupportedException
- if any nodes in the tree explicitly prevents cloning
-
loadAll
Causes any lazy or slow loading nodes in the tree to load their children so that the tree is fully loaded. Nodes that are already loaded (including normal nodes which are always loaded) do nothing except recursively callloadAll(TaskMonitor)
on their children.- Parameters:
monitor
- the TaskMonitor to monitor progress and provide cancel checking- Returns:
- the total number of nodes in the subtree of this node
- Throws:
CancelledException
- if the operation is cancelled using the monitor
-
hashCode
public int hashCode() -
equals
-
stream
Returns a stream of the GTree nodes in the subtree of this node- Parameters:
depthFirst
- if true, the nodes will be streamed in depth-first order, otherwise breadth-first order- Returns:
- a stream of the GTree nodes in the subtree of this node
-
iterator
Returns an iterator of the GTree nodes in the subtree of this node- Parameters:
depthFirst
- if true, the nodes will be returned in depth-first order, otherwise breadth-first order- Returns:
- an iterator of the GTree nodes in the subtree of this node
-
toString
-
fireNodeStructureChanged
public void fireNodeStructureChanged()Notifies the tree that the node has different children. -
fireNodeChanged
public void fireNodeChanged()Notifies the tree that a node has changed, excluding its children. If it has gained or lost children, then usefireNodeStructureChanged()
instead. -
expand
public void expand()Convenience method for expanding (opening) this node in the tree. If this node is not currently attached to a visible tree, then this call does nothing -
isAutoExpandPermitted
public boolean isAutoExpandPermitted()Determine if this node may be auto-expanded. Some special node cases may need to prevent or limit auto-expansion due to tree depth or other special conditions.- Returns:
- true if this node allows auto-expansion, else false.
-
collapse
public void collapse()Convenience method for collapsing (closing) this node in the tree. If this node is not currently attached to a visible tree, then this call does nothing -
isExpanded
public boolean isExpanded()Convenience method determining if this node is expanded in a tree. If the node is not currently attached to a visible tree, then this call returns false- Returns:
- true if the node is expanded in a currently visible tree.
-
getParent
Returns the parent of this node. Note: this method is deliberately not synchronized (See comments above)- Returns:
- the parent of this node.
-
isRoot
public final boolean isRoot()Returns true if this is a root node of a GTree- Returns:
- true if this is a root node of a GTree
-
children
-
doSetChildrenAndFireEvent
Sets the children of this node to the given list of child nodes and fires the appropriate tree event to kick the tree to update the display. Note: This method must be called from the swing thread because it will notify the underlying JTree.- Parameters:
childList
- the list of child nodes to assign as children to this node- See Also:
-
doSetChildren
Sets the children of this node to the given list of child nodes, but does not notify the tree. This method does not have to be called from the swing thread. It is intended to be used by background threads that want to populate all or part of the tree, but wait until the bulk operations are completed before notifying the tree.- Parameters:
childList
- the list of child nodes to assign as children to this node
-
doAddNode
Adds a node to this node's children. Must be called from the swing thread.- Parameters:
node
- the node to add as a child to this node
-
doAddNode
Adds a node to this node's children at the given index and notifies the tree. Must be called from the swing thread.- Parameters:
index
- the index at which to add the new nodenode
- the node to add as a child to this node
-
doRemoveNode
Removes the node from this node's children and notifies the tree. Must be called from the swing thread.- Parameters:
node
- the node to remove
-
doAddNodes
Adds the given nodes to this node's children. Must be called from the swing thread.- Parameters:
nodes
- the nodes to add to the children this node
-
clone
Creates a clone of this node. The clone should contain a shallow copy of all the node's attributes except that the parent and children are null.- Overrides:
clone
in classObject
- Returns:
- the clone of this object.
- Throws:
CloneNotSupportedException
- if some implementation prevents itself from being cloned.
-
dispose
public void dispose() -
isInProgress
public final boolean isInProgress()Returns true if the node is in the process of loading its children. SeeGTreeSlowLoadingNode
- Returns:
- true if the node is in the process of loading its children.
-
isLoaded
public boolean isLoaded()True if the children for this node have been loaded yet. Some GTree nodes are lazy in that they don't load their children until needed. Nodes that have the IN_PROGRESS node as it child is considered loaded if in the swing thread, otherwise they are considered not loaded.- Returns:
- true if the children for this node have been loaded.
-
getTree
Returns the GTree that this node is attached to- Returns:
- the GTree that this node is attached to
-
doFireNodeAdded
-
doFireNodeRemoved
-
doFireNodeStructureChanged
protected void doFireNodeStructureChanged() -
doFireNodeChanged
protected void doFireNodeChanged()
-