diff --git a/platform/platform-api/src/com/intellij/util/ui/tree/TreeUtil.java b/platform/platform-api/src/com/intellij/util/ui/tree/TreeUtil.java index 26cac065f60b..6a547d5d9579 100644 --- a/platform/platform-api/src/com/intellij/util/ui/tree/TreeUtil.java +++ b/platform/platform-api/src/com/intellij/util/ui/tree/TreeUtil.java @@ -35,8 +35,8 @@ import com.intellij.util.containers.JBTreeTraverser; import com.intellij.util.ui.UIUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.concurrency.AsyncPromise; import org.jetbrains.concurrency.Promise; +import org.jetbrains.concurrency.Promises; import javax.swing.*; import javax.swing.plaf.basic.BasicTreeUI; @@ -807,14 +807,13 @@ public final class TreeUtil { } /** - * Promises to expands all nodes in the specified tree. + * Promises to expand all nodes in the specified tree. * * @param tree a tree, which nodes should be expanded */ - public static Promise promiseExpandAll(@NotNull JTree tree) { - AsyncPromise promise = new AsyncPromise<>(); - expandAll(tree, () -> promise.setResult(tree)); - return promise; + @NotNull + public static Promise promiseExpandAll(@NotNull JTree tree) { + return promiseExpand(tree, Integer.MAX_VALUE); } /** @@ -824,14 +823,8 @@ public final class TreeUtil { * @param onDone a task to run after expanding nodes */ public static void expandAll(@NotNull JTree tree, Runnable onDone) { - Function> acceptor = getTreeAcceptor(tree); - if (acceptor != null) { - expand(tree, acceptor, Integer.MAX_VALUE, onDone); - } - else { - expandAll(tree); - if (onDone != null) onDone.run(); - } + Promise promise = promiseExpandAll(tree); + if (onDone != null) promise.processed(path -> onDone.run()); } /** @@ -860,48 +853,31 @@ public final class TreeUtil { } /** - * Promises to expands some nodes in the specified tree. + * Promises to expand some nodes in the specified tree. * * @param tree a tree, which nodes should be expanded - * @param depth a depth from visible root + * @param depth a depth starting from the root node */ - public static Promise promiseExpand(@NotNull JTree tree, int depth) { - AsyncPromise promise = new AsyncPromise<>(); - expand(tree, depth, () -> promise.setResult(tree)); - return promise; + @NotNull + public static Promise promiseExpand(@NotNull JTree tree, int depth) { + return promiseAccept(tree, path -> { + int count = path.getPathCount(); + if (count > depth) return TreeVisitor.Action.SKIP_SIBLINGS; + tree.expandPath(path); + return TreeVisitor.Action.CONTINUE; + }); } /** * Expands some nodes in the specified tree and runs the specified task on done. * * @param tree a tree, which nodes should be expanded - * @param depth a depth from visible root + * @param depth a depth starting from the root node * @param onDone a task to run after expanding nodes */ public static void expand(@NotNull JTree tree, int depth, Runnable onDone) { - if (depth < Integer.MAX_VALUE && !tree.isRootVisible()) depth++; - Function> acceptor = depth <= 0 ? null : getTreeAcceptor(tree); - if (acceptor != null) { - expand(tree, acceptor, depth, onDone); - } - else { - if (depth > 0) expand(tree, depth); - if (onDone != null) onDone.run(); - } - } - - private static void expand(@NotNull JTree tree, @NotNull Function> acceptor, int depth, Runnable onDone) { - Promise promise = acceptor.apply(new TreeVisitor() { - @NotNull - @Override - public Action visit(@NotNull TreePath path) { - int count = path.getPathCount(); - if (count > depth) return Action.SKIP_SIBLINGS; - tree.expandPath(path); - return Action.CONTINUE; - } - }); - if (onDone != null) promise.processed(ignored -> onDone.run()); + Promise promise = promiseExpand(tree, depth); + if (onDone != null) promise.processed(path -> onDone.run()); } @NotNull @@ -1139,6 +1115,20 @@ public final class TreeUtil { return Comparator.comparingInt(tree::getRowForPath); } + /** + * Promises to process nodes in the specified tree. + * + * @param tree a tree, which nodes should be processed + * @param visitor a visitor that controls processing of tree nodes + */ + @NotNull + public static Promise promiseAccept(@NotNull JTree tree, @NotNull TreeVisitor visitor) { + Function> acceptor = getTreeAcceptor(tree); + if (acceptor != null) return acceptor.apply(visitor); + TreeModel model = tree.getModel(); + return model == null ? Promises.rejectedPromise() : Promises.resolvedPromise(accept(model, visitor)); + } + /** * Processes nodes in the specified tree. * @@ -1147,16 +1137,8 @@ public final class TreeUtil { * @param consumer a path consumer called on done */ public static void accept(@NotNull JTree tree, @NotNull TreeVisitor visitor, @Nullable Consumer consumer) { - Function> acceptor = getTreeAcceptor(tree); - if (acceptor != null) { - Promise promise = acceptor.apply(visitor); - if (consumer != null) promise.processed(path -> consumer.accept(path)); - } - else { - TreeModel model = tree.getModel(); - TreePath path = model == null ? null : accept(model, visitor); - if (consumer != null) consumer.accept(path); - } + Promise promise = promiseAccept(tree, visitor); + if (consumer != null) promise.processed(path -> consumer.accept(path)); } private static TreePath accept(@NotNull TreeModel model, @NotNull TreeVisitor visitor) {