Skip to content

Commit

Permalink
Migrate SaveAction
Browse files Browse the repository at this point in the history
  • Loading branch information
calixtus committed Nov 28, 2024
1 parent 4bce28a commit e09a149
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 24 deletions.
4 changes: 3 additions & 1 deletion src/main/java/org/jabref/gui/LibraryTabContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.List;

import javafx.collections.ObservableList;

import org.jabref.model.database.BibDatabaseContext;

import org.jspecify.annotations.NonNull;
Expand All @@ -10,7 +12,7 @@

@NullMarked
public interface LibraryTabContainer {
List<LibraryTab> getLibraryTabs();
ObservableList<LibraryTab> getLibraryTabs();

@Nullable
LibraryTab getCurrentLibraryTab();
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/org/jabref/gui/exporter/SaveAction.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.jabref.gui.exporter;

import java.util.function.Supplier;
import java.util.Optional;

import org.jabref.gui.DialogService;
import org.jabref.gui.LibraryTab;
Expand All @@ -20,20 +20,19 @@ public class SaveAction extends SimpleCommand {
public enum SaveMethod { SAVE, SAVE_AS, SAVE_SELECTED }

private final SaveMethod saveMethod;
private final Supplier<LibraryTab> tabSupplier;

private final DialogService dialogService;
private final GuiPreferences preferences;
private final StateManager stateManager;

public SaveAction(SaveMethod saveMethod,
Supplier<LibraryTab> tabSupplier,
DialogService dialogService,
GuiPreferences preferences,
StateManager stateManager) {
this.saveMethod = saveMethod;
this.tabSupplier = tabSupplier;
this.dialogService = dialogService;
this.preferences = preferences;
this.stateManager = stateManager;

if (saveMethod == SaveMethod.SAVE_SELECTED) {
this.executable.bind(ActionHelper.needsEntriesSelected(stateManager));
Expand All @@ -44,8 +43,13 @@ public SaveAction(SaveMethod saveMethod,

@Override
public void execute() {
Optional<LibraryTab> libraryTab = stateManager.activeTabProperty().get();
if (libraryTab.isEmpty()) {
return;
}

SaveDatabaseAction saveDatabaseAction = new SaveDatabaseAction(
tabSupplier.get(),
libraryTab.get(),
dialogService,
preferences,
Injector.instantiateModelOrService(BibEntryTypesManager.class));
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/org/jabref/gui/exporter/SaveAllAction.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jabref.gui.exporter;

import java.util.List;
import java.util.function.Supplier;
import javafx.collections.ObservableList;

import org.jabref.gui.DialogService;
import org.jabref.gui.LibraryTab;
Expand All @@ -17,12 +16,12 @@

public class SaveAllAction extends SimpleCommand {

private final Supplier<List<LibraryTab>> tabsSupplier;
private final ObservableList<LibraryTab> libraryTabs;
private final DialogService dialogService;
private final GuiPreferences preferences;

public SaveAllAction(Supplier<List<LibraryTab>> tabsSupplier, GuiPreferences preferences, DialogService dialogService, StateManager stateManager) {
this.tabsSupplier = tabsSupplier;
public SaveAllAction(ObservableList<LibraryTab> libraryTabs, GuiPreferences preferences, DialogService dialogService, StateManager stateManager) {
this.libraryTabs = libraryTabs;
this.dialogService = dialogService;
this.preferences = preferences;
this.executable.bind(needsDatabase(stateManager));
Expand All @@ -32,7 +31,7 @@ public SaveAllAction(Supplier<List<LibraryTab>> tabsSupplier, GuiPreferences pre
public void execute() {
dialogService.notify(Localization.lang("Saving all libraries..."));

for (LibraryTab libraryTab : tabsSupplier.get()) {
for (LibraryTab libraryTab : libraryTabs) {
SaveDatabaseAction saveDatabaseAction = new SaveDatabaseAction(libraryTab, dialogService, preferences, Injector.instantiateModelOrService(BibEntryTypesManager.class));
boolean saveResult = saveDatabaseAction.save();
if (!saveResult) {
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/org/jabref/gui/frame/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import javafx.beans.InvalidationListener;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.StringBinding;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.event.Event;
import javafx.geometry.Orientation;
Expand Down Expand Up @@ -454,11 +455,8 @@ private void initBindings() {
/**
* Returns a list of all LibraryTabs in this frame.
*/
public @NonNull List<LibraryTab> getLibraryTabs() {
return tabbedPane.getTabs().stream()
.filter(LibraryTab.class::isInstance)
.map(LibraryTab.class::cast)
.toList();
public @NonNull ObservableList<LibraryTab> getLibraryTabs() {
return EasyBind.map(tabbedPane.getTabs().filtered(LibraryTab.class::isInstance), LibraryTab.class::cast);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/jabref/gui/frame/MainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ private void createMenu() {
factory.createMenuItem(StandardActions.NEW_LIBRARY, new NewDatabaseAction(frame, preferences)),
factory.createMenuItem(StandardActions.OPEN_LIBRARY, openDatabaseActionSupplier.get()),
fileHistoryMenu,
factory.createMenuItem(StandardActions.SAVE_LIBRARY, new SaveAction(SaveAction.SaveMethod.SAVE, frame::getCurrentLibraryTab, dialogService, preferences, stateManager)),
factory.createMenuItem(StandardActions.SAVE_LIBRARY_AS, new SaveAction(SaveAction.SaveMethod.SAVE_AS, frame::getCurrentLibraryTab, dialogService, preferences, stateManager)),
factory.createMenuItem(StandardActions.SAVE_ALL, new SaveAllAction(frame::getLibraryTabs, preferences, dialogService, stateManager)),
factory.createMenuItem(StandardActions.SAVE_LIBRARY, new SaveAction(SaveAction.SaveMethod.SAVE, dialogService, preferences, stateManager)),
factory.createMenuItem(StandardActions.SAVE_LIBRARY_AS, new SaveAction(SaveAction.SaveMethod.SAVE_AS, dialogService, preferences, stateManager)),
factory.createMenuItem(StandardActions.SAVE_ALL, new SaveAllAction(frame.getLibraryTabs(), preferences, dialogService, stateManager)),
factory.createMenuItem(StandardActions.CLOSE_LIBRARY, new JabRefFrame.CloseDatabaseAction(frame, stateManager)),

new SeparatorMenuItem(),
Expand All @@ -168,7 +168,7 @@ private void createMenu() {
factory.createSubMenu(StandardActions.EXPORT,
factory.createMenuItem(StandardActions.EXPORT_ALL, new ExportCommand(ExportCommand.ExportMethod.EXPORT_ALL, frame::getCurrentLibraryTab, stateManager, dialogService, preferences, entryTypesManager, abbreviationRepository, taskExecutor)),
factory.createMenuItem(StandardActions.EXPORT_SELECTED, new ExportCommand(ExportCommand.ExportMethod.EXPORT_SELECTED, frame::getCurrentLibraryTab, stateManager, dialogService, preferences, entryTypesManager, abbreviationRepository, taskExecutor)),
factory.createMenuItem(StandardActions.SAVE_SELECTED_AS_PLAIN_BIBTEX, new SaveAction(SaveAction.SaveMethod.SAVE_SELECTED, frame::getCurrentLibraryTab, dialogService, preferences, stateManager))),
factory.createMenuItem(StandardActions.SAVE_SELECTED_AS_PLAIN_BIBTEX, new SaveAction(SaveAction.SaveMethod.SAVE_SELECTED, dialogService, preferences, stateManager))),

new SeparatorMenuItem(),

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/frame/MainToolBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private void createToolBar() {
new HBox(
factory.createIconButton(StandardActions.NEW_LIBRARY, new NewDatabaseAction(frame, preferences)),
factory.createIconButton(StandardActions.OPEN_LIBRARY, new OpenDatabaseAction(frame, preferences, aiService, dialogService, stateManager, fileUpdateMonitor, entryTypesManager, undoManager, clipBoardManager, taskExecutor)),
factory.createIconButton(StandardActions.SAVE_LIBRARY, new SaveAction(SaveAction.SaveMethod.SAVE, frame::getCurrentLibraryTab, dialogService, preferences, stateManager))),
factory.createIconButton(StandardActions.SAVE_LIBRARY, new SaveAction(SaveAction.SaveMethod.SAVE, dialogService, preferences, stateManager))),

leftSpacer,

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/jabref/gui/importer/NewEntryActionTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.jabref.gui.importer;

import java.util.List;
import javafx.collections.FXCollections;

import org.jabref.gui.DialogService;
import org.jabref.gui.LibraryTab;
Expand Down Expand Up @@ -40,7 +40,7 @@ void setUp() {
void executeOnSuccessWithFixedType() {
EntryType type = StandardEntryType.Article;
newEntryAction = new NewEntryAction(() -> libraryTab, type, dialogService, preferences, stateManager);
when(tabContainer.getLibraryTabs()).thenReturn(List.of(libraryTab));
when(tabContainer.getLibraryTabs()).thenReturn(FXCollections.observableArrayList(libraryTab));

newEntryAction.execute();
verify(libraryTab, times(1)).insertEntry(new BibEntry(type));
Expand Down

0 comments on commit e09a149

Please sign in to comment.