Skip to content

Commit

Permalink
Refactor NewEntryAction
Browse files Browse the repository at this point in the history
  • Loading branch information
calixtus committed Nov 28, 2024
1 parent 6ec77c9 commit fe21d9b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,10 @@ public void runFetcherWorker() {
if (addEntryFlag) {
new NewEntryAction(
StandardEntryType.Article,
() -> libraryTab,
dialogService,
preferences,
stateManager).execute(libraryTab);
stateManager).execute();
searchSuccesfulProperty.set(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});

Expand Down
29 changes: 13 additions & 16 deletions src/main/java/org/jabref/gui/importer/NewEntryAction.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,39 +17,35 @@ public class NewEntryAction extends SimpleCommand {
/**
* The type of the entry to create.
*/
private Optional<EntryType> type;

private final Optional<EntryType> type;
private final Supplier<LibraryTab> 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<LibraryTab> 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<LibraryTab> 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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Expand All @@ -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));
}
}

0 comments on commit fe21d9b

Please sign in to comment.