diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/BaseLibrariesConfigurable.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/BaseLibrariesConfigurable.java index 5db898a1b9d8..17ca042c1850 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/BaseLibrariesConfigurable.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/BaseLibrariesConfigurable.java @@ -43,6 +43,8 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import static com.intellij.ui.tree.TreePathUtil.toTreePathArray; + public abstract class BaseLibrariesConfigurable extends BaseStructureConfigurable { @NotNull protected final String myLevel; @@ -248,7 +250,7 @@ public abstract class BaseLibrariesConfigurable extends BaseStructureConfigurabl } } myContext.getDaemonAnalyzer().removeElements(libraries); - removePaths(pathsToRemove.toArray(new TreePath[0])); + removePaths(toTreePathArray(pathsToRemove)); } @Override diff --git a/platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionTree.java b/platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionTree.java index d3d9d5903ffa..cea24ca2cb12 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionTree.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionTree.java @@ -56,6 +56,7 @@ import java.util.*; import java.util.stream.Stream; import static com.intellij.codeInspection.CommonProblemDescriptor.DESCRIPTOR_COMPARATOR; +import static com.intellij.ui.tree.TreePathUtil.toTreePathArray; public class InspectionTree extends Tree { private static final Logger LOG = Logger.getInstance(InspectionTree.class); @@ -456,7 +457,7 @@ public class InspectionTree extends Tree { for (InspectionTreeNode parent : parents) { parent.dropProblemCountCaches(); } - TreeUtil.selectPath(this, TreeUtil.findCommonPath(pathsToSelect.toArray(new TreePath[0]))); + TreeUtil.selectPath(this, TreeUtil.findCommonPath(toTreePathArray(pathsToSelect))); revalidate(); repaint(); diff --git a/platform/lang-impl/src/com/intellij/ide/projectView/impl/AbstractProjectViewPSIPane.java b/platform/lang-impl/src/com/intellij/ide/projectView/impl/AbstractProjectViewPSIPane.java index e932a6c15179..1738a03c9afb 100644 --- a/platform/lang-impl/src/com/intellij/ide/projectView/impl/AbstractProjectViewPSIPane.java +++ b/platform/lang-impl/src/com/intellij/ide/projectView/impl/AbstractProjectViewPSIPane.java @@ -181,7 +181,7 @@ public abstract class AbstractProjectViewPSIPane extends AbstractProjectViewPane TreeBuilderUtil.storePaths(builder, (DefaultMutableTreeNode)myTree.getModel().getRoot(), pathsToExpand, selectionPaths, true); afterUpdate = () -> { if (myTree != null && !builder.isDisposed()) { - myTree.setSelectionPaths(new TreePath[0]); + myTree.clearSelection(); TreeBuilderUtil.restorePaths(builder, pathsToExpand, selectionPaths, true); } cb.setDone(); diff --git a/platform/lang-impl/src/com/intellij/ide/projectView/impl/ProjectViewImpl.java b/platform/lang-impl/src/com/intellij/ide/projectView/impl/ProjectViewImpl.java index b5b7b14c7695..9ecc92cfdc9c 100644 --- a/platform/lang-impl/src/com/intellij/ide/projectView/impl/ProjectViewImpl.java +++ b/platform/lang-impl/src/com/intellij/ide/projectView/impl/ProjectViewImpl.java @@ -91,6 +91,8 @@ import java.awt.*; import java.util.List; import java.util.*; +import static com.intellij.ui.tree.TreePathUtil.toTreePathArray; + @State(name = "ProjectView", storages = { @Storage(StoragePathMacros.PRODUCT_WORKSPACE_FILE), @Storage(value = StoragePathMacros.WORKSPACE_FILE, deprecated = true) @@ -1896,7 +1898,7 @@ public class ProjectViewImpl extends ProjectView implements PersistentStateCompo } } if (!paths.isEmpty()) { - tree.setSelectionPaths(paths.toArray(new TreePath[0])); + tree.setSelectionPaths(toTreePathArray(paths)); } } else { diff --git a/platform/lang-impl/src/com/intellij/ide/util/MemberChooser.java b/platform/lang-impl/src/com/intellij/ide/util/MemberChooser.java index 95f4005b9ae6..242aac59b3ac 100644 --- a/platform/lang-impl/src/com/intellij/ide/util/MemberChooser.java +++ b/platform/lang-impl/src/com/intellij/ide/util/MemberChooser.java @@ -22,7 +22,6 @@ import com.intellij.ui.treeStructure.Tree; import com.intellij.util.PlatformIcons; import com.intellij.util.containers.FactoryMap; import com.intellij.util.ui.JBUI; -import com.intellij.util.ui.UIUtil; import com.intellij.util.ui.tree.TreeUtil; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; @@ -38,6 +37,7 @@ import java.util.List; import java.util.*; import static com.intellij.openapi.keymap.KeymapUtil.getActiveKeymapShortcuts; +import static com.intellij.ui.tree.TreePathUtil.toTreePathArray; public class MemberChooser extends DialogWrapper implements TypeSafeDataProvider { private static final Logger LOG = Logger.getInstance("#com.intellij.ide.util.MemberChooser"); @@ -242,7 +242,7 @@ public class MemberChooser extends DialogWrapper implemen selectionPaths.add(new TreePath(((DefaultMutableTreeNode)treeNode).getPath())); } } - final TreePath[] paths = selectionPaths.toArray(new TreePath[0]); + final TreePath[] paths = toTreePathArray(selectionPaths); myTree.setSelectionPaths(paths); if (paths.length > 0) { @@ -633,7 +633,7 @@ public class MemberChooser extends DialogWrapper implemen } if (!toSelect.isEmpty()) { - myTree.setSelectionPaths(toSelect.toArray(new TreePath[0])); + myTree.setSelectionPaths(toTreePathArray(toSelect)); } ElementNode leadNode = pair.first; diff --git a/platform/platform-api/src/com/intellij/ide/util/treeView/TreeBuilderUtil.java b/platform/platform-api/src/com/intellij/ide/util/treeView/TreeBuilderUtil.java index 0e5381890de1..8ae71e6fd00d 100644 --- a/platform/platform-api/src/com/intellij/ide/util/treeView/TreeBuilderUtil.java +++ b/platform/platform-api/src/com/intellij/ide/util/treeView/TreeBuilderUtil.java @@ -78,7 +78,7 @@ public class TreeBuilderUtil { for (Object path : pathsToExpand) { tree.expandPath((TreePath)path); } - tree.addSelectionPaths(selectionPaths.toArray(new TreePath[0])); + tree.addSelectionPaths(selectionPaths.toArray(TreeUtil.EMPTY_TREE_PATH)); } else{ for (Object element : pathsToExpand) { diff --git a/platform/platform-api/src/com/intellij/ide/util/treeView/TreeState.java b/platform/platform-api/src/com/intellij/ide/util/treeView/TreeState.java index e21e13a283c6..06132d5dc0c9 100644 --- a/platform/platform-api/src/com/intellij/ide/util/treeView/TreeState.java +++ b/platform/platform-api/src/com/intellij/ide/util/treeView/TreeState.java @@ -280,7 +280,7 @@ public class TreeState implements JDOMExternalizable { ContainerUtil.addIfNotNull(selection, treePath); } if (selection.isEmpty()) return; - tree.setSelectionPaths(selection.toArray(new TreePath[0])); + tree.setSelectionPaths(selection.toArray(TreeUtil.EMPTY_TREE_PATH)); if (myScrollToSelection) { TreeUtil.showRowCentered(tree, tree.getRowForPath(selection.get(0)), true, true); } diff --git a/platform/platform-api/src/com/intellij/ui/treeStructure/treetable/TreeTable.java b/platform/platform-api/src/com/intellij/ui/treeStructure/treetable/TreeTable.java index 45e2e285659c..e8688c9fced5 100644 --- a/platform/platform-api/src/com/intellij/ui/treeStructure/treetable/TreeTable.java +++ b/platform/platform-api/src/com/intellij/ui/treeStructure/treetable/TreeTable.java @@ -6,6 +6,7 @@ import com.intellij.ui.TableUtil; import com.intellij.ui.scale.JBUIScale; import com.intellij.ui.table.JBTable; import com.intellij.util.ui.accessibility.ScreenReader; +import com.intellij.util.ui.tree.TreeUtil; import org.jetbrains.annotations.NotNull; import javax.swing.*; @@ -312,7 +313,7 @@ public class TreeTable extends JBTable { } } if (!selectionPaths.isEmpty()) { - addSelectionPaths(selectionPaths.toArray(new TreePath[0])); + addSelectionPaths(selectionPaths.toArray(TreeUtil.EMPTY_TREE_PATH)); } } } 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 3941dd4643b6..b98a59403dd3 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 @@ -52,6 +52,7 @@ import static com.intellij.util.ReflectionUtil.getDeclaredMethod; import static java.util.stream.Collectors.toList; public final class TreeUtil { + public static final TreePath[] EMPTY_TREE_PATH = new TreePath[0]; private static final Logger LOG = Logger.getInstance("#com.intellij.util.ui.tree.TreeUtil"); private static final String TREE_UTIL_SCROLL_TIME_STAMP = "TreeUtil.scrollTimeStamp"; private static final JBIterable NUMBERS = JBIterable.generate(0, i -> i + 1); @@ -358,20 +359,20 @@ public final class TreeUtil { for (final TreePath path : paths) { if (!result.contains(path)) result.add(path); } - return result.toArray(new TreePath[0]); + return result.toArray(EMPTY_TREE_PATH); } @NotNull public static TreePath[] selectMaximals(@Nullable final TreePath[] paths) { - if (paths == null) return new TreePath[0]; + if (paths == null) return EMPTY_TREE_PATH; final TreePath[] noDuplicates = removeDuplicates(paths); final ArrayList result = new ArrayList<>(); for (final TreePath path : noDuplicates) { final ArrayList otherPaths = new ArrayList<>(Arrays.asList(noDuplicates)); otherPaths.remove(path); - if (!isDescendants(path, otherPaths.toArray(new TreePath[0]))) result.add(path); + if (!isDescendants(path, otherPaths.toArray(EMPTY_TREE_PATH))) result.add(path); } - return result.toArray(new TreePath[0]); + return result.toArray(EMPTY_TREE_PATH); } public static void sort(@NotNull final DefaultTreeModel model, @Nullable Comparator comparator) { @@ -421,7 +422,7 @@ public final class TreeUtil { public static void selectPaths(@NotNull JTree tree, @NotNull Collection paths) { if (paths.isEmpty()) return; - selectPaths(tree, paths.toArray(new TreePath[0])); + selectPaths(tree, paths.toArray(EMPTY_TREE_PATH)); } public static void selectPaths(@NotNull JTree tree, @NotNull TreePath... paths) { @@ -1173,7 +1174,7 @@ public final class TreeUtil { if (toRetain == null) return; TreePath[] selection = tree.getSelectionModel().getSelectionPaths(); - selection = selection == null ? new TreePath[0] : selection; + selection = selection == null ? EMPTY_TREE_PATH : selection; for (TreePath each : selection) { if (toRetain.equals(each)) continue; tree.getSelectionModel().removeSelectionPath(each); @@ -1486,7 +1487,7 @@ public final class TreeUtil { private static void internalSelectPaths(@NotNull JTree tree, @NotNull List paths) { assert EventQueue.isDispatchThread(); if (paths.isEmpty()) return; - tree.setSelectionPaths(paths.toArray(new TreePath[0])); + tree.setSelectionPaths(paths.toArray(EMPTY_TREE_PATH)); for (TreePath path : paths) { if (scrollToVisible(tree, path, true)) { break; diff --git a/platform/platform-impl/src/com/intellij/openapi/options/newEditor/SettingsTreeView.java b/platform/platform-impl/src/com/intellij/openapi/options/newEditor/SettingsTreeView.java index f3ffb7e4e3f9..9ff2770e0a05 100644 --- a/platform/platform-impl/src/com/intellij/openapi/options/newEditor/SettingsTreeView.java +++ b/platform/platform-impl/src/com/intellij/openapi/options/newEditor/SettingsTreeView.java @@ -977,11 +977,7 @@ public class SettingsTreeView extends JComponent implements Accessible, Disposab } private void restoreExpandedState(List toRestore) { - TreePath[] selected = myTree.getSelectionPaths(); - if (selected == null) { - selected = new TreePath[0]; - } - + List selected = TreeUtil.collectSelectedPaths(myTree); List toCollapse = new ArrayList<>(); for (int eachRow = 0; eachRow < myTree.getRowCount(); eachRow++) { diff --git a/platform/platform-impl/src/com/intellij/packageDependencies/ui/TreeExpansionMonitor.java b/platform/platform-impl/src/com/intellij/packageDependencies/ui/TreeExpansionMonitor.java index 96098fbe68cc..116cf21e6b95 100644 --- a/platform/platform-impl/src/com/intellij/packageDependencies/ui/TreeExpansionMonitor.java +++ b/platform/platform-impl/src/com/intellij/packageDependencies/ui/TreeExpansionMonitor.java @@ -14,6 +14,8 @@ import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreePath; import java.util.*; +import static com.intellij.ui.tree.TreePathUtil.toTreePathArray; + public abstract class TreeExpansionMonitor { public static TreeExpansionMonitor install(final JTree tree) { @@ -75,7 +77,7 @@ public abstract class TreeExpansionMonitor { if (myFrozen) return; TreePath path = event.getPath(); if (path != null) { - TreePath[] allPaths = myExpandedPaths.toArray(new TreePath[0]); + TreePath[] allPaths = toTreePathArray(myExpandedPaths); for (TreePath treePath : allPaths) { if (treePath.equals(path) || path.isDescendant(treePath)) { myExpandedPaths.remove(treePath); diff --git a/platform/platform-impl/src/com/intellij/ui/TreeSpeedSearch.java b/platform/platform-impl/src/com/intellij/ui/TreeSpeedSearch.java index 8357956af2cd..51ce9a7dd7b0 100644 --- a/platform/platform-impl/src/com/intellij/ui/TreeSpeedSearch.java +++ b/platform/platform-impl/src/com/intellij/ui/TreeSpeedSearch.java @@ -22,6 +22,7 @@ import java.util.ArrayList; import java.util.List; import java.util.ListIterator; +import static com.intellij.ui.tree.TreePathUtil.toTreePathArray; import static javax.swing.tree.TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION; public class TreeSpeedSearch extends SpeedSearchBase { @@ -88,10 +89,9 @@ public class TreeSpeedSearch extends SpeedSearchBase { } paths = JBIterable.of(arr); } - List result = paths + return paths .filter(o -> !(o.getLastPathComponent() instanceof LoadingNode)) - .toList(); - return result.toArray(new TreePath[0]); + .toArray(TreeUtil.EMPTY_TREE_PATH); } @Override @@ -157,7 +157,7 @@ public class TreeSpeedSearch extends SpeedSearchBase { TreePath currentElement = (TreePath)mySearch.findElement(query); TreePath anchor = ObjectUtils.chooseNotNull(currentElement, filtered.get(0)); - sm.setSelectionPaths(filtered.toArray(new TreePath[0])); + sm.setSelectionPaths(toTreePathArray(filtered)); myTree.setAnchorSelectionPath(anchor); } } diff --git a/platform/platform-impl/src/com/intellij/ui/tree/TreePathUtil.java b/platform/platform-impl/src/com/intellij/ui/tree/TreePathUtil.java index 578938365075..9101d2439406 100644 --- a/platform/platform-impl/src/com/intellij/ui/tree/TreePathUtil.java +++ b/platform/platform-impl/src/com/intellij/ui/tree/TreePathUtil.java @@ -9,10 +9,13 @@ import javax.swing.tree.TreeNode; import javax.swing.tree.TreePath; import java.util.ArrayDeque; import java.util.Arrays; +import java.util.Collection; import java.util.Objects; import java.util.function.Function; import java.util.stream.Stream; +import static com.intellij.util.ui.tree.TreeUtil.EMPTY_TREE_PATH; + public class TreePathUtil { /** * @param parent the parent path or {@code null} to indicate the root @@ -189,6 +192,10 @@ public class TreePathUtil { return object == null ? null : converter.apply(object); } + public static TreePath[] toTreePathArray(@NotNull Collection collection) { + return collection.isEmpty() ? EMPTY_TREE_PATH : collection.toArray(EMPTY_TREE_PATH); + } + public static TreeNode toTreeNode(TreePath path) { Object component = path == null ? null : path.getLastPathComponent(); return component instanceof TreeNode ? (TreeNode)component : null; diff --git a/platform/platform-impl/src/com/intellij/ui/tree/TreeSmartSelectProvider.java b/platform/platform-impl/src/com/intellij/ui/tree/TreeSmartSelectProvider.java index 46fc48e43524..8ce47d5b9d9c 100644 --- a/platform/platform-impl/src/com/intellij/ui/tree/TreeSmartSelectProvider.java +++ b/platform/platform-impl/src/com/intellij/ui/tree/TreeSmartSelectProvider.java @@ -28,6 +28,7 @@ import java.util.ArrayList; import java.util.function.Consumer; import java.util.function.Predicate; +import static com.intellij.ui.tree.TreePathUtil.toTreePathArray; import static javax.swing.tree.TreeSelectionModel.SINGLE_TREE_SELECTION; /** @@ -123,7 +124,7 @@ public class TreeSmartSelectProvider implements SmartSelectProvider { return true; // visit all descendants }); if (list.isEmpty()) return false; // selection is not changed - consumer.accept(list.toArray(new TreePath[0])); + consumer.accept(toTreePathArray(list)); return true; } } diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesTree.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesTree.java index a89aebc01792..8e1f43a851d6 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesTree.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesTree.java @@ -54,6 +54,7 @@ import java.util.*; import static com.intellij.openapi.vcs.changes.ui.ChangesGroupingSupport.DIRECTORY_GROUPING; import static com.intellij.openapi.vcs.changes.ui.ChangesGroupingSupport.MODULE_GROUPING; +import static com.intellij.ui.tree.TreePathUtil.toTreePathArray; import static com.intellij.util.ObjectUtils.notNull; import static com.intellij.util.containers.ContainerUtil.ar; import static com.intellij.util.containers.ContainerUtil.set; @@ -734,7 +735,7 @@ public abstract class ChangesTree extends Tree implements DataProvider { } return true; }); - setSelectionPaths(treeSelection.toArray(new TreePath[0])); + setSelectionPaths(toTreePathArray(treeSelection)); if (treeSelection.size() == 1) scrollPathToVisible(treeSelection.get(0)); } diff --git a/platform/xdebugger-impl/src/com/intellij/xdebugger/impl/ui/tree/XDebuggerTreeSpeedSearch.java b/platform/xdebugger-impl/src/com/intellij/xdebugger/impl/ui/tree/XDebuggerTreeSpeedSearch.java index 298fec271e6f..c5aec8accb39 100644 --- a/platform/xdebugger-impl/src/com/intellij/xdebugger/impl/ui/tree/XDebuggerTreeSpeedSearch.java +++ b/platform/xdebugger-impl/src/com/intellij/xdebugger/impl/ui/tree/XDebuggerTreeSpeedSearch.java @@ -102,7 +102,6 @@ class XDebuggerTreeSpeedSearch extends TreeSpeedSearch { .expand(n -> myComponent.isExpanded(n) || n.getPathCount() - initialLevel < SEARCH_DEPTH) .traverse() .filter(o -> !(o.getLastPathComponent() instanceof LoadingNode)) - .toList() - .toArray(new TreePath[0]); + .toArray(TreeUtil.EMPTY_TREE_PATH); } } \ No newline at end of file diff --git a/plugins/stream-debugger/src/com/intellij/debugger/streams/ui/impl/CollectionTree.java b/plugins/stream-debugger/src/com/intellij/debugger/streams/ui/impl/CollectionTree.java index 70f90fb5f6f1..74feab6d8479 100644 --- a/plugins/stream-debugger/src/com/intellij/debugger/streams/ui/impl/CollectionTree.java +++ b/plugins/stream-debugger/src/com/intellij/debugger/streams/ui/impl/CollectionTree.java @@ -42,11 +42,12 @@ import java.util.List; import java.util.*; import java.util.stream.Collectors; +import static com.intellij.util.ui.tree.TreeUtil.collectSelectedPaths; + /** * @author Vitaliy.Bibaev */ public class CollectionTree extends XDebuggerTree implements TraceContainer { - private static final TreePath[] EMPTY_PATHS = new TreePath[0]; private static final Map COLORS_CACHE = new HashMap<>(); private static final Object NULL_MARKER = ObjectUtils.sentinel("CollectionTree.NULL_MARKER"); @@ -117,11 +118,8 @@ public class CollectionTree extends XDebuggerTree implements TraceContainer { if (myIgnoreInternalSelectionEvents) { return; } - - final TreePath[] selectedPaths = getSelectionPaths(); - final TreePath[] paths = selectedPaths == null ? EMPTY_PATHS : selectedPaths; final List selectedItems = - Arrays.stream(paths) + collectSelectedPaths(this).stream() .map(this::getTopPath) .map(myPath2Value::get) .filter(Objects::nonNull)