Bug 166153 - [Viewers] Allow per column sorting in JFace viewers
Summary: [Viewers] Allow per column sorting in JFace viewers
Status: NEW
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.3   Edit
Hardware: All All
: P3 enhancement with 2 votes (vote)
Target Milestone: ---   Edit
Assignee: Platform UI Triaged CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-11-29 01:05 EST by Brock Janiczak CLA
Modified: 2019-09-06 16:14 EDT (History)
8 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Brock Janiczak CLA 2006-11-29 01:05:40 EST
The current process for adding column based sorting to Table and Tree viewers is to manually add a selection listener to the individual Columns of the widget.  This selection listener either needs to know which comparator to use for which column or the comparator needs to linked to the column using the setData method.  It would be nice if there were methods in JFace to make per column sorting easier.  I was thinking of something like:

1. Add an optional Sorter or Comparator property to ViewerColumn
2. Have the Table and Tree Viewers add selection listeners to the Columns in the underlying SWT widget
3. When the selection listener is fired for a column it wil look up the ViewerColumn and if it has an attached sorter will set the sorter on the Viewer.  Ascending/descending sorting can be handled by wrapping the comparator supplied in the ViewerColumn and multiplying the result by 1 or -1 depending on direction.  The selection listener would also be responsible for updating the sort direction indicator on the table.
Comment 1 Thomas Schindl CLA 2006-11-29 06:16:21 EST
I think this has been proposed at some other point from somebody different. To some extend I like this new feature.
Comment 2 Wayne Beaton CLA 2006-11-29 09:06:47 EST
Hi Tom. I think you might be thinking of an email exchange we had.

I implemented a table viewer that attaches this sort of behaviour directly to a "JFace" representation of the column. 

With my implementation, you specify the following for each column:

- An "ascending sort" comparator. This is used (oddly enough) to sort the column. If you click on the header once, it is sorted ascending. If you click a second time, it sorts descending. I was thinking of adding support for sorting on multiple columns.

- An "ObjectAccessor" that is used to get or set the value for an object displayed in the row.

- A cell editor (optional). If no cell editor is provided, the column is not editable.

- A "Cell draw listener" that is called to draw the contents of the cell. I have specialized subclasses of the TableColumnViewer class that do some default drawing, and then use these listeners to further customize.

- Columns can be marked as resizable (or not)

- Columns be given a minimum width

- Columns can be dragged (reordered)

- Columns can be shown/hidden

- You can ask the table to populate a menu with actions to hide/show columns.

I'm happy to contribute it (it's already in Eclipse CVS as part of an RCP sample).

It's hardly API quality (I built what I needed), but there may be some scavengable code/ideas in there...
Comment 3 Andy Maleh CLA 2007-12-09 16:12:04 EST
Hi,

I built an enhanced TableViewer (SortableTableViewer) that can support sorting automatically through type inference. You do not need to specify comparators explicitly every time you instantiate the table; just configure them once in your application, and then whenever a column is clicked, the table intelligently picks the most suitable comparator based on the inferred data type. Optionally, SortableTableViewer lets you specify the comparator directly on a column if you do not want to rely on type-inference. I had this done for a client project that is currently used by more than 25,000 users. I demonstrated it during my presentation "Practical Design Patterns for Rich Client Development" at EclipseWorld 2007, and the audience expressed interest in having such a widget, so I would like to contribute it to Eclipse.

What is the best way to do so? 

Andy Maleh
Comment 4 Boris Bokowski CLA 2007-12-18 23:42:09 EST
(In reply to comment #3)
> I built an enhanced TableViewer (SortableTableViewer) that can support sorting
> automatically through type inference.

This sounds too "magic" to be a good fit for JFace, but I don't want to discourage you from contributing to Eclipse. It would be useful to have column-based sorters for a ColumnViewer. Contributions should be made as attachments in bugzilla. See also:
http://wiki.eclipse.org/Platform_UI/How_to_Contribute
Comment 5 Boris Bokowski CLA 2009-11-26 09:54:07 EST
Hitesh is now responsible for watching bugs in the [Viewers] component area.
Comment 6 Henno Vermeulen CLA 2010-11-05 11:46:22 EDT
Tom Schindl already wrote a Snippet with an inner class ColumnViewerSorter which does most of what is needed here.

http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jface.snippets/Eclipse%20JFace%20Snippets/org/eclipse/jface/snippets/viewers/Snippet040TableViewerSorting.java?view=markup
Comment 7 Eclipse Webmaster CLA 2019-09-06 16:14:34 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.