Search in sources :

Example 51 with AbstractTreeNode

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

the class ProjectViewModuleNode method getChildren.

@Override
@NotNull
public Collection<AbstractTreeNode> getChildren() {
    Module module = getValue();
    if (module == null || module.isDisposed()) {
        // module has been disposed
        return Collections.emptyList();
    }
    final List<VirtualFile> contentRoots = ProjectViewDirectoryHelper.getInstance(myProject).getTopLevelModuleRoots(module, getSettings());
    final List<AbstractTreeNode> children = new ArrayList<>(contentRoots.size());
    final PsiManager psiManager = PsiManager.getInstance(module.getProject());
    for (final VirtualFile contentRoot : contentRoots) {
        if (contentRoot.isDirectory()) {
            PsiDirectory directory = psiManager.findDirectory(contentRoot);
            if (directory != null) {
                children.add(new PsiDirectoryNode(getProject(), directory, getSettings()));
            }
        } else {
            PsiFile file = psiManager.findFile(contentRoot);
            if (file != null) {
                children.add(new PsiFileNode(getProject(), file, getSettings()));
            }
        }
    }
    return children;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiDirectory(com.intellij.psi.PsiDirectory) ArrayList(java.util.ArrayList) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) PsiManager(com.intellij.psi.PsiManager) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Example 52 with AbstractTreeNode

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

the class GroupByTypeComparator method compare.

@Override
public int compare(NodeDescriptor descriptor1, NodeDescriptor descriptor2) {
    if (!isSortByType() && descriptor1 instanceof ProjectViewNode && ((ProjectViewNode) descriptor1).isSortByFirstChild()) {
        final Collection<AbstractTreeNode> children = ((ProjectViewNode) descriptor1).getChildren();
        if (!children.isEmpty()) {
            descriptor1 = children.iterator().next();
            descriptor1.update();
        }
    }
    if (!isSortByType() && descriptor2 instanceof ProjectViewNode && ((ProjectViewNode) descriptor2).isSortByFirstChild()) {
        final Collection<AbstractTreeNode> children = ((ProjectViewNode) descriptor2).getChildren();
        if (!children.isEmpty()) {
            descriptor2 = children.iterator().next();
            descriptor2.update();
        }
    }
    if (descriptor1 instanceof ProjectViewNode && descriptor2 instanceof ProjectViewNode) {
        final Project project = descriptor1.getProject();
        final ProjectView projectView = ProjectView.getInstance(project);
        ProjectViewNode node1 = (ProjectViewNode) descriptor1;
        ProjectViewNode node2 = (ProjectViewNode) descriptor2;
        if (isManualOrder()) {
            final Comparable key1 = node1.getManualOrderKey();
            final Comparable key2 = node2.getManualOrderKey();
            int result = compare(key1, key2);
            if (result != 0)
                return result;
        }
        boolean isFoldersOnTop = !(projectView instanceof ProjectViewImpl && !((ProjectViewImpl) projectView).isFoldersAlwaysOnTop());
        if (isFoldersOnTop) {
            int typeWeight1 = node1.getTypeSortWeight(isSortByType());
            int typeWeight2 = node2.getTypeSortWeight(isSortByType());
            if (typeWeight1 != 0 && typeWeight2 == 0) {
                return -1;
            }
            if (typeWeight1 == 0 && typeWeight2 != 0) {
                return 1;
            }
            if (typeWeight1 != 0 && typeWeight2 != typeWeight1) {
                return typeWeight1 - typeWeight2;
            }
        }
        if (isSortByType()) {
            final Comparable typeSortKey1 = node1.getTypeSortKey();
            final Comparable typeSortKey2 = node2.getTypeSortKey();
            int result = compare(typeSortKey1, typeSortKey2);
            if (result != 0)
                return result;
        } else {
            final Comparable typeSortKey1 = node1.getSortKey();
            final Comparable typeSortKey2 = node2.getSortKey();
            if (typeSortKey1 != null && typeSortKey2 != null) {
                int result = compare(typeSortKey1, typeSortKey2);
                if (result != 0)
                    return result;
            }
        }
        if (isAbbreviateQualifiedNames()) {
            String key1 = node1.getQualifiedNameSortKey();
            String key2 = node2.getQualifiedNameSortKey();
            if (key1 != null && key2 != null) {
                return naturalCompare(key1, key2);
            }
        }
    }
    if (descriptor1 == null)
        return -1;
    if (descriptor2 == null)
        return 1;
    return AlphaComparator.INSTANCE.compare(descriptor1, descriptor2);
}
Also used : Project(com.intellij.openapi.project.Project) ProjectViewNode(com.intellij.ide.projectView.ProjectViewNode) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) ProjectView(com.intellij.ide.projectView.ProjectView)

Example 53 with AbstractTreeNode

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

the class AbstractProjectNode method modulesAndGroups.

protected Collection<AbstractTreeNode> modulesAndGroups(Module[] modules) {
    Map<String, List<Module>> groups = new THashMap<>();
    List<Module> nonGroupedModules = new ArrayList<>(Arrays.asList(modules));
    for (final Module module : modules) {
        final String[] path = ModuleManager.getInstance(getProject()).getModuleGroupPath(module);
        if (path != null) {
            final String topLevelGroupName = path[0];
            groups.computeIfAbsent(topLevelGroupName, k -> new ArrayList<>()).add(module);
            nonGroupedModules.remove(module);
        }
    }
    List<AbstractTreeNode> result = new ArrayList<>();
    try {
        for (String groupPath : groups.keySet()) {
            result.add(createModuleGroupNode(new ModuleGroup(Collections.singletonList(groupPath))));
        }
        for (Module module : nonGroupedModules) {
            result.add(createModuleGroup(module));
        }
    } catch (Exception e) {
        LOG.error(e);
        return new ArrayList<>();
    }
    return result;
}
Also used : PlatformIcons(com.intellij.util.PlatformIcons) java.util(java.util) ProjectViewNode(com.intellij.ide.projectView.ProjectViewNode) ModuleGroup(com.intellij.ide.projectView.impl.ModuleGroup) ModuleManager(com.intellij.openapi.module.ModuleManager) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) THashMap(gnu.trove.THashMap) InvocationTargetException(java.lang.reflect.InvocationTargetException) PresentationData(com.intellij.ide.projectView.PresentationData) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) Project(com.intellij.openapi.project.Project) VfsUtil(com.intellij.openapi.vfs.VfsUtil) ViewSettings(com.intellij.ide.projectView.ViewSettings) Module(com.intellij.openapi.module.Module) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) NotNull(org.jetbrains.annotations.NotNull) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) ModuleGroup(com.intellij.ide.projectView.impl.ModuleGroup) InvocationTargetException(java.lang.reflect.InvocationTargetException) THashMap(gnu.trove.THashMap) Module(com.intellij.openapi.module.Module)

Example 54 with AbstractTreeNode

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

the class AbstractPsiBasedNode method getChildren.

@Override
@NotNull
public final Collection<AbstractTreeNode> getChildren() {
    final PsiElement psiElement = extractPsiFromValue();
    if (psiElement == null) {
        return new ArrayList<>();
    }
    final boolean valid = psiElement.isValid();
    if (!LOG.assertTrue(valid)) {
        return Collections.emptyList();
    }
    final Collection<AbstractTreeNode> children = getChildrenImpl();
    return children != null ? children : Collections.<AbstractTreeNode>emptyList();
}
Also used : ArrayList(java.util.ArrayList) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 55 with AbstractTreeNode

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

the class ExternalLibrariesNode method getChildren.

@NotNull
@Override
public Collection<? extends AbstractTreeNode> getChildren() {
    Project project = Objects.requireNonNull(getProject());
    List<AbstractTreeNode> children = new ArrayList<>();
    ProjectFileIndex fileIndex = ProjectFileIndex.getInstance(project);
    Module[] modules = ModuleManager.getInstance(project).getModules();
    Set<Library> processedLibraries = new THashSet<>();
    Set<Sdk> processedSdk = new THashSet<>();
    for (Module module : modules) {
        final ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
        final OrderEntry[] orderEntries = moduleRootManager.getOrderEntries();
        for (final OrderEntry orderEntry : orderEntries) {
            if (orderEntry instanceof LibraryOrderEntry) {
                final LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry) orderEntry;
                final Library library = libraryOrderEntry.getLibrary();
                if (library == null)
                    continue;
                if (processedLibraries.contains(library))
                    continue;
                processedLibraries.add(library);
                if (!hasExternalEntries(fileIndex, libraryOrderEntry))
                    continue;
                final String libraryName = library.getName();
                if (libraryName == null || libraryName.length() == 0) {
                    addLibraryChildren(libraryOrderEntry, children, project, this);
                } else {
                    children.add(new NamedLibraryElementNode(project, new NamedLibraryElement(null, libraryOrderEntry), getSettings()));
                }
            } else if (orderEntry instanceof JdkOrderEntry) {
                final JdkOrderEntry jdkOrderEntry = (JdkOrderEntry) orderEntry;
                final Sdk jdk = jdkOrderEntry.getJdk();
                if (jdk != null) {
                    if (processedSdk.contains(jdk))
                        continue;
                    processedSdk.add(jdk);
                    children.add(new NamedLibraryElementNode(project, new NamedLibraryElement(null, jdkOrderEntry), getSettings()));
                }
            }
        }
    }
    for (AdditionalLibraryRootsProvider provider : AdditionalLibraryRootsProvider.EP_NAME.getExtensions()) {
        Collection<SyntheticLibrary> libraries = provider.getAdditionalProjectLibraries(project);
        for (SyntheticLibrary library : libraries) {
            //noinspection InstanceofIncompatibleInterface
            if (library instanceof ItemPresentation) {
                children.add(new SyntheticLibraryElementNode(project, library, getSettings()));
            }
        }
    }
    return children;
}
Also used : ItemPresentation(com.intellij.navigation.ItemPresentation) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) Sdk(com.intellij.openapi.projectRoots.Sdk) THashSet(gnu.trove.THashSet) Project(com.intellij.openapi.project.Project) Library(com.intellij.openapi.roots.libraries.Library) Module(com.intellij.openapi.module.Module) 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