I am applying a ViewerFilter
to a tree of a few branches, but mostly leaves. The filter really applies to the leaves, using properties within the leaf. All branches are left untouched so that their leaves can appear.
我将ViewerFilter应用于几个分支的树,但主要是离开。过滤器确实适用于叶子,使用叶子内的属性。所有树枝都保持不变,以便它们的叶子可以出现。
However I would like to filter out branches that contain no selected leaves, and I can see no mechanism within ViewerFilter
that allows this.
但是我想过滤掉不包含选定叶子的分支,我在ViewerFilter中看不到允许这样做的机制。
Is this possible at all?
这有可能吗?
For example, given the notional tree below (where b
is a branch, a L
is a leaf)
例如,给定下面的概念树(其中b是分支,L是叶子)
b0
b1
L2
L4
L8
b2
L1
L3
L5
I would like to apply a ViewerFilter
that only selects even leaves and branches that contain even leaves. The resulting tree would be ..
我想应用一个ViewerFilter,它只选择包含偶数叶子的叶子和分支。结果树将是......
b0
b1
L2
L4
L8
.. where branch b2
does not display as it contains no selected children, but branches b0
and b1
do.
..其中分支b2不显示,因为它不包含选定的子项,但分支b0和b1不包含。
4 个解决方案
#1
class MyFilter extends ViewerFilter{
private boolean isLeaf(Object element){
// implement this
}
private boolean isEvenLeaf(Object leaf){
// implement this
}
@Override
public boolean select(Viewer viewer, Object parentElement, Object element){
if (isLeaf(element))
return isEventLeaf(element);
else {
StructuredViewer sviewer = (StructuredViewer) viewer;
ITreeContentProvider provider = (ITreeContentProvider) sviewer.getContentProvider();
for (Object child: provider.getChildren(element)){
if (select(viewer, element, child))
return true;
}
return false;
}
}
}
#2
Also have a look at org.eclipse.ui.dialogs.FilteredTree
which the right thing in regard to child leaves.
另外看看org.eclipse.ui.dialogs.FilteredTree哪个关于孩子的正确事物离开了。
#3
Yes, if you don't filter out the branch nodes, they'll be shown even if there are no leaves in it. If you want the filter to be permanently on, something you can consider is using the ITreeContentProvider as a filter.
是的,如果你没有过滤掉分支节点,即使它们没有叶子也会显示它们。如果您希望过滤器永久打开,您可以考虑使用ITreeContentProvider作为过滤器。
Since the content provider has both getChildren() and hasChildren() methods, you have a little more control.
由于内容提供程序同时具有getChildren()和hasChildren()方法,因此您可以获得更多控制权。
#4
I'm not sure what you mean by selected leaves. If you mean selected in the view, you can find this out by calling Viewer.getSelection(). The select method that you implement in your filter passes in the viewer, parent, and leaf. You should be able to use this information to decide if the leaf is selected or not and filter them out. If you can give some more information, I can probably answer with more detail.
我不确定你选择的叶子是什么意思。如果您的意思是在视图中选择,您可以通过调用Viewer.getSelection()来找到它。您在过滤器中实现的select方法将传递给viewer,parent和leaf。您应该能够使用此信息来决定是否选择了叶子并将其过滤掉。如果您可以提供更多信息,我可以回答更详细的信息。
#1
class MyFilter extends ViewerFilter{
private boolean isLeaf(Object element){
// implement this
}
private boolean isEvenLeaf(Object leaf){
// implement this
}
@Override
public boolean select(Viewer viewer, Object parentElement, Object element){
if (isLeaf(element))
return isEventLeaf(element);
else {
StructuredViewer sviewer = (StructuredViewer) viewer;
ITreeContentProvider provider = (ITreeContentProvider) sviewer.getContentProvider();
for (Object child: provider.getChildren(element)){
if (select(viewer, element, child))
return true;
}
return false;
}
}
}
#2
Also have a look at org.eclipse.ui.dialogs.FilteredTree
which the right thing in regard to child leaves.
另外看看org.eclipse.ui.dialogs.FilteredTree哪个关于孩子的正确事物离开了。
#3
Yes, if you don't filter out the branch nodes, they'll be shown even if there are no leaves in it. If you want the filter to be permanently on, something you can consider is using the ITreeContentProvider as a filter.
是的,如果你没有过滤掉分支节点,即使它们没有叶子也会显示它们。如果您希望过滤器永久打开,您可以考虑使用ITreeContentProvider作为过滤器。
Since the content provider has both getChildren() and hasChildren() methods, you have a little more control.
由于内容提供程序同时具有getChildren()和hasChildren()方法,因此您可以获得更多控制权。
#4
I'm not sure what you mean by selected leaves. If you mean selected in the view, you can find this out by calling Viewer.getSelection(). The select method that you implement in your filter passes in the viewer, parent, and leaf. You should be able to use this information to decide if the leaf is selected or not and filter them out. If you can give some more information, I can probably answer with more detail.
我不确定你选择的叶子是什么意思。如果您的意思是在视图中选择,您可以通过调用Viewer.getSelection()来找到它。您在过滤器中实现的select方法将传递给viewer,parent和leaf。您应该能够使用此信息来决定是否选择了叶子并将其过滤掉。如果您可以提供更多信息,我可以回答更详细的信息。