Don't add a child to a tree node if the child already exists in that tree node. This situations resulted from the addition of tabs... I'm not sure I like the fix. It repairs the problem after the fact. It might be better to figure out how to prevent the problem in the first place by, (probably) partitioning the node-maps by tabs.

This commit is contained in:
Robert C. Martin 2022-05-26 11:25:18 -05:00
parent ac1962b386
commit 041345517d
2 changed files with 45 additions and 5 deletions

View File

@ -280,4 +280,31 @@
)
)
(context "avoiding duplicate children in a node"
(it "does not find a child in an empty node"
(let [node (DefaultMutableTreeNode. nil)]
(should-not (node-contains? node 1))))
(it "finds first child"
(let [node (DefaultMutableTreeNode. nil)
child (DefaultMutableTreeNode. 1)
_ (.add node child)]
(should (node-contains? node 1))
(should-not (node-contains? node 2))))
(it "finds children from beginning to end"
(let [node (DefaultMutableTreeNode. nil)
child1 (DefaultMutableTreeNode. 1)
child2 (DefaultMutableTreeNode. 2)
child3 (DefaultMutableTreeNode. 3)
_ (.add node child1)
_ (.add node child2)
_ (.add node child3)]
(should (node-contains? node 1))
(should (node-contains? node 2))
(should (node-contains? node 3))
(should-not (node-contains? node 4))))
)
)

View File

@ -131,12 +131,25 @@
(defn add-orphaned-reference [referent id]
(swap! ui-context update-in [:orphaned-references referent] conj id))
(defn node-contains? [node id]
(loop [child-indeces (range (.getChildCount node))]
(if (empty? child-indeces)
false
(let [child-index (first child-indeces)
child (.getChildAt node child-index)
child-id (.getUserObject child)]
(if (= child-id id)
true
(recur (rest child-indeces)))))
))
(defn add-this-node-to-reference-nodes [reference-nodes this-id]
(loop [nodes reference-nodes]
(if (empty? nodes)
nil
(let [node (first nodes)
child (DefaultMutableTreeNode. this-id)]
(.add ^DefaultMutableTreeNode node child)
(swap! ui-context update-in [:node-map this-id] conj child)
(recur (rest nodes))))))
(when-not (node-contains? (first nodes) this-id)
(let [node (first nodes)
child (DefaultMutableTreeNode. this-id)]
(.add ^DefaultMutableTreeNode node child)
(swap! ui-context update-in [:node-map this-id] conj child)
(recur (rest nodes)))))))