]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/ColumnViewerComparator.java
8db5d4fe815101f1605d39dd923c404f8158add8
2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.eclipse
.ui
;
18 import java
.util
.Comparator
;
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
;
28 /** Generic column viewer sorter */
29 public class ColumnViewerComparator
<T
> extends ViewerComparator
{
30 private static final long serialVersionUID
= -2266218906355859909L;
32 public static final int ASC
= 1;
34 public static final int NONE
= 0;
36 public static final int DESC
= -1;
38 private int direction
= 0;
40 private TableViewerColumn column
;
42 private ColumnViewer viewer
;
44 public ColumnViewerComparator(TableViewerColumn column
,
45 Comparator
<T
> comparator
) {
48 this.viewer
= column
.getViewer();
49 this.column
.getColumn().addSelectionListener(new SelectionAdapter() {
50 private static final long serialVersionUID
= 7586796298965472189L;
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
;
57 if (tdirection
== ASC
) {
58 setSortDirection(DESC
);
59 } else if (tdirection
== DESC
) {
60 setSortDirection(NONE
);
63 setSortDirection(ASC
);
66 setSortDirection(ASC
);
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);
78 column
.getColumn().getParent().setSortColumn(column
.getColumn());
79 this.direction
= direction
;
81 if (direction
== ASC
) {
82 column
.getColumn().getParent().setSortDirection(SWT
.DOWN
);
84 column
.getColumn().getParent().setSortDirection(SWT
.UP
);
87 if (viewer
.getComparator() == this) {
90 viewer
.setComparator(this);
96 @SuppressWarnings("unchecked")
97 public int compare(Viewer viewer
, Object e1
, Object e2
) {
98 return direction
* getComparator().compare((T
) e1
, (T
) e2
);