Hiding empty parents - JFace's TreeViewer

2019-07-03 10:55发布

问题:

I'm creating a tree viewer in JFace and I want to achieve something.

I use filter in it. The filter works well, but not perfectly. When I use filter.expandAll() method, the filter filters all of the tree's items, but there's something really annoying. Although it filters all of the tree's items, it doesn't hide item parents, that become now empty.

Example:

As you can see, there's only one item that matches the filter: "General/Map/New". But all of the parents are now shown although that they actually doesn't contain any children.

Part of the code of my filter:

if (element instanceof HotkeyCategory) {
    return true;
} else if (element instanceof HotkeyItem) {
    return ((HotkeyItem) element).getLabel().toLowerCase()
            .matches(searchString.toLowerCase());
}

return false;

As you can see above, I permit all of the childrens' parents. But maybe there's some automagic way of hiding empty parents?

I had some ideas. Do the filtering before redirecting it to tree viewer and create ArrayList with permitted items and their parents OR adding boolean show; to every item and every parent and return it by the filter, but none of these methods satisify me. They're IMHO a bit too complicated for this task.

So, my question is: is there any simple way to achieve what I want?

Regards.

回答1:

Take a look at this. It might help you: Vogella Blog: I like my trees filtered (Eclipse FilteredTree)