update copyrights
[fedora-idea.git] / platform / lang-impl / src / com / intellij / ide / projectView / impl / nodes / ExternalLibrariesNode.java
blobb39bdb70352f1c99b46041d25527254b345e7546
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @author max
20 package com.intellij.ide.projectView.impl.nodes;
22 import com.intellij.ide.IdeBundle;
23 import com.intellij.ide.projectView.PresentationData;
24 import com.intellij.ide.projectView.ProjectViewNode;
25 import com.intellij.ide.projectView.ViewSettings;
26 import com.intellij.ide.util.treeView.AbstractTreeNode;
27 import com.intellij.openapi.module.Module;
28 import com.intellij.openapi.module.ModuleManager;
29 import com.intellij.openapi.project.Project;
30 import com.intellij.openapi.projectRoots.Sdk;
31 import com.intellij.openapi.roots.*;
32 import com.intellij.openapi.roots.libraries.Library;
33 import com.intellij.openapi.vfs.VirtualFile;
34 import com.intellij.psi.PsiDirectory;
35 import com.intellij.psi.PsiManager;
36 import com.intellij.util.Icons;
37 import com.intellij.util.PathUtil;
38 import gnu.trove.THashSet;
39 import org.jetbrains.annotations.NotNull;
41 import java.util.ArrayList;
42 import java.util.Collection;
43 import java.util.List;
44 import java.util.Set;
46 public class ExternalLibrariesNode extends ProjectViewNode<String> {
47 public ExternalLibrariesNode(Project project, ViewSettings viewSettings) {
48 super(project, "External Libraries", viewSettings);
51 @Override
52 public boolean contains(@NotNull VirtualFile file) {
53 return someChildContainsFile(file);
56 @NotNull
57 @Override
58 public Collection<? extends AbstractTreeNode> getChildren() {
59 final List<AbstractTreeNode> children = new ArrayList<AbstractTreeNode>();
60 ProjectFileIndex fileIndex = ProjectRootManager.getInstance(getProject()).getFileIndex();
61 Module[] modules = ModuleManager.getInstance(getProject()).getModules();
62 Set<Library> processedLibraries = new THashSet<Library>();
63 Set<Sdk> processedSdk = new THashSet<Sdk>();
65 for (Module module : modules) {
66 final ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
67 final OrderEntry[] orderEntries = moduleRootManager.getOrderEntries();
68 for (final OrderEntry orderEntry : orderEntries) {
69 if (orderEntry instanceof LibraryOrderEntry) {
70 final LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry)orderEntry;
71 final Library library = libraryOrderEntry.getLibrary();
72 if (library == null) continue;
73 if (processedLibraries.contains(library)) continue;
74 processedLibraries.add(library);
76 if (!hasExternalEntries(fileIndex, libraryOrderEntry)) continue;
78 final String libraryName = library.getName();
79 if (libraryName == null || libraryName.length() == 0) {
80 addLibraryChildren(orderEntry, children, getProject(), this);
82 else {
83 children.add(new NamedLibraryElementNode(getProject(), new NamedLibraryElement(null, orderEntry), getSettings()));
86 else if (orderEntry instanceof JdkOrderEntry) {
87 final Sdk jdk = ((JdkOrderEntry)orderEntry).getJdk();
88 if (jdk != null) {
89 if (processedSdk.contains(jdk)) continue;
90 processedSdk.add(jdk);
91 children.add(new NamedLibraryElementNode(getProject(), new NamedLibraryElement(null, orderEntry), getSettings()));
96 return children;
99 public static void addLibraryChildren(final OrderEntry entry, final List<AbstractTreeNode> children, Project project, ProjectViewNode node) {
100 final PsiManager psiManager = PsiManager.getInstance(project);
101 final VirtualFile[] files = entry.getFiles(OrderRootType.CLASSES);
102 for (final VirtualFile file : files) {
103 final PsiDirectory psiDir = psiManager.findDirectory(file);
104 if (psiDir == null) {
105 continue;
107 children.add(new PsiDirectoryNode(project, psiDir, node.getSettings()));
111 private static boolean hasExternalEntries(ProjectFileIndex index, OrderEntry orderEntry) {
112 VirtualFile[] files = orderEntry.getFiles(OrderRootType.CLASSES);
113 for (VirtualFile file : files) {
114 if (!index.isInContent(PathUtil.getLocalFile(file))) return true;
117 return false;
120 @Override
121 protected void update(PresentationData presentation) {
122 presentation.setPresentableText(IdeBundle.message("node.projectview.external.libraries"));
123 presentation.setIcons(Icons.LIBRARY_ICON);