Search in sources :

Example 71 with AbstractTreeNode

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

the class AndroidProjectTreeBuilder method findNodeByElement.

/**
   * Returns the tree node corresponding to a model element.
   * e.g. from a PsiDirectory -> tree node corresponding to that PsiDirectory
   *
   * When {@link com.intellij.ide.util.treeView.AbstractTreeUi} creates a {@link javax.swing.tree.DefaultMutableTreeNode} for a given
   * {@link com.intellij.ide.util.treeView.AbstractTreeNode}, it maintains a mapping between. This mapping between the model element to
   * the tree node is necessary when locating items by their model element (PsiFile or PsiDirectory).
   *
   * In the Android view, we have virtual nodes that don't correspond to Psi Elements, or a single virtual node corresponding to
   * multiple files/directories. Since such mappings aren't saved by the tree UI, these are handled in this method.
   *
   * The way this works is that every time a virtual node is created, it calls back to {@link #createMapping()} to save that mapping
   * between the virtual file and the node that represents it. When we need to map a virtual file to its tree node, we look at the
   * saved mapping to see if that virtual file corresponds to any node. If so, we obtain the tree node corresponding to the node's parent,
   * then iterate through its children to locate the tree node corresponding to the element.
   */
@Nullable
@Override
protected Object findNodeByElement(@Nullable Object element) {
    if (element == null) {
        return null;
    }
    Object node = super.findNodeByElement(element);
    if (node != null) {
        return node;
    }
    VirtualFile virtualFile = null;
    if (element instanceof PsiDirectory) {
        virtualFile = ((PsiDirectory) element).getVirtualFile();
    } else if (element instanceof PsiFile) {
        virtualFile = ((PsiFile) element).getVirtualFile();
    }
    if (virtualFile == null) {
        return null;
    }
    AbstractTreeNode treeNode = getNodeForFile(virtualFile);
    if (treeNode == null) {
        return null;
    }
    // recurse and find the tree node corresponding to the parent
    Object parentNode = findNodeByElement(treeNode.getParent());
    if (!(parentNode instanceof DefaultMutableTreeNode)) {
        return null;
    }
    // examine all the children of the parent tree node and return the one that maps to this virtual file.
    Enumeration children = ((DefaultMutableTreeNode) parentNode).children();
    while (children.hasMoreElements()) {
        DefaultMutableTreeNode child = (DefaultMutableTreeNode) children.nextElement();
        if (child.getUserObject() instanceof DirectoryGroupNode) {
            for (PsiDirectory folder : ((DirectoryGroupNode) child.getUserObject()).getDirectories()) {
                if (folder.getVirtualFile().equals(virtualFile)) {
                    return child;
                }
            }
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DirectoryGroupNode(com.android.tools.idea.navigator.nodes.DirectoryGroupNode) Enumeration(java.util.Enumeration) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) PsiDirectory(com.intellij.psi.PsiDirectory) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) PsiFile(com.intellij.psi.PsiFile) Nullable(org.jetbrains.annotations.Nullable)

Example 72 with AbstractTreeNode

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

the class AndroidManifestsGroupNode method getChildren.

@NotNull
@Override
public Collection<? extends AbstractTreeNode> getChildren() {
    PsiManager psiManager = PsiManager.getInstance(myProject);
    List<AbstractTreeNode> children = Lists.newArrayList();
    for (VirtualFile manifest : mySources) {
        if (!manifest.isValid()) {
            continue;
        }
        PsiFile psiFile = psiManager.findFile(manifest);
        if (psiFile != null) {
            AndroidFacet facet = getValue();
            assert facet != null : "Android Facet for module cannot be null";
            children.add(new AndroidManifestFileNode(myProject, psiFile, getSettings(), facet));
        }
    }
    return children;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiManager(com.intellij.psi.PsiManager) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) PsiFile(com.intellij.psi.PsiFile) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) NotNull(org.jetbrains.annotations.NotNull)

Example 73 with AbstractTreeNode

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

the class AndroidTreeStructureProvider method modify.

@Override
@NotNull
public Collection<AbstractTreeNode> modify(@NotNull AbstractTreeNode parent, @NotNull Collection<AbstractTreeNode> children, ViewSettings settings) {
    Project project = parent.getProject();
    if (project != null && AndroidProjectInfo.getInstance(project).requiresAndroidModel()) {
        if (parent instanceof NamedLibraryElementNode) {
            NamedLibraryElement value = ((NamedLibraryElementNode) parent).getValue();
            LibraryOrSdkOrderEntry orderEntry = value.getOrderEntry();
            if (orderEntry instanceof JdkOrderEntry) {
                Sdk sdk = ((JdkOrderEntry) orderEntry).getJdk();
                if (sdk.getSdkType() instanceof JavaSdk) {
                    List<AbstractTreeNode> newChildren = Lists.newArrayList();
                    for (AbstractTreeNode child : children) {
                        if (isRtJar(child)) {
                            newChildren.add(child);
                        }
                    }
                    if (!newChildren.isEmpty()) {
                        myEventDispatcher.getMulticaster().nodeChanged(parent, newChildren);
                        return newChildren;
                    }
                }
            }
        } else if (isRtJar(parent)) {
            List<AbstractTreeNode> newChildren = Lists.newArrayList();
            for (AbstractTreeNode child : children) {
                if (child instanceof PsiDirectoryNode) {
                    VirtualFile file = ((PsiDirectoryNode) child).getVirtualFile();
                    if (file != null && ("java".equals(file.getName()) || "javax".equals(file.getName()))) {
                        newChildren.add(child);
                    }
                }
            }
            if (!newChildren.isEmpty()) {
                myEventDispatcher.getMulticaster().nodeChanged(parent, newChildren);
                return newChildren;
            }
        }
    }
    return children;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) NamedLibraryElement(com.intellij.ide.projectView.impl.nodes.NamedLibraryElement) JdkOrderEntry(com.intellij.openapi.roots.JdkOrderEntry) LibraryOrSdkOrderEntry(com.intellij.openapi.roots.LibraryOrSdkOrderEntry) JavaSdk(com.intellij.openapi.projectRoots.JavaSdk) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) List(java.util.List) PsiDirectoryNode(com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode) JavaSdk(com.intellij.openapi.projectRoots.JavaSdk) Sdk(com.intellij.openapi.projectRoots.Sdk) NamedLibraryElementNode(com.intellij.ide.projectView.impl.nodes.NamedLibraryElementNode) NotNull(org.jetbrains.annotations.NotNull)

Example 74 with AbstractTreeNode

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

the class SwfProjectViewStructureProvider method getChildrenForPackage.

static Collection<AbstractTreeNode> getChildrenForPackage(String aPackage, List<JSQualifiedNamedElement> elements, int from, int to, Project project, ViewSettings settings) {
    List<AbstractTreeNode> packages = new ArrayList<>();
    List<AbstractTreeNode> classes = new ArrayList<>();
    int subpackageStart = -1;
    String currentSubpackage = null;
    for (int i = from; i < to; i++) {
        JSQualifiedNamedElement element = elements.get(i);
        String qName = element.getQualifiedName();
        assert aPackage.isEmpty() || qName.startsWith(aPackage + ".") : qName + " does not start with " + aPackage;
        if (StringUtil.getPackageName(qName).equals(aPackage)) {
            classes.add(new SwfQualifiedNamedElementNode(project, element, settings));
        } else {
            final int endIndex = qName.indexOf('.', aPackage.length() + 1);
            if (endIndex <= 0) {
                final Attachment attachment = element.getParent() != null ? new Attachment("Parent element.txt", element.getParent().getText()) : new Attachment("Element text.txt", element.getText());
                LOG.error(LogMessageEx.createEvent("package=[" + aPackage + "], qName=[" + qName + "]", DebugUtil.currentStackTrace(), attachment));
                continue;
            }
            String subpackage = settings.isFlattenPackages() ? StringUtil.getPackageName(qName) : qName.substring(0, endIndex);
            if (currentSubpackage == null) {
                subpackageStart = i;
            } else if (!currentSubpackage.equals(subpackage)) {
                packages.add(createSubpackageNode(elements, project, settings, subpackageStart, i, currentSubpackage));
                subpackageStart = i;
            }
            currentSubpackage = subpackage;
        }
    }
    if (currentSubpackage != null) {
        packages.add(createSubpackageNode(elements, project, settings, subpackageStart, to, currentSubpackage));
    }
    return ContainerUtil.concat(packages, classes);
}
Also used : AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) JSQualifiedNamedElement(com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement) Attachment(com.intellij.openapi.diagnostic.Attachment)

Example 75 with AbstractTreeNode

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

the class ResourceDirectoryNode method collectChildren.

public void collectChildren() {
    for (AbstractTreeNode child : myBaseNode.getChildren()) {
        Object value = child.getValue();
        if (value instanceof PsiNamedElement) {
            String name = ((PsiNamedElement) value).getName();
            if (!myChildMap.containsKey(name)) {
                myChildMap.put(name, child);
                myChildren.add(child);
            }
        } else {
            myChildren.add(child);
        }
    }
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) 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