diff --git a/CHANGELOG.md b/CHANGELOG.md index 480401772903..515dee362c54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -200,6 +200,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv - We fixed missing bibliography paragraph styles blocking JStyle citation insertion in LibreOffice/OpenOffice when bibliography synchronization is disabled. [#17673](https://github.com/JabRef/jabref/pull/17673) - We fixed LibreOffice/OpenOffice citation updates modifying citations marked for deletion through Track Changes after change recording was disabled. [#17673](https://github.com/JabRef/jabref/pull/17673) - We fixed edits to selected JStyle files not taking effect when inserting or refreshing citations in LibreOffice/OpenOffice. [#17673](https://github.com/JabRef/jabref/pull/17673) +- We fixed an issue where the generate citation keys toolbar button stopped working after canceling an overwrite. [#17748](https://github.com/JabRef/jabref/issues/17748) ### Removed diff --git a/jabgui/src/main/java/org/jabref/gui/citationkeypattern/GenerateCitationKeyAction.java b/jabgui/src/main/java/org/jabref/gui/citationkeypattern/GenerateCitationKeyAction.java index bd7a18c62c86..600def5565c1 100644 --- a/jabgui/src/main/java/org/jabref/gui/citationkeypattern/GenerateCitationKeyAction.java +++ b/jabgui/src/main/java/org/jabref/gui/citationkeypattern/GenerateCitationKeyAction.java @@ -31,9 +31,6 @@ public class GenerateCitationKeyAction extends SimpleCommand { private final DialogService dialogService; private final StateManager stateManager; - private List entries; - private boolean isCanceled; - private final TaskExecutor taskExecutor; private final CliPreferences preferences; @@ -53,11 +50,11 @@ public GenerateCitationKeyAction(Supplier tabSupplier, @Override public void execute() { - entries = new ArrayList<>(stateManager.getSelectedEntries()); - stateManager.getActiveDatabase().ifPresent(this::generateKeysIn); + List entries = new ArrayList<>(stateManager.getSelectedEntries()); + stateManager.getActiveDatabase().ifPresent(databaseContext -> generateKeysIn(databaseContext, entries)); } - private void generateKeysIn(BibDatabaseContext databaseContext) { + private void generateKeysIn(BibDatabaseContext databaseContext, List entries) { if (entries.isEmpty()) { dialogService.showWarningDialogAndWait(Localization.lang("Autogenerate citation keys"), Localization.lang("First select the entries you want keys to be generated for.")); @@ -65,16 +62,16 @@ private void generateKeysIn(BibDatabaseContext databaseContext) { } dialogService.notify(formatOutputMessage(Localization.lang("Generating citation key for"), entries.size())); - checkOverwriteKeysChosen(); + if (hasUserCanceledOverwrite(entries)) { + return; + } - if (!this.isCanceled) { - BackgroundTask backgroundTask = this.generateKeysInBackground(databaseContext); - backgroundTask.showToUser(true); - backgroundTask.titleProperty().set(Localization.lang("Autogenerate citation keys")); - backgroundTask.messageProperty().set(Localization.lang("%0/%1 entries", 0, entries.size())); + BackgroundTask backgroundTask = this.generateKeysInBackground(databaseContext, entries); + backgroundTask.showToUser(true); + backgroundTask.titleProperty().set(Localization.lang("Autogenerate citation keys")); + backgroundTask.messageProperty().set(Localization.lang("%0/%1 entries", 0, entries.size())); - backgroundTask.executeWith(this.taskExecutor); - } + backgroundTask.executeWith(this.taskExecutor); } public static boolean confirmOverwriteKeys(DialogService dialogService, CliPreferences preferences) { @@ -92,22 +89,22 @@ public static boolean confirmOverwriteKeys(DialogService dialogService, CliPrefe } } - private void checkOverwriteKeysChosen() { + private boolean hasUserCanceledOverwrite(List entries) { // We don't want to generate keys for entries which already have one thus remove the entries if (this.preferences.getCitationKeyPatternPreferences().shouldAvoidOverwriteCiteKey()) { entries.removeIf(BibEntry::hasCitationKey); + return false; // if we're going to override some citation keys warn the user about it } else if (entries.parallelStream().anyMatch(BibEntry::hasCitationKey)) { boolean overwriteKeys = confirmOverwriteKeys(dialogService, this.preferences); // The user doesn't want to override citation keys - if (!overwriteKeys) { - isCanceled = true; - } + return !overwriteKeys; } + return false; } - private BackgroundTask generateKeysInBackground(BibDatabaseContext databaseContext) { + private BackgroundTask generateKeysInBackground(BibDatabaseContext databaseContext, List entries) { UndoManager undoManager = stateManager.getUndoManager(databaseContext); CompoundEdit compound = new CompoundEdit(StandardActions.GENERATE_CITE_KEYS.getText()); // The keys are written entry by entry in call(), which is also where they are handed over @@ -129,9 +126,6 @@ public Void call() { } private Void generateKeys() { - if (isCanceled) { - return null; - } UiTaskExecutor.runInJavaFXThread(() -> { updateProgress(0, entries.size()); messageProperty().set(Localization.lang("%0/%1 entries", 0, entries.size())); diff --git a/jabgui/src/test/java/org/jabref/gui/citationkeypattern/GenerateCitationKeyActionTest.java b/jabgui/src/test/java/org/jabref/gui/citationkeypattern/GenerateCitationKeyActionTest.java index a0413ac6e7a5..4c864f5e4737 100644 --- a/jabgui/src/test/java/org/jabref/gui/citationkeypattern/GenerateCitationKeyActionTest.java +++ b/jabgui/src/test/java/org/jabref/gui/citationkeypattern/GenerateCitationKeyActionTest.java @@ -1,7 +1,10 @@ package org.jabref.gui.citationkeypattern; +import java.util.ArrayList; import java.util.List; import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Future; import javafx.beans.property.SimpleObjectProperty; import javafx.collections.FXCollections; @@ -13,7 +16,9 @@ import org.jabref.logic.citationkeypattern.CitationKeyPatternPreferences; import org.jabref.logic.citationkeypattern.GlobalCitationKeyPatterns; import org.jabref.logic.preferences.CliPreferences; +import org.jabref.logic.util.BackgroundTask; import org.jabref.logic.util.CurrentThreadTaskExecutor; +import org.jabref.logic.util.TaskExecutor; import org.jabref.model.database.BibDatabase; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; @@ -105,4 +110,73 @@ void regeneratingAfterCollidingEntryIsRemovedKeepsKeyWhenOverwriteIsAvoided() { verify(dialogService, never()).showConfirmationDialogWithOptOutAndWait(anyString(), anyString(), anyString(), anyString(), anyString(), any()); assertEquals(Optional.of("Wang2020a"), remainingWang.getCitationKey()); } + + @Test + void executeGeneratesKeyAfterPreviousExecutionWasCanceled() { + useKeyPreferences(false); + when(dialogService.showConfirmationDialogWithOptOutAndWait(anyString(), anyString(), anyString(), anyString(), anyString(), any())) + .thenReturn(false); + + GenerateCitationKeyAction action = new GenerateCitationKeyAction( + () -> null, dialogService, stateManager, new CurrentThreadTaskExecutor(), preferences); + + JavaFxExtension.invokeAndWait(action::execute); + JavaFxExtension.awaitEvents(); + + assertEquals(Optional.of("Wang2020a"), remainingWang.getCitationKey()); + + BibEntry entryWithoutKey = new BibEntry() + .withField(StandardField.AUTHOR, "Smith, Jane") + .withField(StandardField.YEAR, "2026"); + stateManager.getActiveDatabase().get().getDatabase().insertEntry(entryWithoutKey); + when(stateManager.getSelectedEntries()).thenReturn(FXCollections.observableArrayList(entryWithoutKey)); + + JavaFxExtension.invokeAndWait(action::execute); + JavaFxExtension.awaitEvents(); + + assertEquals(Optional.of("Smith2026"), entryWithoutKey.getCitationKey()); + } + + @Test + void delayedTaskGeneratesKeyWhenSubsequentExecutionIsCanceled() { + useKeyPreferences(false); + when(dialogService.showConfirmationDialogWithOptOutAndWait(anyString(), anyString(), anyString(), anyString(), anyString(), any())) + .thenReturn(false); + + List queuedTasks = new ArrayList<>(); + TaskExecutor delayingTaskExecutor = new CurrentThreadTaskExecutor() { + @Override + public Future execute(BackgroundTask task) { + queuedTasks.add(() -> super.execute(task)); + return CompletableFuture.completedFuture(null); + } + }; + + GenerateCitationKeyAction action = new GenerateCitationKeyAction( + () -> null, dialogService, stateManager, delayingTaskExecutor, preferences); + + BibEntry entryWithoutKey = new BibEntry() + .withField(StandardField.AUTHOR, "Smith, Jane") + .withField(StandardField.YEAR, "2026"); + stateManager.getActiveDatabase().get().getDatabase().insertEntry(entryWithoutKey); + when(stateManager.getSelectedEntries()).thenReturn(FXCollections.observableArrayList(entryWithoutKey)); + + JavaFxExtension.invokeAndWait(action::execute); + JavaFxExtension.awaitEvents(); + + assertEquals(Optional.empty(), entryWithoutKey.getCitationKey()); + + when(stateManager.getSelectedEntries()).thenReturn(FXCollections.observableArrayList(remainingWang)); + + JavaFxExtension.invokeAndWait(action::execute); + JavaFxExtension.awaitEvents(); + + for (Runnable task : queuedTasks) { + JavaFxExtension.invokeAndWait(task); + } + JavaFxExtension.awaitEvents(); + + assertEquals(Optional.of("Smith2026"), entryWithoutKey.getCitationKey()); + assertEquals(Optional.of("Wang2020a"), remainingWang.getCitationKey()); + } }