diff --git a/src/main/java/org/jabref/gui/entrytype/EntryTypeViewModel.java b/src/main/java/org/jabref/gui/entrytype/EntryTypeViewModel.java index 77fd0fad13f5..327947624cde 100644 --- a/src/main/java/org/jabref/gui/entrytype/EntryTypeViewModel.java +++ b/src/main/java/org/jabref/gui/entrytype/EntryTypeViewModel.java @@ -199,11 +199,10 @@ public void runFetcherWorker() { Localization.lang("Return to dialog")); if (addEntryFlag) { new NewEntryAction( - () -> libraryTab, StandardEntryType.Article, dialogService, preferences, - stateManager).execute(); + stateManager).execute(libraryTab); searchSuccesfulProperty.set(true); } } diff --git a/src/main/java/org/jabref/gui/frame/JabRefFrame.java b/src/main/java/org/jabref/gui/frame/JabRefFrame.java index 401753260bc8..c72d9d909f78 100644 --- a/src/main/java/org/jabref/gui/frame/JabRefFrame.java +++ b/src/main/java/org/jabref/gui/frame/JabRefFrame.java @@ -334,31 +334,31 @@ private void initKeyBindings() { globalSearchBar.openGlobalSearchDialog(); break; case NEW_ARTICLE: - new NewEntryAction(this::getCurrentLibraryTab, StandardEntryType.Article, dialogService, preferences, stateManager).execute(); + new NewEntryAction(StandardEntryType.Article, dialogService, preferences, stateManager).execute(); break; case NEW_BOOK: - new NewEntryAction(this::getCurrentLibraryTab, StandardEntryType.Book, dialogService, preferences, stateManager).execute(); + new NewEntryAction(StandardEntryType.Book, dialogService, preferences, stateManager).execute(); break; case NEW_INBOOK: - new NewEntryAction(this::getCurrentLibraryTab, StandardEntryType.InBook, dialogService, preferences, stateManager).execute(); + new NewEntryAction(StandardEntryType.InBook, dialogService, preferences, stateManager).execute(); break; case NEW_MASTERSTHESIS: - new NewEntryAction(this::getCurrentLibraryTab, StandardEntryType.MastersThesis, dialogService, preferences, stateManager).execute(); + new NewEntryAction(StandardEntryType.MastersThesis, dialogService, preferences, stateManager).execute(); break; case NEW_PHDTHESIS: - new NewEntryAction(this::getCurrentLibraryTab, StandardEntryType.PhdThesis, dialogService, preferences, stateManager).execute(); + new NewEntryAction(StandardEntryType.PhdThesis, dialogService, preferences, stateManager).execute(); break; case NEW_PROCEEDINGS: - new NewEntryAction(this::getCurrentLibraryTab, StandardEntryType.Proceedings, dialogService, preferences, stateManager).execute(); + new NewEntryAction(StandardEntryType.Proceedings, dialogService, preferences, stateManager).execute(); break; case NEW_TECHREPORT: - new NewEntryAction(this::getCurrentLibraryTab, StandardEntryType.TechReport, dialogService, preferences, stateManager).execute(); + new NewEntryAction(StandardEntryType.TechReport, dialogService, preferences, stateManager).execute(); break; case NEW_UNPUBLISHED: - new NewEntryAction(this::getCurrentLibraryTab, StandardEntryType.Unpublished, dialogService, preferences, stateManager).execute(); + new NewEntryAction(StandardEntryType.Unpublished, dialogService, preferences, stateManager).execute(); break; case NEW_INPROCEEDINGS: - new NewEntryAction(this::getCurrentLibraryTab, StandardEntryType.InProceedings, dialogService, preferences, stateManager).execute(); + new NewEntryAction(StandardEntryType.InProceedings, dialogService, preferences, stateManager).execute(); break; case PASTE: if (OS.OS_X) { // Workaround for a jdk issue that executes paste twice when using cmd+v in a TextField diff --git a/src/main/java/org/jabref/gui/frame/MainMenu.java b/src/main/java/org/jabref/gui/frame/MainMenu.java index c50d91264385..44c248e0880b 100644 --- a/src/main/java/org/jabref/gui/frame/MainMenu.java +++ b/src/main/java/org/jabref/gui/frame/MainMenu.java @@ -236,7 +236,7 @@ private void createMenu() { }); library.getItems().addAll( - factory.createMenuItem(StandardActions.NEW_ENTRY, new NewEntryAction(frame::getCurrentLibraryTab, dialogService, preferences, stateManager)), + factory.createMenuItem(StandardActions.NEW_ENTRY, new NewEntryAction(dialogService, preferences, stateManager)), factory.createMenuItem(StandardActions.NEW_ENTRY_FROM_PLAIN_TEXT, new PlainCitationParserAction(dialogService, stateManager)), factory.createMenuItem(StandardActions.DELETE_ENTRY, new EditAction(StandardActions.DELETE_ENTRY, frame::getCurrentLibraryTab, stateManager, undoManager)), diff --git a/src/main/java/org/jabref/gui/frame/MainToolBar.java b/src/main/java/org/jabref/gui/frame/MainToolBar.java index cab527e654b3..a5f722aa7baa 100644 --- a/src/main/java/org/jabref/gui/frame/MainToolBar.java +++ b/src/main/java/org/jabref/gui/frame/MainToolBar.java @@ -120,8 +120,8 @@ private void createToolBar() { rightSpacer, new HBox( - factory.createIconButton(StandardActions.NEW_ARTICLE, new NewEntryAction(frame::getCurrentLibraryTab, StandardEntryType.Article, dialogService, preferences, stateManager)), - factory.createIconButton(StandardActions.NEW_ENTRY, new NewEntryAction(frame::getCurrentLibraryTab, dialogService, preferences, stateManager)), + factory.createIconButton(StandardActions.NEW_ARTICLE, new NewEntryAction(StandardEntryType.Article, dialogService, preferences, stateManager)), + factory.createIconButton(StandardActions.NEW_ENTRY, new NewEntryAction(dialogService, preferences, stateManager)), createNewEntryFromIdButton(), factory.createIconButton(StandardActions.NEW_ENTRY_FROM_PLAIN_TEXT, new PlainCitationParserAction(dialogService, stateManager)), factory.createIconButton(StandardActions.DELETE_ENTRY, new EditAction(StandardActions.DELETE_ENTRY, frame::getCurrentLibraryTab, stateManager, undoManager))), diff --git a/src/main/java/org/jabref/gui/importer/GenerateEntryFromIdAction.java b/src/main/java/org/jabref/gui/importer/GenerateEntryFromIdAction.java index 581be1802dab..f3a949927c1f 100644 --- a/src/main/java/org/jabref/gui/importer/GenerateEntryFromIdAction.java +++ b/src/main/java/org/jabref/gui/importer/GenerateEntryFromIdAction.java @@ -62,6 +62,7 @@ public void execute() { entryFromIdPopOver.hide(); dialogService.notify("%s".formatted(backgroundTask.messageProperty().get())); }); + backgroundTask.onFailure(exception -> { String fetcherExceptionMessage = exception.getMessage(); @@ -78,10 +79,11 @@ public void execute() { if (dialogService.showConfirmationDialogAndWait(Localization.lang("Failed to import by ID"), msg, Localization.lang("Add entry manually"))) { // add entry manually - new NewEntryAction(() -> libraryTab, StandardEntryType.Article, dialogService, - preferences, stateManager).execute(); + new NewEntryAction(StandardEntryType.Article, dialogService, preferences, stateManager) + .execute(libraryTab); } }); + backgroundTask.onSuccess(result -> { if (result.isPresent()) { final BibEntry entry = result.get(); diff --git a/src/main/java/org/jabref/gui/importer/NewEntryAction.java b/src/main/java/org/jabref/gui/importer/NewEntryAction.java index 1da8291e9f20..67817f1bfcdc 100644 --- a/src/main/java/org/jabref/gui/importer/NewEntryAction.java +++ b/src/main/java/org/jabref/gui/importer/NewEntryAction.java @@ -1,7 +1,6 @@ package org.jabref.gui.importer; import java.util.Optional; -import java.util.function.Supplier; import org.jabref.gui.DialogService; import org.jabref.gui.LibraryTab; @@ -13,15 +12,7 @@ import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.types.EntryType; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - public class NewEntryAction extends SimpleCommand { - - private static final Logger LOGGER = LoggerFactory.getLogger(NewEntryAction.class); - - private final Supplier tabSupplier; - /** * The type of the entry to create. */ @@ -30,39 +21,44 @@ public class NewEntryAction extends SimpleCommand { private final DialogService dialogService; private final GuiPreferences preferences; + private final StateManager stateManager; - public NewEntryAction(Supplier tabSupplier, DialogService dialogService, GuiPreferences preferences, StateManager stateManager) { - this.tabSupplier = tabSupplier; + public NewEntryAction(DialogService dialogService, GuiPreferences preferences, StateManager stateManager) { this.dialogService = dialogService; this.preferences = preferences; + this.stateManager = stateManager; this.type = Optional.empty(); this.executable.bind(ActionHelper.needsDatabase(stateManager)); } - public NewEntryAction(Supplier tabSupplier, EntryType type, DialogService dialogService, GuiPreferences preferences, StateManager stateManager) { - this(tabSupplier, dialogService, preferences, stateManager); + public NewEntryAction(EntryType type, DialogService dialogService, GuiPreferences preferences, StateManager stateManager) { + this(dialogService, preferences, stateManager); this.type = Optional.ofNullable(type); } @Override public void execute() { - if (tabSupplier.get() == null) { - LOGGER.error("Action 'New entry' must be disabled when no database is open."); + Optional activeTab = stateManager.activeTabProperty().get(); + if (activeTab.isEmpty()) { return; } + execute(activeTab.get()); + } + + public void execute(LibraryTab libraryTab) { if (type.isPresent()) { - tabSupplier.get().insertEntry(new BibEntry(type.get())); + libraryTab.insertEntry(new BibEntry(type.get())); } else { - EntryTypeView typeChoiceDialog = new EntryTypeView(tabSupplier.get(), dialogService, preferences); + EntryTypeView typeChoiceDialog = new EntryTypeView(libraryTab, dialogService, preferences); EntryType selectedType = dialogService.showCustomDialogAndWait(typeChoiceDialog).orElse(null); if (selectedType == null) { return; } - tabSupplier.get().insertEntry(new BibEntry(selectedType)); + libraryTab.insertEntry(new BibEntry(selectedType)); } } } diff --git a/src/test/java/org/jabref/gui/importer/NewEntryActionTest.java b/src/test/java/org/jabref/gui/importer/NewEntryActionTest.java index b87771b336cc..7ace17f65307 100644 --- a/src/test/java/org/jabref/gui/importer/NewEntryActionTest.java +++ b/src/test/java/org/jabref/gui/importer/NewEntryActionTest.java @@ -1,48 +1,33 @@ package org.jabref.gui.importer; -import javafx.collections.FXCollections; - import org.jabref.gui.DialogService; import org.jabref.gui.LibraryTab; -import org.jabref.gui.LibraryTabContainer; import org.jabref.gui.StateManager; import org.jabref.gui.preferences.GuiPreferences; -import org.jabref.gui.util.OptionalObjectProperty; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.types.EntryType; import org.jabref.model.entry.types.StandardEntryType; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.mockito.Mockito.mock; 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 { - private NewEntryAction newEntryAction; private final LibraryTab libraryTab = mock(LibraryTab.class); - private final LibraryTabContainer tabContainer = mock(LibraryTabContainer.class); private final DialogService dialogService = spy(DialogService.class); private final GuiPreferences preferences = mock(GuiPreferences.class); private final StateManager stateManager = mock(StateManager.class); - @BeforeEach - void setUp() { - when(stateManager.activeDatabaseProperty()).thenReturn(OptionalObjectProperty.empty()); - newEntryAction = new NewEntryAction(() -> libraryTab, dialogService, preferences, stateManager); - } - @Test void executeOnSuccessWithFixedType() { EntryType type = StandardEntryType.Article; - newEntryAction = new NewEntryAction(() -> libraryTab, type, dialogService, preferences, stateManager); - when(tabContainer.getLibraryTabs()).thenReturn(FXCollections.observableArrayList(libraryTab)); + NewEntryAction newEntryAction = new NewEntryAction(type, dialogService, preferences, stateManager); - newEntryAction.execute(); + newEntryAction.execute(libraryTab); verify(libraryTab, times(1)).insertEntry(new BibEntry(type)); } }