I have a BeanTreeView that shows a some nodes. And I have another component that isn't netbeans-related that needs to know what is in the tree at this moment. Preferably just a straight list of the nodes in the tree. (Clarification; when I talk about nodes here I mean netbeans nodes, not the TreeNodes in a JTree.)
I haven't found any useful methods of doing this. I can't seem to get the information from the associated ExplorerManager that's connected to the BeanTreeView. So far I've subclassed the BeanTreeView and added the private method
private List<Object> getNodesAsList(){
LinkedList<Object> result = new LinkedList<>();
for (int i = 0; i < tree.getRowCount(); i++) {
TreePath pathForRow = tree.getPathForRow(i);
Object lastPathComponent = pathForRow.getLastPathComponent();
result.add(lastPathComponent);
}
return result;
}
The Object I get from getLastPathComponent is a VisualizerNode, which holds the node I want to get. But I can't cast to VisualizerNode since it's not public in org.openide.explorer.view. And it's final. And there's no getters for the node anyway...
Any ideas? I feel like there's something I've missed with the ExplorerManager...
Update; this worked for me, could probably be done more elegant. Thanks paddy!
private List<Node> getNodesInTree(){
LinkedList<Node> result = new LinkedList<>();
ExplorerManager em = ExplorerManager.find(this);
for (Node node : em.getRootContext().getChildren().getNodes()) {
result.add(node);
result.addAll(getChildNodesInTree(node));
}
return result;
}
private List<Node> getChildNodesInTree(Node root){
LinkedList<Node> result = new LinkedList<>();
if(root.getChildren().getNodesCount() > 0){
if(isExpanded(root)){
for (Node node : root.getChildren().getNodes()) {
result.add(node);
result.addAll(getChildNodesInTree(node));
}
}
}
return result;
}
You could use ExplorerManager.getRootContext()
. This returns the root Node shown in your ExplorerManager. From this node you could iterate through all the other nodes (using Node.getChildren()
) and create your own list. I am not aware of a function that handles that for you.