Skip to content

Commit afdb194

Browse files
authored
Only disable move to file dir when path equals (#7269)
* Only disable move to file dir when path equals Fix equals in path method Fixes #7194 * fix checkstyle * Allow rename and move when in file dir
1 parent 020cc97 commit afdb194

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
1818
### Fixed
1919

2020
- We fixed an issue with the style of highlighted check boxes while searching in preferences. [#7226](https://github.com/JabRef/jabref/issues/7226)
21+
- 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)
2122

2223
### Removed
2324

src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -317,14 +317,14 @@ public boolean isGeneratedNameSameAsOriginal() {
317317
}
318318

319319
/**
320-
* Compares suggested filepath of current linkedFile with existing filepath.
320+
* Compares suggested directory of current linkedFile with existing filepath directory.
321321
*
322322
* @return true if suggested filepath is same as existing filepath.
323323
*/
324324
public boolean isGeneratedPathSameAsOriginal() {
325325
Optional<Path> newDir = databaseContext.getFirstExistingFileDir(filePreferences);
326326

327-
Optional<Path> currentDir = linkedFile.findIn(databaseContext, filePreferences);
327+
Optional<Path> currentDir = linkedFile.findIn(databaseContext, filePreferences).map(Path::getParent);
328328

329329
BiPredicate<Path, Path> equality = (fileA, fileB) -> {
330330
try {
@@ -434,7 +434,7 @@ public void download() {
434434
List<LinkedFile> linkedFiles = entry.getFiles();
435435
int oldFileIndex = -1;
436436
int i = 0;
437-
while (i < linkedFiles.size() && oldFileIndex == -1) {
437+
while ((i < linkedFiles.size()) && (oldFileIndex == -1)) {
438438
LinkedFile file = linkedFiles.get(i);
439439
// The file type changes as part of download process (see prepareDownloadTask), thus we only compare by link
440440
if (file.getLink().equalsIgnoreCase(linkedFile.getLink())) {

src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -267,15 +267,15 @@ public ContextAction(StandardActions command, LinkedFileViewModel linkedFile, Pr
267267

268268
this.executable.bind(
269269
switch (command) {
270-
case RENAME_FILE_TO_PATTERN -> Bindings.createBooleanBinding(
270+
case RENAME_FILE_TO_PATTERN, MOVE_FILE_TO_FOLDER_AND_RENAME -> Bindings.createBooleanBinding(
271271
() -> !linkedFile.getFile().isOnlineLink()
272272
&& linkedFile.getFile().findIn(databaseContext, preferencesService.getFilePreferences()).isPresent()
273273
&& !linkedFile.isGeneratedNameSameAsOriginal(),
274274
linkedFile.getFile().linkProperty());
275-
case MOVE_FILE_TO_FOLDER_AND_RENAME, MOVE_FILE_TO_FOLDER -> Bindings.createBooleanBinding(
275+
case MOVE_FILE_TO_FOLDER -> Bindings.createBooleanBinding(
276276
() -> !linkedFile.getFile().isOnlineLink()
277277
&& linkedFile.getFile().findIn(databaseContext, preferencesService.getFilePreferences()).isPresent()
278-
&& linkedFile.isGeneratedPathSameAsOriginal(),
278+
&& !linkedFile.isGeneratedPathSameAsOriginal(),
279279
linkedFile.getFile().linkProperty());
280280
case DOWNLOAD_FILE -> Bindings.createBooleanBinding(
281281
() -> linkedFile.getFile().isOnlineLink(),

src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java

+23-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.jabref.model.entry.BibEntry;
2424
import org.jabref.model.entry.LinkedFile;
2525
import org.jabref.preferences.FilePreferences;
26-
import org.jabref.testutils.category.FetcherTest;
2726

2827
import org.junit.jupiter.api.Assertions;
2928
import org.junit.jupiter.api.BeforeEach;
@@ -152,12 +151,11 @@ void deleteWhenDialogCancelledReturnsFalseAndDoesNotRemoveFile() {
152151
assertTrue(Files.exists(tempFile));
153152
}
154153

155-
@FetcherTest
156154
void downloadDoesNotOverwriteFileTypeExtension() throws MalformedURLException {
157155
linkedFile = new LinkedFile(new URL("http://arxiv.org/pdf/1207.0408v1"), "");
158156

159157
databaseContext = mock(BibDatabaseContext.class);
160-
when(filePreferences.getFileNamePattern()).thenReturn("[citationkey]"); // use this variant, as we cannot mock the linkedFileHandler cause it's initialized inside the viewModel
158+
when(filePreferences.getFileNamePattern()).thenReturn("[citationkey]");
161159

162160
LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, new CurrentThreadTaskExecutor(), dialogService, xmpPreferences, filePreferences, externalFileType);
163161

@@ -170,4 +168,26 @@ void downloadDoesNotOverwriteFileTypeExtension() throws MalformedURLException {
170168
task.onFailure(Assertions::fail);
171169
new CurrentThreadTaskExecutor().execute(task);
172170
}
171+
172+
@Test
173+
void isNotSamePath() {
174+
linkedFile = new LinkedFile("desc", tempFile, "pdf");
175+
databaseContext = mock(BibDatabaseContext.class);
176+
when(filePreferences.getFileNamePattern()).thenReturn("[citationkey]");
177+
when(databaseContext.getFirstExistingFileDir(filePreferences)).thenReturn(Optional.of(Path.of("/home")));
178+
179+
LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, xmpPreferences, filePreferences, externalFileType);
180+
assertFalse(viewModel.isGeneratedPathSameAsOriginal());
181+
}
182+
183+
@Test
184+
void isSamePath() {
185+
linkedFile = new LinkedFile("desc", tempFile, "pdf");
186+
databaseContext = mock(BibDatabaseContext.class);
187+
when(filePreferences.getFileNamePattern()).thenReturn("[citationkey]");
188+
when(databaseContext.getFirstExistingFileDir(filePreferences)).thenReturn(Optional.of(tempFile.getParent()));
189+
190+
LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, xmpPreferences, filePreferences, externalFileType);
191+
assertTrue(viewModel.isGeneratedPathSameAsOriginal());
192+
}
173193
}

0 commit comments

Comments
 (0)