1

I have filter on my CheckboxTreeViewer and before applying the filter on the treeviewer I checked an item. My filter is working on textbox and using ModifyListener function while writing.

text.addModifyListener(new ModifyListener() {
    public void modifyText(ModifyEvent e) {
        refreshFilter();
    }
});

private void refreshFilter() {
    myFilter.setFilter(text.getText());
    checkboxTreeViewer.refresh();
}

Filter is working perfect but after applying filter my older selections becomes unchecked. Maybe situation is about LabelProvider or ContentProvider.

I tried to call checkboxTreeViewer.refresh(false); not worked.

Also I've read about collapse problem by refresh. Suggestion is implement an IElementComparer and override equals() and hashCode() functions and maybe uncheck problem can be resolved with this way.

Here is my ContentProvider:

private class MyTreeContentProvider implements ITreeContentProvider {

    @Override
    public void dispose() {

    }

    @Override
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

    }

    @Override
    public Object[] getElements(Object inputElement) {
        return ((List) inputElement).toArray(new Object[0]);
    }

    @Override
    public Object[] getChildren(Object parentElement) {
        if(parentElement instanceof FirstClass) {
            FirstClass is = (FirstClass)parentElement;
            return dbc.runJob(Util.byId(is.getId()).joinPrsl(true).joinIsltm(true)).getList().toArray(new SecondClass[0]);
        } else
            return new Object[0];
    }

    @Override
    public Object getParent(Object element) {
        return null;
    }

    @Override
    public boolean hasChildren(Object element) {            
        if(element instanceof SecondClass)
            return false;
        else
            return true;
    }

}

As conclusion, I can't solve my problem and I need your helps.

cgrgcn
  • 361
  • 2
  • 6
  • 24

1 Answers1

1

It looks like your getChildren method returns new objects for a given parent element each time it is called.

In this case the equals method of the new object you create must return true when it is given the previous object representing the same value. The hashCode method must also return the same hash value.

Sometimes it can be problematic to change these methods. In this case you can create an IElementComparer class to provide special equals and hashCode methods for the tree. Call the viewer setComparer method to set the comparer for the tree.

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • How should I implement these methods also where? there are `equals` and `hashCode` methods in `ContentProvider` class and also there is `IElementComparer` while setting Comparer for treeViewer. – cgrgcn Sep 03 '14 at 12:17
  • You implement them in the classes you return from `getChildren` and `getElements` - from the look of it `SecondClass` is one you need to do. Added something on IElementComparer to the answer. – greg-449 Sep 03 '14 at 12:24