Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » NatTable » GroupBy - how to refresh groups after data changed?
GroupBy - how to refresh groups after data changed? [message #1744603] Wed, 28 September 2016 19:38 Go to next message
Joachim Fox is currently offline Joachim FoxFriend
Messages: 5
Registered: January 2011
Junior Member
Hi,

I'm working with Nattable (and I'm fascinated how well it works), but I'm stumbling over a problem: I'm using the GroupBy feature. When I have the data grouped, I start updating some of the data in the grouped columns. So for example, I have data grouped by the column 'Last Name' and I change the last name of one row from 'Meyer' to 'Mayer'.

My expectation was that the grouping of the data would be re-calculated (and a new group 'Mayer' would be created and the row moved to the new group). But this does not happen. Calls like

groupByDataLayer.refresh();
groupByModel.update();
natTable.doCommand(new VisualRefreshCommand());
groupByDataLayer.killCache();


etc. don't help.
What do I need to do here?

Thank you and regards,
Godot
Re: GroupBy - how to refresh groups after data changed? [message #1744737 is a reply to message #1744603] Thu, 29 September 2016 18:38 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
You need to rebuild the tree structure. This can be triggered by forcing an EventList update. Changing a value in one model instance doesn't trigger a list change event, but you can force it by using a custom UpdateDataCommandHandler.

The following code is from the NatTable Examples Application: Tutorial Examples -> Integration -> EditableGroupBySummarySummaryRowExample
            // we register a custom UpdateDataCommandHandler that ensures to
            // update the tree structure
            this.bodyDataLayer.unregisterCommandHandler(UpdateDataCommand.class);
            this.bodyDataLayer.registerCommandHandler(new UpdateDataCommandHandler(this.bodyDataLayer) {
                @SuppressWarnings("unchecked")
                @Override
                protected boolean doCommand(UpdateDataCommand command) {
                    if (super.doCommand(command)) {
                        T o = ((IRowDataProvider<T>) BodyLayerStack.this.bodyDataProvider).getRowObject(command.getRowPosition());
                        int rowIndex = BodyLayerStack.this.sortedList.indexOf(o);
                        if (rowIndex >= 0) {
                            BodyLayerStack.this.sortedList.set(rowIndex, o);
                        }
                        return true;
                    }
                    return false;
                }
            });
Re: GroupBy - how to refresh groups after data changed? [message #1744747 is a reply to message #1744737] Thu, 29 September 2016 21:01 Go to previous messageGo to next message
Joachim Fox is currently offline Joachim FoxFriend
Messages: 5
Registered: January 2011
Junior Member
Hi,

thank you! This doesn't feel straightforward, though (it feels expensive - actually, only a single row would have to be moved, ideally...).
In the meantime, I had come up with a different workaround solution which is probably similarily expensive:

bodyDataLayer.registerCommandHandler(new ILayerCommandHandler() {

			@Override
			public Class getCommandClass() {
				return UpdateDataCommand.class;
			}

			@Override
			public boolean doCommand(ILayer targetLayer, final ILayerCommand cmd) {
				// some stuff to do the UpdateData goes here (actually, I'm using command and an EditingDomain here to support Undo/Redo, but that's not important...)
				
                                // test if we are really modifying data which requires a tree structure update
				if (groupByModel.getGroupByColumnIndexes().contains(((UpdateDataCommand)cmd).getColumnPosition())) {
                                        // read out the tree structure, clear it, rebuild it
					List<Integer> groups = new LinkedList<Integer>();
					groups.addAll(groupByModel.getGroupByColumnIndexes());
					groupByModel.clearGroupByColumnIndexes();
					for (Integer index: groups)
						groupByModel.addGroupByColumnIndex(index);
				}
				return true;
			}

		});		


Regards,
Joachim


P.S.: I'll go with your solution because admittedly, mine only works if there are no dependencies between the values of columns...

[Updated on: Thu, 29 September 2016 21:03]

Report message to a moderator

Re: GroupBy - how to refresh groups after data changed? [message #1744766 is a reply to message #1744747] Fri, 30 September 2016 06:40 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Quote:
This doesn't feel straightforward


Well actually it is straightforward.

Quote:
only a single row would have to be moved


And where should it be moved to?

My solution simply triggers a list update event for the list item that has changed. What happens underneath is in the responsibility of the framework. The problem is that the tree implementation in NatTable is based on a list, and that list needs to be sorted in the correct way to satisfy the tree structure. On editing you change the value of the criteria that is used for sorting. And to find out where to put the list item now you need to sort again. Not sure how you would solve that otherwise, but maybe there are some algorithms that perform this task very well.

It would be of course the best if GlazedLists TreeList would handle this internally somehow, as they have very good sorting algorithms, but as of now we have implemented a workaround to completely rebuild the tree structure. The reason is the TreeList doesn't implement such an update algorithm.

As a small remark on your solution: while we have focused on optimizing the rebuilding as much as possible by keeping the amount of calls and events less, your solution (if it would be implemented correctly) triggers the rebuilding of the tree structure twice. The first time on clearing the groupby structure, and then again on re-applying it. That means your solution would not be similar expensive, it would be at least twice as expensive as the suggested solution.
Re: GroupBy - how to refresh groups after data changed? [message #1744851 is a reply to message #1744766] Fri, 30 September 2016 19:14 Go to previous message
Joachim Fox is currently offline Joachim FoxFriend
Messages: 5
Registered: January 2011
Junior Member
I see. Anyway, my workaround only works in the special case that an edit in one cell does not affect other cells, so I'll go for your solution.

Thank you again!
Previous Topic:CalculatedValueCache Deadlock
Next Topic:RowReorderCommand works only in one direction
Goto Forum:
  


Current Time: Tue Apr 23 11:36:14 GMT 2024

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

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

Back to the top