Columns & FilteredTree in Eclipse
I'm trying to create a FilteredTree
with several colum开发者_JS百科ns with no luck. If I use a TreeViewer
the columns work fine but when I switch to using a FilteredTree
I can only get it to work if I remove the columns, so I'm wondering if there is a way to use FilteredTree
with columns.
You can certainly use a FilteredTree with a column viewer. For example (and this also demonstrates preserving the use of a TreeColumnLayout using a FilteredTree):
final TreeColumnLayout tl = new TreeColumnLayout();
PatternFilter patternFilter = new PatternFilter();
patternFilter.setIncludeLeadingWildcard(true);
FilteredTree ft = new FilteredTree(treeViewerComposite, SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL | toolkit.getBorderStyle() | Window.getDefaultOrientation(), patternFilter, true) {
@Override
protected Control createTreeControl(Composite parent, int style) {
Control c = super.createTreeControl(parent, style);
c.setLayoutData(null);
c.getParent().setLayout(tl);
return c;
}
};
viewer = ft.getViewer();
viewer.getTree().setHeaderVisible(true);
Now you can just create TreeColumn's the usual way and your columns will appear.
You can do it the same way as with a normal Tree. Here is a full setup.
PatternFilter patternFilter = new PatternFilter();
patternFilter.setIncludeLeadingWildcard(true);
filteredTree = new FilteredTree(getContainer(), SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL, patternFilter, true);
TreeViewer viewer = filteredTree.getViewer();
tree = viewer.getTree();
tree.setHeaderVisible(true);
TreeColumn modelColumn = new TreeColumn(tree, SWT.LEFT);
modelColumn.setText("Model");
modelColumn.setWidth(400);
TreeViewerColumn treeViewerModelColumn = new TreeViewerColumn(viewer, modelColumn);
treeViewerModelColumn.setLabelProvider(new ColumnLabelProvider());
Yes, there is a way, and the solution it is outlined here: http://eclipsesource.com/blogs/2012/10/26/filtering-tables-in-swtjface/
The point is the default PatternFilter
needs tree leaves to be instance of ILabelProvider
. In a column-based treeviewer there is no single text representation for the tree leaf.
So, if you are using TreeViewerColumns
as tree columns, the PatternFilter
will use the ColumnLabelProvider
for each column. In this case you'll fix by subclassing the PatternFilter
and overriding it with your own isLeafMatch(..)
Otherwise, when using TreeColumns
to define columns, the TreeViewer
will need to have a ITableLabelProvider
. Beyond this, you'll subclass the PatternFilter
and, again, override the method isLeafMatch(..)
精彩评论