Added some convenience methods to GTree nodes.

This commit is contained in:
ghidravore 2020-01-10 16:43:59 -05:00
parent 3ce8d3fa39
commit 7cfa80f8ac
5 changed files with 143 additions and 7 deletions

View file

@ -22,7 +22,9 @@ import java.util.List;
* Also, children of this node can be unloaded by calling {@link #unloadChildren()}. This
* can be used by nodes in large trees to save memory by unloading children that are no longer
* in the current tree view (collapsed). Of course, that decision would need to be balanced
* against the extra time to reload the nodes in the event that a filter is applied.
* against the extra time to reload the nodes in the event that a filter is applied. Also, if
* some external event occurs that changes the set of children for a GTreeLazyNode, you can call
* {@link #reload()} to refresh the node's children.
*/
public abstract class GTreeLazyNode extends GTreeNode {
@ -36,6 +38,9 @@ public abstract class GTreeLazyNode extends GTreeNode {
/**
* Sets this lazy node back to the "unloaded" state such that if
* its children are accessed, it will reload its children as needed.
* NOTE: This method does not trigger a call to {@link #fireNodeChanged(GTreeNode, GTreeNode)}
* because doing will often trigger a call from the JTree will will immediately cause the node
* to reload its children. If that is the effect you want, call {@link #reload()}.
*/
public void unloadChildren() {
if (isLoaded()) {
@ -43,6 +48,19 @@ public abstract class GTreeLazyNode extends GTreeNode {
}
}
/**
* Tells this node that its children are stale and that it needs to regenerate them. This will
* unload any existing children and call {@link #fireNodeStructureChanged(GTreeNode)} which will
* inform the JTree that this node has changed and when the JTree queries this node for its children,
* the {@link #generateChildren()} will get called to populate the node.
*/
public void reload() {
if (isLoaded()) {
unloadChildren();
fireNodeStructureChanged(this);
}
}
@Override
public void addNode(GTreeNode node) {
if (isLoaded()) {
@ -66,9 +84,7 @@ public abstract class GTreeLazyNode extends GTreeNode {
@Override
public void removeAll() {
if (isLoaded()) {
unloadChildren();
}
reload();
}
@Override

View file

@ -441,6 +441,42 @@ public abstract class GTreeNode extends CoreGTreeNode implements Comparable<GTre
Swing.runNow(() -> doFireNodeChanged());
}
/**
* 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
*/
public void expand() {
GTree tree = getTree();
if (tree != null) {
tree.expandPath(this);
}
}
/**
* 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
*/
public void collapse() {
GTree tree = getTree();
if (tree != null) {
tree.collapseAll(this);
}
}
/**
* 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
*
* @return true if the node is expanded in a currently visible tree.
*/
public boolean isExpanded() {
GTree tree = getTree();
if (tree != null) {
return tree.isExpanded(this.getTreePath());
}
return false;
}
private GTreeNode[] getPathToRoot(GTreeNode node, int depth) {
GTreeNode[] returnNodes;

View file

@ -56,7 +56,7 @@ public class GTreeRestoreTreeStateTask extends GTreeTask {
monitor.setMessage("Restoring tree selection state");
selectPathsInThisTask(state, monitor, true);
// this allows some tress to perform cleanup
// this allows some trees to perform cleanup
tree.expandedStateRestored(monitor);
tree.clearFilterRestoreState();
}