GT-3332 Fixed several issues with GTreeNodes

This commit is contained in:
ghidravore 2019-11-15 13:10:40 -05:00
parent 5feab045d2
commit b5a7246523
4 changed files with 80 additions and 12 deletions

View file

@ -16,7 +16,6 @@
package docking.widgets.tree;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Stream;
import javax.swing.Icon;
@ -36,15 +35,40 @@ import util.CollectionUtils;
* <P>
* All methods in this class that mutate the children node must perform that operation in
* the swing thread.
* <P>
* To create a simple GTreeNode where nodes will be added immediately
* using the addNode() methods, simply extend this class and implement the following methods:
* <ul>
* <li>getName()</li>
* <li>getToolTip()</li>
* <li>isLeaf()</li>
* <li>getIcon()</li>
* </ul>
*
* <a name="usage"></a>Usage Notes:
* <ul>
* <li>The <b><tt>equals()</tt></b> method: The <tt>GTree</tt> 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 the <tt>GTree</tt>. Java will do this by using the <tt>equals()</tt> method.
* There is a potential problem with this usage. If nodes within the <tt>GTree</tt> 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 default <tt>equals()</tt>
* method on <tt>GTreeNode</tt> performs a comparison based upon instances. To fix this problem,
* the {@link #equals()} method has been implemented such that nodes are considered equals if they have
* the same name. The {@link #hashCode()} method will return the hash of the name.
* <p><br>
* <p>
* There are two situations where the {@link #equals(Object)} and {@link #hashCode()} 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 the
* {@link #equals(Object)} and {@link #hashCode()} methods to make them more robust.
* <p><br>
* </li>
* </ul>
*/
public abstract class GTreeNode extends CoreGTreeNode implements Comparable<GTreeNode> {
private static AtomicLong NEXT_ID = new AtomicLong();
private final long id;
protected GTreeNode() {
id = NEXT_ID.incrementAndGet();
}
@Override
protected List<GTreeNode> generateChildren() {
@ -170,6 +194,9 @@ public abstract class GTreeNode extends CoreGTreeNode implements Comparable<GTre
* @return the total number of leaf nodes in the subtree from this node
*/
public int getLeafCount() {
if (isLeaf() || !isLoaded()) {
return 1;
}
int count = 0;
for (GTreeNode node : children()) {
count += node.getLeafCount();
@ -349,7 +376,7 @@ public abstract class GTreeNode extends CoreGTreeNode implements Comparable<GTre
@Override
public int hashCode() {
return (int) id;
return getName().hashCode();
}
@Override
@ -364,7 +391,7 @@ public abstract class GTreeNode extends CoreGTreeNode implements Comparable<GTre
return false;
}
GTreeNode other = (GTreeNode) obj;
return id == other.id;
return getName().equals(other.getName());
}
/**