]> git.argeo.org Git - lgpl/argeo-commons.git/blob - base/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/ColumnViewerComparator.java
8db5d4fe815101f1605d39dd923c404f8158add8
[lgpl/argeo-commons.git] / base / runtime / org.argeo.eclipse.ui / src / main / java / org / argeo / eclipse / ui / ColumnViewerComparator.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.eclipse.ui;
17
18 import java.util.Comparator;
19
20 import org.eclipse.jface.viewers.ColumnViewer;
21 import org.eclipse.jface.viewers.TableViewerColumn;
22 import org.eclipse.jface.viewers.Viewer;
23 import org.eclipse.jface.viewers.ViewerComparator;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.SelectionAdapter;
26 import org.eclipse.swt.events.SelectionEvent;
27
28 /** Generic column viewer sorter */
29 public class ColumnViewerComparator<T> extends ViewerComparator {
30 private static final long serialVersionUID = -2266218906355859909L;
31
32 public static final int ASC = 1;
33
34 public static final int NONE = 0;
35
36 public static final int DESC = -1;
37
38 private int direction = 0;
39
40 private TableViewerColumn column;
41
42 private ColumnViewer viewer;
43
44 public ColumnViewerComparator(TableViewerColumn column,
45 Comparator<T> comparator) {
46 super(comparator);
47 this.column = column;
48 this.viewer = column.getViewer();
49 this.column.getColumn().addSelectionListener(new SelectionAdapter() {
50 private static final long serialVersionUID = 7586796298965472189L;
51
52 public void widgetSelected(SelectionEvent e) {
53 if (ColumnViewerComparator.this.viewer.getComparator() != null) {
54 if (ColumnViewerComparator.this.viewer.getComparator() == ColumnViewerComparator.this) {
55 int tdirection = ColumnViewerComparator.this.direction;
56
57 if (tdirection == ASC) {
58 setSortDirection(DESC);
59 } else if (tdirection == DESC) {
60 setSortDirection(NONE);
61 }
62 } else {
63 setSortDirection(ASC);
64 }
65 } else {
66 setSortDirection(ASC);
67 }
68 }
69 });
70 }
71
72 private void setSortDirection(int direction) {
73 if (direction == NONE) {
74 column.getColumn().getParent().setSortColumn(null);
75 column.getColumn().getParent().setSortDirection(SWT.NONE);
76 viewer.setComparator(null);
77 } else {
78 column.getColumn().getParent().setSortColumn(column.getColumn());
79 this.direction = direction;
80
81 if (direction == ASC) {
82 column.getColumn().getParent().setSortDirection(SWT.DOWN);
83 } else {
84 column.getColumn().getParent().setSortDirection(SWT.UP);
85 }
86
87 if (viewer.getComparator() == this) {
88 viewer.refresh();
89 } else {
90 viewer.setComparator(this);
91 }
92
93 }
94 }
95
96 @SuppressWarnings("unchecked")
97 public int compare(Viewer viewer, Object e1, Object e2) {
98 return direction * getComparator().compare((T) e1, (T) e2);
99 }
100 }