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.swt.SWT;
20: import org.geotools.feature.Feature;
21: import org.geotools.filter.Filter;
22:
23: /**
24: * Promotes the Features selected by the filter to the top if SWT.UP.
25: *
26: * @author Jesse
27: * @since 1.1.0
28: */
29: public class SelectionComparator implements Comparator<Feature> {
30:
31: private static final Comparator<Feature> NATURAL_ORDER_COMPARATOR = new Comparator<Feature>() {
32:
33: public int compare(Feature o1, Feature o2) {
34: return 0;
35: }
36: };
37:
38: private final Filter filter;
39: private final int direction;
40: private final Comparator<Feature> subComparator;
41:
42: /**
43: *
44: * @param filter the filter to use to promote the selected features
45: * @param direction SWT.UP to put selected features at the start of the list, SWT.DOWN to put the selected features on the end
46: */
47: public SelectionComparator(Filter filter, int direction) {
48: this (filter, direction, NATURAL_ORDER_COMPARATOR);
49: }
50:
51: /**
52: *
53: * @param filter the filter to use to promote the selected features
54: * @param direction SWT.UP to put selected features at the start of the list, SWT.DOWN to put the selected features on the end
55: * @param subComparator this comparator will sort the same-type items. IE selection will all be at the top but sorted by this comparator.
56: */
57: public SelectionComparator(Filter filter, int direction,
58: Comparator<Feature> subComparator) {
59: this .filter = filter;
60: this .direction = direction == SWT.UP ? 1 : -1;
61: this .subComparator = subComparator;
62: }
63:
64: public int compare(Feature o1, Feature o2) {
65: boolean f1Contained = filter.contains(o1);
66: boolean f2Contained = filter.contains(o2);
67:
68: if (f1Contained && !f2Contained)
69: return -1 * direction;
70:
71: if (f2Contained && !f1Contained)
72: return 1 * direction;
73:
74: return subComparator.compare(o1, o2);
75: }
76:
77: }
|