Skip to content

Commit e738501

Browse files
committed
Deprecate more ViewerSorter(s) forRemoval
Replace them by straight copies with parent ViewerComparator.
1 parent b941407 commit e738501

File tree

10 files changed

+379
-55
lines changed

10 files changed

+379
-55
lines changed

bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/AbstractTreeViewer.java

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ private Widget internalFindItem(TreePath path) {
261261
* the child elements to add
262262
* @since 3.1
263263
*/
264+
@SuppressWarnings("removal")
264265
protected void internalAdd(Widget widget, Object parentElementOrTreePath,
265266
Object[] childElements) {
266267
Object parent;
@@ -309,6 +310,11 @@ protected void internalAdd(Widget widget, Object parentElementOrTreePath,
309310
path = internalGetSorterParentPath(widget, comparator);
310311
}
311312
tpvs.sort(this, path, filtered);
313+
} else if (comparator instanceof TreePathViewerComparator tpvs) {
314+
if (path == null) {
315+
path = internalGetSorterParentPath(widget, comparator);
316+
}
317+
tpvs.sort(this, path, filtered);
312318
} else {
313319
comparator.sort(this, filtered);
314320
}
@@ -500,23 +506,18 @@ private boolean itemExists(Item[] items, Object element) {
500506

501507
/**
502508
* Returns the index where the item should be inserted. It uses sorter to
503-
* determine the correct position, if sorter is not assigned, returns the
504-
* index of the element after the last.
505-
*
506-
* @param items
507-
* the items to search
508-
* @param comparator
509-
* The comparator to use.
510-
* @param lastInsertion
511-
* the start index to start search for position from this allows
512-
* optimizing search for multiple elements that are sorted
513-
* themselves.
514-
* @param element
515-
* element to find position for.
516-
* @param parentPath
517-
* the tree path for the element's parent or <code>null</code>
518-
* if the element is a root element or the sorter is not a
519-
* {@link TreePathViewerSorter}
509+
* determine the correct position, if sorter is not assigned, returns the index
510+
* of the element after the last.
511+
*
512+
* @param items the items to search
513+
* @param comparator The comparator to use.
514+
* @param lastInsertion the start index to start search for position from this
515+
* allows optimizing search for multiple elements that are
516+
* sorted themselves.
517+
* @param element element to find position for.
518+
* @param parentPath the tree path for the element's parent or
519+
* <code>null</code> if the element is a root element or
520+
* the sorter is not a {@link TreePathViewerComparator}
520521
* @return the index to use when inserting the element.
521522
*/
522523

@@ -615,22 +616,22 @@ protected int indexForElement(Widget parent, Object element) {
615616

616617
/**
617618
* Return the tree path that should be used as the parent path for the given
618-
* widget and sorter. A <code>null</code> is returned if either the sorter
619-
* is not a {@link TreePathViewerSorter} or if the parent widget is not an
619+
* widget and sorter. A <code>null</code> is returned if either the sorter is
620+
* not a {@link TreePathViewerComparator} or if the parent widget is not an
620621
* {@link Item} (i.e. is the root of the tree).
621622
*
622-
* @param parent
623-
* the parent widget
624-
* @param comparator
625-
* the sorter
626-
* @return the tree path that should be used as the parent path for the
627-
* given widget and sorter
623+
* @param parent the parent widget
624+
* @param comparator the sorter
625+
* @return the tree path that should be used as the parent path for the given
626+
* widget and sorter
628627
*/
628+
@SuppressWarnings("removal")
629629
private TreePath internalGetSorterParentPath(Widget parent,
630630
ViewerComparator comparator) {
631631
TreePath path;
632-
if (comparator instanceof TreePathViewerSorter
633-
&& parent instanceof Item item) {
632+
if (comparator instanceof TreePathViewerSorter && parent instanceof Item item) {
633+
path = getTreePathFromItem(item);
634+
} else if (comparator instanceof TreePathViewerComparator && parent instanceof Item item) {
634635
path = getTreePathFromItem(item);
635636
} else {
636637
path = null;
@@ -640,24 +641,23 @@ private TreePath internalGetSorterParentPath(Widget parent,
640641

641642
/**
642643
* Compare the two elements using the given sorter. If the sorter is a
643-
* {@link TreePathViewerSorter}, the provided tree path will be used. If
644-
* the tree path is null and the sorter is a tree path sorter, then the
645-
* elements are root elements
646-
*
647-
* @param comparator
648-
* the sorter
649-
* @param parentPath
650-
* the path of the elements' parent
651-
* @param e1
652-
* the first element
653-
* @param e2
654-
* the second element
644+
* {@link TreePathViewerComparator}, the provided tree path will be used. If the
645+
* tree path is null and the sorter is a tree path sorter, then the elements are
646+
* root elements
647+
*
648+
* @param comparator the sorter
649+
* @param parentPath the path of the elements' parent
650+
* @param e1 the first element
651+
* @param e2 the second element
655652
* @return the result of comparing the two elements
656653
*/
654+
@SuppressWarnings("removal")
657655
private int internalCompare(ViewerComparator comparator,
658656
TreePath parentPath, Object e1, Object e2) {
659657
if (comparator instanceof TreePathViewerSorter tpvs) {
660658
return tpvs.compare(this, parentPath, e1, e2);
659+
} else if (comparator instanceof TreePathViewerComparator tpvs) {
660+
return tpvs.compare(this, parentPath, e1, e2);
661661
}
662662
return comparator.compare(this, e1, e2);
663663
}
@@ -667,7 +667,7 @@ protected Object[] getSortedChildren(Object parentElementOrTreePath) {
667667
Object[] result = null;
668668
ViewerComparator comparator = getComparator();
669669
if (parentElementOrTreePath != null
670-
&& comparator instanceof TreePathViewerSorter tpvs) {
670+
&& comparator instanceof TreePathViewerComparator tpvs) {
671671
result = getFilteredChildren(parentElementOrTreePath);
672672
// be sure we're not modifying the original array from the model
673673
result = result.clone();
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2026 IBM Corporation and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* IBM Corporation - initial API and implementation
13+
*******************************************************************************/
14+
15+
package org.eclipse.jface.viewers;
16+
17+
import java.util.Arrays;
18+
19+
/**
20+
* A viewer sorter that is provided extra context in the form of the path of the
21+
* parent element of the elements being sorted.
22+
*
23+
* @since 3.39
24+
*/
25+
public class TreePathViewerComparator extends ViewerComparator {
26+
27+
/**
28+
* Provide a category for the given element that will have the given parent
29+
* path when it is added to the viewer. The provided path is relative to the
30+
* viewer input. The parent path will be <code>null</code> when the elements
31+
* are root elements.
32+
* <p>
33+
* By default, the this method calls {@code ViewerSorter#category(Object)}.
34+
* Subclasses may override.
35+
*
36+
* @param parentPath
37+
* the parent path for the element
38+
* @param element
39+
* the element
40+
* @return the category of the element
41+
*/
42+
public int category(TreePath parentPath, Object element) {
43+
return category(element);
44+
}
45+
46+
/**
47+
* Compare the given elements that will have the given parent
48+
* path when they are added to the viewer. The provided path is
49+
* relative to the viewer input. The parent path will
50+
* be <code>null</code> when the elements are root elements.
51+
* <p>
52+
* By default, the this method calls
53+
* {@code ViewerSorter#sort(Viewer, Object[])}. Subclasses may override.
54+
* @param viewer the viewer
55+
* @param parentPath the parent path for the two elements
56+
* @param e1 the first element
57+
* @param e2 the second element
58+
* @return a negative number if the first element is less than the
59+
* second element; the value <code>0</code> if the first element is
60+
* equal to the second element; and a positive
61+
*/
62+
public int compare(Viewer viewer, TreePath parentPath, Object e1, Object e2) {
63+
return compare(viewer, e1, e2);
64+
}
65+
66+
/**
67+
* Returns whether this viewer sorter would be affected
68+
* by a change to the given property of the given element.
69+
* The provided path is
70+
* relative to the viewer input. The parent path will
71+
* be <code>null</code> when the elements are root elements.
72+
* <p>
73+
* The default implementation of this method calls
74+
* {@code ViewerSorter#isSorterProperty(Object, String)}.
75+
* Subclasses may reimplement.
76+
* @param parentPath the parent path of the element
77+
* @param element the element
78+
* @param property the property
79+
* @return <code>true</code> if the sorting would be affected,
80+
* and <code>false</code> if it would be unaffected
81+
*/
82+
public boolean isSorterProperty(TreePath parentPath, Object element, String property) {
83+
return isSorterProperty(element, property);
84+
}
85+
86+
/**
87+
* Sorts the given elements in-place, modifying the given array.
88+
* The provided path is
89+
* relative to the viewer input. The parent path will
90+
* be <code>null</code> when the elements are root elements.
91+
* <p>
92+
* The default implementation of this method uses the
93+
* java.util.Arrays#sort algorithm on the given array,
94+
* calling {@link #compare(Viewer, TreePath, Object, Object)} to compare elements.
95+
* </p>
96+
* <p>
97+
* Subclasses may reimplement this method to provide a more optimized implementation.
98+
* </p>
99+
*
100+
* @param viewer the viewer
101+
* @param parentPath the parent path of the given elements
102+
* @param elements the elements to sort
103+
*/
104+
public void sort(final Viewer viewer, final TreePath parentPath, Object[] elements) {
105+
Arrays.sort(elements, (a, b) -> TreePathViewerComparator.this.compare(viewer, parentPath, a, b));
106+
}
107+
}

bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/TreePathViewerSorter.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2006, 2015 IBM Corporation and others.
2+
* Copyright (c) 2006, 2026 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -21,7 +21,9 @@
2121
* parent element of the elements being sorted.
2222
*
2323
* @since 3.2
24+
* @deprecated Use {@link TreePathViewerComparator} instead.
2425
*/
26+
@Deprecated(forRemoval = true, since = "2026-03")
2527
public class TreePathViewerSorter extends ViewerSorter {
2628

2729
/**
@@ -39,6 +41,7 @@ public class TreePathViewerSorter extends ViewerSorter {
3941
* the element
4042
* @return the category of the element
4143
*/
44+
@Deprecated(forRemoval = true, since = "2026-03")
4245
public int category(TreePath parentPath, Object element) {
4346
return category(element);
4447
}
@@ -59,6 +62,7 @@ public int category(TreePath parentPath, Object element) {
5962
* second element; the value <code>0</code> if the first element is
6063
* equal to the second element; and a positive
6164
*/
65+
@Deprecated(forRemoval = true, since = "2026-03")
6266
public int compare(Viewer viewer, TreePath parentPath, Object e1, Object e2) {
6367
return compare(viewer, e1, e2);
6468
}
@@ -79,6 +83,7 @@ public int compare(Viewer viewer, TreePath parentPath, Object e1, Object e2) {
7983
* @return <code>true</code> if the sorting would be affected,
8084
* and <code>false</code> if it would be unaffected
8185
*/
86+
@Deprecated(forRemoval = true, since = "2026-03")
8287
public boolean isSorterProperty(TreePath parentPath, Object element, String property) {
8388
return isSorterProperty(element, property);
8489
}
@@ -101,6 +106,7 @@ public boolean isSorterProperty(TreePath parentPath, Object element, String prop
101106
* @param parentPath the parent path of the given elements
102107
* @param elements the elements to sort
103108
*/
109+
@Deprecated(forRemoval = true, since = "2026-03")
104110
public void sort(final Viewer viewer, final TreePath parentPath, Object[] elements) {
105111
Arrays.sort(elements, (a, b) -> TreePathViewerSorter.this.compare(viewer, parentPath, a, b));
106112
}

bundles/org.eclipse.search/search/org/eclipse/search/internal/ui/SorterDescriptor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.eclipse.core.runtime.Platform;
2222

2323
import org.eclipse.jface.resource.ImageDescriptor;
24-
import org.eclipse.jface.viewers.ViewerSorter;
24+
import org.eclipse.jface.viewers.ViewerComparator;
2525

2626
import org.eclipse.search.internal.ui.util.ExceptionHandler;
2727

@@ -53,9 +53,9 @@ public SorterDescriptor(IConfigurationElement element) {
5353
* Creates a new sorter from this node.
5454
* @return new sorter
5555
*/
56-
public ViewerSorter createObject() {
56+
public ViewerComparator createObject() {
5757
try {
58-
return (ViewerSorter)fElement.createExecutableExtension(CLASS_ATTRIBUTE);
58+
return (ViewerComparator) fElement.createExecutableExtension(CLASS_ATTRIBUTE);
5959
} catch (CoreException ex) {
6060
ExceptionHandler.handle(ex, SearchMessages.Search_Error_createSorter_title, SearchMessages.Search_Error_createSorter_message);
6161
return null;

bundles/org.eclipse.ui.navigator/src/org/eclipse/ui/navigator/CommonNavigator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2003, 2023 IBM Corporation and others.
2+
* Copyright (c) 2003, 2026 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -192,7 +192,7 @@ public void createPartControl(Composite aParent) {
192192
commonViewer.addFilter(visibleFilter);
193193
}
194194

195-
commonViewer.setComparator(new CommonViewerSorter());
195+
commonViewer.setComparator(new CommonViewerComparator());
196196

197197
/*
198198
* make sure input is set after sorters and filters to avoid unnecessary

bundles/org.eclipse.ui.navigator/src/org/eclipse/ui/navigator/CommonViewer.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ public void dispose() {
268268
* @param sorter a viewer sorter, or <code>null</code> if none
269269
* @deprecated Use {@link #setComparator(ViewerComparator)} instead.
270270
*/
271+
@SuppressWarnings("removal")
271272
@Override
272273
@Deprecated(forRemoval = true, since = "2025-03")
273274
public void setSorter(ViewerSorter sorter) {
@@ -284,10 +285,15 @@ public void setSorter(ViewerSorter sorter) {
284285
*
285286
* @param comparator a viewer sorter, or <code>null</code> if none
286287
*/
288+
@SuppressWarnings("removal")
287289
@Override
288290
public void setComparator(ViewerComparator comparator) {
289-
if (comparator != null && comparator instanceof CommonViewerSorter commonSorter) {
290-
commonSorter.setContentService(contentService);
291+
if (comparator != null) {
292+
if (comparator instanceof CommonViewerSorter commonSorter) {
293+
commonSorter.setContentService(contentService);
294+
} else if (comparator instanceof CommonViewerComparator commonComparator) {
295+
commonComparator.setContentService(contentService);
296+
}
291297
}
292298

293299
super.setComparator(comparator);

0 commit comments

Comments
 (0)