eclipsejfacercptreeviewertreepath

when is the filter(Viewer,TreePath,Object[]) method called?


I have created and added a new filter to my TreeViewer object. It works fine but I cannot seem to understand when is the filter(Viewer,TreePath,Object[]) called.

So the filter is actually an instance of a class which extends ViewerFilter and needs to implement the abstract method select. In my case, select always returns true and I filter the elements by overriding the public Object[] filter(Viewer viewer, Object parent, Object[] elements).

However, when I try to override the public Object[] filter(Viewer viewer, TreePath parentPath, Object[] elements), this function doesn't get called at all.

Here's how it looks:

    myTreeViewer.addFilter(new ViewerFilter()
    {
        @Override
        public Object[] filter(Viewer viewer, TreePath parentPath, Object[] elements)
        {
            /* doesn't get called */
            return elements;
        }

        @Override
        public Object[] filter(Viewer viewer, Object parent, Object[] elements)
        {
            /* gets called normally */
            return elements;
        }

        @Override
        public boolean select(Viewer arg0, Object arg1, Object arg2)
        {
            // TODO Auto-generated method stub
            return true;
        }
    });

I need to override the filter method with the TreePath argument in order to be afle to filter elements based on a specified level.

In the documentation I could only find the following explanation:

Filters the given elements for the given viewer. The input array is not modified. The default implementation of this method calls filter(Viewer, Object, Object[]) with the parent from the path. Subclasses may override

Later edit

In the end I could not find any way of having this function called so I implemented a workaround: I created a wrapper over my model instances that also has a "parent" field. Whenever I insert my instances into the tree (in my getChildren and getElements methods), I actually create instances of my wrapper and set the parents accordingly.

This way, whenever the hasChildren gets called, I can navigate from the current node to the root.


Solution

  • Looking at the source code the Eclipse 'search references' says that nothing calls the TreePath version of filter.

    Your ITreeContentProvider has a getParent(object) method which you can use to traverse the tree.