Skip to content

Commit ac6a148

Browse files
committed
Restore recursive behavior of AbstractTreeViwer#internalExpandToLevel()
The method AbstractTreeViewer#internalExpandToLevel() used be a recursively called method (by implementation and specification). As a protected method, it is part of the API. With a recent change, the method was changed to delegate to a different method, removing it's recursive property. Consumers that have created subtypes of the AbstractTreeViewer and rely the recursive behavior of that method by overwriting it face a regression by that change. This change restores the existing, recursive behavior of that method while still keeping the reuse of the implementation across other use cases.
1 parent 9853481 commit ac6a148

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

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

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.util.Iterator;
2929
import java.util.LinkedList;
3030
import java.util.List;
31-
import java.util.function.Function;
3231

3332
import org.eclipse.core.runtime.Assert;
3433
import org.eclipse.core.runtime.IStatus;
@@ -1833,23 +1832,21 @@ protected Widget internalGetWidgetToSelect(Object elementOrTreePath) {
18331832
}
18341833

18351834
/**
1836-
* Recursively, conditionally expands the subtree rooted at the given widget to
1837-
* the given level. Takes the {@code shouldChildrenExpand} predicate that
1838-
* defines for a given widget if it shall be expanded.
1835+
* Recursively expands the subtree rooted at the given widget to the given level
1836+
* based on the {@code childExpansionFunction} function being executed for a
1837+
* child to be (potentially conditionally) expanded.
18391838
* <p>
18401839
* Note that the default implementation of this method does not call
18411840
* {@code setRedraw}.
18421841
* </p>
18431842
*
1844-
* @param widget the widget
1845-
* @param level non-negative level, or {@code ALL_LEVELS} to
1846-
* expand all levels of the tree
1847-
* @param shouldChildrenExpand predicate that defines for a given widget if it
1848-
* should be expanded.
1849-
* @since 3.32
1843+
* @param widget the widget
1844+
* @param level non-negative level, or {@code ALL_LEVELS} to
1845+
* expand all levels of the tree
1846+
* @param childExpansionFunction function to be called on a child to be expanded
18501847
*/
1851-
private void internalConditionalExpandToLevel(Widget widget, int level,
1852-
Function<Widget, Boolean> shouldChildrenExpand) {
1848+
private void internalCustomizedExpandToLevel(Widget widget, int level,
1849+
ExpansionFunction childExpansionFunction) {
18531850
if (level == ALL_LEVELS || level > 0) {
18541851
Object data = widget.getData();
18551852
if (widget instanceof Item it && data != null && !isExpandable(it, null, data)) {
@@ -1862,18 +1859,23 @@ private void internalConditionalExpandToLevel(Widget widget, int level,
18621859
}
18631860
if (level == ALL_LEVELS || level > 1) {
18641861
Item[] children = getChildren(widget);
1865-
if (children != null && shouldChildrenExpand.apply(widget).booleanValue()) {
1862+
if (children != null) {
18661863
int newLevel = (level == ALL_LEVELS ? ALL_LEVELS
18671864
: level - 1);
18681865
for (Item element : children) {
1869-
internalConditionalExpandToLevel(element, newLevel, shouldChildrenExpand);
1866+
childExpansionFunction.expand(widget, element, newLevel);
18701867
}
18711868
}
18721869
}
18731870
// XXX expanding here fails on linux
18741871
}
18751872
}
18761873

1874+
@FunctionalInterface
1875+
private interface ExpansionFunction {
1876+
void expand(Widget parent, Widget widget, int level);
1877+
}
1878+
18771879
/**
18781880
* Recursively expands the subtree rooted at the given widget to the given
18791881
* level.
@@ -1887,7 +1889,8 @@ private void internalConditionalExpandToLevel(Widget widget, int level,
18871889
* levels of the tree
18881890
*/
18891891
protected void internalExpandToLevel(Widget widget, int level) {
1890-
internalConditionalExpandToLevel(widget, level, w -> Boolean.TRUE);
1892+
internalCustomizedExpandToLevel(widget, level,
1893+
(parent, child, newLevel) -> internalExpandToLevel(child, newLevel));
18911894
}
18921895

18931896
/**
@@ -2532,14 +2535,19 @@ public void treeCollapsed(TreeExpansionEvent event) {
25322535
@Override
25332536
public void treeExpanded(TreeExpansionEvent e) {
25342537
Widget item = doFindItem(e.getElement());
2535-
2536-
internalConditionalExpandToLevel(item, autoExpandOnSingleChildLevels,
2537-
w -> Boolean.valueOf(doesWidgetHaveExactlyOneChild(w)));
2538+
internalCustomizedExpandToLevel(item, autoExpandOnSingleChildLevels, oneChildExpansionFunction);
25382539
}
25392540
};
25402541
addTreeListener(autoExpandOnSingleChildListener);
25412542
}
25422543

2544+
private ExpansionFunction oneChildExpansionFunction = (parent, widget, level) -> {
2545+
boolean hasOneChild = getChildren(parent).length == 1;
2546+
if (hasOneChild) {
2547+
internalCustomizedExpandToLevel(widget, level, this.oneChildExpansionFunction);
2548+
}
2549+
};
2550+
25432551
private void removeAutoExpandOnSingleChildListener() {
25442552
if (autoExpandOnSingleChildListener != null) {
25452553
removeTreeListener(autoExpandOnSingleChildListener);
@@ -2702,8 +2710,7 @@ public void setExpandedStateWithAutoExpandOnSingleChild(Object elementOrTreePath
27022710
Widget item = internalGetWidgetToSelect(elementOrTreePath);
27032711

27042712
if (autoExpandOnSingleChildLevels != NO_EXPAND && expanded) {
2705-
internalConditionalExpandToLevel(item, autoExpandOnSingleChildLevels,
2706-
w -> Boolean.valueOf(doesWidgetHaveExactlyOneChild(w)));
2713+
internalCustomizedExpandToLevel(item, autoExpandOnSingleChildLevels, oneChildExpansionFunction);
27072714
}
27082715
}
27092716

@@ -3535,8 +3542,4 @@ ISelection getUpdatedSelection(ISelection selection) {
35353542
return selection;
35363543
}
35373544

3538-
private boolean doesWidgetHaveExactlyOneChild(Widget w) {
3539-
return getChildren(w).length == 1;
3540-
}
3541-
35423545
}

0 commit comments

Comments
 (0)