Sort JFace Treeviewer multiple column - java

I'd like the user to be able to sort the TreeViewer anytime he wants to just by clicking the column header. However I don't know the proper way to do it.
I discovered that we can use ViewerComparator to sort different elements. However, I don't know how to set a listener to be able to ascending or descending sort properly.
Is there a way to have an ARROW automaticly with JFace Library to let the user choose descending or ascending in the column sort ?
Regards,
Waldo

This isn't really automatic.
You need to call TreeColumn.addSelectionListener to add a selection listener to deal with the clicks on the tree column headers.
You will then need to get the Tree from the TreeViewer with
Tree tree = viewer.getTree();
and then call
tree.setSortColumn(treeColumn);
to set the indicated main sort column and
tree.setSortDirection(SWT.UP) // or SWT.DOWN
to set the indicated sort direction.
Call
viewer.refresh();
to make the viewer redo the sorting.
You will need to track the column(s) to sort on in your comparator.

Related

Can nattable group by functionality be used to sort?

So i am thinking about Nattable for a project, it looks excellent, but one of my requirement is to have a good configurable sort on multiple columns with a clear display of what is happening, having see the group example, this is a great bit of functionality:
Where you can drag columns in to a bar that then groups the data by those values, this is what I would like for sorting, ie I don't want the tree expansion, or new rows added, I just want the table sorted in the order that I have dragged the columns. I am trying to work out if this is going to be possible in nattable because the sorting examples for multiple columns are lacklustre compared to this grouping bar, so can it be done?
Sorting multiple columns in NatTable works if the ISortModel supports it.
The _509_SortHeaderLayerExample has a custom ISortModel that does not evaluate the accumulate parameter. The _602_GlazedListsSortingExample uses the GlazedListsSortModel that supports sorting by multiple columns.
Using the DefaultSortConfiguration sorting by multiple columns works if SHIFT + ALT are pressed while clicking on the column header. Using the SingleClickSortConfiguration only the ALT key is pressed while clicking on the column header.
With a good understanding of the NatTable mechanisms it is possible to implement a sorting similar to the grouping UI. You need the grouping header and implement that it should sort instead of group. It is not supported out of the box.

Grid component and sorting with custom sort listener

I'm using grid component and default column sort behavior doesnt work for me correctly. I'm using a paged data so current grid data content is just a part of the entire data set. And default sorting feature sorts only data on screen and I need send the sort options to my database query. For this I added custom sort listener as on the example ...
e.g. grid.addSortListener(e -> this.sortListener(e));
... so I can catch the sort order change user does on screen which works fine. But how can I disable the default sorting the Vaadin does in grid?
The Grid redirects the sort to Container.sort() so you could override this method in your Container and ignore it.
David Marko,
You will you that following code
grid.sort("Column Name", SortDirection.ASCENDING);
It was working well

Is there a way to remove the sort after an SWT Table is sorted?

I have a Table and TableViewer that are set up to take a ViewerComparator:
this.viewer.setComparator( this.comparator );
Now I am looking at my table. I click on a column header. The table sorts on that row. I click on that column header again and it sorts in the other direction. I click on a different column header and the sorting switches to the new column. This works beautifully.
My question is if there is a way to remove the sorting. This would mean getting rid of the sort arrow on top of the column header that I clicked, the gray background on the sorted column's elements would disappear, and the data would go back into its original order. I am open to adding some sort of button somewhere to start this command.
Is this possible?
The Tables sort indicator and the comparator used by the TableViewer are actually independent.
To remove the sort indicator, call table.setSortDirection( SWT.NONE ). To remove the comparator, call viewer.setComparator( null ).
You could let the user switch through UP, DOWN, NONE when the column header is selected or provide an extra UI element to reset the sorting.

When to use convertRowIndexToModel

I have some JDialogs displaying JTables.
When the header columns are clicked a sort occurs on that column.
My question is : how can I know when a column header has been clicked and thus made a sort active.
When the sort is active, I know I should user the .convertRowIndexToModel method.
But how do I detect that a column is sorting in order not to mess the correct index if no sort is active?
Generally speaking, you should ALWAYS uses the convertRowIndexToModel when you take an index value from the view (JTable) and try and look up some value within the model. The JTable does this automatically when you use it's methods, but incase you're not, you need to take care of it yourself.
There's no need to know if the view is sorted or not...
If you "really" want to know when a table is sorted, you could attach a RowSorterListener to the TableRowSorter used by the table.
You could also use the TableRowSorter#getSortKeys to see which columns are included in the sort...
I have used it for my selection table. When the auto order is activated (setAutoCreateRowSorter(true))
the indices of the model table and the visual change, so you have to tell it to look for it within the model with respect to the one you are seeing.
((CustomTable)form.getAvailListView().getModel()).data.get(form.getListView().convertRowIndexToModel(i))

Java ZK ListBox listModel out of sync after sorting

hey guys i am new to ZK framework i have a listbox being sorted in the view later i pass the listBox to the controller and i need the items being selected by the user but in the model the items are syncronized with the sorting but in the getSelection array is not syncronized with the sort insted with the original data here is the code.
public void createPDFFromModel(Listbox list,String ref){
BindingListModelList model = (BindingListModelList)list.getModel();
for(int i=0;i<model.size();i++){
System.out.println((((ZamoraListitemAdapter)model.get(i)).getName()));
}
System.out.println("-------------------------------------------");
//Data Printed OK.
java.util.ArrayList<ZamoraListitemAdapter>selections = new java.util.ArrayList<ZamoraListitemAdapter>(model.getSelection());
for(int i=0;i<selections.size();i++){
ZamoraListitemAdapter clazz = (ZamoraListitemAdapter)selections.get(i);
System.out.println(clazz.getName());
//Out of sync with model and with sorting
}
my question is how i get the order of the items after the sort in getSelection model. i am using ZK 5.2.8
You could sort selections after you create it.
There are maybe other/better solutions, but you have
to write what you try to achieveif you need more help.
Reply to Comment
What I mean is
ArrayList<ZamoraListitemAdapter>selections =
new ArrayList<ZamoraListitemAdapter>(model.getSelection());
Collections.sort(selections);
So selections is a List of all your selected items and as long
as you compare the items the same way, they should be in the right order
In case you mean reorder and not sorting...
For me sorting means it is automatic and done by a algorithm.
Reordering instead means you, for example, drag the objects around.
If you mean reordering, and your Model hasn't a way to know if
a item is selected, you probably did something wrong.
Cos zk has got the two classes ListModelList and AbstractListModel
wich implement all methods needed for selection behavior and a custom
Model should, if the programmer wants a selectable Model, inherent
from one of them, cos it's the easiest way.
You should may also read this.
model.getSelection()
returns a Set (no order), instead use
model.getInnerList()
that returns ListModeList

Categories