Wednesday, July 3, 2013

Nattable filter for tree grid


Nattable is one of the popular grid available for the eclipse platform which is highly configurable. But there isnt lot of documentation readily available for the grid except for their  Nattable Examples  which has example grid configuration and the corresponding source code. Their support forum on the eclipse community forum is really good and are highly responsive.

One of the examples that's missing in their site is the filter configuration for the tree grid. For the standard grid they have the filter example but not for the tree grid. Even though it can be modified easily for tree grid, this post just makes it readily available. The follwing code block shows only the changes needed on the TreeGridExample source in the Nattable Examples.

Code:

//Wrap the eventlist in a FilterList(import ca.odell.glazedlists.*)
//Here DataRow is the object that is bound to the grid. 
EventList eventList = GlazedLists.eventList( rows );
FilterList filterList = new FilterList<>(eventList);

.....
.....
//change the TreeList to use FilterList instead of TreeList
final TreeList treeList = new TreeList( filterList, new TreeFormat( sortModel ), new TreeExpansionModel() );
.....
.....
.....
//Then create a Filter row layer
ColumnHeaderLayer columnHeaderLayer = new ColumnHeaderLayer( columnHeaderDataLayer, viewportLayer, selectionLayer );
CompositeMatcherEditor autoFilterMatcherEditor = new CompositeMatcherEditor();
   filterList.setMatcherEditor(autoFilterMatcherEditor);
   
FilterRowHeaderComposite filterRowHeaderLayer =
    new FilterRowHeaderComposite(
      new DefaultGlazedListsFilterStrategy(autoFilterMatcherEditor, columnPropertyAccessor, configRegistry),
      columnHeaderLayer, columnHeaderDataProvider, configRegistry
    );
......
//In the SortHeader layer use the filter layer instead of columnheaderlayer
SortHeaderLayer sortHeaderLayer = new SortHeaderLayer<>( filterRowHeaderLayer, sortModel, false );
.........
//Modify hte CornerLayer to use the filter layer
DataLayer cornerDataLayer = new DataLayer( cornerDataProvider );
CornerLayer cornerLayer = new CornerLayer( cornerDataLayer, rowHeaderLayer, filterRowHeaderLayer );
// Note that the column header layer is the filterRowHeaderLayer
GridLayer gridLayer = new GridLayer( viewportLayer, filterRowHeaderLayer, rowHeaderLayer, cornerLayer);
   
Now if you run with these modifications on the example code as an RCP or Eclipe plugin, you will see a filter row on the tree grid.

No comments:

Post a Comment