Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ public class GenerateCitationKeyAction extends SimpleCommand {
private final DialogService dialogService;
private final StateManager stateManager;

private List<BibEntry> entries;
private boolean isCanceled;

private final TaskExecutor taskExecutor;
private final CliPreferences preferences;

Expand All @@ -53,28 +50,28 @@ public GenerateCitationKeyAction(Supplier<LibraryTab> tabSupplier,

@Override
public void execute() {
entries = new ArrayList<>(stateManager.getSelectedEntries());
stateManager.getActiveDatabase().ifPresent(this::generateKeysIn);
List<BibEntry> entries = new ArrayList<>(stateManager.getSelectedEntries());
stateManager.getActiveDatabase().ifPresent(databaseContext -> generateKeysIn(databaseContext, entries));
}

private void generateKeysIn(BibDatabaseContext databaseContext) {
private void generateKeysIn(BibDatabaseContext databaseContext, List<BibEntry> entries) {
if (entries.isEmpty()) {
dialogService.showWarningDialogAndWait(Localization.lang("Autogenerate citation keys"),
Localization.lang("First select the entries you want keys to be generated for."));
return;
}
dialogService.notify(formatOutputMessage(Localization.lang("Generating citation key for"), entries.size()));

checkOverwriteKeysChosen();
if (hasUserCanceledOverwrite(entries)) {
return;
}

if (!this.isCanceled) {
BackgroundTask<Void> 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<Void> 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) {
Expand All @@ -92,22 +89,22 @@ public static boolean confirmOverwriteKeys(DialogService dialogService, CliPrefe
}
}

private void checkOverwriteKeysChosen() {
private boolean hasUserCanceledOverwrite(List<BibEntry> 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<Void> generateKeysInBackground(BibDatabaseContext databaseContext) {
private BackgroundTask<Void> generateKeysInBackground(BibDatabaseContext databaseContext, List<BibEntry> 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
Expand All @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Comment thread
SachinyadavAug20 marked this conversation as resolved.
Resolved
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<Runnable> queuedTasks = new ArrayList<>();
TaskExecutor delayingTaskExecutor = new CurrentThreadTaskExecutor() {
@Override
public <V> Future<V> execute(BackgroundTask<V> 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());
}
}
Loading