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());
}
}
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);
}
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;
}
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);
}
}
}
}
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;
}
Aggregations