KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > TreePathViewerSorter


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jface.viewers;
13
14 import java.util.Arrays JavaDoc;
15 import java.util.Comparator JavaDoc;
16
17 /**
18  * A viewer sorter that is provided extra context in the form of the path of the
19  * parent element of the elements being sorted.
20  *
21  * @since 3.2
22  */

23 public class TreePathViewerSorter extends ViewerSorter {
24
25     /**
26      * Provide a category for the given element that will have the given parent
27      * path when it is added to the viewer. The provided path is
28      * relative to the viewer input. The parent path will
29      * be <code>null</code> when the elements are root elements.
30      * <p>
31      * By default, the this method calls
32      * {@link ViewerSorter#category(Object)}. Subclasses may override.
33      *
34      * @param parentPath
35      * the parent path for the element
36      * @param element
37      * the element
38      * @return the category of the element
39      */

40     public int category(TreePath parentPath, Object JavaDoc element) {
41         return category(element);
42     }
43     
44     /**
45      * Compare the given elements that will have the given parent
46      * path when they are added to the viewer. The provided path is
47      * relative to the viewer input. The parent path will
48      * be <code>null</code> when the elements are root elements.
49      * <p>
50      * By default, the this method calls
51      * {@link ViewerSorter#sort(Viewer, Object[])}. Subclasses may override.
52      * @param viewer the viewer
53      * @param parentPath the parent path for the two elements
54      * @param e1 the first element
55      * @param e2 the second element
56      * @return a negative number if the first element is less than the
57      * second element; the value <code>0</code> if the first element is
58      * equal to the second element; and a positive
59      */

60     public int compare(Viewer viewer, TreePath parentPath, Object JavaDoc e1, Object JavaDoc e2) {
61         return compare(viewer, e1, e2);
62     }
63     
64     /**
65      * Returns whether this viewer sorter would be affected
66      * by a change to the given property of the given element.
67      * The provided path is
68      * relative to the viewer input. The parent path will
69      * be <code>null</code> when the elements are root elements.
70      * <p>
71      * The default implementation of this method calls
72      * {@link ViewerSorter#isSorterProperty(Object, String)}.
73      * Subclasses may reimplement.
74      * @param parentPath the parent path of the element
75      * @param element the element
76      * @param property the property
77      * @return <code>true</code> if the sorting would be affected,
78      * and <code>false</code> if it would be unaffected
79      */

80     public boolean isSorterProperty(TreePath parentPath, Object JavaDoc element, String JavaDoc property) {
81         return isSorterProperty(element, property);
82     }
83     
84     /**
85      * Sorts the given elements in-place, modifying the given array.
86      * The provided path is
87      * relative to the viewer input. The parent path will
88      * be <code>null</code> when the elements are root elements.
89      * <p>
90      * The default implementation of this method uses the
91      * java.util.Arrays#sort algorithm on the given array,
92      * calling {@link #compare(Viewer, TreePath, Object, Object)} to compare elements.
93      * </p>
94      * <p>
95      * Subclasses may reimplement this method to provide a more optimized implementation.
96      * </p>
97      *
98      * @param viewer the viewer
99      * @param parentPath the parent path of the given elements
100      * @param elements the elements to sort
101      */

102     public void sort(final Viewer viewer, final TreePath parentPath, Object JavaDoc[] elements) {
103         Arrays.sort(elements, new Comparator JavaDoc() {
104             public int compare(Object JavaDoc a, Object JavaDoc b) {
105                 return TreePathViewerSorter.this.compare(viewer, parentPath, a, b);
106             }
107         });
108     }
109 }
110
Popular Tags