From 9d67aae792607425545ed24c4bdada78401355d7 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Tue, 29 Dec 2020 13:33:36 +0100 Subject: [PATCH 1/3] Only disable move to file dir when path equals Fix equals in path method Fixes #7194 --- CHANGELOG.md | 1 + .../gui/fieldeditors/LinkedFileViewModel.java | 6 ++--- .../gui/fieldeditors/LinkedFilesEditor.java | 2 +- .../fieldeditors/LinkedFileViewModelTest.java | 27 ++++++++++++++++--- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index de35529c656..8ee5078e4fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve ### Fixed - We fixed an issue with the style of highlighted check boxes while searching in preferences. [#7226](https://github.com/JabRef/jabref/issues/7226) +- We fixed an issue where the option "Move file to file directory" was disabled in the entry editor for all files [#7194](https://github.com/JabRef/jabref/issues/7194) ### Removed diff --git a/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java b/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java index 7f14652a529..4586b3ad689 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java +++ b/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java @@ -317,14 +317,14 @@ public boolean isGeneratedNameSameAsOriginal() { } /** - * Compares suggested filepath of current linkedFile with existing filepath. + * Compares suggested directory of current linkedFile with existing filepath directory. * * @return true if suggested filepath is same as existing filepath. */ public boolean isGeneratedPathSameAsOriginal() { Optional newDir = databaseContext.getFirstExistingFileDir(filePreferences); - Optional currentDir = linkedFile.findIn(databaseContext, filePreferences); + Optional currentDir = linkedFile.findIn(databaseContext, filePreferences).map(Path::getParent); BiPredicate equality = (fileA, fileB) -> { try { @@ -434,7 +434,7 @@ public void download() { List linkedFiles = entry.getFiles(); int oldFileIndex = -1; int i = 0; - while (i < linkedFiles.size() && oldFileIndex == -1) { + while ((i < linkedFiles.size()) && (oldFileIndex == -1)) { LinkedFile file = linkedFiles.get(i); // The file type changes as part of download process (see prepareDownloadTask), thus we only compare by link if (file.getLink().equalsIgnoreCase(linkedFile.getLink())) { diff --git a/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java b/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java index 846d2c7a709..b08d2f67c9d 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java +++ b/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java @@ -275,7 +275,7 @@ public ContextAction(StandardActions command, LinkedFileViewModel linkedFile, Pr case MOVE_FILE_TO_FOLDER_AND_RENAME, MOVE_FILE_TO_FOLDER -> Bindings.createBooleanBinding( () -> !linkedFile.getFile().isOnlineLink() && linkedFile.getFile().findIn(databaseContext, preferencesService.getFilePreferences()).isPresent() - && linkedFile.isGeneratedPathSameAsOriginal(), + && !linkedFile.isGeneratedPathSameAsOriginal(), linkedFile.getFile().linkProperty()); case DOWNLOAD_FILE -> Bindings.createBooleanBinding( () -> linkedFile.getFile().isOnlineLink(), diff --git a/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java b/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java index b84b6493bfc..f09df185686 100644 --- a/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java +++ b/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java @@ -23,8 +23,6 @@ import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.LinkedFile; import org.jabref.preferences.FilePreferences; -import org.jabref.testutils.category.FetcherTest; - import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -152,7 +150,6 @@ void deleteWhenDialogCancelledReturnsFalseAndDoesNotRemoveFile() { assertTrue(Files.exists(tempFile)); } - @FetcherTest void downloadDoesNotOverwriteFileTypeExtension() throws MalformedURLException { linkedFile = new LinkedFile(new URL("http://arxiv.org/pdf/1207.0408v1"), ""); @@ -170,4 +167,28 @@ void downloadDoesNotOverwriteFileTypeExtension() throws MalformedURLException { task.onFailure(Assertions::fail); new CurrentThreadTaskExecutor().execute(task); } + + @Test + void isNotSamePath() { + linkedFile = new LinkedFile("desc", tempFile, "pdf"); + databaseContext = mock(BibDatabaseContext.class); + when(filePreferences.getFileNamePattern()).thenReturn("[citationkey]"); // use this variant, as we cannot mock the linkedFileHandler cause it's initialized inside the viewModel + when(databaseContext.getFirstExistingFileDir(filePreferences)).thenReturn(Optional.of(Path.of("/home"))); + + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, xmpPreferences, filePreferences, externalFileType); + + assertFalse(viewModel.isGeneratedPathSameAsOriginal()); + } + + @Test + void isSamePath() { + linkedFile = new LinkedFile("desc", tempFile, "pdf"); + databaseContext = mock(BibDatabaseContext.class); + when(filePreferences.getFileNamePattern()).thenReturn("[citationkey]"); // use this variant, as we cannot mock the linkedFileHandler cause it's initialized inside the viewModel + when(databaseContext.getFirstExistingFileDir(filePreferences)).thenReturn(Optional.of(tempFile.getParent())); + + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, xmpPreferences, filePreferences, externalFileType); + + assertTrue(viewModel.isGeneratedPathSameAsOriginal()); + } } From 57887baac14fd9102e10f009b4eb45439f735f36 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Tue, 29 Dec 2020 13:38:48 +0100 Subject: [PATCH 2/3] fix checkstyle --- .../org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java b/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java index f09df185686..4eefee9768a 100644 --- a/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java +++ b/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java @@ -23,6 +23,7 @@ import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.LinkedFile; import org.jabref.preferences.FilePreferences; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; From 46925314ce1d32af89efa6161b33b64f5e131c0d Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Tue, 29 Dec 2020 18:23:38 +0100 Subject: [PATCH 3/3] Allow rename and move when in file dir --- .../org/jabref/gui/fieldeditors/LinkedFilesEditor.java | 4 ++-- .../jabref/gui/fieldeditors/LinkedFileViewModelTest.java | 8 +++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java b/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java index b08d2f67c9d..b005a1a0624 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java +++ b/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java @@ -267,12 +267,12 @@ public ContextAction(StandardActions command, LinkedFileViewModel linkedFile, Pr this.executable.bind( switch (command) { - case RENAME_FILE_TO_PATTERN -> Bindings.createBooleanBinding( + case RENAME_FILE_TO_PATTERN, MOVE_FILE_TO_FOLDER_AND_RENAME -> Bindings.createBooleanBinding( () -> !linkedFile.getFile().isOnlineLink() && linkedFile.getFile().findIn(databaseContext, preferencesService.getFilePreferences()).isPresent() && !linkedFile.isGeneratedNameSameAsOriginal(), linkedFile.getFile().linkProperty()); - case MOVE_FILE_TO_FOLDER_AND_RENAME, MOVE_FILE_TO_FOLDER -> Bindings.createBooleanBinding( + case MOVE_FILE_TO_FOLDER -> Bindings.createBooleanBinding( () -> !linkedFile.getFile().isOnlineLink() && linkedFile.getFile().findIn(databaseContext, preferencesService.getFilePreferences()).isPresent() && !linkedFile.isGeneratedPathSameAsOriginal(), diff --git a/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java b/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java index 4eefee9768a..7e37ca24303 100644 --- a/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java +++ b/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java @@ -155,7 +155,7 @@ void downloadDoesNotOverwriteFileTypeExtension() throws MalformedURLException { linkedFile = new LinkedFile(new URL("http://arxiv.org/pdf/1207.0408v1"), ""); databaseContext = mock(BibDatabaseContext.class); - when(filePreferences.getFileNamePattern()).thenReturn("[citationkey]"); // use this variant, as we cannot mock the linkedFileHandler cause it's initialized inside the viewModel + when(filePreferences.getFileNamePattern()).thenReturn("[citationkey]"); LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, new CurrentThreadTaskExecutor(), dialogService, xmpPreferences, filePreferences, externalFileType); @@ -173,11 +173,10 @@ void downloadDoesNotOverwriteFileTypeExtension() throws MalformedURLException { void isNotSamePath() { linkedFile = new LinkedFile("desc", tempFile, "pdf"); databaseContext = mock(BibDatabaseContext.class); - when(filePreferences.getFileNamePattern()).thenReturn("[citationkey]"); // use this variant, as we cannot mock the linkedFileHandler cause it's initialized inside the viewModel + when(filePreferences.getFileNamePattern()).thenReturn("[citationkey]"); when(databaseContext.getFirstExistingFileDir(filePreferences)).thenReturn(Optional.of(Path.of("/home"))); LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, xmpPreferences, filePreferences, externalFileType); - assertFalse(viewModel.isGeneratedPathSameAsOriginal()); } @@ -185,11 +184,10 @@ void isNotSamePath() { void isSamePath() { linkedFile = new LinkedFile("desc", tempFile, "pdf"); databaseContext = mock(BibDatabaseContext.class); - when(filePreferences.getFileNamePattern()).thenReturn("[citationkey]"); // use this variant, as we cannot mock the linkedFileHandler cause it's initialized inside the viewModel + when(filePreferences.getFileNamePattern()).thenReturn("[citationkey]"); when(databaseContext.getFirstExistingFileDir(filePreferences)).thenReturn(Optional.of(tempFile.getParent())); LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, xmpPreferences, filePreferences, externalFileType); - assertTrue(viewModel.isGeneratedPathSameAsOriginal()); } }