]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - eclipse/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/ColumnViewerComparator.java
2 * Copyright (C) 2007-2012 Mathieu Baudier
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 sortter */
29 public class ColumnViewerComparator
<T
> extends ViewerComparator
{
30 public static final int ASC
= 1;
32 public static final int NONE
= 0;
34 public static final int DESC
= -1;
36 private int direction
= 0;
38 private TableViewerColumn column
;
40 private ColumnViewer viewer
;
42 public ColumnViewerComparator(TableViewerColumn column
,
43 Comparator
<T
> comparator
) {
46 this.viewer
= column
.getViewer();
47 this.column
.getColumn().addSelectionListener(new SelectionAdapter() {
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
;
54 if (tdirection
== ASC
) {
55 setSortDirection(DESC
);
56 } else if (tdirection
== DESC
) {
57 setSortDirection(NONE
);
60 setSortDirection(ASC
);
63 setSortDirection(ASC
);
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);
75 column
.getColumn().getParent().setSortColumn(column
.getColumn());
76 this.direction
= direction
;
78 if (direction
== ASC
) {
79 column
.getColumn().getParent().setSortDirection(SWT
.DOWN
);
81 column
.getColumn().getParent().setSortDirection(SWT
.UP
);
84 if (viewer
.getComparator() == this) {
87 viewer
.setComparator(this);
93 @SuppressWarnings("unchecked")
94 public int compare(Viewer viewer
, Object e1
, Object e2
) {
95 return direction
* getComparator().compare((T
) e1
, (T
) e2
);