]> git.argeo.org Git - lgpl/argeo-commons.git/blob - base/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/ColumnViewerComparator.java
Update and clean comments
[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 sortter */
29 public class ColumnViewerComparator<T> extends ViewerComparator {
30 public static final int ASC = 1;
31
32 public static final int NONE = 0;
33
34 public static final int DESC = -1;
35
36 private int direction = 0;
37
38 private TableViewerColumn column;
39
40 private ColumnViewer viewer;
41
42 public ColumnViewerComparator(TableViewerColumn column,
43 Comparator<T> comparator) {
44 super(comparator);
45 this.column = column;
46 this.viewer = column.getViewer();
47 this.column.getColumn().addSelectionListener(new SelectionAdapter() {
48
49 public void widgetSelected(SelectionEvent e) {
50 if (ColumnViewerComparator.this.viewer.getComparator() != null) {
51 if (ColumnViewerComparator.this.viewer.getComparator() == ColumnViewerComparator.this) {
52 int tdirection = ColumnViewerComparator.this.direction;
53
54 if (tdirection == ASC) {
55 setSortDirection(DESC);
56 } else if (tdirection == DESC) {
57 setSortDirection(NONE);
58 }
59 } else {
60 setSortDirection(ASC);
61 }
62 } else {
63 setSortDirection(ASC);
64 }
65 }
66 });
67 }
68
69 private void setSortDirection(int direction) {
70 if (direction == NONE) {
71 column.getColumn().getParent().setSortColumn(null);
72 column.getColumn().getParent().setSortDirection(SWT.NONE);
73 viewer.setComparator(null);
74 } else {
75 column.getColumn().getParent().setSortColumn(column.getColumn());
76 this.direction = direction;
77
78 if (direction == ASC) {
79 column.getColumn().getParent().setSortDirection(SWT.DOWN);
80 } else {
81 column.getColumn().getParent().setSortDirection(SWT.UP);
82 }
83
84 if (viewer.getComparator() == this) {
85 viewer.refresh();
86 } else {
87 viewer.setComparator(this);
88 }
89
90 }
91 }
92
93 @SuppressWarnings("unchecked")
94 public int compare(Viewer viewer, Object e1, Object e2) {
95 return direction * getComparator().compare((T) e1, (T) e2);
96 }
97 }