@Override public void setSelected(final AnActionEvent e, final boolean isSelected) { final VirtualFile[] selectedFiles = getSelectedFiles(); assert selectedFiles.length != 0; final ContentEntryEditor contentEntryEditor = myEntryTreeEditor.getContentEntryEditor(); for (VirtualFile selectedFile : selectedFiles) { final SourceFolder sourceFolder = contentEntryEditor.getSourceFolder(selectedFile); if (isSelected) { if (sourceFolder == null) { // not marked yet contentEntryEditor.addSourceFolder(selectedFile, myEditTestSources, ""); } else { if (myEditTestSources != sourceFolder.isTestSource()) { final String packagePrefix = sourceFolder.getPackagePrefix(); contentEntryEditor.removeSourceFolder(sourceFolder); contentEntryEditor.addSourceFolder(selectedFile, myEditTestSources, packagePrefix); } } } else { if (sourceFolder != null) { // already marked contentEntryEditor.removeSourceFolder(sourceFolder); } } } }
private static boolean changePackagePrefixes( PsiPackage psiPackage, final String oldQualifiedName, final String newQualifiedName) { final Module[] modules = ModuleManager.getInstance(psiPackage.getProject()).getModules(); List<ModifiableRootModel> modelsToCommit = new ArrayList<ModifiableRootModel>(); for (final Module module : modules) { boolean anyChange = false; final ModifiableRootModel rootModel = ModuleRootManager.getInstance(module).getModifiableModel(); for (final ContentEntry contentEntry : rootModel.getContentEntries()) { for (final SourceFolder sourceFolder : contentEntry.getSourceFolders(JavaModuleSourceRootTypes.SOURCES)) { final String packagePrefix = sourceFolder.getPackagePrefix(); if (packagePrefix.startsWith(oldQualifiedName)) { sourceFolder.setPackagePrefix( newQualifiedName + packagePrefix.substring(oldQualifiedName.length())); anyChange = true; } } } if (anyChange) { modelsToCommit.add(rootModel); } else { rootModel.dispose(); } } if (!modelsToCommit.isEmpty()) { ModifiableRootModel[] rootModels = modelsToCommit.toArray(new ModifiableRootModel[modelsToCommit.size()]); if (rootModels.length > 0) { ModifiableModelCommitter.multiCommit( rootModels, ModuleManager.getInstance(rootModels[0].getProject()).getModifiableModel()); } return true; } else { return false; } }
@Override public VirtualFile[] occursInPackagePrefixes(PsiPackage psiPackage) { List<VirtualFile> result = new ArrayList<VirtualFile>(); final Module[] modules = ModuleManager.getInstance(psiPackage.getProject()).getModules(); for (final Module module : modules) { for (final ContentEntry contentEntry : ModuleRootManager.getInstance(module).getContentEntries()) { final List<SourceFolder> sourceFolders = contentEntry.getSourceFolders(JavaModuleSourceRootTypes.SOURCES); for (final SourceFolder sourceFolder : sourceFolders) { final String packagePrefix = sourceFolder.getPackagePrefix(); if (packagePrefix.startsWith(psiPackage.getQualifiedName())) { final VirtualFile file = sourceFolder.getFile(); if (file != null) { result.add(file); } } } } } return VfsUtil.toVirtualFileArray(result); }
@NotNull private RootInfo buildRootInfo(@NotNull Project project) { final RootInfo info = new RootInfo(); for (final Module module : ModuleManager.getInstance(project).getModules()) { final ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module); for (final VirtualFile contentRoot : moduleRootManager.getContentRoots()) { if (!info.contentRootOf.containsKey(contentRoot)) { info.contentRootOf.put(contentRoot, module); } } for (ContentEntry contentEntry : moduleRootManager.getContentEntries()) { if (!(contentEntry instanceof ContentEntryImpl) || !((ContentEntryImpl) contentEntry).isDisposed()) { for (VirtualFile excludeRoot : contentEntry.getExcludeFolderFiles()) { info.excludedFromModule.put(excludeRoot, module); } } // Init module sources for (final SourceFolder sourceFolder : contentEntry.getSourceFolders()) { final VirtualFile sourceFolderRoot = sourceFolder.getFile(); if (sourceFolderRoot != null) { info.rootTypeId.put(sourceFolderRoot, getRootTypeId(sourceFolder.getRootType())); info.classAndSourceRoots.add(sourceFolderRoot); info.sourceRootOf.putValue(sourceFolderRoot, module); info.packagePrefix.put(sourceFolderRoot, sourceFolder.getPackagePrefix()); } } } for (OrderEntry orderEntry : moduleRootManager.getOrderEntries()) { if (orderEntry instanceof LibraryOrSdkOrderEntry) { final LibraryOrSdkOrderEntry entry = (LibraryOrSdkOrderEntry) orderEntry; final VirtualFile[] sourceRoots = entry.getRootFiles(OrderRootType.SOURCES); final VirtualFile[] classRoots = entry.getRootFiles(OrderRootType.CLASSES); // Init library sources for (final VirtualFile sourceRoot : sourceRoots) { info.classAndSourceRoots.add(sourceRoot); info.libraryOrSdkSources.add(sourceRoot); info.packagePrefix.put(sourceRoot, ""); } // init library classes for (final VirtualFile classRoot : classRoots) { info.classAndSourceRoots.add(classRoot); info.libraryOrSdkClasses.add(classRoot); info.packagePrefix.put(classRoot, ""); } if (orderEntry instanceof LibraryOrderEntry) { Library library = ((LibraryOrderEntry) orderEntry).getLibrary(); if (library != null) { for (VirtualFile root : ((LibraryEx) library).getExcludedRoots()) { info.excludedFromLibraries.putValue(root, library); } for (VirtualFile root : sourceRoots) { info.sourceOfLibraries.putValue(root, library); } for (VirtualFile root : classRoots) { info.classOfLibraries.putValue(root, library); } } } } } } for (DirectoryIndexExcludePolicy policy : Extensions.getExtensions(DirectoryIndexExcludePolicy.EP_NAME, project)) { Collections.addAll(info.excludedFromProject, policy.getExcludeRootsForProject()); } return info; }