Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » NatTable » Update column comparators after column reorder
Update column comparators after column reorder [message #1714792] Mon, 16 November 2015 16:56 Go to next message
Tommy R is currently offline Tommy RFriend
Messages: 32
Registered: October 2011
Location: Hamburg
Member
Hello,

I followed the tutorial on https://www.eclipse.org/nattable/documentation.php?page=sorting to enable sorting in my table. Everything works fine when the table is initially created.

My table also supports DnD of columns and here is the problem:
When a user has reordered the columns via DnD, how can I update the comparator of each column?

Example
A Table has 3 columns: String, Integer, Boolean (in this order).
Now the user moves the Boolean column in front of the Integer column.
Now the comparator of the Integer column must me moved to the third column too and the comparator of the Boolean column must be moved to the second column.

How can I achieve this? Comparators are registered via column index and do not get updated by default. I also tried to update them by myself by clearing the column overrides and set them again. But this does not seem to have any effect.

Any ideas?

Regards
Tommy
Re: Update column comparators after column reorder [message #1714798 is a reply to message #1714792] Mon, 16 November 2015 18:33 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
This should work automatically. The question is, at which level do you add the index based labels? You need to do this on the DataLayer, because at that level index == position. If you attach the label accumulator on NatTable or any layer above the ColumnReorderLayer, the labels are applied position based, which is not correct.
Re: Update column comparators after column reorder [message #1714851 is a reply to message #1714798] Tue, 17 November 2015 09:41 Go to previous messageGo to next message
Tommy R is currently offline Tommy RFriend
Messages: 32
Registered: October 2011
Location: Hamburg
Member
Hello Dirk, thanks for your answer.
I already add the labels to the DataLayer of the column header. Here is how my ColumnHeaderLayerStack (shortened) looks like:
IDataProvider headerDataProvider = new ColumnHeaderDataProvider();
DataLayer columnHeaderDataLayer = new DataLayer(headerDataProvider);

// enable sorting
ColumnOverrideLabelAccumulator labelAccumulator = new ColumnOverrideLabelAccumulator(columnHeaderDataLayer);
columnHeaderDataLayer.setConfigLabelAccumulator(labelAccumulator);
for (int i = 0; i < getColumns().size(); i++) {
	labelAccumulator.registerColumnOverrides(i, ColumnComparator.CUSTOM_COMPARATOR_LABEL);
}
configRegistry.registerConfigAttribute(SortConfigAttributes.SORT_COMPARATOR, new ColumnComparator(),
	DisplayMode.NORMAL, ColumnComparator.CUSTOM_COMPARATOR_LABEL);

ILayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, bodyLayer, bodyLayer.getSelectionLayer());
SortHeaderLayer<IGeoDataRow> sortHeaderLayer = new SortHeaderLayer<>(
	columnHeaderLayer,
	new GlazedListsSortModel<>(sortedList, columnPropertyAccessor, configRegistry, columnHeaderDataLayer),
	false);

setUnderlyingLayer(sortHeaderLayer);


Am I doing something wrong here?
Re: Update column comparators after column reorder [message #1714852 is a reply to message #1714851] Tue, 17 November 2015 09:52 Go to previous messageGo to next message
Tommy R is currently offline Tommy RFriend
Messages: 32
Registered: October 2011
Location: Hamburg
Member
Also useful info. My BodyLayerStack (shortend again):
The dataProvider is a GlazedListsDataProvider.
// add a GlazedListsEventLayer event layer that is responsible for updating the grid on list changes
DataLayer bodyDataLayer = new DataLayer(dataProvider);
GlazedListsEventLayer<IGeoDataRow> glazedListsEventLayer = new GlazedListsEventLayer<>(bodyDataLayer, filterList);
ColumnReorderLayer columnReorderLayer = new ColumnReorderLayer(glazedListsEventLayer);
selectionLayer = new SelectionLayer(columnReorderLayer);
ViewportLayer viewportLayer = new ViewportLayer(selectionLayer);

setUnderlyingLayer(viewportLayer);


I create the BodyLayerStack first and on base of that i create the ColumnHeaderLayerStack. So the ColumnReorderLayer is under the ColumnHeaderLayerStack which is wrong since u said:
Quote:
If you attach the label accumulator on NatTable or any layer above the ColumnReorderLayer, the labels are applied position based, which is not correct.

The examples I found always add the ColumnReorderLayer in the BodyLayerStack.
So am I misunderstanding something here?
Re: Update column comparators after column reorder [message #1714876 is a reply to message #1714852] Tue, 17 November 2015 12:27 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
No you are right, the ColumnReoderLayer belongs to the body layer stack.

Quote:
How can I achieve this? Comparators are registered via column index and do not get updated by default.


They don't need to get updated. They are registered for a label that is set for a column index. So simplyfied, the labels should move with the columns.

From looking into the code snippets you provided, everything should work fine. The ColumnHeaderLayer is dimensionally dependent to the SelectionLayer and therefore the index to position transformation should work out of the box. I tested this in short in our examples (Tutorial Examples section) and it works.

You need to check why the labels are not correctly set in case of reordering. We have a debug command that can be bound to a context menu that opens a dialog showing the current labels of a cell. Maybe that helps in identifying what is going wrong.
Re: Update column comparators after column reorder [message #1714909 is a reply to message #1714876] Tue, 17 November 2015 16:06 Go to previous messageGo to next message
Tommy R is currently offline Tommy RFriend
Messages: 32
Registered: October 2011
Location: Hamburg
Member
Ok I guess the actual problem is, that I use my own ColumnReorderCommandHandler to update the model. So when I call ColumnReorderCommand within my handler the comparators seem to be switched correctly. But when it tries to redraw the table it gets a problem with my CellPainters as it seems to check an old model (the one before the DnD). So in the example of my first post it tries to paint for Integer values when the column is still Boolean.

But I guess I got a conceptual problem here. The more I learn abot NatTable the more I think: Why do I have to write my own ColumnReorderCommandHandler to update my model? Shouldn't the NatTable be able to update it for me via some mechanism?
So I thought that might be the setDataValue method in my ColumnPropertyAccessor. But this method never gets called.

So what would be the right way to update the model after/While DnD?

Maybe some words about my model:
My model also looks like a table, so it got rows and columns and when I reorder columns in the NatTable I also have to reorder them in my table model.

[Updated on: Tue, 17 November 2015 16:08]

Report message to a moderator

Re: Update column comparators after column reorder [message #1714910 is a reply to message #1714909] Tue, 17 November 2015 16:11 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Quote:
when I reorder columns in the NatTable I also have to reorder them in my table model


Possibly this is conceptual problem. Why do you need to reorder your table model? The base does never change in a NatTable. The ColumnReorderLayer for example only shows a transformed state, but it does not change the underlying model.
Re: Update column comparators after column reorder [message #1714914 is a reply to message #1714910] Tue, 17 November 2015 16:32 Go to previous messageGo to next message
Tommy R is currently offline Tommy RFriend
Messages: 32
Registered: October 2011
Location: Hamburg
Member
I need to change the model to save it in the exact state the NatTable is showing. So I can load it again and use it elsewhere. So the NatTable should just be an editor for the model. The model will later be used to show a table on a website and this table should of course have the same structure and content as the user specified in the NatTable.

So I actually got no problem with changing the model when changing the content or structure of the table. The problems started with the Sorting because the Comparators are not moved correctly when I use my own ColumnMoveCommandHandler (which is reasonable).
Re: Update column comparators after column reorder [message #1714936 is a reply to message #1714914] Tue, 17 November 2015 18:11 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
The problem is that NatTable is not changing the structure and expects the underlying model to be the same. If you change the underlying model, the transformation in the layers in between (e.g. ColumnReorderLayer) needs to be removed.

Technically you don't need the ColumnReorderLayer because you want to change the underlying data model. Therefore you need a customized command handler and register the necessary handlers for example to the DataLayer. The default mechanisms in NatTable doesn't fit to your needs.
Re: Update column comparators after column reorder [message #1715100 is a reply to message #1714936] Thu, 19 November 2015 08:59 Go to previous message
Tommy R is currently offline Tommy RFriend
Messages: 32
Registered: October 2011
Location: Hamburg
Member
So I removed the ColumnReorderLayer and added only one Comparator for each column that can handle every data type. So I don't have to move them when reordering columns manually. I also added my own SortConfiguration which also imitates the behavior of columns where a NullComparator has been added.
That works well for me.

Thanks for all your hints and explanations.
Previous Topic:Indexing for sorted GlazedList
Next Topic:Disable row DnD while sorted
Goto Forum:
  


Current Time: Fri Apr 19 13:34:24 GMT 2024

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

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

Back to the top