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(); } } } }
@Nullable @Override public List<Pair<VirtualFile, DownloadableFileDescription>> downloadWithProgress(@Nullable String targetDirectoryPath, @Nullable Project project, @Nullable JComponent parentComponent) { File dir; if (targetDirectoryPath != null) { dir = new File(targetDirectoryPath); } else { VirtualFile virtualDir = chooseDirectoryForFiles(project, parentComponent); if (virtualDir != null) { dir = VfsUtilCore.virtualToIoFile(virtualDir); } else { return null; } } return downloadWithProcess(dir, project, parentComponent); }
private static List<Pair<File, DownloadableFileDescription>> moveToDir(List<Pair<File, DownloadableFileDescription>> downloadedFiles, final File targetDir) throws IOException { FileUtil.createDirectory(targetDir); List<Pair<File, DownloadableFileDescription>> result = new ArrayList<Pair<File, DownloadableFileDescription>>(); for (Pair<File, DownloadableFileDescription> pair : downloadedFiles) { final DownloadableFileDescription description = pair.getSecond(); final String fileName = description.generateFileName(new Condition<String>() { @Override public boolean value(String s) { return !new File(targetDir, s).exists(); } }); final File toFile = new File(targetDir, fileName); FileUtil.rename(pair.getFirst(), toFile); result.add(Pair.create(toFile, description)); } return result; }
/** * 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(); } } } }
@Override public List<Pair<VirtualFile, DownloadableFileDescription>> downloadAndReturnWithDescriptions() { VirtualFile dir = null; if (myDirectoryForDownloadedFilesPath != null) { File ioDir = new File(FileUtil.toSystemDependentName(myDirectoryForDownloadedFilesPath)); ioDir.mkdirs(); dir = LocalFileSystem.getInstance().refreshAndFindFileByPath(myDirectoryForDownloadedFilesPath); } if (dir == null) { dir = chooseDirectoryForFiles(); } if (dir != null) { return doDownload(dir); } return null; }
public FileDownloaderImpl(@NotNull List<? extends DownloadableFileDescription> fileDescriptions, @Nullable Project project, @Nullable JComponent parentComponent, @NotNull String presentableDownloadName) { myProject = project; myFileDescriptions = fileDescriptions; myParentComponent = parentComponent; myDialogTitle = IdeBundle.message("progress.download.0.title", StringUtil.capitalize(presentableDownloadName)); }
@Nullable @Override public List<VirtualFile> downloadFilesWithProgress(@Nullable String targetDirectoryPath, @Nullable Project project, @Nullable JComponent parentComponent) { final List<Pair<VirtualFile, DownloadableFileDescription>> pairs = downloadWithProgress(targetDirectoryPath, project, parentComponent); if (pairs == null) return null; List<VirtualFile> files = new ArrayList<VirtualFile>(); for (Pair<VirtualFile, DownloadableFileDescription> pair : pairs) { files.add(pair.getFirst()); } return files; }
@Nullable private List<Pair<VirtualFile,DownloadableFileDescription>> downloadWithProcess(final File targetDir, Project project, JComponent parentComponent) { final Ref<List<Pair<File, DownloadableFileDescription>>> localFiles = Ref.create(null); final Ref<IOException> exceptionRef = Ref.create(null); boolean completed = ProgressManager.getInstance().runProcessWithProgressSynchronously(new Runnable() { @Override public void run() { try { localFiles.set(download(targetDir)); } catch (IOException e) { exceptionRef.set(e); } } }, myDialogTitle, true, project, parentComponent); if (!completed) { return null; } @SuppressWarnings("ThrowableResultOfMethodCallIgnored") Exception exception = exceptionRef.get(); if (exception != null) { final boolean tryAgain = IOExceptionDialog.showErrorDialog(myDialogTitle, exception.getMessage()); if (tryAgain) { return downloadWithProcess(targetDir, project, parentComponent); } return null; } return findVirtualFiles(localFiles.get()); }
public FileDownloaderImpl(final List<? extends DownloadableFileDescription> fileDescriptions, final @Nullable Project project, JComponent parent, @NotNull String presentableDownloadName) { myProject = project; myFileDescriptions = fileDescriptions; myParent = parent; myDialogTitle = IdeBundle.message("progress.download.0.title", StringUtil.capitalize(presentableDownloadName)); }
@Override public VirtualFile[] download() { final List<Pair<VirtualFile, DownloadableFileDescription>> pairs = downloadAndReturnWithDescriptions(); if (pairs == null) return null; List<VirtualFile> files = new ArrayList<VirtualFile>(); for (Pair<VirtualFile, DownloadableFileDescription> pair : pairs) { files.add(pair.getFirst()); } return VfsUtilCore.toVirtualFileArray(files); }
private static File generateName(DownloadableFileDescription info, final File dir) { final String fileName = info.generateFileName(new Condition<String>() { @Override public boolean value(String s) { return !new File(dir, s).exists(); } }); return new File(dir, fileName); }
@Nullable DownloadableFileDescription getSourcesDescription();
@Nullable DownloadableFileDescription getDocumentationDescription();
private static void deleteFiles(final List<Pair<File, DownloadableFileDescription>> pairs) { for (Pair<File, DownloadableFileDescription> pair : pairs) { FileUtil.delete(pair.getFirst()); } }
@Nullable @Override public List<Pair<VirtualFile, DownloadableFileDescription>> downloadAndReturnWithDescriptions() { return downloadWithProgress(myDirectoryForDownloadedFilesPath, myProject, myParentComponent); }
private static void deleteFiles(final List<Pair<File, DownloadableFileDescription>> pairs) { for (Pair<File, DownloadableFileDescription> pair : pairs) { FileUtil.delete(pair.getFirst()); } pairs.clear(); }
/** * 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); }