]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - eclipse/org.argeo.cms.swt/src/org/argeo/eclipse/ui/ColumnViewerComparator.java
1 package org
.argeo
.eclipse
.ui
;
3 import org
.eclipse
.jface
.viewers
.ColumnViewer
;
4 import org
.eclipse
.jface
.viewers
.TableViewerColumn
;
5 import org
.eclipse
.jface
.viewers
.Viewer
;
6 import org
.eclipse
.jface
.viewers
.ViewerComparator
;
7 import org
.eclipse
.swt
.SWT
;
8 import org
.eclipse
.swt
.events
.SelectionAdapter
;
9 import org
.eclipse
.swt
.events
.SelectionEvent
;
11 /** Generic column viewer sorter */
12 public class ColumnViewerComparator
extends ViewerComparator
{
13 private static final long serialVersionUID
= -2266218906355859909L;
15 public static final int ASC
= 1;
17 public static final int NONE
= 0;
19 public static final int DESC
= -1;
21 private int direction
= 0;
23 private TableViewerColumn column
;
25 private ColumnViewer viewer
;
27 public ColumnViewerComparator(TableViewerColumn column
) {
30 this.viewer
= column
.getViewer();
31 this.column
.getColumn().addSelectionListener(new SelectionAdapter() {
32 private static final long serialVersionUID
= 7586796298965472189L;
34 public void widgetSelected(SelectionEvent e
) {
35 if (ColumnViewerComparator
.this.viewer
.getComparator() != null) {
36 if (ColumnViewerComparator
.this.viewer
.getComparator() == ColumnViewerComparator
.this) {
37 int tdirection
= ColumnViewerComparator
.this.direction
;
39 if (tdirection
== ASC
) {
40 setSortDirection(DESC
);
41 } else if (tdirection
== DESC
) {
42 setSortDirection(NONE
);
45 setSortDirection(ASC
);
48 setSortDirection(ASC
);
54 private void setSortDirection(int direction
) {
55 if (direction
== NONE
) {
56 column
.getColumn().getParent().setSortColumn(null);
57 column
.getColumn().getParent().setSortDirection(SWT
.NONE
);
58 viewer
.setComparator(null);
60 column
.getColumn().getParent().setSortColumn(column
.getColumn());
61 this.direction
= direction
;
63 if (direction
== ASC
) {
64 column
.getColumn().getParent().setSortDirection(SWT
.DOWN
);
66 column
.getColumn().getParent().setSortDirection(SWT
.UP
);
69 if (viewer
.getComparator() == this) {
72 viewer
.setComparator(this);
78 public int compare(Viewer viewer
, Object e1
, Object e2
) {
79 return direction
* super.compare(viewer
, e1
, e2
);