Skip to content

Commit fe21d9b

Browse files
committed
Refactor NewEntryAction
1 parent 6ec77c9 commit fe21d9b

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

src/main/java/org/jabref/gui/entrytype/EntryTypeViewModel.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,10 @@ public void runFetcherWorker() {
200200
if (addEntryFlag) {
201201
new NewEntryAction(
202202
StandardEntryType.Article,
203+
() -> libraryTab,
203204
dialogService,
204205
preferences,
205-
stateManager).execute(libraryTab);
206+
stateManager).execute();
206207
searchSuccesfulProperty.set(true);
207208
}
208209
}

src/main/java/org/jabref/gui/importer/GenerateEntryFromIdAction.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public void execute() {
7979

8080
if (dialogService.showConfirmationDialogAndWait(Localization.lang("Failed to import by ID"), msg, Localization.lang("Add entry manually"))) {
8181
// add entry manually
82-
new NewEntryAction(StandardEntryType.Article, dialogService, preferences, stateManager)
83-
.execute(libraryTab);
82+
new NewEntryAction(StandardEntryType.Article, () -> libraryTab, dialogService, preferences, stateManager)
83+
.execute();
8484
}
8585
});
8686

src/main/java/org/jabref/gui/importer/NewEntryAction.java

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.jabref.gui.importer;
22

33
import java.util.Optional;
4+
import java.util.function.Supplier;
45

56
import org.jabref.gui.DialogService;
67
import org.jabref.gui.LibraryTab;
@@ -16,39 +17,35 @@ public class NewEntryAction extends SimpleCommand {
1617
/**
1718
* The type of the entry to create.
1819
*/
19-
private Optional<EntryType> type;
20-
20+
private final Optional<EntryType> type;
21+
private final Supplier<LibraryTab> tabSupplier;
2122
private final DialogService dialogService;
22-
2323
private final GuiPreferences preferences;
24-
private final StateManager stateManager;
2524

26-
public NewEntryAction(DialogService dialogService, GuiPreferences preferences, StateManager stateManager) {
25+
public NewEntryAction(EntryType type, Supplier<LibraryTab> tabSupplier, DialogService dialogService, GuiPreferences preferences, StateManager stateManager) {
26+
this.type = Optional.ofNullable(type);
27+
this.tabSupplier = tabSupplier;
2728
this.dialogService = dialogService;
2829
this.preferences = preferences;
29-
this.stateManager = stateManager;
30-
31-
this.type = Optional.empty();
3230

3331
this.executable.bind(ActionHelper.needsDatabase(stateManager));
3432
}
3533

3634
public NewEntryAction(EntryType type, DialogService dialogService, GuiPreferences preferences, StateManager stateManager) {
37-
this(dialogService, preferences, stateManager);
38-
this.type = Optional.ofNullable(type);
35+
this(type, () -> stateManager.activeTabProperty().get().get(), dialogService, preferences, stateManager);
36+
}
37+
38+
public NewEntryAction(DialogService dialogService, GuiPreferences preferences, StateManager stateManager) {
39+
this(null, dialogService, preferences, stateManager);
3940
}
4041

4142
@Override
4243
public void execute() {
43-
Optional<LibraryTab> activeTab = stateManager.activeTabProperty().get();
44-
if (activeTab.isEmpty()) {
44+
LibraryTab libraryTab = tabSupplier.get();
45+
if (libraryTab == null) {
4546
return;
4647
}
4748

48-
execute(activeTab.get());
49-
}
50-
51-
public void execute(LibraryTab libraryTab) {
5249
if (type.isPresent()) {
5350
libraryTab.insertEntry(new BibEntry(type.get()));
5451
} else {

src/test/java/org/jabref/gui/importer/NewEntryActionTest.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.jabref.gui.importer;
22

3+
import java.util.Optional;
4+
35
import org.jabref.gui.DialogService;
46
import org.jabref.gui.LibraryTab;
57
import org.jabref.gui.StateManager;
@@ -14,6 +16,7 @@
1416
import static org.mockito.Mockito.spy;
1517
import static org.mockito.Mockito.times;
1618
import static org.mockito.Mockito.verify;
19+
import static org.mockito.Mockito.when;
1720

1821
class NewEntryActionTest {
1922

@@ -25,9 +28,10 @@ class NewEntryActionTest {
2528
@Test
2629
void executeOnSuccessWithFixedType() {
2730
EntryType type = StandardEntryType.Article;
31+
when(stateManager.activeTabProperty().get()).thenReturn(Optional.ofNullable(libraryTab));
2832
NewEntryAction newEntryAction = new NewEntryAction(type, dialogService, preferences, stateManager);
2933

30-
newEntryAction.execute(libraryTab);
34+
newEntryAction.execute();
3135
verify(libraryTab, times(1)).insertEntry(new BibEntry(type));
3236
}
3337
}

0 commit comments

Comments
 (0)