KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > properties > PropertySheetSorter


1 /*******************************************************************************
2  * Copyright (c) 2005 Gunnar Wagenknecht 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  * Gunnar Wagenknecht - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.views.properties;
12
13 import java.text.Collator JavaDoc; // can't use ICU, in public API
14
import java.util.Arrays JavaDoc;
15 import java.util.Comparator JavaDoc;
16
17 /**
18  * Class used by {@link org.eclipse.ui.views.properties.PropertySheetPage} to
19  * sort properties.
20  * <p>
21  * The default implementation sorts alphabetically. Subclasses may overwrite to
22  * implement custom sorting.
23  * </p>
24  *
25  * @since 3.1
26  */

27 public class PropertySheetSorter {
28
29     /**
30      * The collator used to sort strings.
31      */

32     private Collator JavaDoc collator;
33
34     /**
35      * Creates a new sorter, which uses the default collator to sort strings.
36      */

37     public PropertySheetSorter() {
38         this(Collator.getInstance());
39     }
40
41     /**
42      * Creates a new sorter, which uses the given collator to sort strings.
43      *
44      * @param collator
45      * the collator to use to sort strings
46      */

47     public PropertySheetSorter(Collator JavaDoc collator) {
48         this.collator = collator;
49     }
50
51     /**
52      * Returns a negative, zero, or positive number depending on whether the
53      * first element is less than, equal to, or greater than the second element.
54      * <p>
55      * The default implementation of this method uses the collator to
56      * compare the display names. Subclasses may override.
57      * </p>
58      *
59      * @param entryA
60      * the first element
61      * @param entryB
62      * the second element
63      * @return a negative number if the first element is less than the second
64      * element; the value <code>0</code> if the first element is equal
65      * to the second element; and a positive number if the first element
66      * is greater than the second element
67      */

68     public int compare(IPropertySheetEntry entryA, IPropertySheetEntry entryB) {
69         return getCollator().compare(entryA.getDisplayName(),
70                 entryB.getDisplayName());
71     }
72
73     /**
74      * Returns a negative, zero, or positive number depending on whether the
75      * first element is less than, equal to, or greater than the second element.
76      * <p>
77      * The default implementation of this method uses the collator to
78      * compare the strings. Subclasses may override.
79      * </p>
80      *
81      * @param categoryA
82      * the first element
83      * @param categoryB
84      * the second element
85      * @return a negative number if the first element is less than the second
86      * element; the value <code>0</code> if the first element is equal
87      * to the second element; and a positive number if the first element
88      * is greater than the second element
89      */

90     public int compareCategories(String JavaDoc categoryA, String JavaDoc categoryB) {
91         return getCollator().compare(categoryA, categoryB);
92     }
93
94     /**
95      * Returns the collator used to sort strings.
96      *
97      * @return the collator used to sort strings
98      */

99     protected Collator JavaDoc getCollator() {
100         return collator;
101     }
102
103     /**
104      * Sorts the given elements in-place, modifying the given array.
105      * <p>
106      * The default implementation of this method uses the java.util.Arrays#sort
107      * algorithm on the given array, calling <code>compare</code> to compare
108      * elements.
109      * </p>
110      * <p>
111      * Subclasses may reimplement this method to provide a more optimized
112      * implementation.
113      * </p>
114      *
115      * @param entries
116      * the elements to sort
117      */

118     public void sort(IPropertySheetEntry[] entries) {
119         Arrays.sort(entries, new Comparator JavaDoc() {
120             public int compare(Object JavaDoc a, Object JavaDoc b) {
121                 return PropertySheetSorter.this.compare(
122                         (IPropertySheetEntry) a, (IPropertySheetEntry) b);
123             }
124         });
125     }
126
127     /**
128      * Sorts the given categories in-place, modifying the given array.
129      *
130      * @param categories
131      * the categories to sort
132      */

133     void sort(PropertySheetCategory[] categories) {
134         Arrays.sort(categories, new Comparator JavaDoc() {
135             public int compare(Object JavaDoc a, Object JavaDoc b) {
136                 return PropertySheetSorter.this.compareCategories(
137                         ((PropertySheetCategory) a).getCategoryName(),
138                         ((PropertySheetCategory) b).getCategoryName());
139             }
140         });
141     }
142 }
143
Popular Tags