Search in sources :

Example 1 with Library

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

the class MavenRootModelAdapter method addSystemDependency.

public void addSystemDependency(MavenArtifact artifact, DependencyScope scope) {
    assert MavenConstants.SCOPE_SYSTEM.equals(artifact.getScope());
    String libraryName = artifact.getLibraryName();
    Library library = myRootModel.getModuleLibraryTable().getLibraryByName(libraryName);
    if (library == null) {
        library = myRootModel.getModuleLibraryTable().createLibrary(libraryName);
    }
    LibraryOrderEntry orderEntry = myRootModel.findLibraryOrderEntry(library);
    assert orderEntry != null;
    orderEntry.setScope(scope);
    Library.ModifiableModel modifiableModel = library.getModifiableModel();
    updateUrl(modifiableModel, OrderRootType.CLASSES, artifact, null, null, true);
    modifiableModel.commit();
    if (myOrderEntriesBeforeJdk.contains(libraryName)) {
        moveLastOrderEntryBeforeJdk();
    }
}
Also used : Library(com.intellij.openapi.roots.libraries.Library)

Example 2 with Library

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

the class RepositoryLibrarySupport method addSupport.

public void addSupport(@NotNull Module module, @NotNull final ModifiableRootModel rootModel, @NotNull ModifiableModelsProvider modifiableModelsProvider) {
    LibraryTable.ModifiableModel modifiableModel = modifiableModelsProvider.getLibraryTableModifiableModel(module.getProject());
    Library library = Iterables.find(Arrays.asList(modifiableModel.getLibraries()), new Predicate<Library>() {

        @Override
        public boolean apply(@Nullable Library library) {
            return isLibraryEqualsToSelected(library);
        }
    }, null);
    if (library == null) {
        library = createNewLibrary(module, modifiableModel);
    } else {
        modifiableModelsProvider.disposeLibraryTableModifiableModel(modifiableModel);
    }
    final DependencyScope dependencyScope = LibraryDependencyScopeSuggester.getDefaultScope(library);
    final ModifiableRootModel moduleModifiableModel = modifiableModelsProvider.getModuleModifiableModel(module);
    LibraryOrderEntry foundEntry = (LibraryOrderEntry) Iterables.find(Arrays.asList(moduleModifiableModel.getOrderEntries()), new Predicate<OrderEntry>() {

        @Override
        public boolean apply(@Nullable OrderEntry entry) {
            return entry instanceof LibraryOrderEntry && ((LibraryOrderEntry) entry).getScope() == dependencyScope && isLibraryEqualsToSelected(((LibraryOrderEntry) entry).getLibrary());
        }
    }, null);
    modifiableModelsProvider.disposeModuleModifiableModel(moduleModifiableModel);
    if (foundEntry == null) {
        rootModel.addLibraryEntry(library).setScope(dependencyScope);
    }
}
Also used : LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) Library(com.intellij.openapi.roots.libraries.Library) Nullable(org.jetbrains.annotations.Nullable) Predicate(com.google.common.base.Predicate)

Example 3 with Library

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

the class RepositoryLibrarySynchronizer method collectLibraries.

private static Collection<Library> collectLibraries(@NotNull final Project project, @NotNull final Predicate<Library> predicate) {
    final HashSet<Library> result = new HashSet<>();
    ApplicationManager.getApplication().runReadAction(() -> {
        for (final Module module : ModuleManager.getInstance(project).getModules()) {
            OrderEnumerator.orderEntries(module).withoutSdk().forEachLibrary(library -> {
                if (predicate.apply(library)) {
                    result.add(library);
                }
                return true;
            });
        }
        for (Library library : ProjectLibraryTable.getInstance(project).getLibraries()) {
            if (predicate.apply(library)) {
                result.add(library);
            }
        }
    });
    return result;
}
Also used : Library(com.intellij.openapi.roots.libraries.Library) Module(com.intellij.openapi.module.Module) HashSet(com.intellij.util.containers.HashSet)

Example 4 with Library

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

the class RepositoryLibrarySynchronizer method runActivity.

@Override
public void runActivity(@NotNull final Project project) {
    StartupManager.getInstance(project).registerPostStartupActivity(new DumbAwareRunnable() {

        @Override
        public void run() {
            ApplicationManager.getApplication().invokeLater(new DumbAwareRunnable() {

                @Override
                public void run() {
                    final Collection<Library> libraries = collectLibraries(project, new Predicate<Library>() {

                        @Override
                        public boolean apply(Library library) {
                            if (!(library instanceof LibraryEx)) {
                                return false;
                            }
                            LibraryEx libraryEx = (LibraryEx) library;
                            return libraryEx.getKind() == RepositoryLibraryType.REPOSITORY_LIBRARY_KIND && libraryEx.getProperties() instanceof RepositoryLibraryProperties && isLibraryNeedToBeReloaded(libraryEx, (RepositoryLibraryProperties) libraryEx.getProperties());
                        }
                    });
                    for (Library library : libraries) {
                        final LibraryEx libraryEx = (LibraryEx) library;
                        RepositoryUtils.reloadDependencies(project, libraryEx);
                    }
                }
            }, project.getDisposed());
        }
    });
}
Also used : LibraryEx(com.intellij.openapi.roots.impl.libraries.LibraryEx) Library(com.intellij.openapi.roots.libraries.Library) DumbAwareRunnable(com.intellij.openapi.project.DumbAwareRunnable)

Example 5 with Library

use of com.intellij.openapi.roots.libraries.Library 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)

Aggregations

Library (com.intellij.openapi.roots.libraries.Library)266 LibraryTable (com.intellij.openapi.roots.libraries.LibraryTable)69 VirtualFile (com.intellij.openapi.vfs.VirtualFile)65 Module (com.intellij.openapi.module.Module)43 NotNull (org.jetbrains.annotations.NotNull)32 File (java.io.File)31 LibraryOrderEntry (com.intellij.openapi.roots.LibraryOrderEntry)28 LibraryEx (com.intellij.openapi.roots.impl.libraries.LibraryEx)27 Project (com.intellij.openapi.project.Project)25 ProjectLibraryTable (com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable)25 Nullable (org.jetbrains.annotations.Nullable)25 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)19 ArrayList (java.util.ArrayList)15 Sdk (com.intellij.openapi.projectRoots.Sdk)13 OrderEntry (com.intellij.openapi.roots.OrderEntry)12 THashSet (gnu.trove.THashSet)9 Element (org.jdom.Element)9 OrderRoot (com.intellij.openapi.roots.libraries.ui.OrderRoot)8 Result (com.intellij.openapi.application.Result)7 Logger (com.intellij.openapi.diagnostic.Logger)7