Search in sources :

Example 1 with OrderRootType

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

the class LibraryDataService method syncPaths.

private void syncPaths(@NotNull final LibraryData externalLibrary, @NotNull final Library ideLibrary, @NotNull final IdeModifiableModelsProvider modelsProvider) {
    if (externalLibrary.isUnresolved()) {
        return;
    }
    final Map<OrderRootType, Set<String>> toRemove = ContainerUtilRt.newHashMap();
    final Map<OrderRootType, Set<String>> toAdd = ContainerUtilRt.newHashMap();
    for (LibraryPathType pathType : LibraryPathType.values()) {
        OrderRootType ideType = myLibraryPathTypeMapper.map(pathType);
        HashSet<String> toAddPerType = ContainerUtilRt.newHashSet(externalLibrary.getPaths(pathType));
        toAdd.put(ideType, toAddPerType);
        HashSet<String> toRemovePerType = ContainerUtilRt.newHashSet();
        toRemove.put(ideType, toRemovePerType);
        for (VirtualFile ideFile : ideLibrary.getFiles(ideType)) {
            String idePath = ExternalSystemApiUtil.getLocalFileSystemPath(ideFile);
            if (!toAddPerType.remove(idePath)) {
                toRemovePerType.add(ideFile.getUrl());
            }
        }
    }
    if (toRemove.isEmpty() && toAdd.isEmpty()) {
        return;
    }
    final Library.ModifiableModel libraryModel = modelsProvider.getModifiableLibraryModel(ideLibrary);
    for (Map.Entry<OrderRootType, Set<String>> entry : toRemove.entrySet()) {
        for (String path : entry.getValue()) {
            libraryModel.removeRoot(path, entry.getKey());
        }
    }
    for (Map.Entry<OrderRootType, Set<String>> entry : toAdd.entrySet()) {
        Map<OrderRootType, Collection<File>> roots = ContainerUtilRt.newHashMap();
        roots.put(entry.getKey(), ContainerUtil.map(entry.getValue(), PATH_TO_FILE));
        registerPaths(externalLibrary.isUnresolved(), roots, libraryModel, externalLibrary.getInternalName());
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) OrderRootType(com.intellij.openapi.roots.OrderRootType) LibraryPathType(com.intellij.openapi.externalSystem.model.project.LibraryPathType) Library(com.intellij.openapi.roots.libraries.Library)

Example 2 with OrderRootType

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

the class LibraryDataService method importLibrary.

private void importLibrary(@NotNull final LibraryData toImport, @NotNull final IdeModifiableModelsProvider modelsProvider) {
    Map<OrderRootType, Collection<File>> libraryFiles = prepareLibraryFiles(toImport);
    final String libraryName = toImport.getInternalName();
    Library library = modelsProvider.getLibraryByName(libraryName);
    if (library != null) {
        syncPaths(toImport, library, modelsProvider);
        return;
    }
    library = modelsProvider.createLibrary(libraryName);
    final Library.ModifiableModel libraryModel = modelsProvider.getModifiableLibraryModel(library);
    registerPaths(toImport.isUnresolved(), libraryFiles, libraryModel, libraryName);
}
Also used : OrderRootType(com.intellij.openapi.roots.OrderRootType) Library(com.intellij.openapi.roots.libraries.Library)

Example 3 with OrderRootType

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

the class LibraryDataService method prepareLibraryFiles.

@NotNull
public Map<OrderRootType, Collection<File>> prepareLibraryFiles(@NotNull LibraryData data) {
    Map<OrderRootType, Collection<File>> result = ContainerUtilRt.newHashMap();
    for (LibraryPathType pathType : LibraryPathType.values()) {
        Set<String> paths = data.getPaths(pathType);
        if (paths.isEmpty()) {
            continue;
        }
        result.put(myLibraryPathTypeMapper.map(pathType), ContainerUtil.map(paths, PATH_TO_FILE));
    }
    return result;
}
Also used : OrderRootType(com.intellij.openapi.roots.OrderRootType) LibraryPathType(com.intellij.openapi.externalSystem.model.project.LibraryPathType) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with OrderRootType

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

the class LibraryEditingUtil method copyLibrary.

public static void copyLibrary(LibraryEx from, Map<String, String> rootMapping, LibraryEx.ModifiableModelEx target) {
    target.setProperties(from.getProperties());
    for (OrderRootType type : OrderRootType.getAllTypes()) {
        final String[] urls = from.getUrls(type);
        for (String url : urls) {
            final String protocol = VirtualFileManager.extractProtocol(url);
            if (protocol == null)
                continue;
            final String fullPath = VirtualFileManager.extractPath(url);
            final int sep = fullPath.indexOf(JarFileSystem.JAR_SEPARATOR);
            String localPath;
            String pathInJar;
            if (sep != -1) {
                localPath = fullPath.substring(0, sep);
                pathInJar = fullPath.substring(sep);
            } else {
                localPath = fullPath;
                pathInJar = "";
            }
            final String targetPath = rootMapping.get(localPath);
            String targetUrl = targetPath != null ? VirtualFileManager.constructUrl(protocol, targetPath + pathInJar) : url;
            if (from.isJarDirectory(url, type)) {
                target.addJarDirectory(targetUrl, false, type);
            } else {
                target.addRoot(targetUrl, type);
            }
        }
    }
}
Also used : OrderRootType(com.intellij.openapi.roots.OrderRootType)

Example 5 with OrderRootType

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

the class LibraryTreeStructure method getChildElements.

@Override
public Object[] getChildElements(Object element) {
    final LibraryEditor libraryEditor = myParentEditor.getLibraryEditor();
    if (element == myRootElementDescriptor) {
        ArrayList<LibraryTableTreeContentElement> elements = new ArrayList<>(3);
        for (OrderRootType type : myComponentDescriptor.getRootTypes()) {
            final String[] urls = libraryEditor.getUrls(type);
            if (urls.length > 0) {
                OrderRootTypePresentation presentation = myComponentDescriptor.getRootTypePresentation(type);
                if (presentation == null) {
                    presentation = DefaultLibraryRootsComponentDescriptor.getDefaultPresentation(type);
                }
                elements.add(new OrderRootTypeElement(myRootElementDescriptor, type, presentation.getNodeText(), presentation.getIcon()));
            }
        }
        return elements.toArray();
    }
    if (element instanceof OrderRootTypeElement) {
        OrderRootTypeElement rootTypeElement = (OrderRootTypeElement) element;
        OrderRootType orderRootType = rootTypeElement.getOrderRootType();
        ArrayList<ItemElement> items = new ArrayList<>();
        final String[] urls = libraryEditor.getUrls(orderRootType).clone();
        Arrays.sort(urls, LibraryRootsComponent.ourUrlComparator);
        for (String url : urls) {
            items.add(new ItemElement(rootTypeElement, url, orderRootType, libraryEditor.isJarDirectory(url, orderRootType), libraryEditor.isValid(url, orderRootType)));
        }
        return items.toArray();
    }
    if (element instanceof ItemElement) {
        ItemElement itemElement = (ItemElement) element;
        List<String> excludedUrls = new ArrayList<>();
        for (String excludedUrl : libraryEditor.getExcludedRootUrls()) {
            if (VfsUtilCore.isEqualOrAncestor(itemElement.getUrl(), excludedUrl)) {
                excludedUrls.add(excludedUrl);
            }
        }
        ExcludedRootElement[] items = new ExcludedRootElement[excludedUrls.size()];
        Collections.sort(excludedUrls, LibraryRootsComponent.ourUrlComparator);
        for (int i = 0; i < excludedUrls.size(); i++) {
            items[i] = new ExcludedRootElement(itemElement, itemElement.getUrl(), excludedUrls.get(i));
        }
        return items;
    }
    return ArrayUtil.EMPTY_OBJECT_ARRAY;
}
Also used : ArrayList(java.util.ArrayList) OrderRootTypePresentation(com.intellij.openapi.roots.libraries.ui.OrderRootTypePresentation) OrderRootType(com.intellij.openapi.roots.OrderRootType)

Aggregations

OrderRootType (com.intellij.openapi.roots.OrderRootType)31 VirtualFile (com.intellij.openapi.vfs.VirtualFile)10 File (java.io.File)7 Element (org.jdom.Element)7 PersistentOrderRootType (com.intellij.openapi.roots.PersistentOrderRootType)6 JavadocOrderRootType (com.intellij.openapi.roots.JavadocOrderRootType)5 Library (com.intellij.openapi.roots.libraries.Library)4 VirtualFilePointerContainer (com.intellij.openapi.vfs.pointers.VirtualFilePointerContainer)4 NotNull (org.jetbrains.annotations.NotNull)4 AnnotationOrderRootType (com.intellij.openapi.roots.AnnotationOrderRootType)3 ArrayList (java.util.ArrayList)3 Nullable (org.jetbrains.annotations.Nullable)3 Disposable (com.intellij.openapi.Disposable)2 LibraryPathType (com.intellij.openapi.externalSystem.model.project.LibraryPathType)2 RootProvider (com.intellij.openapi.roots.RootProvider)2 OrderRoot (com.intellij.openapi.roots.libraries.ui.OrderRoot)2 Pair (com.intellij.openapi.util.Pair)2 VirtualFileManager (com.intellij.openapi.vfs.VirtualFileManager)2 HashMap (com.intellij.util.containers.HashMap)2 HashMap (java.util.HashMap)2