Search in sources :

Example 96 with AbstractTreeNode

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

the class TodoTreeHelper method getDirectoryChildren.

public Collection<AbstractTreeNode> getDirectoryChildren(PsiDirectory psiDirectory, TodoTreeBuilder builder, boolean isFlatten) {
    ArrayList<AbstractTreeNode> children = new ArrayList<>();
    if (!isFlatten || !skipDirectory(psiDirectory)) {
        final Iterator<PsiFile> iterator = builder.getFiles(psiDirectory);
        while (iterator.hasNext()) {
            final PsiFile psiFile = iterator.next();
            // Add files
            final PsiDirectory containingDirectory = psiFile.getContainingDirectory();
            TodoFileNode todoFileNode = new TodoFileNode(getProject(), psiFile, builder, false);
            if (psiDirectory.equals(containingDirectory) && !children.contains(todoFileNode)) {
                children.add(todoFileNode);
                continue;
            }
            // Add directories (find first ancestor directory that is in our psiDirectory)
            PsiDirectory _dir = psiFile.getContainingDirectory();
            while (_dir != null) {
                if (skipDirectory(_dir)) {
                    break;
                }
                final PsiDirectory parentDirectory = _dir.getParentDirectory();
                TodoDirNode todoDirNode = new TodoDirNode(getProject(), _dir, builder);
                if (parentDirectory != null && psiDirectory.equals(parentDirectory) && !children.contains(todoDirNode)) {
                    children.add(todoDirNode);
                    break;
                }
                _dir = parentDirectory;
            }
        }
    } else {
        // flatten packages
        final PsiDirectory parentDirectory = psiDirectory.getParentDirectory();
        if (parentDirectory == null || !skipDirectory(parentDirectory) || !ProjectRootManager.getInstance(getProject()).getFileIndex().isInContent(parentDirectory.getVirtualFile())) {
            final Iterator<PsiFile> iterator = builder.getFiles(psiDirectory);
            while (iterator.hasNext()) {
                final PsiFile psiFile = iterator.next();
                // Add files
                TodoFileNode todoFileNode = new TodoFileNode(getProject(), psiFile, builder, false);
                if (psiDirectory.equals(psiFile.getContainingDirectory()) && !children.contains(todoFileNode)) {
                    children.add(todoFileNode);
                    continue;
                }
                // Add directories
                final PsiDirectory _dir = psiFile.getContainingDirectory();
                if (skipDirectory(_dir)) {
                    continue;
                }
                TodoDirNode todoDirNode = new TodoDirNode(getProject(), _dir, builder);
                if (PsiTreeUtil.isAncestor(psiDirectory, _dir, true) && !children.contains(todoDirNode) && !builder.isDirectoryEmpty(_dir)) {
                    children.add(todoDirNode);
                }
            }
        } else {
            final Iterator<PsiFile> iterator = builder.getFiles(psiDirectory);
            while (iterator.hasNext()) {
                final PsiFile psiFile = iterator.next();
                final PsiDirectory containingDirectory = psiFile.getContainingDirectory();
                TodoFileNode todoFileNode = new TodoFileNode(getProject(), psiFile, builder, false);
                if (psiDirectory.equals(containingDirectory) && !children.contains(todoFileNode)) {
                    children.add(todoFileNode);
                }
            }
        }
    }
    Collections.sort(children, TodoFileDirAndModuleComparator.INSTANCE);
    return children;
}
Also used : PsiDirectory(com.intellij.psi.PsiDirectory) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) PsiFile(com.intellij.psi.PsiFile)

Example 97 with AbstractTreeNode

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

the class CachingChildrenTreeNode method groupElements.

private void groupElements(Grouper grouper) {
    ArrayList<AbstractTreeNode<TreeElement>> ungrouped = new ArrayList<>();
    Collection<AbstractTreeNode> children = getChildren();
    for (final AbstractTreeNode child : children) {
        CachingChildrenTreeNode<TreeElement> node = (CachingChildrenTreeNode<TreeElement>) child;
        if (node instanceof TreeElementWrapper) {
            ungrouped.add(node);
        }
    }
    if (!ungrouped.isEmpty()) {
        processUngrouped(ungrouped, grouper);
    }
    Collection<AbstractTreeNode> result = new LinkedHashSet<>();
    for (AbstractTreeNode child : children) {
        AbstractTreeNode parent = child.getParent();
        if (parent != this) {
            if (!result.contains(parent))
                result.add(parent);
        } else {
            result.add(child);
        }
    }
    setChildren(result);
}
Also used : AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode)

Example 98 with AbstractTreeNode

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

the class CachingChildrenTreeNode method processUngrouped.

private void processUngrouped(@NotNull List<AbstractTreeNode<TreeElement>> ungrouped, @NotNull Grouper grouper) {
    Map<TreeElement, AbstractTreeNode> ungroupedObjects = collectValues(ungrouped);
    Collection<Group> groups = grouper.group(this, ungroupedObjects.keySet());
    Map<Group, GroupWrapper> groupNodes = createGroupNodes(groups);
    for (Group group : groups) {
        if (group == null) {
            LOG.error(grouper + " returned null group: " + groups);
        }
        GroupWrapper groupWrapper = groupNodes.get(group);
        Collection<TreeElement> children = group.getChildren();
        for (TreeElement node : children) {
            if (node == null) {
                LOG.error(group + " returned null child: " + children);
            }
            CachingChildrenTreeNode child = createChildNode(node);
            groupWrapper.addSubElement(child);
            AbstractTreeNode abstractTreeNode = ungroupedObjects.get(node);
            abstractTreeNode.setParent(groupWrapper);
        }
    }
}
Also used : AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode)

Example 99 with AbstractTreeNode

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

the class CachingChildrenTreeNode method setChildren.

protected void setChildren(Collection<AbstractTreeNode> children) {
    clearChildren();
    for (AbstractTreeNode node : children) {
        myChildren.add((CachingChildrenTreeNode) node);
        node.setParent(this);
    }
}
Also used : AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode)

Example 100 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)

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