javauser-interfaceswtjfacetreeviewer

CheckboxTreeViewer with multiple instances of same element in different branches


I have a CheckboxTreeViewer with multiple instances of the same element in different branches. My problem is when implementing the setSubtreeChecked elements in different branches are getting checked if they are under the same element in a different branch. Here's the structure:

-Parent1
    -MidLevel1
        -BottomLevel1
        -BottomLevel2
    -MidLevel2
        -BottomLevel1
        -BottomLevel2
- Parent2
     -MidLevel1
        -BottomLevel1
        -BottomLevel2
    -MidLevel2
        -BottomLevel1
        -BottomLevel2

So checking element MidLevel1 under Parent2 will check all of the elements under MidLevel1 under Parent1.

Also under this structure when using the setChecked(element, false) to uncheck everything in the tree not every element is unchecked when using getCheckedElements to get the elements.


Solution

  • You must make the objects returned by your content provider appear to be different to the tree viewer.

    You can do this by having your content provider return some unique object for each object in the tree (instead of the same object for multiple branches of the tree).

    Alternatively you can use call the viewer setComparer method with a class that implements IElementComparer. This lets you provide

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

    methods to distinguish between objects in the tree. This is probably more difficult to use in this case.