Skip to content

Commit

Permalink
Migrate NewEntryAction
Browse files Browse the repository at this point in the history
  • Loading branch information
calixtus committed Nov 28, 2024
1 parent e09a149 commit 83cc4b1
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/org/jabref/gui/frame/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/frame/MainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)),

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/frame/MainToolBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -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))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void execute() {
entryFromIdPopOver.hide();
dialogService.notify("%s".formatted(backgroundTask.messageProperty().get()));
});

backgroundTask.onFailure(exception -> {
String fetcherExceptionMessage = exception.getMessage();

Expand All @@ -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();
Expand Down
32 changes: 14 additions & 18 deletions src/main/java/org/jabref/gui/importer/NewEntryAction.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<LibraryTab> tabSupplier;

/**
* The type of the entry to create.
*/
Expand All @@ -30,39 +21,44 @@ public class NewEntryAction extends SimpleCommand {
private final DialogService dialogService;

private final GuiPreferences preferences;
private final StateManager stateManager;

public NewEntryAction(Supplier<LibraryTab> 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<LibraryTab> 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<LibraryTab> 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));
}
}
}
19 changes: 2 additions & 17 deletions src/test/java/org/jabref/gui/importer/NewEntryActionTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit 83cc4b1

Please sign in to comment.