28
28
import java .util .Iterator ;
29
29
import java .util .LinkedList ;
30
30
import java .util .List ;
31
- import java .util .function .Function ;
32
31
33
32
import org .eclipse .core .runtime .Assert ;
34
33
import org .eclipse .core .runtime .IStatus ;
@@ -1833,23 +1832,22 @@ protected Widget internalGetWidgetToSelect(Object elementOrTreePath) {
1833
1832
}
1834
1833
1835
1834
/**
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.
1839
1838
* <p>
1840
1839
* Note that the default implementation of this method does not call
1841
1840
* {@code setRedraw}.
1842
1841
* </p>
1843
1842
*
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 childrenExpansionFunction function to be called on a child to be
1847
+ * expanded
1850
1848
*/
1851
- private void internalConditionalExpandToLevel (Widget widget , int level ,
1852
- Function < Widget , Boolean > shouldChildrenExpand ) {
1849
+ private void internalCustomizedExpandToLevel (Widget widget , int level ,
1850
+ CustomChildrenExpansionFunction childrenExpansionFunction ) {
1853
1851
if (level == ALL_LEVELS || level > 0 ) {
1854
1852
Object data = widget .getData ();
1855
1853
if (widget instanceof Item it && data != null && !isExpandable (it , null , data )) {
@@ -1861,19 +1859,18 @@ private void internalConditionalExpandToLevel(Widget widget, int level,
1861
1859
setExpanded (it , true );
1862
1860
}
1863
1861
if (level == ALL_LEVELS || level > 1 ) {
1864
- Item [] children = getChildren (widget );
1865
- if (children != null && shouldChildrenExpand .apply (widget ).booleanValue ()) {
1866
- int newLevel = (level == ALL_LEVELS ? ALL_LEVELS
1867
- : level - 1 );
1868
- for (Item element : children ) {
1869
- internalConditionalExpandToLevel (element , newLevel , shouldChildrenExpand );
1870
- }
1871
- }
1862
+ int newLevel = (level == ALL_LEVELS ? ALL_LEVELS : level - 1 );
1863
+ childrenExpansionFunction .expandChildren (widget , newLevel );
1872
1864
}
1873
1865
// XXX expanding here fails on linux
1874
1866
}
1875
1867
}
1876
1868
1869
+ @ FunctionalInterface
1870
+ private interface CustomChildrenExpansionFunction {
1871
+ void expandChildren (Widget parent , int previousLevel );
1872
+ }
1873
+
1877
1874
/**
1878
1875
* Recursively expands the subtree rooted at the given widget to the given
1879
1876
* level.
@@ -1887,7 +1884,14 @@ private void internalConditionalExpandToLevel(Widget widget, int level,
1887
1884
* levels of the tree
1888
1885
*/
1889
1886
protected void internalExpandToLevel (Widget widget , int level ) {
1890
- internalConditionalExpandToLevel (widget , level , w -> Boolean .TRUE );
1887
+ internalCustomizedExpandToLevel (widget , level , (parent , newLevel ) -> {
1888
+ Item [] children = getChildren (parent );
1889
+ if (children != null ) {
1890
+ for (Item child : children ) {
1891
+ internalExpandToLevel (child , newLevel );
1892
+ }
1893
+ }
1894
+ });
1891
1895
}
1892
1896
1893
1897
/**
@@ -2532,14 +2536,22 @@ public void treeCollapsed(TreeExpansionEvent event) {
2532
2536
@ Override
2533
2537
public void treeExpanded (TreeExpansionEvent e ) {
2534
2538
Widget item = doFindItem (e .getElement ());
2535
-
2536
- internalConditionalExpandToLevel (item , autoExpandOnSingleChildLevels ,
2537
- w -> Boolean .valueOf (doesWidgetHaveExactlyOneChild (w )));
2539
+ internalCustomizedExpandToLevel (item , autoExpandOnSingleChildLevels , singleChildExpansionFunction );
2538
2540
}
2539
2541
};
2540
2542
addTreeListener (autoExpandOnSingleChildListener );
2541
2543
}
2542
2544
2545
+ private CustomChildrenExpansionFunction singleChildExpansionFunction = (widget , newLevel ) -> {
2546
+ Item [] children = getChildren (widget );
2547
+ boolean hasExactlyOneChild = children != null && children .length == 1 ;
2548
+ if (hasExactlyOneChild ) {
2549
+ for (Item child : children ) {
2550
+ internalCustomizedExpandToLevel (child , newLevel , this .singleChildExpansionFunction );
2551
+ }
2552
+ }
2553
+ };
2554
+
2543
2555
private void removeAutoExpandOnSingleChildListener () {
2544
2556
if (autoExpandOnSingleChildListener != null ) {
2545
2557
removeTreeListener (autoExpandOnSingleChildListener );
@@ -2702,8 +2714,7 @@ public void setExpandedStateWithAutoExpandOnSingleChild(Object elementOrTreePath
2702
2714
Widget item = internalGetWidgetToSelect (elementOrTreePath );
2703
2715
2704
2716
if (autoExpandOnSingleChildLevels != NO_EXPAND && expanded ) {
2705
- internalConditionalExpandToLevel (item , autoExpandOnSingleChildLevels ,
2706
- w -> Boolean .valueOf (doesWidgetHaveExactlyOneChild (w )));
2717
+ internalCustomizedExpandToLevel (item , autoExpandOnSingleChildLevels , singleChildExpansionFunction );
2707
2718
}
2708
2719
}
2709
2720
@@ -3535,8 +3546,4 @@ ISelection getUpdatedSelection(ISelection selection) {
3535
3546
return selection ;
3536
3547
}
3537
3548
3538
- private boolean doesWidgetHaveExactlyOneChild (Widget w ) {
3539
- return getChildren (w ).length == 1 ;
3540
- }
3541
-
3542
3549
}
0 commit comments