diff --git a/src/main/java/org/jabref/gui/entrytype/EntryTypeViewModel.java b/src/main/java/org/jabref/gui/entrytype/EntryTypeViewModel.java index 327947624cd..c5f09b607e9 100644 --- a/src/main/java/org/jabref/gui/entrytype/EntryTypeViewModel.java +++ b/src/main/java/org/jabref/gui/entrytype/EntryTypeViewModel.java @@ -200,9 +200,10 @@ public void runFetcherWorker() { if (addEntryFlag) { new NewEntryAction( StandardEntryType.Article, + () -> libraryTab, dialogService, preferences, - stateManager).execute(libraryTab); + stateManager).execute(); searchSuccesfulProperty.set(true); } } diff --git a/src/main/java/org/jabref/gui/importer/GenerateEntryFromIdAction.java b/src/main/java/org/jabref/gui/importer/GenerateEntryFromIdAction.java index f3a949927c1..02376d630cb 100644 --- a/src/main/java/org/jabref/gui/importer/GenerateEntryFromIdAction.java +++ b/src/main/java/org/jabref/gui/importer/GenerateEntryFromIdAction.java @@ -79,8 +79,8 @@ public void execute() { if (dialogService.showConfirmationDialogAndWait(Localization.lang("Failed to import by ID"), msg, Localization.lang("Add entry manually"))) { // add entry manually - new NewEntryAction(StandardEntryType.Article, dialogService, preferences, stateManager) - .execute(libraryTab); + new NewEntryAction(StandardEntryType.Article, () -> libraryTab, dialogService, preferences, stateManager) + .execute(); } }); diff --git a/src/main/java/org/jabref/gui/importer/NewEntryAction.java b/src/main/java/org/jabref/gui/importer/NewEntryAction.java index 67817f1bfcd..8eb133f2a45 100644 --- a/src/main/java/org/jabref/gui/importer/NewEntryAction.java +++ b/src/main/java/org/jabref/gui/importer/NewEntryAction.java @@ -1,6 +1,7 @@ package org.jabref.gui.importer; import java.util.Optional; +import java.util.function.Supplier; import org.jabref.gui.DialogService; import org.jabref.gui.LibraryTab; @@ -16,39 +17,35 @@ public class NewEntryAction extends SimpleCommand { /** * The type of the entry to create. */ - private Optional type; - + private final Optional type; + private final Supplier tabSupplier; private final DialogService dialogService; - private final GuiPreferences preferences; - private final StateManager stateManager; - public NewEntryAction(DialogService dialogService, GuiPreferences preferences, StateManager stateManager) { + public NewEntryAction(EntryType type, Supplier tabSupplier, DialogService dialogService, GuiPreferences preferences, StateManager stateManager) { + this.type = Optional.ofNullable(type); + this.tabSupplier = tabSupplier; this.dialogService = dialogService; this.preferences = preferences; - this.stateManager = stateManager; - - this.type = Optional.empty(); this.executable.bind(ActionHelper.needsDatabase(stateManager)); } public NewEntryAction(EntryType type, DialogService dialogService, GuiPreferences preferences, StateManager stateManager) { - this(dialogService, preferences, stateManager); - this.type = Optional.ofNullable(type); + this(type, () -> stateManager.activeTabProperty().get().get(), dialogService, preferences, stateManager); + } + + public NewEntryAction(DialogService dialogService, GuiPreferences preferences, StateManager stateManager) { + this(null, dialogService, preferences, stateManager); } @Override public void execute() { - Optional activeTab = stateManager.activeTabProperty().get(); - if (activeTab.isEmpty()) { + LibraryTab libraryTab = tabSupplier.get(); + if (libraryTab == null) { return; } - execute(activeTab.get()); - } - - public void execute(LibraryTab libraryTab) { if (type.isPresent()) { libraryTab.insertEntry(new BibEntry(type.get())); } else { diff --git a/src/test/java/org/jabref/gui/importer/NewEntryActionTest.java b/src/test/java/org/jabref/gui/importer/NewEntryActionTest.java index 7ace17f6530..962ac9078ec 100644 --- a/src/test/java/org/jabref/gui/importer/NewEntryActionTest.java +++ b/src/test/java/org/jabref/gui/importer/NewEntryActionTest.java @@ -1,5 +1,7 @@ package org.jabref.gui.importer; +import java.util.Optional; + import org.jabref.gui.DialogService; import org.jabref.gui.LibraryTab; import org.jabref.gui.StateManager; @@ -14,6 +16,7 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; class NewEntryActionTest { @@ -25,9 +28,10 @@ class NewEntryActionTest { @Test void executeOnSuccessWithFixedType() { EntryType type = StandardEntryType.Article; + when(stateManager.activeTabProperty().get()).thenReturn(Optional.ofNullable(libraryTab)); NewEntryAction newEntryAction = new NewEntryAction(type, dialogService, preferences, stateManager); - newEntryAction.execute(libraryTab); + newEntryAction.execute(); verify(libraryTab, times(1)).insertEntry(new BibEntry(type)); } }