Java TreeNode: how to prevent getChildCount from expensive work?

I am writing a Java tree in which tree nodes can have children that take a lot of time to compute (in this case, it is a file system where there can be network timeouts that prevent getting a list of files from an attached ride).

The problem I find is this:

  • getChildCount() is called before the user requests a specific tree branch. I believe this is done, so JTree knows whether to show the + icon next to node.

  • Accurate child counting from getChildCount() would have to perform a potentially costly operation

  • If I fake the getChildCount() value, the tree allocates space for this set of child nodes before requesting an enumeration of child elements. (If I return '1', I will see only 1 child, despite the fact that there are more)

Listing children can be expensive and time consuming, I'm fine. But getChildCount() does not suit me to find out the exact number of children.

How can I get around this?

Added: Another problem is that if one of the nodes is a floppy drive (how archaic!), The disk will be polled before the user requests its files; if there is no disk in the drive, this will result in a system error.

Update: Unfortunately, implementing a TreeWillExpand listener is not a solution. This may allow you to veto the extension, but the number of nodes displayed is still limited by the value returned by TreeNode.getChildCount() .

+4
source share
4 answers

There are several parts to the solution:

  • As Lorenzo Boccaccia said, use TreeWillExpandListener

  • You also need to call nodeWereInserted in the tree, so the corresponding number of nodes will be displayed. See this code

  • I decided that if you do not know the counter of children, TreeNode.getChildCount () should return at least 1 (it cannot return 0)

0
source

http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html#data

scroll down a bit, there is an exact guide on creating lazy boot nodes for jtree with examples and documentation.

+3
source

I'm not sure that it is fully applicable, but I recently worked on problems with a slow tree, having previously calculated the answers to methods that usually require going through a list of children. I only reprogram them when children are added or removed or updated. In my case, some of the methods would have to recursively go through the tree to figure out how β€œhow many bytes are stored” for each node.

0
source

If you need a lot of access to a specific function of your data structure that is expensive to calculate, it may be advisable to pre-calculate it.

In the case of TreeNodes, this means that your TreeNodes will need to store their Child counter. Explain this in more detail: when creating node n0 this node has a child group ( cc ) equal to 0. When you add node n1 as a child of this, you are n1.cc + cc++ .

The complex bit is the delete operation. You must maintain parent backlinks and go up the hierarchy to subtract the cc your current node.

If you just want to use the hasChildren function for your nodes or override getChildCount , a logical element may be enough and will not force you to raise the entire hierarchy if deleted. Or you can remove backlinks and just say that you lose accuracy when deleting operations. The TreeNode interface TreeNode n't really force you to perform a delete operation, but you probably want to anyway.

Good thing the deal. In order to come up with pre-calculated exact values, you will need to maintain backlinks. If you do not, better call your hasHadChildren method or the more fun isVirgin .

0
source

Source: https://habr.com/ru/post/1277335/


All Articles