Skip to content

Commit

Permalink
Provide method to avoid expanding widgets in AbstractTreeViewer
Browse files Browse the repository at this point in the history
  • Loading branch information
fedejeanne committed Jan 15, 2025
1 parent bbc28ce commit fc46dd4
Showing 1 changed file with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IStatus;
Expand Down Expand Up @@ -1850,12 +1851,40 @@ protected Widget internalGetWidgetToSelect(Object elementOrTreePath) {
*/
private void internalConditionalExpandToLevel(Widget widget, int level,
Function<Widget, Boolean> shouldChildrenExpand) {
internalConditionalExpandToLevel(widget, level, getShouldWigdetExpand(), shouldChildrenExpand);
}

/**
* @return the predicate that determines if a widget should be expanded when
* expanding the tree.
* @since 3.36
*/
protected Predicate<Widget> getShouldWigdetExpand() {
return w -> true;
}

/**
* Same as
* {@link #internalConditionalExpandToLevel(Widget, int, Function, Function)}
* but with an extra parameter to determine if the widget itself should be
* expanded.
*
* @param shouldWidgetExpand evaluates if the widget itself needs to be
* expanded.
*/
private void internalConditionalExpandToLevel(Widget widget, int level, Predicate<Widget> shouldWidgetExpand,
Function<Widget, Boolean> shouldChildrenExpand) {
if (level == ALL_LEVELS || level > 0) {
Object data = widget.getData();
if (widget instanceof Item it && data != null && !isExpandable(it, null, data)) {
return;
}
createChildren(widget, false);

if (!shouldWidgetExpand.test(widget)) {
return;
}

// XXX for performance widget should be expanded after expanding children:
if (widget instanceof Item it) {
setExpanded(it, true);
Expand All @@ -1866,7 +1895,7 @@ private void internalConditionalExpandToLevel(Widget widget, int level,
int newLevel = (level == ALL_LEVELS ? ALL_LEVELS
: level - 1);
for (Item element : children) {
internalConditionalExpandToLevel(element, newLevel, shouldChildrenExpand);
internalConditionalExpandToLevel(element, newLevel, shouldWidgetExpand, shouldChildrenExpand);
}
}
}
Expand Down

0 comments on commit fc46dd4

Please sign in to comment.