javaswtjfacercptreeviewer

Update not happening properly for similar nodes in a TreeViewer


In a TreeViewer, there are two similar nodes. They use the same data object. When I select the bottom node and modify, it updates the top node but there is no change in the bottom node.

Before update:

Before update

After update:

After

I am using TreeViewer.refresh(problemNode) after modifying the name of node. Javadoc of TreeViewer.refresh(Object element) says, the tree is updated starting from the given element. Any reason, why it is updating the top node? Since, it uses the same data object, shouldn't both nodes be updated?


Solution

  • If you use the same object in two parts of a tree you can get this behavior.

    By 'same object' I mean anything where the equals and hashCode methods make the objects look the same.

    You can work around this by using an IElementComparer class for your tree viewer. With this you must implement

    public boolean equals(Object a, Object b);
    
    public int hashCode(Object element);
    

    and make sure that the comparer equals method returns false when comparing the two elements in the tree and the elements preferably have a different hashCode.

    Call TreeViewer.setComparer to tell the tree viewer about the comparer.