Wednesday, August 19, 2009

Applying filters to trees and tables in Eclipse RCP (3.4 - Ganymede)


This article talks about applying filters to trees and tables. Basically, this would apply to any viewer (which extends StructuredViewer) in Eclipse RCP framework.

Implementing filter:

All filters extend from ViewerFilter and need to implement select method. select method looks like this:

public boolean select(final Viewer viewer, final Object parentElement, final Object element)

Here viewer is the table or tree viewer from your view.
parentElement is the parent node (for the node to be selected) in the tree and for table it is the whole content of the table
element is the current node (to be selected) in the tree and for table it is the current row in the table.

element and parentElement are actually model objects which form the content in your content provider. This method needs to return true if the node/row needs to be selected based on the filter criteria.

Say you have a tree which displays names and we are writing a filter to only display names in the tree, which starts with "XYZ", then the filter class might look like this:

public class NameFilter extends ViewerFilter {

* @see ViewerFilter#select(Viewer, Object, Object)
public boolean select(final Viewer viewer, final Object parentElement, final Object element) {
if (element instanceof NameNode) { // Assume your tree node is of type NameNode
NameNode node = (NameNode) element;
String nodeName = node.getName();
return nodeName.startsWith("XYZ");

return false;

For a table, once you get the object (which represents the row) you can get the individual column values from the object, check the column value you want and return true or false depending on if the row needs to be displayed or not.

Attaching filter to the viewer:

Once you define the filter, next step is to attach the filter to the viewer. For example, in your view you could create a menu which lists filters. Each menuitem in the menu could be an Action (org.eclipse.jface.Action).

Your action can be like this. It basically updates the filter everytime when user clicks on the menuitem.

this.xyzNameFilter = new NameFilter(); // Creating filter instance

this.xyzNameAction = new Action("Names starting with XYZ") {
public void run() {
this.xyzNameAction .setChecked(false); // turned off initially

and updateFilter adds/removes the filter to the viewer.

public void updateFilter(final Action action) {
if (action == this.xyzNameAction) {
if (action.isChecked()) {
} else {

treeViewer.addFilter and removeFilter takes care of adding/removing filters. Good thing about filters is you can have multiple filters and they are chained in the order as you add them. The filter filters on the results from the previous filters.


This article explained about how to implement filters and adding them to your view. Though it talked about trees and tables, similar approach would work for any type of viewer.


Mike said...

Ok, so if you have a bunch of filters set on a tableviewer with about 20000 rows of data in the model, and now you want the Table to be repopulated with fresh data and the filters to no longer apply, how is this done efficiently? If you call resetFilters() on the table, it takes a while to back out all the filters on the model data. I basically want a fresh start on table data and filters without having to wait for all the adjustments based on the old model data. Know what i mean?


Watson Zion said...

If you request reset Filters on the fare, it takes a interval to am a source of strength out bodily the filters on the exemplar data. I basically desire a fresh propel on snack announcement and filters without having to warble for the most part the adjustments based on the old ideal data. A table, grid or tree is Marketing Research Paper Help hand me down to envision word in a structured way. NatTable is a frame of reference to entwine table, grid and tree controls. It has a rich dish fit for a king set and is designed to handle as a matter of fact large data sets.