Skip to content

Commit c2eddc0

Browse files
committed
Provide method to avoid expanding widgets in AbstractTreeViewer
1 parent 9bea968 commit c2eddc0

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Iterator;
2929
import java.util.LinkedList;
3030
import java.util.List;
31+
import java.util.function.Predicate;
3132

3233
import org.eclipse.core.runtime.Assert;
3334
import org.eclipse.core.runtime.IStatus;
@@ -139,6 +140,8 @@ public abstract class AbstractTreeViewer extends ColumnViewer {
139140
*/
140141
private boolean isTreePathContentProvider = false;
141142

143+
private Predicate<Item> shouldItemExpand;
144+
142145
/**
143146
* Safe runnable used to update an item.
144147
*/
@@ -1162,6 +1165,7 @@ public void expandToLevel(Object elementOrTreePath, int level, boolean disableRe
11621165
control.setRedraw(false);
11631166
}
11641167
Widget w = internalExpand(elementOrTreePath, true);
1168+
shouldItemExpand = createShouldItemExpand();
11651169
if (w != null) {
11661170
internalExpandToLevel(w, level);
11671171
}
@@ -1172,6 +1176,17 @@ public void expandToLevel(Object elementOrTreePath, int level, boolean disableRe
11721176
}
11731177
}
11741178

1179+
/**
1180+
* @return the predicate. It should return {@code true} if the received
1181+
* {@code Item} should be expanded when expanding the whole tree and
1182+
* {@code false} otherwise.
1183+
*
1184+
* @since 3.36
1185+
*/
1186+
protected Predicate<Item> createShouldItemExpand() {
1187+
return w -> true;
1188+
}
1189+
11751190
/**
11761191
* Fires a tree collapsed event. Only listeners registered at the time this
11771192
* method is called are notified.
@@ -1856,6 +1871,9 @@ private void internalCustomizedExpandToLevel(Widget widget, int level,
18561871
createChildren(widget, false);
18571872
// XXX for performance widget should be expanded after expanding children:
18581873
if (widget instanceof Item it) {
1874+
if (!shouldItemExpand.test(it)) {
1875+
return;
1876+
}
18591877
setExpanded(it, true);
18601878
}
18611879
if (level == ALL_LEVELS || level > 1) {

tests/org.eclipse.jface.tests/src/org/eclipse/jface/tests/viewers/TreeViewerTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*******************************************************************************/
1414
package org.eclipse.jface.tests.viewers;
1515

16+
import static org.junit.Assert.assertEquals;
1617
import static org.junit.Assert.assertFalse;
1718
import static org.junit.Assert.assertTrue;
1819

@@ -21,13 +22,15 @@
2122
import java.util.List;
2223
import java.util.Queue;
2324
import java.util.concurrent.ConcurrentLinkedQueue;
25+
import java.util.function.Predicate;
2426

2527
import org.eclipse.jface.viewers.AbstractTreeViewer;
2628
import org.eclipse.jface.viewers.StructuredViewer;
2729
import org.eclipse.jface.viewers.TreeViewer;
2830
import org.eclipse.swt.SWT;
2931
import org.eclipse.swt.widgets.Composite;
3032
import org.eclipse.swt.widgets.Event;
33+
import org.eclipse.swt.widgets.Item;
3134
import org.eclipse.swt.widgets.Tree;
3235
import org.eclipse.swt.widgets.TreeItem;
3336
import org.eclipse.swt.widgets.Widget;
@@ -114,4 +117,47 @@ protected void internalExpandToLevel(Widget widget, int level) {
114117
}
115118
}
116119

120+
@Test
121+
public void testExpandOnlyArbitraryChild() {
122+
123+
TestElement modelRoot = TestElement.createModel(3, 5);
124+
125+
// select an arbitrary child to be expanded
126+
TestElement childToBeExpanded = modelRoot.getChildAt(modelRoot.getChildCount() - 1);
127+
128+
TreeViewer viewer = new TreeViewer(fShell) {
129+
@Override
130+
protected Predicate<Item> createShouldItemExpand() {
131+
// Expand only "childToBeExpanded"
132+
return it -> {
133+
if (!(it.getData() instanceof TestElement)) {
134+
throw new RuntimeException("Data: " + it.getData());
135+
}
136+
137+
return it.getData() instanceof TestElement d && d.equals(childToBeExpanded);
138+
};
139+
}
140+
};
141+
142+
viewer.setContentProvider(new TestModelContentProvider());
143+
viewer.setInput(modelRoot);
144+
145+
// Even when expanding all elements, the provided predicate will take precedence
146+
viewer.expandToLevel(AbstractTreeViewer.ALL_LEVELS);
147+
148+
Queue<TestElement> elements = new ConcurrentLinkedQueue<>(Arrays.asList(modelRoot.getChildren()));
149+
while (!elements.isEmpty()) {
150+
TestElement currentElement = elements.poll();
151+
152+
boolean shouldExpand = currentElement.equals(childToBeExpanded);
153+
154+
assertEquals("The expanded state of the element " + currentElement + " is wrong", shouldExpand,
155+
viewer.getExpandedState(currentElement));
156+
elements.addAll(Arrays.asList(currentElement.getChildren()));
157+
}
158+
159+
// Double check that the desired element is there and that it was expanded
160+
assertTrue("The element " + childToBeExpanded + " should have been expanded",
161+
viewer.getExpandedState(childToBeExpanded));
162+
}
117163
}

0 commit comments

Comments
 (0)