Search in sources :

Example 1 with LibraryOrderEntry

use of com.intellij.openapi.roots.LibraryOrderEntry in project intellij-community by JetBrains.

the class MavenAttachSourcesProvider method getActions.

@Override
@NotNull
public Collection<AttachSourcesAction> getActions(final List<LibraryOrderEntry> orderEntries, final PsiFile psiFile) {
    Collection<MavenProject> projects = getMavenProjects(psiFile);
    if (projects.isEmpty())
        return Collections.emptyList();
    if (findArtifacts(projects, orderEntries).isEmpty())
        return Collections.emptyList();
    return Collections.singleton(new AttachSourcesAction() {

        @Override
        public String getName() {
            return ProjectBundle.message("maven.action.download.sources");
        }

        @Override
        public String getBusyText() {
            return ProjectBundle.message("maven.action.download.sources.busy.text");
        }

        @Override
        public ActionCallback perform(List<LibraryOrderEntry> orderEntries) {
            // may have been changed by this time...
            Collection<MavenProject> mavenProjects = getMavenProjects(psiFile);
            if (mavenProjects.isEmpty()) {
                return ActionCallback.REJECTED;
            }
            MavenProjectsManager manager = MavenProjectsManager.getInstance(psiFile.getProject());
            Collection<MavenArtifact> artifacts = findArtifacts(mavenProjects, orderEntries);
            if (artifacts.isEmpty())
                return ActionCallback.REJECTED;
            final AsyncResult<MavenArtifactDownloader.DownloadResult> result = new AsyncResult<>();
            manager.scheduleArtifactsDownloading(mavenProjects, artifacts, true, false, result);
            final ActionCallback resultWrapper = new ActionCallback();
            result.doWhenDone(new Consumer<MavenArtifactDownloader.DownloadResult>() {

                @Override
                public void consume(MavenArtifactDownloader.DownloadResult downloadResult) {
                    if (!downloadResult.unresolvedSources.isEmpty()) {
                        final StringBuilder message = new StringBuilder();
                        message.append("<html>Sources not found for:");
                        int count = 0;
                        for (MavenId each : downloadResult.unresolvedSources) {
                            if (count++ > 5) {
                                message.append("<br>and more...");
                                break;
                            }
                            message.append("<br>").append(each.getDisplayString());
                        }
                        message.append("</html>");
                        Notifications.Bus.notify(new Notification(MavenUtil.MAVEN_NOTIFICATION_GROUP, "Cannot download sources", message.toString(), NotificationType.WARNING), psiFile.getProject());
                    }
                    if (downloadResult.resolvedSources.isEmpty()) {
                        resultWrapper.setRejected();
                    } else {
                        resultWrapper.setDone();
                    }
                }
            });
            return resultWrapper;
        }
    });
}
Also used : MavenProjectsManager(org.jetbrains.idea.maven.project.MavenProjectsManager) ActionCallback(com.intellij.openapi.util.ActionCallback) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry) Notification(com.intellij.notification.Notification) MavenArtifactDownloader(org.jetbrains.idea.maven.project.MavenArtifactDownloader) MavenId(org.jetbrains.idea.maven.model.MavenId) MavenProject(org.jetbrains.idea.maven.project.MavenProject) Consumer(com.intellij.util.Consumer) Collection(java.util.Collection) AsyncResult(com.intellij.openapi.util.AsyncResult) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with LibraryOrderEntry

use of com.intellij.openapi.roots.LibraryOrderEntry in project intellij-community by JetBrains.

the class GroovyFacetUtil method tryToSetUpGroovyFacetOnTheFly.

public static boolean tryToSetUpGroovyFacetOnTheFly(final Module module) {
    final Project project = module.getProject();
    GroovyConfigUtils utils = GroovyConfigUtils.getInstance();
    final Library[] libraries = utils.getAllSDKLibraries(project);
    if (libraries.length > 0) {
        final Library library = libraries[0];
        int result = Messages.showOkCancelDialog(GroovyBundle.message("groovy.like.library.found.text", module.getName(), library.getName(), utils.getSDKLibVersion(library)), GroovyBundle.message("groovy.like.library.found"), JetgroovyIcons.Groovy.Groovy_32x32);
        if (result == Messages.OK) {
            WriteAction.run(() -> {
                ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel();
                LibraryOrderEntry entry = model.addLibraryEntry(libraries[0]);
                LibrariesUtil.placeEntryToCorrectPlace(model, entry);
                model.commit();
            });
            return true;
        }
    }
    return false;
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) Project(com.intellij.openapi.project.Project) Library(com.intellij.openapi.roots.libraries.Library) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry)

Example 3 with LibraryOrderEntry

use of com.intellij.openapi.roots.LibraryOrderEntry in project intellij-community by JetBrains.

the class ModuleLibraryTable method createLibrary.

@Override
public Library createLibrary(String name, @Nullable PersistentLibraryKind kind) {
    LibraryOrderEntry orderEntry = new ModuleLibraryOrderEntryImpl(name, kind, myRootModel, myProjectRootManager);
    myRootModel.addOrderEntry(orderEntry);
    return orderEntry.getLibrary();
}
Also used : LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry)

Example 4 with LibraryOrderEntry

use of com.intellij.openapi.roots.LibraryOrderEntry in project intellij-community by JetBrains.

the class LibraryCompositionSettings method addLibraries.

@Nullable
public Library addLibraries(@NotNull final ModifiableRootModel rootModel, @NotNull final List<Library> addedLibraries, @Nullable final LibrariesContainer librariesContainer) {
    Library newLibrary = createLibrary(rootModel, librariesContainer);
    if (newLibrary != null) {
        addedLibraries.add(newLibrary);
        DependencyScope scope = LibraryDependencyScopeSuggester.getDefaultScope(newLibrary);
        if (getLibraryLevel() != LibrariesContainer.LibraryLevel.MODULE) {
            rootModel.addLibraryEntry(newLibrary).setScope(scope);
        } else {
            LibraryOrderEntry orderEntry = rootModel.findLibraryOrderEntry(newLibrary);
            assert orderEntry != null;
            orderEntry.setScope(scope);
        }
    }
    if (mySelectedLibrary != null) {
        addedLibraries.add(mySelectedLibrary);
        rootModel.addLibraryEntry(mySelectedLibrary).setScope(LibraryDependencyScopeSuggester.getDefaultScope(mySelectedLibrary));
    }
    if (myLibraryProvider != null) {
        Library library = myLibraryProvider.createLibrary(myLibraryDescription.getSuitableLibraryKinds());
        addedLibraries.add(library);
        rootModel.addLibraryEntry(library).setScope(LibraryDependencyScopeSuggester.getDefaultScope(library));
    }
    return newLibrary;
}
Also used : DependencyScope(com.intellij.openapi.roots.DependencyScope) Library(com.intellij.openapi.roots.libraries.Library) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with LibraryOrderEntry

use of com.intellij.openapi.roots.LibraryOrderEntry in project intellij-community by JetBrains.

the class InternetAttachSourceProvider method getActions.

@NotNull
@Override
public Collection<AttachSourcesAction> getActions(List<LibraryOrderEntry> orderEntries, @Nullable PsiFile psiFile) {
    final VirtualFile jar = getJarByPsiFile(psiFile);
    if (jar == null)
        return Collections.emptyList();
    final String jarName = jar.getNameWithoutExtension();
    int index = jarName.lastIndexOf('-');
    if (index == -1)
        return Collections.emptyList();
    final String version = jarName.substring(index + 1);
    final String artifactId = jarName.substring(0, index);
    if (!ARTIFACT_IDENTIFIER.matcher(version).matches() || !ARTIFACT_IDENTIFIER.matcher(artifactId).matches()) {
        return Collections.emptyList();
    }
    final Set<Library> libraries = new HashSet<>();
    for (LibraryOrderEntry orderEntry : orderEntries) {
        ContainerUtil.addIfNotNull(libraries, orderEntry.getLibrary());
    }
    if (libraries.isEmpty())
        return Collections.emptyList();
    final String sourceFileName = jarName + "-sources.jar";
    for (Library library : libraries) {
        for (VirtualFile file : library.getFiles(OrderRootType.SOURCES)) {
            if (file.getPath().contains(sourceFileName)) {
                if (isRootInExistingFile(file)) {
                    // Sources already attached, but source-jar doesn't contain current class.
                    return Collections.emptyList();
                }
            }
        }
    }
    final File libSourceDir = getLibrarySourceDir();
    final File sourceFile = new File(libSourceDir, sourceFileName);
    if (sourceFile.exists()) {
        return Collections.singleton(new LightAttachSourcesAction() {

            @Override
            public String getName() {
                return "Attach downloaded source";
            }

            @Override
            public String getBusyText() {
                return getName();
            }

            @Override
            public ActionCallback perform(List<LibraryOrderEntry> orderEntriesContainingFile) {
                attachSourceJar(sourceFile, libraries);
                return ActionCallback.DONE;
            }
        });
    }
    return Collections.singleton(new LightAttachSourcesAction() {

        @Override
        public String getName() {
            return "Download...";
        }

        @Override
        public String getBusyText() {
            return "Searching...";
        }

        @Override
        public ActionCallback perform(List<LibraryOrderEntry> orderEntriesContainingFile) {
            final Task task = new Task.Modal(psiFile.getProject(), "Searching source...", true) {

                @Override
                public void run(@NotNull final ProgressIndicator indicator) {
                    String artifactUrl = null;
                    SourceSearcher[] searchers = { new MavenCentralSourceSearcher(), new SonatypeSourceSearcher() };
                    for (SourceSearcher searcher : searchers) {
                        try {
                            artifactUrl = searcher.findSourceJar(indicator, artifactId, version, jar);
                        } catch (SourceSearchException e) {
                            LOG.warn(e);
                            showMessage("Downloading failed", e.getMessage(), NotificationType.ERROR);
                            continue;
                        }
                        if (artifactUrl != null)
                            break;
                    }
                    if (artifactUrl == null) {
                        showMessage("Sources not found", "Sources for '" + jarName + ".jar' not found", NotificationType.WARNING);
                        return;
                    }
                    if (!(libSourceDir.isDirectory() || libSourceDir.mkdirs())) {
                        showMessage("Downloading failed", "Failed to create directory to store sources: " + libSourceDir, NotificationType.ERROR);
                        return;
                    }
                    try {
                        File tmpDownload = FileUtil.createTempFile(libSourceDir, "download.", ".tmp", false, false);
                        HttpRequests.request(artifactUrl).saveToFile(tmpDownload, indicator);
                        if (!sourceFile.exists() && !tmpDownload.renameTo(sourceFile)) {
                            LOG.warn("Failed to rename file " + tmpDownload + " to " + sourceFileName);
                        }
                    } catch (IOException e) {
                        LOG.warn(e);
                        showMessage("Downloading failed", "Connection problem. See log for more details.", NotificationType.ERROR);
                    }
                }

                @Override
                public void onSuccess() {
                    attachSourceJar(sourceFile, libraries);
                }

                private void showMessage(String title, String message, NotificationType notificationType) {
                    new Notification("Source searcher", title, message, notificationType).notify(getProject());
                }
            };
            task.queue();
            return ActionCallback.DONE;
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Task(com.intellij.openapi.progress.Task) ActionCallback(com.intellij.openapi.util.ActionCallback) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry) IOException(java.io.IOException) Notification(com.intellij.notification.Notification) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) NotificationType(com.intellij.notification.NotificationType) Library(com.intellij.openapi.roots.libraries.Library) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

LibraryOrderEntry (com.intellij.openapi.roots.LibraryOrderEntry)40 Library (com.intellij.openapi.roots.libraries.Library)27 OrderEntry (com.intellij.openapi.roots.OrderEntry)17 Module (com.intellij.openapi.module.Module)8 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 LibraryEx (com.intellij.openapi.roots.impl.libraries.LibraryEx)7 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)6 ModuleRootManager (com.intellij.openapi.roots.ModuleRootManager)5 Nullable (org.jetbrains.annotations.Nullable)5 Project (com.intellij.openapi.project.Project)4 File (java.io.File)4 NotNull (org.jetbrains.annotations.NotNull)4 LibraryTable (com.intellij.openapi.roots.libraries.LibraryTable)3 ActionCallback (com.intellij.openapi.util.ActionCallback)3 THashSet (gnu.trove.THashSet)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 Notification (com.intellij.notification.Notification)2 IdeModifiableModelsProvider (com.intellij.openapi.externalSystem.service.project.IdeModifiableModelsProvider)2 IdeModifiableModelsProviderImpl (com.intellij.openapi.externalSystem.service.project.IdeModifiableModelsProviderImpl)2