private void downloadJar(String jarUrl, String jarName) { final Project project = myModule.getProject(); final String dirPath = PropertiesComponent.getInstance(project).getValue("findjar.last.used.dir"); VirtualFile toSelect = dirPath == null ? null : LocalFileSystem.getInstance().findFileByIoFile(new File(dirPath)); final VirtualFile file = FileChooser.chooseFile(FileChooserDescriptorFactory.createSingleFolderDescriptor(), project, toSelect); if (file != null) { PropertiesComponent.getInstance(project).setValue("findjar.last.used.dir", file.getPath()); final DownloadableFileService downloader = DownloadableFileService.getInstance(); final DownloadableFileDescription description = downloader.createFileDescription(jarUrl, jarName); final List<VirtualFile> jars = downloader.createDownloader(Arrays.asList(description), jarName) .downloadFilesWithProgress(file.getPath(), project, myEditorComponent); if (jars != null && jars.size() == 1) { AccessToken token = WriteAction.start(); try { OrderEntryFix.addJarToRoots(jars.get(0).getPresentableUrl(), myModule, myRef); } finally { token.finish(); } } } }
/** * Download files from the specified URLs. * * @param urls The URLs of the files. * @param targetPath The path to download the file to. * @return The downloaded files. */ private Collection<VirtualFile> downloadFiles(final Collection<String> urls, final String targetPath) { final DownloadableFileService fileService = DownloadableFileService.getInstance(); final List<DownloadableFileDescription> fileDescriptions = ContainerUtil.map(urls, (u) -> toFileDescription(fileService, u)); @Nullable final List<VirtualFile> files = fileService .createDownloader(fileDescriptions, "") .downloadFilesWithProgress(targetPath, null, null); if (files == null || files.size() != fileDescriptions.size()) { throw LoggerUtils2.exception(this.logger, RuntimeException.class, "Not all files were downloaded!"); } return files; }
private void downloadJar(String jarUrl, String jarName) { final Project project = myModule.getProject(); final String dirPath = PropertiesComponent.getInstance(project).getValue("findjar.last.used.dir"); VirtualFile toSelect = dirPath == null ? null : LocalFileSystem.getInstance().findFileByIoFile(new File(dirPath)); final VirtualFile file = FileChooser.chooseFile(FileChooserDescriptorFactory.createSingleFolderDescriptor(), project, toSelect); if (file != null) { PropertiesComponent.getInstance(project).setValue("findjar.last.used.dir", file.getPath()); final DownloadableFileService downloader = DownloadableFileService.getInstance(); final DownloadableFileDescription description = downloader.createFileDescription(jarUrl, jarName); final VirtualFile[] jars = downloader.createDownloader(Arrays.asList(description), project, myEditorComponent, jarName) .toDirectory(file.getPath()).download(); if (jars != null && jars.length == 1) { AccessToken token = WriteAction.start(); try { OrderEntryFix.addJarToRoots(jars[0].getPresentableUrl(), myModule, myRef); } finally { token.finish(); } } } }
/** * Gets the file description of the specified URL. * * @param fileService The file service. * @param url The URL. * @return The file description. */ private DownloadableFileDescription toFileDescription(final DownloadableFileService fileService, final String url) { final String filename = FilenameUtils.getName(url); return fileService.createFileDescription(url, filename); }