Source:
public void download() { if (!linkedFile.isOnlineLink()) { throw new UnsupportedOperationException("In order to download the file it has to be an online link"); } try { Optional<Path> targetDirectory = databaseContext.getFirstExistingFileDir(filePreferences); if (targetDirectory.isEmpty()) { dialogService.showErrorDialogAndWait(Localization.lang("Download file"), Localization.lang("File directory is not set or does not exist!")); return; } URLDownload urlDownload = new URLDownload(linkedFile.getLink()); BackgroundTask<Path> downloadTask = prepareDownloadTask(targetDirectory.get(), urlDownload); downloadTask.onSuccess(destination -> { LinkedFile newLinkedFile = LinkedFilesEditorViewModel.fromFile(destination, databaseContext.getFileDirectoriesAsPaths(filePreferences), externalFileTypes); var linkedFiles = entry.getFiles(); Optional<Integer> oldFileIndex = Optional.empty(); for (int i = 0; i < linkedFiles.size(); i++) { LinkedFile file = linkedFiles.get(i); if (file.getLink().equalsIgnoreCase(linkedFile.getLink())) { oldFileIndex = Optional.of(i); } } if (oldFileIndex.isPresent()) { linkedFiles.set(oldFileIndex.get(), newLinkedFile); } else { linkedFiles.add(0, newLinkedFile); } entry.setFiles(linkedFiles); }); downloadProgress.bind(downloadTask.workDonePercentageProperty()); downloadTask.titleProperty().set(Localization.lang("Downloading")); downloadTask.messageProperty().set( Localization.lang("Fulltext for") + ": " + entry.getCiteKeyOptional().orElse(Localization.lang("New entry"))); downloadTask.showToUser(true); taskExecutor.execute(downloadTask); } catch (MalformedURLException exception) { dialogService.showErrorDialogAndWait(Localization.lang("Invalid URL"), exception); } }

Target:
public void download() { if (!linkedFile.isOnlineLink()) { throw new UnsupportedOperationException("In order to download the file it has to be an online link"); } try { Optional<Path> targetDirectory = databaseContext.getFirstExistingFileDir(filePreferences); if (targetDirectory.isEmpty()) { dialogService.showErrorDialogAndWait(Localization.lang("Download file"), Localization.lang("File directory is not set or does not exist!")); return; } URLDownload urlDownload = new URLDownload(linkedFile.getLink()); BackgroundTask<Path> downloadTask = prepareDownloadTask(targetDirectory.get(), urlDownload); downloadTask.onSuccess(destination -> { LinkedFile newLinkedFile = LinkedFilesEditorViewModel.fromFile(destination, databaseContext.getFileDirectoriesAsPaths(filePreferences), externalFileTypes); List<LinkedFile> linkedFiles = entry.getFiles(); int oldFileIndex = -1; int i = 0; while (i < linkedFiles.size() && oldFileIndex == -1) { LinkedFile file = linkedFiles.get(i); if (file.getLink().equalsIgnoreCase(linkedFile.getLink())) { oldFileIndex = i; } i++; } if (oldFileIndex == -1) { linkedFiles.add(0, newLinkedFile); } else { linkedFiles.set(oldFileIndex, newLinkedFile); } entry.setFiles(linkedFiles); }); downloadProgress.bind(downloadTask.workDonePercentageProperty()); downloadTask.titleProperty().set(Localization.lang("Downloading")); downloadTask.messageProperty().set( Localization.lang("Fulltext for") + ": " + entry.getCiteKeyOptional().orElse(Localization.lang("New entry"))); downloadTask.showToUser(true); taskExecutor.execute(downloadTask); } catch (MalformedURLException exception) { dialogService.showErrorDialogAndWait(Localization.lang("Invalid URL"), exception); } }