01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: * Darrell Meyer <darrell@mygwt.net> - derived implementation
11: *******************************************************************************/package net.mygwt.ui.client.viewer;
12:
13: import java.util.Arrays;
14: import java.util.Comparator;
15:
16: import net.mygwt.ui.client.util.DefaultComparator;
17:
18: /**
19: * A viewer sorter is used by a <code>Viewer</code> to reorder the elements
20: * provided by its content provider.
21: */
22: public class ViewerSorter {
23:
24: public static DefaultComparator DEFAULT_COMPARATOR = new DefaultComparator();
25:
26: protected Comparator comparator;
27:
28: /**
29: * Creates a new instance that uses a <code>DefaultComparator</code>.
30: */
31: public ViewerSorter() {
32: comparator = DEFAULT_COMPARATOR;
33: }
34:
35: /**
36: * Creates a new sorter instance with the given comparator.
37: *
38: * @param comparator the sorter comparator
39: */
40: public ViewerSorter(Comparator comparator) {
41: this .comparator = comparator;
42: }
43:
44: /**
45: * Returns a negative, zero, or positive number depending on whether the first
46: * element is less than, equal to, or greater than the second element.
47: *
48: * @param viewer the viewer
49: * @param e1 the first element
50: * @param e2 the second element
51: * @return a negative number if the first element is less than the second
52: * element; the value <code>0</code> if the first element is equal
53: * to the second element; and a positive number if the first element
54: * is greater than the second element
55: */
56: public int compare(Viewer viewer, Object e1, Object e2) {
57: return comparator.compare(e1, e2);
58: }
59:
60: /**
61: * Sorts the given elements in-place, modifying the given array.
62: *
63: * @param viewer the viewer
64: * @param elements the elemnts to sort
65: */
66: public void sort(final Viewer viewer, Object[] elements) {
67: Arrays.sort(elements, new Comparator() {
68: public int compare(Object a, Object b) {
69: return ViewerSorter.this.compare(viewer, a, b);
70: }
71: });
72: }
73:
74: }
|