Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » NatTable » Using FilterList for tree in NatTable
Using FilterList for tree in NatTable [message #989156] Tue, 04 December 2012 21:58 Go to next message
Rashmy A is currently offline Rashmy AFriend
Messages: 149
Registered: July 2009
Senior Member
Hello,
I have a requirement for tree display in NatTable. So started out with the example TreeGridExample that is provided by NatTable. Things look good but I wanted to add filtering support.

Modified the TreeGridExample to support filters
-Wrapped the TreeList <Datum> treeList in FilterList
-Added TextMatcherEditor
See code below for FilterTreeGridExample.java (unfortunately cannot attach file in this forum)

If you launch the attached example code, you will see that it does filter out things but the UI is messed up. It shows expand/collapse images incorrectly.

Now, I'm assuming this is because the TreeLayer actually shows/hides things based on its own list of 'hiddenRowIndexes'. Could you please suggest how I should proceed? Do I stop using FilterList and instead fire TreeExpandCollapseCommand to show/hide rows?
FilterTreeGridExample.java
public class FilterTreeGridExample extends AbstractNatExample {

	protected static final String NO_SORT_LABEL = "noSortLabel";
	
	public static void main(String[] args) {
		StandaloneNatExampleRunner.run(800, 400, new FilterTreeGridExample());
	}
	
	public Control createExampleControl(Composite parent) {
		ConfigRegistry configRegistry = new ConfigRegistry();
		configRegistry.registerConfigAttribute(SortConfigAttributes.SORT_COMPARATOR, new DefaultComparator());
		
		// Underlying data source
		createDatums();
		EventList<Datum> eventList = GlazedLists.eventList(datums.values());
		SortedList<Datum> sortedList = new SortedList<Datum>(eventList, null);
//		TreeList <RowDataFixture> treeList = new TreeList<RowDataFixture>(eventList, new RowDataFixtureTreeFormat(), new RowDataFixtureExpansionModel());
		
		String[] propertyNames = new String[] { "foo", "bar" };
		IColumnPropertyAccessor<Datum> columnPropertyAccessor = new ReflectiveColumnPropertyAccessor<Datum>(propertyNames);
		
		// Column header layer
		IDataProvider columnHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames);
		DataLayer columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(columnHeaderDataProvider);
		
		ISortModel sortModel = new GlazedListsSortModel<Datum>(
				sortedList,
				columnPropertyAccessor,
				configRegistry, 
				columnHeaderDataLayer);
		
		final TreeList <Datum> treeList = new TreeList<Datum>(sortedList, new DatumTreeFormat(sortModel), new DatumExpansionModel());
		GlazedListTreeData <Datum> treeData = new DatumTreeData(treeList);

		//-----------START: CODE TO SUPPORT FILTER -----------//
		//Create filter
		TextFilterator textFilterator = new TextFilterator<Datum>()
        {
			public void getFilterStrings(List<String> arg0, Datum arg1)
			{
				arg0.add( arg1.foo );
			}
        };
        CompositeMatcherEditor groupMatcherEditor = new CompositeMatcherEditor<Datum>();
        groupMatcherEditor.setMode( CompositeMatcherEditor.OR );

        TextMatcherEditor matcher1 = new TextMatcherEditor<Datum>(
                textFilterator );
        matcher1.setMode( TextMatcherEditor.CONTAINS );
        matcher1.setFilterText( new String[] { "A" } );
        TextMatcherEditor matcher2 = new TextMatcherEditor<Datum>(
                textFilterator );
        matcher2.setMode( TextMatcherEditor.EXACT );
        matcher2.setFilterText( new String[] { "C.1" } );
        TextMatcherEditor matcher3 = new TextMatcherEditor<Datum>(
                textFilterator );
        matcher3.setMode( TextMatcherEditor.EXACT );
        matcher3.setFilterText( new String[] { "root2" } );
        TextMatcherEditor matcher4 = new TextMatcherEditor<Datum>(
                textFilterator );
        matcher4.setMode( TextMatcherEditor.CONTAINS );
        matcher4.setFilterText( new String[] { "Y" } );

        groupMatcherEditor.getMatcherEditors().add( matcher1 );
        groupMatcherEditor.getMatcherEditors().add( matcher2 );
        groupMatcherEditor.getMatcherEditors().add( matcher3 );
        groupMatcherEditor.getMatcherEditors().add( matcher4 );

        EventList<MatcherEditor<Datum>> matcherEditors = new BasicEventList<MatcherEditor<Datum>>();
        matcherEditors.add( groupMatcherEditor );

        CompositeMatcherEditor filterMatcherEditor = new CompositeMatcherEditor<Datum>();
        filterMatcherEditor.getMatcherEditors().addAll( matcherEditors );
        filterMatcherEditor.setMode( CompositeMatcherEditor.OR );

        FilterList<Datum> filterList = new FilterList<Datum>( treeList );
		filterList.setMatcherEditor( filterMatcherEditor );
		//-----------END: CODE TO SUPPORT FILTER -----------//

		GlazedListsDataProvider<Datum> bodyDataProvider = new GlazedListsDataProvider<Datum>(filterList, columnPropertyAccessor);
		DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
		
		// Body layer
		ColumnReorderLayer columnReorderLayer = new ColumnReorderLayer(bodyDataLayer);
		ColumnHideShowLayer columnHideShowLayer = new ColumnHideShowLayer(columnReorderLayer);
		SelectionLayer selectionLayer = new SelectionLayer(columnHideShowLayer);
		
		// Switch the ITreeRowModel implementation between using native grid Hide/Show or GlazedList TreeList Hide/Show  
//		TreeLayer treeLayer = new TreeLayer(selectionLayer, new TreeRowModel<Datum>(treeData), true);
		TreeLayer treeLayer = new TreeLayer(selectionLayer, new GlazedListTreeRowModel<Datum>(treeData), false);
		
		ViewportLayer viewportLayer = new ViewportLayer(treeLayer);
		
		
		ColumnHeaderLayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, viewportLayer, selectionLayer);
		//	Note: The column header layer is wrapped in a filter row composite.
		//	This plugs in the filter row functionality
	

		ColumnOverrideLabelAccumulator labelAccumulator = new ColumnOverrideLabelAccumulator(columnHeaderDataLayer);
		columnHeaderDataLayer.setConfigLabelAccumulator(labelAccumulator);

		// Register labels
		SortHeaderLayer<Datum> sortHeaderLayer = new SortHeaderLayer<Datum>(
				columnHeaderLayer, 
				sortModel, 
				false);

		// Row header layer
		DefaultRowHeaderDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyDataProvider);
		DefaultRowHeaderDataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider);
		RowHeaderLayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, viewportLayer, selectionLayer);

		// Corner layer
		DefaultCornerDataProvider cornerDataProvider = new DefaultCornerDataProvider(columnHeaderDataProvider, rowHeaderDataProvider);
		DataLayer cornerDataLayer = new DataLayer(cornerDataProvider);
//		CornerLayer cornerLayer = new CornerLayer(cornerDataLayer, rowHeaderLayer, columnHeaderLayer);
		CornerLayer cornerLayer = new CornerLayer(cornerDataLayer, rowHeaderLayer, sortHeaderLayer);

		// Grid
		GridLayer gridLayer = new GridLayer(
				viewportLayer,
//				columnHeaderLayer,
				sortHeaderLayer,
				rowHeaderLayer,
				cornerLayer);
		
		NatTable natTable = new NatTable(parent, gridLayer, false);
		natTable.setConfigRegistry(configRegistry);
		natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
		natTable.addConfiguration(new HeaderMenuConfiguration(natTable));
		natTable.addConfiguration(new DefaultTreeLayerConfiguration(treeLayer.getModel()));
		natTable.addConfiguration(new SingleClickSortConfiguration());
		
		// Uncomment to see the native tree list printed to stout.
//		printTree(treeList, treeData);
		
		natTable.configure();
		return natTable;
	}

	private static class DatumTreeData extends GlazedListTreeData<Datum>{

		public DatumTreeData(TreeList<Datum> treeList) {
			super(treeList);
		}

		@Override
		public String formatDataForDepth(int depth, Datum object) {
			return object.getFoo();
		}

		
	}
	
	private static class DatumTreeFormat implements TreeList.Format<Datum> {
		
		private final ISortModel sortModel;

		public DatumTreeFormat(ISortModel sortModel) {
			this.sortModel = sortModel;
		}
		
		public void getPath(List<Datum> path, Datum element) {
			path.add(element);
			Datum parent = element.getParent();
			while (parent != null) {
				path.add(parent);
				parent = parent.getParent();
			}
			Collections.reverse(path);
		}
		
		public boolean allowsChildren(Datum element) {
			return true;
		}

		public Comparator<? extends Datum> getComparator(int depth) {
			return new SortableTreeComparator<Datum>(GlazedLists.beanPropertyComparator(Datum.class, "foo"), sortModel);
		}
	}

	private static class DatumExpansionModel implements TreeList.ExpansionModel<Datum> {
		public boolean isExpanded(Datum element, List<Datum> path) {
			return true;
		}

		public void setExpanded(Datum element,	List<Datum> path, boolean expanded) {
		}
	}
	
	private void printTree(TreeList <Datum> treeList, ITreeData<Datum> treeData){
		System.out.println(treeList.size());
		   for (int i = 0; i < treeList.size(); i++) {
	            final Datum location = treeList.get(i);
	            final int depth = treeList.depth(i);
	            final boolean hasChildren = treeList.hasChildren(i);
	            final boolean isExpanded = treeList.isExpanded(i);

	            for (int j = 0; j < depth; j++)
	                System.out.print("\t");

	            if (hasChildren)
	                System.out.print(isExpanded ? "- " : "+ ");
	            else
	                System.out.print("  ");

	            System.out.println(treeData.formatDataForDepth(depth, location));
	        }
	}
	
	public class Datum {
		/**
		 * 
		 */
		private final Datum parent;
		private String foo;
		private int bar;
		
		public Datum(Datum parent, String foo, int bar) {
			this.parent = parent;
			this.foo = foo;
			this.bar = bar;
		}
		
		public Datum getParent() {
			return parent;
		}
		
		public String getFoo() {
			return foo;
		}
		
		public int getBar() {
			return bar;
		}
		
		@Override
		public String toString() {
			return "[" +
					"parent=" + parent +
					", foo=" + foo +
					", bar=" + bar +
					"]";
		}
		
	}
	
	private Map<String, Datum> datums = new HashMap<String, Datum>();
	
	private void createDatum(String parent, String foo, int bar) {
		Datum datum = new Datum(datums.get(parent), foo, bar);
		datums.put(foo, datum);
	}
	
	private void createDatums() {
		createDatum(null, "root", 1);
			createDatum("root", "A", 10);
				createDatum("A", "A.1", 100);
				createDatum("A", "A.2", 110);
				createDatum("A", "A.3", 120);
			createDatum("root", "B", 20);
				createDatum("B", "B.1", 200);
				createDatum("B", "B.2", 210);
			createDatum("root", "C", 30);
				createDatum("C", "C.1", 330);
				createDatum("C", "C.2", 370);
				createDatum("C", "C.3", 322);
				createDatum("C", "C.4", 310);
				createDatum("C", "C.5", 315);
		createDatum(null, "root2", 2);
			createDatum("root2", "X", 70);
				createDatum("X", "X.1", 700);
				createDatum("X", "X.2", 710);
				createDatum("X", "X.3", 720);
			createDatum("root2", "Y", 80);
				createDatum("Y", "Y.1", 800);
				createDatum("Y", "Y.2", 810);
			createDatum("root2", "Z", 90);
				createDatum("Z", "Z.1", 900);
				createDatum("Z", "Z.2", 910);
				createDatum("Z", "Z.3", 920);
				createDatum("Z", "Z.4", 930);
				createDatum("Z", "Z.5", 940);
	}
	
}


Thanks,
Rashmy




[Updated on: Tue, 04 December 2012 21:59]

Report message to a moderator

Re: Using FilterList for tree in NatTable [message #989265 is a reply to message #989156] Wed, 05 December 2012 12:22 Go to previous messageGo to next message
Thomas  Mäder is currently offline Thomas MäderFriend
Messages: 46
Registered: July 2009
Member
I haven't followed the whole example, but intuitively, I'd put the filter list INSIDE the tree list, not around it. Are you getting any exceptions?
Re: Using FilterList for tree in NatTable [message #989303 is a reply to message #989265] Wed, 05 December 2012 15:04 Go to previous message
Rashmy A is currently offline Rashmy AFriend
Messages: 149
Registered: July 2009
Senior Member
Thanks Thomas! I just missed that important bit <oops>.
It works fine once I sent the Filterlist into the TreeList.
Previous Topic:Row Selection
Next Topic:Display value different than data value in editing cells
Goto Forum:
  


Current Time: Tue Nov 25 02:03:16 GMT 2014

Powered by FUDForum. Page generated in 0.01757 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software