Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 34166 Details for
Bug 126459
[Common Navigator] CommonViewer should use TreePathViewerSorter
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
[patch]
Patch to add TreePathViewerSorter to JFace
patch126459.txt (text/plain), 30.62 KB, created by
Michael Valenta
on 2006-02-05 19:46:24 EST
(
hide
)
Description:
Patch to add TreePathViewerSorter to JFace
Filename:
MIME Type:
Creator:
Michael Valenta
Created:
2006-02-05 19:46:24 EST
Size:
30.62 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.ui.navigator >Index: src/org/eclipse/ui/navigator/internal/DelegateTreeViewerSorter.java >=================================================================== >RCS file: src/org/eclipse/ui/navigator/internal/DelegateTreeViewerSorter.java >diff -N src/org/eclipse/ui/navigator/internal/DelegateTreeViewerSorter.java >--- src/org/eclipse/ui/navigator/internal/DelegateTreeViewerSorter.java 3 Feb 2006 20:24:45 -0000 1.1 >+++ /dev/null 1 Jan 1970 00:00:00 -0000 >@@ -1,67 +0,0 @@ >-/******************************************************************************* >- * Copyright (c) 2006 IBM Corporation 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: >- * IBM Corporation - initial API and implementation >- ******************************************************************************/ >- >-package org.eclipse.ui.navigator.internal; >- >-import java.text.Collator; >- >-import org.eclipse.core.runtime.Assert; >-import org.eclipse.jface.viewers.Viewer; >-import org.eclipse.jface.viewers.ViewerSorter; >-import org.eclipse.ui.navigator.TreeViewerSorter; >- >-/** >- * @since 3.2 >- * >- */ >-public class DelegateTreeViewerSorter extends TreeViewerSorter { >- >- private ViewerSorter delegateSorter; >- >- public DelegateTreeViewerSorter(ViewerSorter aSorter) { >- super(); >- Assert.isNotNull(aSorter); >- delegateSorter = aSorter; >- } >- >- public int category(Object element) { >- return delegateSorter.category(element); >- } >- >- public int compare(Viewer viewer, Object e1, Object e2) { >- return delegateSorter.compare(viewer, e1, e2); >- } >- >- public boolean equals(Object obj) { >- return delegateSorter.equals(obj); >- } >- >- public Collator getCollator() { >- return delegateSorter.getCollator(); >- } >- >- public int hashCode() { >- return delegateSorter.hashCode(); >- } >- >- public boolean isSorterProperty(Object element, String property) { >- return delegateSorter.isSorterProperty(element, property); >- } >- >- public void sort(Viewer viewer, Object[] elements) { >- delegateSorter.sort(viewer, elements); >- } >- >- public String toString() { >- return delegateSorter.toString(); >- } >- >-} >Index: src/org/eclipse/ui/navigator/CommonViewerSorter.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.ui.navigator/src/org/eclipse/ui/navigator/CommonViewerSorter.java,v >retrieving revision 1.1 >diff -u -r1.1 CommonViewerSorter.java >--- src/org/eclipse/ui/navigator/CommonViewerSorter.java 3 Feb 2006 20:24:37 -0000 1.1 >+++ src/org/eclipse/ui/navigator/CommonViewerSorter.java 6 Feb 2006 00:44:27 -0000 >@@ -11,12 +11,9 @@ > > package org.eclipse.ui.navigator; > >-import java.util.Arrays; >-import java.util.Comparator; >-import java.util.Set; >+import java.util.*; > >-import org.eclipse.jface.viewers.Viewer; >-import org.eclipse.jface.viewers.ViewerSorter; >+import org.eclipse.jface.viewers.*; > import org.eclipse.ui.navigator.internal.NavigatorContentService; > import org.eclipse.ui.navigator.internal.extensions.NavigatorContentDescriptor; > >@@ -52,7 +49,7 @@ > * @since 3.2 > * > */ >-public final class CommonViewerSorter extends TreeViewerSorter { >+public final class CommonViewerSorter extends TreePathViewerSorter { > > private NavigatorContentService contentService; > >@@ -83,21 +80,18 @@ > : Priority.LOW_PRIORITY_VALUE; > } > >- public void sort(final Viewer viewer, final Object parent, Object[] elements) { >- >- Arrays.sort(elements, new Comparator() { >- public int compare(Object a, Object b) { >- return CommonViewerSorter.this.compare(viewer, parent, a, b); >- } >- }); >- } >- >- public int compare(Viewer viewer, Object parent, Object e1, Object e2) { >+ public int compare(Viewer viewer, TreePath parentPath, Object e1, Object e2) { > INavigatorContentDescriptor sourceOfLvalue = getSource(e1); > INavigatorContentDescriptor sourceOfRvalue = getSource(e2); > > // identity comparison > if (sourceOfLvalue != null && sourceOfLvalue == sourceOfRvalue) { >+ Object parent; >+ if (parentPath == null) { >+ parent = viewer.getInput(); >+ } else { >+ parent = parentPath.getLastSegment(); >+ } > ViewerSorter sorter = sorterService.findSorter(sourceOfLvalue, parent, e1, e2); > if(sorter != null) > return sorter.compare(viewer, e1, e2); >@@ -106,7 +100,7 @@ > if (categoryDelta == 0) { > super.compare(viewer, e1, e2); > } >- return categoryDelta; >+ return -1 * categoryDelta; > } > > private INavigatorContentDescriptor getSource(Object o) { >Index: src/org/eclipse/ui/navigator/CommonViewer.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.ui.navigator/src/org/eclipse/ui/navigator/CommonViewer.java,v >retrieving revision 1.7 >diff -u -r1.7 CommonViewer.java >--- src/org/eclipse/ui/navigator/CommonViewer.java 3 Feb 2006 20:24:37 -0000 1.7 >+++ src/org/eclipse/ui/navigator/CommonViewer.java 6 Feb 2006 00:44:27 -0000 >@@ -13,22 +13,11 @@ > import java.util.ArrayList; > import java.util.List; > >-import org.eclipse.jface.viewers.DecoratingLabelProvider; >-import org.eclipse.jface.viewers.IBaseLabelProvider; >-import org.eclipse.jface.viewers.ITreeContentProvider; >-import org.eclipse.jface.viewers.LabelProviderChangedEvent; >-import org.eclipse.jface.viewers.TreeViewer; >-import org.eclipse.jface.viewers.ViewerSorter; >-import org.eclipse.swt.SWT; >-import org.eclipse.swt.dnd.DND; >-import org.eclipse.swt.dnd.FileTransfer; >-import org.eclipse.swt.dnd.Transfer; >+import org.eclipse.jface.viewers.*; >+import org.eclipse.swt.dnd.*; > import org.eclipse.swt.events.DisposeEvent; >-import org.eclipse.swt.widgets.Composite; >-import org.eclipse.swt.widgets.Item; >-import org.eclipse.swt.widgets.Widget; >+import org.eclipse.swt.widgets.*; > import org.eclipse.ui.PlatformUI; >-import org.eclipse.ui.navigator.internal.DelegateTreeViewerSorter; > import org.eclipse.ui.navigator.internal.NavigatorContentService; > import org.eclipse.ui.navigator.internal.dnd.CommonNavigatorDragAdapter; > import org.eclipse.ui.navigator.internal.dnd.CommonNavigatorDropAdapter; >@@ -151,28 +140,6 @@ > } > > /** >- * Returns the sorted and filtered set of children of the given element. The >- * resulting array must not be modified, as it may come directly from the >- * model's internal state. >- * >- * @param parent >- * the parent element >- * @return a sorted and filtered array of child elements >- */ >- protected Object[] getSortedChildren(Object parent) { >- Object[] result = getFilteredChildren(parent); >- ViewerSorter sorter = getSorter(); >- if (sorter != null) { >- // be sure we're not modifying the original array from the model >- result = (Object[]) result.clone(); >- // safe caste due to setSorter(); >- ((TreeViewerSorter) sorter).sort(this, parent, result); >- >- } >- return result; >- } >- >- /** > * Sets this viewer's sorter and triggers refiltering and resorting of this > * viewer's element. Passing <code>null</code> turns sorting off. > * >@@ -182,211 +149,9 @@ > public void setSorter(ViewerSorter sorter) { > if (sorter != null && sorter instanceof CommonViewerSorter) > ((CommonViewerSorter) sorter).setContentService(contentService); >- >- if (sorter == null || sorter instanceof TreeViewerSorter) >- super.setSorter(sorter); >- else /* we wrap the sorter for convenience (now we can always cast to TreeViewerSorter) */ >- super.setSorter(new DelegateTreeViewerSorter(sorter)); >+ super.setSorter(sorter); > } >- >- // Had to pull down the following code to tap into the sorting >- >- /** >- * Adds the given child elements to this viewer as children of the given >- * parent element. >- * <p> >- * EXPERIMENTAL. Not to be used except by JDT. This method was added to >- * support JDT's explorations into grouping by working sets, which requires >- * viewers to support multiple equal elements. See bug 76482 for more >- * details. This support will likely be removed in Eclipse 3.2 in favour of >- * proper support for multiple equal elements. >- * </p> >- * >- * @param widget >- * the widget for the parent element >- * @param parentElement >- * the parent element >- * @param childElements >- * the child elements to add >- * @since 3.1 >- */ >- protected void internalAdd(Widget widget, Object parentElement, >- Object[] childElements) { >- >- // optimization! >- // if the widget is not expanded we just invalidate the subtree >- if (widget instanceof Item) { >- Item ti = (Item) widget; >- if (!getExpanded(ti)) { >- boolean needDummy = isExpandable(parentElement); >- boolean haveDummy = false; >- // remove all children >- Item[] items = getItems(ti); >- for (int i = 0; i < items.length; i++) { >- if (items[i].getData() != null) { >- disassociate(items[i]); >- items[i].dispose(); >- } else { >- if (needDummy && !haveDummy) { >- haveDummy = true; >- } else { >- items[i].dispose(); >- } >- } >- } >- // append a dummy if necessary >- if (needDummy && !haveDummy) >- newItem(ti, SWT.NULL, -1); >- return; >- } >- } >- >- if (childElements.length > 0) { >- Object[] filtered = filter(childElements); >- if (getSorter() != null) >- ((TreeViewerSorter) getSorter()).sort(this, parentElement, >- filtered); >- createAddedElements(widget, parentElement, filtered); >- } >- } >- >- /** >- * See if element is the data of one of the elements in items. >- * >- * @param items >- * @param element >- * @return <code>true</code> if the element matches. >- */ >- private boolean itemExists(Item[] items, Object element) { >- if (usingElementMap())// if we can do a constant time lookup find it >- return findItem(element) != null; >- for (int i = 0; i < items.length; i++) { >- if (items[i].getData().equals(element)) >- return true; >- } >- return false; >- } >- >- /** >- * Create the new elements in the parent widget. If the child already exists >- * do nothing. >- * >- * @param widget >- * @param elements >- * Sorted list of elements to add. >- */ >- private void createAddedElements(Widget widget, Object parent, >- Object[] elements) { >- >- if (elements.length == 1) { >- if (equals(elements[0], widget.getData())) >- return; >- } >- >- // safe caste due to setSorter(); >- TreeViewerSorter sorter = (TreeViewerSorter) getSorter(); >- Item[] items = getChildren(widget); >- >- // As the items are sorted already we optimize for a >- // start position >- int lastInsertion = 0; >- >- // Optimize for the empty case >- if (items.length == 0) { >- for (int i = 0; i < elements.length; i++) { >- createTreeItem(widget, elements[i], -1); >- } >- return; >- } >- >- for (int i = 0; i < elements.length; i++) { >- boolean newItem = true; >- Object element = elements[i]; >- int index; >- if (getSorter() == null) { >- if (itemExists(items, element)) { >- refresh(element); >- newItem = false; >- } >- index = -1; >- } else { >- lastInsertion = insertionPosition(items, lastInsertion, sorter, >- parent, element); >- // As we are only searching the original array we keep track of >- // those positions only >- if (lastInsertion == items.length) >- index = -1; >- else {// See if we should just refresh >- while (lastInsertion < items.length >- && sorter.compare(this, parent, element, >- items[lastInsertion].getData()) == 0) { >- // As we cannot assume the sorter is consistent with >- // equals() - therefore we can >- // just check against the item prior to this index (if >- // any) >- if (items[lastInsertion].getData().equals(element)) { >- // refresh the element in case it has new children >- refresh(element); >- newItem = false; >- } >- lastInsertion++;// We had an insertion so increment >- } >- // Did we get to the end? >- if (lastInsertion == items.length) >- index = -1; >- else >- index = lastInsertion + i; // Add the index as the >- // array is growing >- } >- } >- if (newItem) >- createTreeItem(widget, element, index); >- } >- } >- >- /** >- * Returns the index where the item should be inserted. It uses sorter to >- * determine the correct position, if sorter is not assigned, returns the >- * index of the element after the last. >- * >- * @param items >- * the items to search >- * @param lastInsertion >- * the start index to start search for position from this allows >- * optimising search for multiple elements that are sorted >- * themself. >- * @param element >- * element to find position for. >- * @return the index to use when inserting the element. >- * >- */ >- >- private int insertionPosition(Item[] items, int lastInsertion, >- TreeViewerSorter sorter, Object parent, Object element) { >- >- int size = items.length; >- if (getSorter() == null) >- return size; >- int min = lastInsertion, max = size - 1; >- >- while (min <= max) { >- int mid = (min + max) / 2; >- Object data = items[mid].getData(); >- int compare = sorter.compare(this, parent, data, element); >- if (compare == 0) { >- return mid;// Return if we already match >- } >- if (compare < 0) >- min = mid + 1; >- else >- max = mid - 1; >- } >- return min; >- >- } >- >- // End of pulled down code >- >+ > /** > * <p> > * Adds DND support to the Navigator. Uses hooks into the extensible >Index: src/org/eclipse/ui/navigator/TreeViewerSorter.java >=================================================================== >RCS file: src/org/eclipse/ui/navigator/TreeViewerSorter.java >diff -N src/org/eclipse/ui/navigator/TreeViewerSorter.java >--- src/org/eclipse/ui/navigator/TreeViewerSorter.java 3 Feb 2006 20:24:36 -0000 1.1 >+++ /dev/null 1 Jan 1970 00:00:00 -0000 >@@ -1,93 +0,0 @@ >-/******************************************************************************* >- * Copyright (c) 2006 IBM Corporation 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: >- * IBM Corporation - initial API and implementation >- ******************************************************************************/ >- >-package org.eclipse.ui.navigator; >- >-import org.eclipse.jface.viewers.Viewer; >-import org.eclipse.jface.viewers.ViewerSorter; >- >-/** >- * >- * Provides context sensitive sorting of elements based on their parent in the >- * tree. >- * >- * <p> >- * Clients may extend this class. >- * </p> >- * >- * <p> >- * <strong>EXPERIMENTAL</strong>. This class or interface has been added as >- * part of a work in progress. There is a guarantee neither that this API will >- * work nor that it will remain the same. Please do not use this API without >- * consulting with the Platform/UI team. >- * </p> >- * >- * @since 3.2 >- * >- */ >-public class TreeViewerSorter extends ViewerSorter { >- >- /** >- * Sorts the given elements in-place taking into account the parent of the >- * elements, modifying the given array. >- * <p> >- * The default implementation of this method uses the java.util.Arrays#sort >- * algorithm on the given array, calling <code>compare</code> to compare >- * elements, and the parent does not affect sorting algorithm. >- * </p> >- * <p> >- * Subclasses may reimplement this method to provide a more optimized >- * implementation. >- * </p> >- * <p> >- * >- * >- * @param viewer >- * the viewer >- * @param parent >- * The parent element in the tree of both children. >- * @param elements >- * the elements to sort >- */ >- public void sort(final Viewer viewer, final Object parent, Object[] elements) { >- super.sort(viewer, elements); >- } >- >- /** >- * Returns a negative, zero, or positive number depending on whether the >- * first element is less than, equal to, or greater than the second element. >- * <p> >- * The default implementation of this method is based on comparing the >- * elements' categories as computed by the <code>category</code> framework >- * method. Elements within the same category are further subjected to a case >- * insensitive compare of their label strings, either as computed by the >- * content viewer's label provider, or their <code>toString</code> values >- * in other cases. Subclasses may override. >- * </p> >- * >- * @param viewer >- * the viewer >- * @param parent >- * The parent element in the tree of both children. >- * @param e1 >- * the first element >- * @param e2 >- * the second element >- * @return a negative number if the first element is less than the second >- * element; the value <code>0</code> 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(Viewer viewer, Object parent, Object e1, Object e2) { >- return super.compare(viewer, e1, e2); >- } >- >-} >#P org.eclipse.jface >Index: src/org/eclipse/jface/viewers/AbstractTreeViewer.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jface/src/org/eclipse/jface/viewers/AbstractTreeViewer.java,v >retrieving revision 1.79 >diff -u -r1.79 AbstractTreeViewer.java >--- src/org/eclipse/jface/viewers/AbstractTreeViewer.java 26 Jan 2006 15:47:36 -0000 1.79 >+++ src/org/eclipse/jface/viewers/AbstractTreeViewer.java 6 Feb 2006 00:44:28 -0000 >@@ -201,6 +201,7 @@ > } > > ViewerSorter sorter = getSorter (); >+ TreePath parentPath = internalGetSorterParentPath(widget, sorter); > Item[] items = getChildren(widget); > > //As the items are sorted already we optimize for a >@@ -227,12 +228,12 @@ > index = -1; > } > else{ >- lastInsertion = insertionPosition(items,sorter,lastInsertion, element); >+ lastInsertion = insertionPosition(items,sorter,lastInsertion, element, parentPath); > //As we are only searching the original array we keep track of those positions only > if(lastInsertion == items.length) > index = -1; > else{//See if we should just refresh >- while(lastInsertion < items.length && sorter.compare(this,element,items[lastInsertion].getData()) == 0){ >+ while(lastInsertion < items.length && internalCompare(sorter,parentPath,element,items[lastInsertion].getData()) == 0){ > //As we cannot assume the sorter is consistent with equals() - therefore we can > // just check against the item prior to this index (if any) > if (items[lastInsertion].getData().equals(element)) { >@@ -285,12 +286,14 @@ > * themself. > * @param element > * element to find position for. >+ * @param parentPath the tree path for the element's parent or <code>null</code> >+ * if the element is a root element or the sorter is not a {@link TreePathViewerSorter} > * @return the index to use when inserting the element. > * > */ > >- private int insertionPosition(Item[] items, ViewerSorter sorter, int lastInsertion, Object element) { >- >+ private int insertionPosition(Item[] items, ViewerSorter sorter, int lastInsertion, Object element, TreePath parentPath) { >+ > int size = items.length; > if (sorter == null) > return size; >@@ -299,7 +302,7 @@ > while (min <= max) { > int mid = (min + max) / 2; > Object data = items[mid].getData(); >- int compare = sorter.compare(this, data, element); >+ int compare = internalCompare(sorter, parentPath, data, element); > if (compare == 0) { > return mid;//Return if we already match > } >@@ -343,7 +346,7 @@ > */ > protected int indexForElement(Widget parent, Object element) { > ViewerSorter sorter = getSorter(); >- >+ TreePath parentPath = internalGetSorterParentPath(parent, sorter); > > Item[] items = getChildren(parent); > int count = items.length; >@@ -355,7 +358,7 @@ > while (min <= max) { > int mid = (min + max) / 2; > Object data = items[mid].getData(); >- int compare = sorter.compare(this, data, element); >+ int compare = internalCompare(sorter, parentPath, data, element); > if (compare == 0) { > // find first item > element > while (compare == 0) { >@@ -364,7 +367,7 @@ > break; > } > data = items[mid].getData(); >- compare = sorter.compare(this, data, element); >+ compare = internalCompare(sorter, parentPath, data, element); > } > return mid; > } >@@ -377,6 +380,108 @@ > } > > /** >+ * Return the tree path that should be used as the parent path for the >+ * given widget and sorter. A <code>null</code> is returned if either >+ * the sorter is not a {@link TreePathViewerSorter} or if the parent >+ * widget is not an {@link Item} (i.e. is the root of the tree). >+ * >+ * @param parent the parent widget >+ * @param sorter the sorter >+ * @return the tree path that should be used as the parent path for the >+ * given widget and sorter >+ */ >+ private TreePath internalGetSorterParentPath(Widget parent, ViewerSorter sorter) { >+ TreePath path; >+ if (sorter instanceof TreePathViewerSorter && parent instanceof Item) { >+ Item item = (Item) parent; >+ path = getTreePathFromItem(item); >+ } else { >+ path = null; >+ } >+ return path; >+ } >+ >+ /** >+ * Compare the two elements using the given sorter. If the >+ * sorter is a {@link TreePathViewerSorter}, the provided >+ * tree path will be used. If the tree path is null and the >+ * sorter is a tree path sorter, then the elements are root elements >+ * @param sorter the sorter >+ * @param parentPath the path of the elements' parent >+ * @param e1 the first element >+ * @param e2 the seocnd element >+ * @return the result of comparing the two elements >+ */ >+ private int internalCompare(ViewerSorter sorter, TreePath parentPath, Object e1, Object e2) { >+ if (sorter instanceof TreePathViewerSorter) { >+ TreePathViewerSorter tpvs = (TreePathViewerSorter) sorter; >+ return tpvs.compare(this, parentPath, e1, e2); >+ } >+ return sorter.compare(this, e1, e2); >+ } >+ >+ /** >+ * Return the sorted children for the given tree path. The >+ * path is relative to the viewer input. >+ * A path of <code>null</code> indicates that the children for >+ * the input of the viewer are desired. >+ * @param parentPath the parent path >+ * @return the sorted children >+ * @since 3.2 >+ */ >+ protected Object[] getSortedChildren(TreePath parentPath) { >+ ViewerSorter sorter = getSorter(); >+ Object parent; >+ if (parentPath == null) { >+ parent = getInput(); >+ } else { >+ parent = parentPath.getLastSegment(); >+ } >+ if (sorter instanceof TreePathViewerSorter) { >+ TreePathViewerSorter tpvs = (TreePathViewerSorter) sorter; >+ Object[] result = getFilteredChildren(parent); >+ // be sure we're not modifying the original array from the model >+ result = (Object[]) result.clone(); >+ tpvs.sort(this, parentPath, result); >+ return result; >+ } >+ return super.getSortedChildren(parent); >+ } >+ >+ /* (non-Javadoc) >+ * @see org.eclipse.jface.viewers.StructuredViewer#getSortedChildren(java.lang.Object) >+ */ >+ protected Object[] getSortedChildren(Object parent) { >+ ViewerSorter sorter = getSorter(); >+ if (sorter instanceof TreePathViewerSorter) { >+ Widget w = internalGetWidgetToSelect(parent); >+ if (w != null) >+ return getSortedChildren(w, parent); >+ } >+ return super.getSortedChildren(parent); >+ } >+ >+ /** >+ * Get the sorted children for the parent using a >+ * {@link TreePathViewerSorter} if there is one and >+ * {@link StructuredViewer#getSortedChildren(Object)} if there isn't. >+ * >+ * @param widget >+ * the widget for the parent >+ * @param parent >+ * the parent element >+ * @return the sorted children >+ */ >+ private Object[] getSortedChildren(Widget widget, Object parent) { >+ ViewerSorter sorter = getSorter(); >+ TreePath parentPath = internalGetSorterParentPath(widget, sorter); >+ if (sorter instanceof TreePathViewerSorter) { >+ return getSortedChildren(parentPath); >+ } >+ return super.getSortedChildren(parent); >+ } >+ >+ /** > * Adds the given child element to this viewer as a child of the given > * parent element. If this viewer does not have a sorter, the element is > * added at the end of the parent's list of children; otherwise, the >@@ -514,7 +619,7 @@ > Object d = widget.getData(); > if (d != null) { > Object parentElement = d; >- Object[] children = getSortedChildren(parentElement); >+ Object[] children = getSortedChildren(widget, parentElement); > for (int i = 0; i < children.length; i++) { > createTreeItem(widget, children[i], -1); > } >@@ -1953,7 +2058,7 @@ > // If the children weren't passed in, get them now since they're needed > // below. > if (elementChildren == null) { >- elementChildren = getSortedChildren(parent); >+ elementChildren = getSortedChildren(widget, parent); > } > > Control tree = getControl(); >@@ -2083,7 +2188,7 @@ > } > } > >- /** >+ /** > * Updates the "+"/"-" icon of the tree node from the given element. It > * calls <code>isExpandable</code> to determine whether an element is > * expandable. >Index: src/org/eclipse/jface/viewers/TreePathViewerSorter.java >=================================================================== >RCS file: src/org/eclipse/jface/viewers/TreePathViewerSorter.java >diff -N src/org/eclipse/jface/viewers/TreePathViewerSorter.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/jface/viewers/TreePathViewerSorter.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,109 @@ >+/******************************************************************************* >+ * Copyright (c) 2006 IBM Corporation 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: >+ * IBM Corporation - initial API and implementation >+ ******************************************************************************/ >+ >+package org.eclipse.jface.viewers; >+ >+import java.util.Arrays; >+import java.util.Comparator; >+ >+/** >+ * A viewer sorter that is provded extra context in the form of the path of the >+ * parent element of the elements being sorted. >+ * >+ * @since 3.2 >+ */ >+public class TreePathViewerSorter extends ViewerSorter { >+ >+ /** >+ * Provide a category for the given element that will have the given parent >+ * path when it is added to the viewer. The provided path is >+ * relative to the viewer input. The parent path will >+ * be <code>null</code> when the elements are root elements. >+ * <p> >+ * By default, the this method calls >+ * {@link ViewerSorter#category(Object)}. Subclasses may override. >+ * >+ * @param parentPath >+ * the parent path for the element >+ * @param element >+ * the element >+ * @return the category of the element >+ */ >+ public int category(TreePath parentPath, Object element) { >+ return category(element); >+ } >+ >+ /** >+ * Compare the givcen elements that will have the given parent >+ * path when they are added to the viewer. The provided path is >+ * relative to the viewer input. The parent path will >+ * be <code>null</code> when the elements are root elements. >+ * <p> >+ * By default, the this method calls >+ * {@link ViewerSorter#sort(Viewer, Object[])}. Subclasses may override. >+ * @param viewer the viewer >+ * @param parentPath the parent path for the two elements >+ * @param e1 the first element >+ * @param e2 the second element >+ * @return a negative number if the first element is less than the >+ * second element; the value <code>0</code> if the first element is >+ * equal to the second element; and a positive >+ */ >+ public int compare(Viewer viewer, TreePath parentPath, Object e1, Object e2) { >+ return compare(viewer, e1, e2); >+ } >+ >+ /** >+ * Returns whether this viewer sorter would be affected >+ * by a change to the given property of the given element. >+ * The provided path is >+ * relative to the viewer input. The parent path will >+ * be <code>null</code> when the elements are root elements. >+ * <p> >+ * The default implementation of this method calls >+ * {@link ViewerSorter#isSorterProperty(Object, String)}. >+ * Subclasses may reimplement. >+ * @param parentPath the parent path of the element >+ * @param element the element >+ * @param property the property >+ * @return <code>true</code> if the sorting would be affected, >+ * and <code>false</code> if it would be unaffected >+ */ >+ public boolean isSorterProperty(TreePath parentPath, Object element, String property) { >+ return isSorterProperty(element, property); >+ } >+ >+ /** >+ * Sorts the given elements in-place, modifying the given array. >+ * The provided path is >+ * relative to the viewer input. The parent path will >+ * be <code>null</code> when the elements are root elements. >+ * <p> >+ * The default implementation of this method uses the >+ * java.util.Arrays#sort algorithm on the given array, >+ * calling {@link #compare(Viewer, TreePath, Object, Object)} to compare elements. >+ * </p> >+ * <p> >+ * Subclasses may reimplement this method to provide a more optimized implementation. >+ * </p> >+ * >+ * @param viewer the viewer >+ * @param parentPath the parent path of the given elements >+ * @param elements the elements to sort >+ */ >+ public void sort(final Viewer viewer, final TreePath parentPath, Object[] elements) { >+ Arrays.sort(elements, new Comparator() { >+ public int compare(Object a, Object b) { >+ return TreePathViewerSorter.this.compare(viewer, parentPath, a, b); >+ } >+ }); >+ } >+}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 126459
: 34166