added code to prevent adding duplicate children to a gtree node

This commit is contained in:
ghidravore 2020-10-05 16:27:09 -04:00
parent 42b8eb3096
commit cc65ab7e4a
2 changed files with 22 additions and 3 deletions

View File

@ -183,9 +183,7 @@ abstract class CoreGTreeNode implements Cloneable {
* @param node the node to add as a child to this node
*/
protected synchronized void doAddNode(GTreeNode node) {
children().add(node);
node.setParent((GTreeNode) this);
doFireNodeAdded(node);
doAddNode(children().size(), node);
}
/**
@ -196,6 +194,9 @@ abstract class CoreGTreeNode implements Cloneable {
*/
protected synchronized void doAddNode(int index, GTreeNode node) {
List<GTreeNode> kids = children();
if (kids.contains(node)) {
return;
}
int insertIndex = Math.min(kids.size(), index);
kids.add(insertIndex, node);
node.setParent((GTreeNode) this);

View File

@ -456,6 +456,24 @@ public class GTreeNodeTest {
assertNotEquals(nodeA.hashCode(), nodeB.hashCode());
}
@Test
public void testCantAddNodeTwice() {
node0 = new TestNode("No Dups");
int childCount = root.getChildCount();
root.addNode(node0);
assertEquals(childCount + 1, root.getChildCount());
// now make sure the count doesn't grow again
root.addNode(node0);
assertEquals(childCount + 1, root.getChildCount());
// try adding it with an index, still shouldn't get added
root.addNode(0, node0);
assertEquals(childCount + 1, root.getChildCount());
}
@Test
public void testCloneEquals() throws CloneNotSupportedException {
GTreeNode nodeA = new TestNode("AAA");