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 16, 2025
1 parent 9bea968 commit c2eddc0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Predicate;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IStatus;
Expand Down Expand Up @@ -139,6 +140,8 @@ public abstract class AbstractTreeViewer extends ColumnViewer {
*/
private boolean isTreePathContentProvider = false;

private Predicate<Item> shouldItemExpand;

/**
* Safe runnable used to update an item.
*/
Expand Down Expand Up @@ -1162,6 +1165,7 @@ public void expandToLevel(Object elementOrTreePath, int level, boolean disableRe
control.setRedraw(false);
}
Widget w = internalExpand(elementOrTreePath, true);
shouldItemExpand = createShouldItemExpand();
if (w != null) {
internalExpandToLevel(w, level);
}
Expand All @@ -1172,6 +1176,17 @@ public void expandToLevel(Object elementOrTreePath, int level, boolean disableRe
}
}

/**
* @return the predicate. It should return {@code true} if the received
* {@code Item} should be expanded when expanding the whole tree and
* {@code false} otherwise.
*
* @since 3.36
*/
protected Predicate<Item> createShouldItemExpand() {
return w -> true;
}

/**
* Fires a tree collapsed event. Only listeners registered at the time this
* method is called are notified.
Expand Down Expand Up @@ -1856,6 +1871,9 @@ private void internalCustomizedExpandToLevel(Widget widget, int level,
createChildren(widget, false);
// XXX for performance widget should be expanded after expanding children:
if (widget instanceof Item it) {
if (!shouldItemExpand.test(it)) {
return;
}
setExpanded(it, true);
}
if (level == ALL_LEVELS || level > 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*******************************************************************************/
package org.eclipse.jface.tests.viewers;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

Expand All @@ -21,13 +22,15 @@
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Predicate;

import org.eclipse.jface.viewers.AbstractTreeViewer;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.widgets.Widget;
Expand Down Expand Up @@ -114,4 +117,47 @@ protected void internalExpandToLevel(Widget widget, int level) {
}
}

@Test
public void testExpandOnlyArbitraryChild() {

TestElement modelRoot = TestElement.createModel(3, 5);

// select an arbitrary child to be expanded
TestElement childToBeExpanded = modelRoot.getChildAt(modelRoot.getChildCount() - 1);

TreeViewer viewer = new TreeViewer(fShell) {
@Override
protected Predicate<Item> createShouldItemExpand() {
// Expand only "childToBeExpanded"
return it -> {
if (!(it.getData() instanceof TestElement)) {
throw new RuntimeException("Data: " + it.getData());
}

return it.getData() instanceof TestElement d && d.equals(childToBeExpanded);
};
}
};

viewer.setContentProvider(new TestModelContentProvider());
viewer.setInput(modelRoot);

// Even when expanding all elements, the provided predicate will take precedence
viewer.expandToLevel(AbstractTreeViewer.ALL_LEVELS);

Queue<TestElement> elements = new ConcurrentLinkedQueue<>(Arrays.asList(modelRoot.getChildren()));
while (!elements.isEmpty()) {
TestElement currentElement = elements.poll();

boolean shouldExpand = currentElement.equals(childToBeExpanded);

assertEquals("The expanded state of the element " + currentElement + " is wrong", shouldExpand,
viewer.getExpandedState(currentElement));
elements.addAll(Arrays.asList(currentElement.getChildren()));
}

// Double check that the desired element is there and that it was expanded
assertTrue("The element " + childToBeExpanded + " should have been expanded",
viewer.getExpandedState(childToBeExpanded));
}
}

0 comments on commit c2eddc0

Please sign in to comment.