diff --git a/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java b/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java index 808764d0231..61c85b34d7f 100644 --- a/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java +++ b/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java @@ -147,7 +147,7 @@ public List call() { entry.clearField(StandardField.FILE); // Modifiers do not work on macOS: https://bugs.openjdk.org/browse/JDK-8264172 // Similar code as org.jabref.gui.preview.PreviewPanel.PreviewPanel - DragDrop.handleDropOfFiles(files, transferMode, fileLinker, entry); + DragDrop.handleDropOfFiles(List.of(file), transferMode, fileLinker, entry); entriesToAdd.addAll(pdfEntriesInFile); addResultToList(file, true, Localization.lang("File was successfully imported as a new entry")); }); diff --git a/src/main/java/org/jabref/gui/externalfiles/UnlinkedFilesDialogViewModel.java b/src/main/java/org/jabref/gui/externalfiles/UnlinkedFilesDialogViewModel.java index 58f634d0067..50163b34515 100644 --- a/src/main/java/org/jabref/gui/externalfiles/UnlinkedFilesDialogViewModel.java +++ b/src/main/java/org/jabref/gui/externalfiles/UnlinkedFilesDialogViewModel.java @@ -10,7 +10,6 @@ import java.util.List; import java.util.Optional; import java.util.function.Predicate; -import java.util.stream.Collectors; import javax.swing.undo.UndoManager; @@ -145,30 +144,32 @@ public void startSearch() { } public void startImport() { - Path directory = this.getSearchDirectory(); - List fileList = checkedFileListProperty.stream() - .map(item -> item.getValue().getPath()) - .filter(path -> path.toFile().isFile()) - .map(path -> directory.relativize(path)) - .collect(Collectors.toList()); + List fileList = checkedFileListProperty + .stream() + .map(TreeItem::getValue) + .map(FileNodeViewModel::getPath) + .filter(Files::isRegularFile) + .toList(); + if (fileList.isEmpty()) { - LOGGER.warn("There are no valid files checked"); + LOGGER.warn("There are no valid files checked for import"); return; } resultList.clear(); - importFilesBackgroundTask = importHandler.importFilesInBackground(fileList, bibDatabase, preferences.getFilePreferences(), TransferMode.LINK) - .onRunning(() -> { - progressValueProperty.bind(importFilesBackgroundTask.workDonePercentageProperty()); - progressTextProperty.bind(importFilesBackgroundTask.messageProperty()); - taskActiveProperty.setValue(true); - }) - .onFinished(() -> { - progressValueProperty.unbind(); - progressTextProperty.unbind(); - taskActiveProperty.setValue(false); - }) - .onSuccess(resultList::addAll); + importFilesBackgroundTask = importHandler + .importFilesInBackground(fileList, bibDatabase, preferences.getFilePreferences(), TransferMode.LINK) + .onRunning(() -> { + progressValueProperty.bind(importFilesBackgroundTask.workDonePercentageProperty()); + progressTextProperty.bind(importFilesBackgroundTask.messageProperty()); + taskActiveProperty.setValue(true); + }) + .onFinished(() -> { + progressValueProperty.unbind(); + progressTextProperty.unbind(); + taskActiveProperty.setValue(false); + }) + .onSuccess(resultList::addAll); importFilesBackgroundTask.executeWith(taskExecutor); } @@ -176,12 +177,13 @@ public void startImport() { * This starts the export of all files of all selected nodes in the file tree view. */ public void startExport() { - List fileList = checkedFileListProperty.stream() - .map(item -> item.getValue().getPath()) - .filter(path -> path.toFile().isFile()) - .toList(); + List fileList = checkedFileListProperty + .stream() + .map(item -> item.getValue().getPath()) + .filter(Files::isRegularFile) + .toList(); if (fileList.isEmpty()) { - LOGGER.warn("There are no valid files checked"); + LOGGER.warn("There are no valid files checked for export"); return; } diff --git a/src/main/java/org/jabref/logic/importer/fileformat/PdfContentImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/PdfContentImporter.java index 7d43be038a4..2c9c94a4745 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/PdfContentImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/PdfContentImporter.java @@ -203,7 +203,7 @@ public ParserResult importDatabase(Path filePath) { List result = new ArrayList<>(1); try (PDDocument document = new XmpUtilReader().loadWithAutomaticDecryption(filePath)) { String firstPageContents = getFirstPageContents(document); - String titleByFontSize = extractTitleFromDocument(document); + Optional titleByFontSize = extractTitleFromDocument(document); Optional entry = getEntryFromPDFContent(firstPageContents, OS.NEWLINE, titleByFontSize); entry.ifPresent(result::add); } catch (EncryptedPdfsNotSupportedException e) { @@ -216,7 +216,7 @@ public ParserResult importDatabase(Path filePath) { return new ParserResult(result); } - private static String extractTitleFromDocument(PDDocument document) throws IOException { + private static Optional extractTitleFromDocument(PDDocument document) throws IOException { TitleExtractorByFontSize stripper = new TitleExtractorByFontSize(); return stripper.getTitle(document); } @@ -230,7 +230,7 @@ public TitleExtractorByFontSize() { this.textPositionsList = new ArrayList<>(); } - public String getTitle(PDDocument document) throws IOException { + public Optional getTitle(PDDocument document) throws IOException { this.setStartPage(1); this.setEndPage(2); this.writeText(document, new StringWriter()); @@ -266,7 +266,7 @@ private boolean isUnwantedText(TextPosition previousTextPosition, TextPosition t return isFarAway(previousTextPosition, textPosition); } - private String findLargestFontText(List textPositions) { + private Optional findLargestFontText(List textPositions) { Map fontSizeTextMap = new TreeMap<>(Collections.reverseOrder()); TextPosition previousTextPosition = null; for (TextPosition textPosition : textPositions) { @@ -285,10 +285,10 @@ private String findLargestFontText(List textPositions) { for (Map.Entry entry : fontSizeTextMap.entrySet()) { String candidateText = entry.getValue().toString().trim(); if (isLegalTitle(candidateText)) { - return candidateText; + return Optional.of(candidateText); } } - return fontSizeTextMap.values().iterator().next().toString().trim(); + return fontSizeTextMap.values().stream().findFirst().map(StringBuilder::toString).map(String::trim); } private boolean isLegalTitle(String candidateText) { @@ -334,7 +334,7 @@ private boolean isThereSpace(TextPosition previous, TextPosition current) { * is successful. Otherwise, an empty {@link Optional}. */ @VisibleForTesting - Optional getEntryFromPDFContent(String firstpageContents, String lineSeparator, String titleByFontSize) { + Optional getEntryFromPDFContent(String firstpageContents, String lineSeparator, Optional titleByFontSize) { String firstpageContentsUnifiedLineBreaks = StringUtil.unifyLineBreaks(firstpageContents, lineSeparator); lines = firstpageContentsUnifiedLineBreaks.split(lineSeparator); @@ -393,8 +393,8 @@ Optional getEntryFromPDFContent(String firstpageContents, String lineS title = streamlineTitle(curString); // i points to the next non-empty line curString = ""; - if (!isNullOrEmpty(titleByFontSize)) { - title = titleByFontSize; + if (titleByFontSize.isPresent() && !isNullOrEmpty(titleByFontSize.get())) { + title = titleByFontSize.get(); } // after title: authors diff --git a/src/main/java/org/jabref/logic/importer/fileformat/PdfMergeMetadataImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/PdfMergeMetadataImporter.java index 21b18e27f84..bb461423a76 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/PdfMergeMetadataImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/PdfMergeMetadataImporter.java @@ -136,22 +136,10 @@ public ParserResult importDatabase(Path filePath) throws IOException { return new ParserResult(List.of(entry)); } - /** - * A modified version of {@link PdfMergeMetadataImporter#importDatabase(Path)}, but it - * relativizes the {@code filePath} if there are working directories before parsing it - * into {@link PdfMergeMetadataImporter#importDatabase(Path)} - * (Otherwise no path modification happens). - * - * @param filePath The unrelativized {@code filePath}. - */ public ParserResult importDatabase(Path filePath, BibDatabaseContext context, FilePreferences filePreferences) throws IOException { Objects.requireNonNull(context); Objects.requireNonNull(filePreferences); - List directories = context.getFileDirectories(filePreferences); - - filePath = FileUtil.relativize(filePath, directories); - return importDatabase(filePath); } diff --git a/src/test/java/org/jabref/logic/importer/fileformat/PdfContentImporterTest.java b/src/test/java/org/jabref/logic/importer/fileformat/PdfContentImporterTest.java index 318cb858508..43a551a8634 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/PdfContentImporterTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/PdfContentImporterTest.java @@ -70,7 +70,7 @@ void parsingEditorWithoutPagesorSeriesInformation() { Corpus linguistics investigates human language by starting out from large """; - assertEquals(Optional.of(entry), importer.getEntryFromPDFContent(firstPageContents, "\n", "")); + assertEquals(Optional.of(entry), importer.getEntryFromPDFContent(firstPageContents, "\n", Optional.empty())); } @Test @@ -93,7 +93,7 @@ Smith, Lucy Anna (2014) Mortality in the Ornamental Fish Retail Sector: an Analy UNSPECIFIED Master of Research (MRes) thesis, University of Kent,."""; - assertEquals(Optional.of(entry), importer.getEntryFromPDFContent(firstPageContents, "\n", "")); + assertEquals(Optional.of(entry), importer.getEntryFromPDFContent(firstPageContents, "\n", Optional.empty())); } @Test @@ -126,7 +126,7 @@ British Journal of Nutrition (2008), 99, 1–11 doi: 10.1017/S0007114507795296 British Journal of Nutrition https://doi.org/10.1017/S0007114507795296 Published online by Cambridge University Press"""; - assertEquals(Optional.of(entry), importer.getEntryFromPDFContent(firstPageContent, "\n", "")); + assertEquals(Optional.of(entry), importer.getEntryFromPDFContent(firstPageContent, "\n", Optional.empty())); } @ParameterizedTest