Search in sources :

Example 86 with AbstractTreeNode

use of com.intellij.ide.util.treeView.AbstractTreeNode in project intellij-community by JetBrains.

the class ProjectViewTestUtil method checkContainsMethod.

public static void checkContainsMethod(final Object rootElement, final AbstractTreeStructure structure, Function<AbstractTreeNode, VirtualFile[]> converterFunction) {
    MultiValuesMap<VirtualFile, AbstractTreeNode> map = new MultiValuesMap<>();
    collect((AbstractTreeNode) rootElement, map, structure, converterFunction);
    for (VirtualFile eachFile : map.keySet()) {
        Collection<AbstractTreeNode> nodes = map.values();
        for (final AbstractTreeNode node : nodes) {
            ProjectViewNode eachNode = (ProjectViewNode) node;
            boolean actual = eachNode.contains(eachFile);
            boolean expected = map.get(eachFile).contains(eachNode);
            if (actual != expected) {
                boolean actual1 = eachNode.contains(eachFile);
                boolean expected1 = map.get(eachFile).contains(eachNode);
                Assert.assertTrue("file=" + eachFile + " node=" + eachNode.getTestPresentation() + " expected:" + expected, false);
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MultiValuesMap(com.intellij.openapi.util.MultiValuesMap) ProjectViewNode(com.intellij.ide.projectView.ProjectViewNode) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode)

Example 87 with AbstractTreeNode

use of com.intellij.ide.util.treeView.AbstractTreeNode in project intellij-community by JetBrains.

the class BreakpointsFavoriteListProvider method replicate.

private void replicate(DefaultMutableTreeNode source, AbstractTreeNode destination, final List<AbstractTreeNode<Object>> destinationChildren) {
    final ArrayList<AbstractTreeNode<Object>> copyChildren = new ArrayList<>();
    AbstractTreeNode<Object> copy = new AbstractTreeNode<Object>(myProject, source.getUserObject()) {

        @NotNull
        @Override
        public Collection<? extends AbstractTreeNode> getChildren() {
            return copyChildren;
        }

        @Override
        protected void update(PresentationData presentation) {
        }
    };
    for (int i = 0; i < source.getChildCount(); i++) {
        final TreeNode treeNode = source.getChildAt(i);
        if (treeNode instanceof DefaultMutableTreeNode) {
            final DefaultMutableTreeNode sourceChild = (DefaultMutableTreeNode) treeNode;
            replicate(sourceChild, copy, copyChildren);
        }
    }
    if (checkNavigatable(copy)) {
        destinationChildren.add(copy);
        copy.setParent(destination);
    }
}
Also used : PresentationData(com.intellij.ide.projectView.PresentationData) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TreeNode(javax.swing.tree.TreeNode) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) CheckedTreeNode(com.intellij.ui.CheckedTreeNode) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode)

Example 88 with AbstractTreeNode

use of com.intellij.ide.util.treeView.AbstractTreeNode in project intellij-community by JetBrains.

the class NestingTreeStructureProvider method modify.

@NotNull
@Override
public Collection<AbstractTreeNode> modify(@NotNull final AbstractTreeNode parent, @NotNull final Collection<AbstractTreeNode> children, final ViewSettings settings) {
    if (!(parent instanceof PsiDirectoryNode))
        return children;
    final Collection<NestingRule> rules = getNestingRules();
    if (rules.isEmpty())
        return children;
    final MultiMap<PsiFileNode, PsiFileNode> parentToChildren = calcParentToChildren(children, rules);
    if (parentToChildren.isEmpty())
        return children;
    // initial ArrayList size may be not exact, not a big problem
    final Collection<AbstractTreeNode> newChildren = new ArrayList<>(children.size() - parentToChildren.size());
    final Set<PsiFileNode> childrenToMoveDown = new THashSet<>(parentToChildren.values());
    for (AbstractTreeNode node : children) {
        if (!(node instanceof PsiFileNode)) {
            newChildren.add(node);
            continue;
        }
        if (childrenToMoveDown.contains(node)) {
            continue;
        }
        final Collection<PsiFileNode> childrenOfThisFile = parentToChildren.get((PsiFileNode) node);
        if (childrenOfThisFile.isEmpty()) {
            newChildren.add(node);
            continue;
        }
        newChildren.add(new NestingTreeNode((PsiFileNode) node, childrenOfThisFile));
    }
    return newChildren;
}
Also used : PsiFileNode(com.intellij.ide.projectView.impl.nodes.PsiFileNode) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) PsiDirectoryNode(com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode) NestingTreeNode(com.intellij.ide.projectView.impl.nodes.NestingTreeNode) THashSet(gnu.trove.THashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 89 with AbstractTreeNode

use of com.intellij.ide.util.treeView.AbstractTreeNode in project intellij-community by JetBrains.

the class NestingTreeStructureProvider method calcParentToChildren.

/*
    This is a graph theory problem. PsiFileNodes are graph nodes.
    Edges go from parent file to child file according to NestingRules, for example foo.js->foo.min.js.
    Parent may have several children. Child may have several parents.
    There may be cycles with 3 or more nodes, but cycle with 2 nodes (A->B and B->A) is impossible because parentFileSuffix != childFileSuffix
    For each child its outbound edges are removed. For example in case of a cycle all edges that form it are removed. In case of A->B->C only A->B remains.
    As a result we get a number of separated parent-to-many-children sub-graphs, and use them to nest child files under parent file in Project View.
    One child still may have more than one parent. For real use cases it is not expected to happen, but anyway it's not a big problem, it will be shown as a subnode more than once.
   */
@NotNull
private static MultiMap<PsiFileNode, PsiFileNode> calcParentToChildren(@NotNull final Collection<AbstractTreeNode> nodes, @NotNull final Collection<NestingRule> rules) {
    // result that will contain number of separated parent-to-many-children sub-graphs
    MultiMap<PsiFileNode, PsiFileNode> parentToChildren = null;
    // helps to remove all outbound edges of a node that has inbound edge itself
    Set<PsiFileNode> allChildNodes = null;
    // temporary map for building edges
    Map<Pair<String, NestingRule>, Edge<PsiFileNode>> baseNameAndRuleToEdge = null;
    for (AbstractTreeNode node : nodes) {
        if (!(node instanceof PsiFileNode))
            continue;
        final PsiFile file = ((PsiFileNode) node).getValue();
        if (file == null)
            continue;
        for (NestingRule rule : rules) {
            final String fileName = file.getName();
            final Couple<Boolean> c = checkMatchingAsParentOrChild(rule, fileName);
            final boolean matchesParent = c.first;
            final boolean matchesChild = c.second;
            if (!matchesChild && !matchesParent)
                continue;
            if (baseNameAndRuleToEdge == null) {
                baseNameAndRuleToEdge = new THashMap<>();
                parentToChildren = new MultiMap<>();
                allChildNodes = new THashSet<>();
            }
            if (matchesParent) {
                final String baseName = fileName.substring(0, fileName.length() - rule.myParentFileSuffix.length());
                final Edge<PsiFileNode> edge = getOrCreateEdge(baseNameAndRuleToEdge, baseName, rule);
                edge.from = (PsiFileNode) node;
                updateInfoIfEdgeComplete(parentToChildren, allChildNodes, edge);
            }
            if (matchesChild) {
                final String baseName = fileName.substring(0, fileName.length() - rule.myChildFileSuffix.length());
                final Edge<PsiFileNode> edge = getOrCreateEdge(baseNameAndRuleToEdge, baseName, rule);
                edge.to = (PsiFileNode) node;
                updateInfoIfEdgeComplete(parentToChildren, allChildNodes, edge);
            }
        }
    }
    return parentToChildren == null ? MultiMap.empty() : parentToChildren;
}
Also used : PsiFileNode(com.intellij.ide.projectView.impl.nodes.PsiFileNode) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) PsiFile(com.intellij.psi.PsiFile) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 90 with AbstractTreeNode

use of com.intellij.ide.util.treeView.AbstractTreeNode in project intellij-community by JetBrains.

the class ImportUsagesAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    final DataContext dc = e.getDataContext();
    final boolean enabled = isEnabled(dc);
    if (!enabled)
        return;
    final Project project = CommonDataKeys.PROJECT.getData(dc);
    final Collection<AbstractTreeNode> nodes = new UsageFavoriteNodeProvider().getFavoriteNodes(dc, ViewSettings.DEFAULT);
    final FavoritesManager favoritesManager = FavoritesManager.getInstance(project);
    if (nodes != null && !nodes.isEmpty()) {
        favoritesManager.addRoots(TaskDefaultFavoriteListProvider.CURRENT_TASK, nodes);
    }
}
Also used : Project(com.intellij.openapi.project.Project) DataContext(com.intellij.openapi.actionSystem.DataContext) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode)

Aggregations

AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)150 NotNull (org.jetbrains.annotations.NotNull)69 ArrayList (java.util.ArrayList)51 VirtualFile (com.intellij.openapi.vfs.VirtualFile)40 Project (com.intellij.openapi.project.Project)33 PsiFile (com.intellij.psi.PsiFile)30 Module (com.intellij.openapi.module.Module)27 PsiDirectory (com.intellij.psi.PsiDirectory)25 PsiElement (com.intellij.psi.PsiElement)16 Nullable (org.jetbrains.annotations.Nullable)15 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)14 ProjectViewNode (com.intellij.ide.projectView.ProjectViewNode)10 PsiDirectoryNode (com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode)9 PsiManager (com.intellij.psi.PsiManager)9 List (java.util.List)9 PresentationData (com.intellij.ide.projectView.PresentationData)6 ViewSettings (com.intellij.ide.projectView.ViewSettings)6 PsiFileNode (com.intellij.ide.projectView.impl.nodes.PsiFileNode)6 NamedLibraryElement (com.intellij.ide.projectView.impl.nodes.NamedLibraryElement)5 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)5