Index: src/org/eclipse/ui/views/properties/IPropertySourceProvider.java =================================================================== RCS file: /home/eclipse/org.eclipse.ui.views/src/org/eclipse/ui/views/properties/IPropertySourceProvider.java,v retrieving revision 1.4 diff -u -r1.4 IPropertySourceProvider.java --- src/org/eclipse/ui/views/properties/IPropertySourceProvider.java 8 Jul 2004 20:07:12 -0000 1.4 +++ src/org/eclipse/ui/views/properties/IPropertySourceProvider.java 12 Feb 2005 14:59:25 -0000 @@ -11,17 +11,22 @@ package org.eclipse.ui.views.properties; /** - * Interface used by PropertySheetRoot to obtain an - * IPropertySource for a given object. + * Interface used by {@link org.eclipse.ui.views.properties.PropertySheetEntry} + * to obtain an {@link org.eclipse.ui.views.properties.IPropertySource} for a + * given object. *

* This interface may be implemented by clients. *

*/ public interface IPropertySourceProvider { - /** - * Returns a property source for the given object. - * - * @param object the object - */ - public IPropertySource getPropertySource(Object object); + + /** + * Returns a property source for the given object. + * + * @param object + * the object + * @return the property source for the object passed in (maybe + * null) + */ + public IPropertySource getPropertySource(Object object); } Index: src/org/eclipse/ui/views/properties/PropertySheetEntry.java =================================================================== RCS file: /home/eclipse/org.eclipse.ui.views/src/org/eclipse/ui/views/properties/PropertySheetEntry.java,v retrieving revision 1.11 diff -u -r1.11 PropertySheetEntry.java --- src/org/eclipse/ui/views/properties/PropertySheetEntry.java 19 Aug 2004 20:37:02 -0000 1.11 +++ src/org/eclipse/ui/views/properties/PropertySheetEntry.java 12 Feb 2005 14:59:26 -0000 @@ -12,14 +12,10 @@ package org.eclipse.ui.views.properties; -import java.text.Collator; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; -import java.util.Comparator; import java.util.HashMap; import java.util.List; -import java.util.Locale; import java.util.Map; import org.eclipse.core.runtime.IAdaptable; @@ -151,7 +147,7 @@ } /** - * Return the sorted intersection of all the IPropertyDescriptors + * Return the unsorted intersection of all the IPropertyDescriptors * for the objects. * @return List */ @@ -189,23 +185,8 @@ } } - // Sort the descriptors - List descriptors = new ArrayList(intersection.values()); - Collections.sort(descriptors, new Comparator() { - Collator coll = Collator.getInstance(Locale.getDefault()); - - public int compare(Object a, Object b) { - IPropertyDescriptor d1, d2; - String dname1, dname2; - d1 = (IPropertyDescriptor) a; - dname1 = d1.getDisplayName(); - d2 = (IPropertyDescriptor) b; - dname2 = d2.getDisplayName(); - return coll.compare(dname1, dname2); - } - }); - - return descriptors; + // sorting is handled in the PropertySheetViewer, return unsorted + return new ArrayList(intersection.values()); } /** Index: src/org/eclipse/ui/views/properties/PropertySheetPage.java =================================================================== RCS file: /home/eclipse/org.eclipse.ui.views/src/org/eclipse/ui/views/properties/PropertySheetPage.java,v retrieving revision 1.20 diff -u -r1.20 PropertySheetPage.java --- src/org/eclipse/ui/views/properties/PropertySheetPage.java 25 Jan 2005 21:27:32 -0000 1.20 +++ src/org/eclipse/ui/views/properties/PropertySheetPage.java 12 Feb 2005 14:59:26 -0000 @@ -85,6 +85,8 @@ public static final String HELP_CONTEXT_PROPERTY_SHEET_PAGE = "org.eclipse.ui.property_sheet_page_help_context"; //$NON-NLS-1$ private PropertySheetViewer viewer; + + private PropertySheetSorter sorter; private IPropertySheetEntry rootEntry; @@ -117,7 +119,8 @@ public void createControl(Composite parent) { // create a new viewer viewer = new PropertySheetViewer(parent); - + viewer.setSorter(sorter); + // set the model for the viewer if (rootEntry == null) { // create a new root @@ -461,4 +464,25 @@ // the following will trigger an update viewer.setRootEntry(rootEntry); } + + /** + * Sets the sorter used for sorting categories and entries in the viewer + * of this page. + *

+ * The default sorter sorts categories and entries alphabetically. + *

+ * @param sorter the sorter to set (null will reset to the + * default sorter) + */ + public void setSorter(PropertySheetSorter sorter) { + this.sorter = sorter; + if (viewer != null) { + viewer.setSorter(sorter); + + // the following will trigger an update + if(null != viewer.getRootEntry()) + viewer.setRootEntry(rootEntry); + } + } + } Index: src/org/eclipse/ui/views/properties/PropertySheetViewer.java =================================================================== RCS file: /home/eclipse/org.eclipse.ui.views/src/org/eclipse/ui/views/properties/PropertySheetViewer.java,v retrieving revision 1.14 diff -u -r1.14 PropertySheetViewer.java --- src/org/eclipse/ui/views/properties/PropertySheetViewer.java 12 Feb 2005 02:19:08 -0000 1.14 +++ src/org/eclipse/ui/views/properties/PropertySheetViewer.java 12 Feb 2005 14:59:27 -0000 @@ -11,16 +11,13 @@ package org.eclipse.ui.views.properties; -import java.text.Collator; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; -import java.util.Comparator; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; -import java.util.Locale; import java.util.Map; import java.util.Set; @@ -105,6 +102,9 @@ // Cell editor activation listeners private ListenerList activationListeners = new ListenerList(3); + + // the property sheet sorter + private PropertySheetSorter sorter = new PropertySheetSorter(); /** * Creates a property sheet viewer on a newly-created tree control @@ -491,7 +491,7 @@ } /** - * Returns the children of the given category or entry + * Returns the sorted children of the given category or entry * * @param node a category or entry * @return the children of the given category or entry @@ -536,8 +536,8 @@ return Arrays.asList(categories); } - // return the filtered child entries - return getFilteredEntries(entry.getChildEntries()); + // return the sorted & filtered child entries + return getSortedEntries(getFilteredEntries(entry.getChildEntries())); } /** @@ -549,7 +549,7 @@ * IPropertySheetEntry) */ private List getChildren(PropertySheetCategory category) { - return getFilteredEntries(category.getChildEntries()); + return getSortedEntries(getFilteredEntries(category.getChildEntries())); } /* @@ -589,13 +589,28 @@ } return filteredEntries; } + + /** + * Returns a sorted list of IPropertySheetEntry entries. + * + * @param unsortedEntries + * unsorted list of IPropertySheetEntry + * @return a sorted list of the specified entries + */ + private List getSortedEntries(List unsortedEntries) { + IPropertySheetEntry[] propertySheetEntries = (IPropertySheetEntry[]) unsortedEntries + .toArray(new IPropertySheetEntry[unsortedEntries.size()]); + sorter.sort(propertySheetEntries); + return Arrays.asList(propertySheetEntries); + } + /** - * The PropertySheetViewer implementation of this method - * declared on IInputProvider returns the objects for which - * the viewer is currently showing properties. It returns an - * Object[] or null. - */ + * The PropertySheetViewer implementation of this method + * declared on IInputProvider returns the objects for which + * the viewer is currently showing properties. It returns an + * Object[] or null. + */ public Object getInput() { return input; } @@ -910,6 +925,21 @@ } /** + * Sets the sorter for this viewer. + *

+ * The default sorter sorts categories and entries alphabetically. + * A viewer update needs to be triggered after the sorter has changed. + *

+ * @param sorter the sorter to set (null will reset to the + * default sorter) + */ + public void setSorter(PropertySheetSorter sorter) { + if (null == sorter) + sorter = new PropertySheetSorter(); + this.sorter = sorter; + } + + /** * Sets the status line manager this view will use to show messages. * * @param manager @@ -997,25 +1027,10 @@ categoryCache.put(MISCELLANEOUS_CATEGORY_NAME, misc); // Sort the categories - List list = new ArrayList(categoryCache.values()); - for (int i = 0; i < categoriesToRemove.size(); i++) - list.remove(categoriesToRemove.get(i)); - Collections.sort(list, new Comparator() { - Collator coll = Collator.getInstance(Locale.getDefault()); - - public int compare(Object a, Object b) { - PropertySheetCategory c1, c2; - String dname1, dname2; - c1 = (PropertySheetCategory) a; - dname1 = c1.getCategoryName(); - c2 = (PropertySheetCategory) b; - dname2 = c2.getCategoryName(); - return coll.compare(dname1, dname2); - } - }); - - categories = (PropertySheetCategory[]) list - .toArray(new PropertySheetCategory[list.size()]); + Collection categoryCacheValues = categoryCache.values(); + categories = (PropertySheetCategory[]) categoryCacheValues + .toArray(new PropertySheetCategory[categoryCacheValues.size()]); + sorter.sort(categories); } /** Index: src/org/eclipse/ui/views/properties/PropertySheetSorter.java =================================================================== RCS file: src/org/eclipse/ui/views/properties/PropertySheetSorter.java diff -N src/org/eclipse/ui/views/properties/PropertySheetSorter.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/ui/views/properties/PropertySheetSorter.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,142 @@ +/******************************************************************************* + * Copyright (c) 2005 Gunnar Wagenknecht and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Gunnar Wagenknecht - initial API and implementation + *******************************************************************************/ +package org.eclipse.ui.views.properties; + +import java.text.Collator; +import java.util.Arrays; +import java.util.Comparator; + +import org.eclipse.jface.viewers.ViewerSorter; + +/** + * Class used by {@link org.eclipse.ui.views.properties.PropertySheetPage} to + * sort properties. + *

+ * The default implementation sorts alphabetically. Subclasses may overwrite to + * implement custom sorting. + *

+ */ +public class PropertySheetSorter extends ViewerSorter { + + /** + * The collator used to sort strings. + */ + private Collator collator; + + /** + * Creates a new sorter, which uses the default collator to sort strings. + */ + public PropertySheetSorter() { + this(Collator.getInstance()); + } + + /** + * Creates a new sorter, which uses the given collator to sort strings. + * + * @param collator + * the collator to use to sort strings + */ + public PropertySheetSorter(Collator collator) { + this.collator = collator; + } + + /** + * Returns a negative, zero, or positive number depending on whether the + * first element is less than, equal to, or greater than the second element. + *

+ * The default implementation of this method is based on a case insensitive + * compare of the display names. Subclasses may override. + *

+ * + * @param entryA + * the first element + * @param entryB + * the second element + * @return a negative number if the first element is less than the second + * element; the value 0 if the first element is equal + * to the second element; and a positive number if the first element + * is greater than the second element + */ + public int compare(IPropertySheetEntry entryA, IPropertySheetEntry entryB) { + return getCollator().compare(entryA.getDisplayName(), + entryB.getDisplayName()); + } + + /** + * Returns a negative, zero, or positive number depending on whether the + * first element is less than, equal to, or greater than the second element. + *

+ * The default implementation of this method is based on a case insensitive + * compare of the strings. Subclasses may override. + *

+ * + * @param categoryA + * the first element + * @param categoryB + * the second element + * @return a negative number if the first element is less than the second + * element; the value 0 if the first element is equal + * to the second element; and a positive number if the first element + * is greater than the second element + */ + public int compareCategories(String categoryA, String categoryB) { + return getCollator().compare(categoryA, categoryB); + } + + /** + * Returns the collator used to sort strings. + * + * @return the collator used to sort strings + */ + public Collator getCollator() { + return collator; + } + + /** + * Sorts the given elements in-place, modifying the given array. + *

+ * The default implementation of this method uses the java.util.Arrays#sort + * algorithm on the given array, calling compare to compare + * elements. + *

+ *

+ * Subclasses may reimplement this method to provide a more optimized + * implementation. + *

+ * + * @param entries + * the elements to sort + */ + public void sort(IPropertySheetEntry[] entries) { + Arrays.sort(entries, new Comparator() { + public int compare(Object a, Object b) { + return PropertySheetSorter.this.compare( + (IPropertySheetEntry) a, (IPropertySheetEntry) b); + } + }); + } + + /** + * Sorts the given categories in-place, modifying the given array. + * + * @param categories + * the categories to sort + */ + void sort(PropertySheetCategory[] categories) { + Arrays.sort(categories, new Comparator() { + public int compare(Object a, Object b) { + return PropertySheetSorter.this.compareCategories( + ((PropertySheetCategory) a).getCategoryName(), + ((PropertySheetCategory) b).getCategoryName()); + } + }); + } +}