01: /* uDig - User Friendly Desktop Internet GIS client
02: * http://udig.refractions.net
03: * (C) 2004, Refractions Research Inc.
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation;
08: * version 2.1 of the License.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: */
15: package net.refractions.udig.ui;
16:
17: import java.util.Comparator;
18:
19: import org.eclipse.jface.viewers.TableViewer;
20: import org.eclipse.swt.SWT;
21: import org.eclipse.swt.widgets.Event;
22: import org.eclipse.swt.widgets.Listener;
23: import org.eclipse.swt.widgets.TableColumn;
24: import org.geotools.feature.Feature;
25:
26: /**
27: * Listens for clicks on the column header and sorts the columns according to the direction of the column.
28: *
29: * @author Jesse
30: * @since 1.1.0
31: */
32: class AttributeColumnSortListener implements Listener {
33:
34: private TableViewer viewer;
35: private String columnProperty;
36: private FeatureTableControl featureTable;
37:
38: public AttributeColumnSortListener(
39: FeatureTableControl featureTable, String columnProperty) {
40: this .viewer = featureTable.getViewer();
41: this .featureTable = featureTable;
42: this .columnProperty = columnProperty;
43: }
44:
45: public void handleEvent(Event e) {
46: // determine new sort column and direction
47: TableColumn sortColumn = viewer.getTable().getSortColumn();
48: final TableColumn selectedColumn = (TableColumn) e.widget;
49: int dir = viewer.getTable().getSortDirection();
50: if (sortColumn == selectedColumn) {
51: dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
52: } else {
53: dir = SWT.DOWN;
54: }
55: // sort the data based on column and direction
56: Comparator<Feature> comparator = getComparator(selectedColumn,
57: SWT.DOWN);
58: featureTable.sort(comparator, dir, selectedColumn);
59: // update data displayed in tree
60: }
61:
62: private Comparator<Feature> getComparator(
63: TableColumn currentColumn, int dir) {
64: if (columnProperty
65: .equals(FeatureTableControl.FEATURE_ID_COLUMN_PROPERTY)) {
66: return new FIDComparator(dir);
67: }
68: return new AttributeComparator(dir, columnProperty);
69: };
70:
71: }
|