Skip to content

Commit

Permalink
Split JabRefFrame to View and ViewModel (#11180)
Browse files Browse the repository at this point in the history
* Generify closeTabs

* Introduce JabRefFrameViewModel

* Simplify close methods

* Move handleUiCommands to viewmodel

* Inject variables by constructor

* Move related classes to distinct package

* Move entrytype dialog to distinct package

* Remove unused Dialog class

* Move SendAsEMailActions to frame package

* Fix Javadoc

* Fix stylesheet location

* Fix ide warning

* Fix JavaDoc

* Reword

* Replace listener workaround with simple call

* Remove superfluous controlsfx DecorationPane and window sizes.

* Rearrange methods to match interface

* Fix updating SplitPaneDivider

* Fix annotations and javadoc

* Restore package private modifier

* Move Telemetry classes to distinct package

---------

Co-authored-by: Oliver Kopp <[email protected]>
  • Loading branch information
calixtus and koppor authored Apr 16, 2024
1 parent 54de527 commit a52d4b7
Show file tree
Hide file tree
Showing 39 changed files with 726 additions and 617 deletions.
7 changes: 0 additions & 7 deletions src/main/java/org/jabref/gui/Dialog.java

This file was deleted.

38 changes: 0 additions & 38 deletions src/main/java/org/jabref/gui/DragAndDropHelper.java

This file was deleted.

1 change: 1 addition & 0 deletions src/main/java/org/jabref/gui/Globals.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jabref.architecture.AllowedToUseAwt;
import org.jabref.gui.keyboard.KeyBindingRepository;
import org.jabref.gui.remote.CLIMessageHandler;
import org.jabref.gui.telemetry.Telemetry;
import org.jabref.gui.undo.CountingUndoManager;
import org.jabref.gui.util.DefaultFileUpdateMonitor;
import org.jabref.gui.util.DefaultTaskExecutor;
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/org/jabref/gui/JabRefGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

import org.jabref.gui.frame.JabRefFrame;
import org.jabref.gui.help.VersionWorker;
import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.keyboard.TextInputKeyBindings;
import org.jabref.gui.openoffice.OOBibBaseConnect;
import org.jabref.gui.telemetry.Telemetry;
import org.jabref.gui.theme.ThemeManager;
import org.jabref.logic.UiCommand;
import org.jabref.logic.l10n.Localization;
Expand All @@ -26,7 +28,6 @@
import org.jabref.preferences.JabRefPreferences;

import com.tobiasdiez.easybind.EasyBind;
import impl.org.controlsfx.skin.DecorationPane;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -73,7 +74,11 @@ public void start(Stage stage) {
mainStage,
dialogService,
fileUpdateMonitor,
preferencesService);
preferencesService,
Globals.stateManager,
Globals.undoManager,
Globals.entryTypesManager,
Globals.TASK_EXECUTOR);

openWindow();

Expand Down Expand Up @@ -154,12 +159,7 @@ private void openWindow() {
}
debugLogWindowState(mainStage);

// We create a decoration pane ourselves for performance reasons
// (otherwise it has to be injected later, leading to a complete redraw/relayout of the complete scene)
DecorationPane root = new DecorationPane();
root.getChildren().add(JabRefGUI.mainFrame);

Scene scene = new Scene(root, 800, 800);
Scene scene = new Scene(JabRefGUI.mainFrame);
themeManager.installCss(scene);

// Handle TextEditor key bindings
Expand All @@ -177,11 +177,15 @@ private void openWindow() {
}

public void onShowing(WindowEvent event) {
Platform.runLater(() -> mainFrame.updateDividerPosition());

// Open last edited databases
if (uiCommands.stream().noneMatch(UiCommand.BlankWorkspace.class::isInstance)
&& preferencesService.getWorkspacePreferences().shouldOpenLastEdited()) {
mainFrame.openLastEditedDatabases();
}

Telemetry.initTrackingNotification(dialogService, preferencesService.getTelemetryPreferences());
}

public void onCloseRequest(WindowEvent event) {
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/org/jabref/gui/LibraryTabContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.jabref.model.database.BibDatabaseContext;

import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

Expand All @@ -16,24 +17,19 @@ public interface LibraryTabContainer {

void showLibraryTab(LibraryTab libraryTab);

void addTab(LibraryTab libraryTab, boolean raisePanel);

void addTab(BibDatabaseContext bibDatabaseContext, boolean raisePanel);

void addTab(LibraryTab libraryTab, boolean raisePanel);

/**
* Closes a designated libraryTab
*
* @param libraryTab to be closed.
* @param tab to be closed.
* @return true if closing the tab was successful
*/
boolean closeTab(LibraryTab libraryTab);
boolean closeTab(@Nullable LibraryTab tab);

/**
* Closes the currently viewed libraryTab
*
* @return true if closing the tab was successful
*/
boolean closeCurrentTab();
boolean closeTabs(@NonNull List<LibraryTab> tabs);

/**
* Refreshes the ui after changes to the preferences
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/actions/JabRefAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import javafx.beans.binding.Bindings;

import org.jabref.gui.Telemetry;
import org.jabref.gui.keyboard.KeyBindingRepository;
import org.jabref.gui.telemetry.Telemetry;

import de.saxsys.mvvmfx.utils.commands.Command;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

import org.jabref.gui.DialogService;
import org.jabref.gui.StateManager;
import org.jabref.gui.Telemetry;
import org.jabref.gui.externalfiles.ImportHandler;
import org.jabref.gui.telemetry.Telemetry;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.importer.FetcherException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<DialogPane xmlns:fx="http://javafx.com/fxml/1" prefWidth="476.0" xmlns="http://javafx.com/javafx/8.0.171"
fx:controller="org.jabref.gui.EntryTypeView">
fx:controller="org.jabref.gui.entrytype.EntryTypeView">
<buttonTypes>
<ButtonType fx:constant="CANCEL"/>
<ButtonType fx:id="generateButton" buttonData="OK_DONE" text="%Generate"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jabref.gui;
package org.jabref.gui.entrytype;

import java.util.Collection;
import java.util.List;
Expand All @@ -17,6 +17,10 @@
import javafx.scene.layout.FlowPane;
import javafx.stage.Screen;

import org.jabref.gui.DialogService;
import org.jabref.gui.Globals;
import org.jabref.gui.LibraryTab;
import org.jabref.gui.StateManager;
import org.jabref.gui.util.BaseDialog;
import org.jabref.gui.util.ControlHelper;
import org.jabref.gui.util.IconValidationDecorator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jabref.gui;
package org.jabref.gui.entrytype;

import java.util.Optional;

Expand All @@ -14,6 +14,9 @@
import javafx.concurrent.Task;
import javafx.concurrent.Worker;

import org.jabref.gui.DialogService;
import org.jabref.gui.LibraryTab;
import org.jabref.gui.StateManager;
import org.jabref.gui.externalfiles.ImportHandler;
import org.jabref.gui.importer.NewEntryAction;
import org.jabref.gui.util.TaskExecutor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jabref.gui.menus;
package org.jabref.gui.frame;

import java.nio.file.Files;
import java.nio.file.Path;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.jabref.gui;
package org.jabref.gui.frame;

import java.io.File;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import javafx.scene.Node;
import javafx.scene.Scene;
Expand All @@ -13,8 +16,12 @@
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;

import org.jabref.gui.DragAndDropDataFormats;
import org.jabref.gui.LibraryTab;
import org.jabref.gui.StateManager;
import org.jabref.gui.importer.actions.OpenDatabaseAction;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.groups.GroupTreeNode;

Expand Down Expand Up @@ -53,7 +60,7 @@ void initDragAndDrop() {
// drag'n'drop on tabs covered dnd on tabbedPane, so dnd on tabs should contain all dnds on tabbedPane
for (Node destinationTabNode : tabPane.lookupAll(".tab")) {
destinationTabNode.setOnDragOver(tabDragEvent -> onTabDragOver(event, tabDragEvent, dndIndicator));
destinationTabNode.setOnDragExited(event1 -> tabPane.getTabs().remove(dndIndicator));
destinationTabNode.setOnDragExited(tabDragEvent -> tabPane.getTabs().remove(dndIndicator));
destinationTabNode.setOnDragDropped(tabDragEvent -> onTabDragDropped(destinationTabNode, tabDragEvent, dndIndicator));
}
event.consume();
Expand All @@ -63,12 +70,12 @@ void initDragAndDrop() {
});
}

void onTabDragDropped(Node destinationTabNode, DragEvent tabDragEvent, Tab dndIndicator) {
private void onTabDragDropped(Node destinationTabNode, DragEvent tabDragEvent, Tab dndIndicator) {
Dragboard dragboard = tabDragEvent.getDragboard();

if (DragAndDropHelper.hasBibFiles(dragboard)) {
if (hasBibFiles(dragboard)) {
tabPane.getTabs().remove(dndIndicator);
List<Path> bibFiles = DragAndDropHelper.getBibFiles(dragboard);
List<Path> bibFiles = getBibFiles(dragboard);
OpenDatabaseAction openDatabaseAction = this.openDatabaseAction.get();
openDatabaseAction.openFiles(bibFiles);
tabDragEvent.setDropCompleted(true);
Expand All @@ -83,8 +90,8 @@ void onTabDragDropped(Node destinationTabNode, DragEvent tabDragEvent, Tab dndIn
if (libraryTab.getId().equals(destinationTabNode.getId()) &&
!tabPane.getSelectionModel().getSelectedItem().equals(libraryTab)) {
LibraryTab destinationLibraryTab = (LibraryTab) libraryTab;
if (DragAndDropHelper.hasGroups(dragboard)) {
List<String> groupPathToSources = DragAndDropHelper.getGroups(dragboard);
if (hasGroups(dragboard)) {
List<String> groupPathToSources = getGroups(dragboard);

copyRootNode(destinationLibraryTab);

Expand Down Expand Up @@ -113,8 +120,8 @@ void onTabDragDropped(Node destinationTabNode, DragEvent tabDragEvent, Tab dndIn
}
}

void onTabDragOver(DragEvent event, DragEvent tabDragEvent, Tab dndIndicator) {
if (DragAndDropHelper.hasBibFiles(tabDragEvent.getDragboard()) || DragAndDropHelper.hasGroups(tabDragEvent.getDragboard())) {
private void onTabDragOver(DragEvent event, DragEvent tabDragEvent, Tab dndIndicator) {
if (hasBibFiles(tabDragEvent.getDragboard()) || hasGroups(tabDragEvent.getDragboard())) {
tabDragEvent.acceptTransferModes(TransferMode.ANY);
if (!tabPane.getTabs().contains(dndIndicator)) {
tabPane.getTabs().add(dndIndicator);
Expand All @@ -130,8 +137,8 @@ void onTabDragOver(DragEvent event, DragEvent tabDragEvent, Tab dndIndicator) {
}
}

void onSceneDragOver(DragEvent event, Tab dndIndicator) {
if (DragAndDropHelper.hasBibFiles(event.getDragboard())) {
private void onSceneDragOver(DragEvent event, Tab dndIndicator) {
if (hasBibFiles(event.getDragboard())) {
event.acceptTransferModes(TransferMode.ANY);
if (!tabPane.getTabs().contains(dndIndicator)) {
tabPane.getTabs().add(dndIndicator);
Expand All @@ -149,7 +156,7 @@ void onSceneDragOver(DragEvent event, Tab dndIndicator) {

private void onSceneDragDropped(DragEvent event, Tab dndIndicator) {
tabPane.getTabs().remove(dndIndicator);
List<Path> bibFiles = DragAndDropHelper.getBibFiles(event.getDragboard());
List<Path> bibFiles = getBibFiles(event.getDragboard());
OpenDatabaseAction openDatabaseAction = this.openDatabaseAction.get();
openDatabaseAction.openFiles(bibFiles);
event.setDropCompleted(true);
Expand Down Expand Up @@ -195,4 +202,28 @@ private void copyGroupTreeNode(LibraryTab destinationLibraryTab, GroupTreeNode p
}
}
}

private boolean hasBibFiles(Dragboard dragboard) {
return !getBibFiles(dragboard).isEmpty();
}

private List<Path> getBibFiles(Dragboard dragboard) {
if (!dragboard.hasFiles()) {
return Collections.emptyList();
} else {
return dragboard.getFiles().stream().map(File::toPath).filter(FileUtil::isBibFile).collect(Collectors.toList());
}
}

private boolean hasGroups(Dragboard dragboard) {
return !getGroups(dragboard).isEmpty();
}

private List<String> getGroups(Dragboard dragboard) {
if (!dragboard.hasContent(DragAndDropDataFormats.GROUP)) {
return Collections.emptyList();
} else {
return (List<String>) dragboard.getContent(DragAndDropDataFormats.GROUP);
}
}
}
Loading

0 comments on commit a52d4b7

Please sign in to comment.