Skip to content

Commit

Permalink
Refactored importAction and addTab methods, introduced MainToolbar an…
Browse files Browse the repository at this point in the history
…d MainMenu (#10305)

* Merged import classes

* refactor

* Removed control flow by exceptions

* Replaced boolean with enum

* Removed comments

* refactored

* Introduced LibraryTabContainer interface

* Extracted Toolbar to proper class

* Extracted MenuBar to proper class

* Move openSharedDatabase

* Expanded use of LibraryTabContainer

* Refactored overloaded addTab method

* Moved openSharedDatabaseAction again

* Fixed highlighting new panel

* Fixed merge conflicts and extracted Globals

* Fixed javadoc and extracted more Globals
  • Loading branch information
calixtus authored Sep 4, 2023
1 parent 27f1ae1 commit c951dd7
Show file tree
Hide file tree
Showing 23 changed files with 1,141 additions and 846 deletions.
626 changes: 104 additions & 522 deletions src/main/java/org/jabref/gui/JabRefFrame.java

Large diffs are not rendered by default.

67 changes: 38 additions & 29 deletions src/main/java/org/jabref/gui/JabRefGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.jabref.gui.importer.ParserResultWarningDialog;
import org.jabref.gui.importer.actions.OpenDatabaseAction;
import org.jabref.gui.keyboard.TextInputKeyBindings;
import org.jabref.gui.shared.SharedDatabaseUIManager;
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.net.ProxyRegisterer;
Expand All @@ -45,7 +44,7 @@ public class JabRefGUI {
private final PreferencesService preferencesService;
private final FileUpdateMonitor fileUpdateMonitor;

private final List<ParserResult> bibDatabases;
private final List<ParserResult> parserResults;
private final boolean isBlank;
private boolean correctedWindowPos;
private final List<ParserResult> failed = new ArrayList<>();
Expand All @@ -56,7 +55,7 @@ public JabRefGUI(Stage mainStage,
boolean isBlank,
PreferencesService preferencesService,
FileUpdateMonitor fileUpdateMonitor) {
this.bibDatabases = databases;
this.parserResults = databases;
this.isBlank = isBlank;
this.preferencesService = preferencesService;
this.fileUpdateMonitor = fileUpdateMonitor;
Expand Down Expand Up @@ -177,6 +176,9 @@ private void openWindow(Stage mainStage) {
}
}

/**
* ToDo: This should be part of JabRefFrame
*/
private void openDatabases() {
// If the option is enabled, open the last edited libraries, if any.
if (!isBlank && preferencesService.getWorkspacePreferences().shouldOpenLastEdited()) {
Expand All @@ -186,57 +188,64 @@ private void openDatabases() {
// From here on, the libraries provided by command line arguments are treated

// Remove invalid databases
List<ParserResult> invalidDatabases = bibDatabases.stream()
.filter(ParserResult::isInvalid)
.toList();
List<ParserResult> invalidDatabases = parserResults.stream()
.filter(ParserResult::isInvalid)
.toList();
failed.addAll(invalidDatabases);
bibDatabases.removeAll(invalidDatabases);
parserResults.removeAll(invalidDatabases);

// passed file (we take the first one) should be focused
Path focusedFile = bibDatabases.stream()
.findFirst()
.flatMap(ParserResult::getPath)
.orElse(preferencesService.getGuiPreferences()
Path focusedFile = parserResults.stream()
.findFirst()
.flatMap(ParserResult::getPath)
.orElse(preferencesService.getGuiPreferences()
.getLastFocusedFile())
.toAbsolutePath();
.toAbsolutePath();

// Add all bibDatabases databases to the frame:
boolean first = false;
for (ParserResult pr : bibDatabases) {
for (ParserResult parserResult : parserResults) {
// Define focused tab
if (pr.getPath().filter(path -> path.toAbsolutePath().equals(focusedFile)).isPresent()) {
if (parserResult.getPath().filter(path -> path.toAbsolutePath().equals(focusedFile)).isPresent()) {
first = true;
}

if (pr.getDatabase().isShared()) {
if (parserResult.getDatabase().isShared()) {
try {
new SharedDatabaseUIManager(mainFrame, preferencesService, fileUpdateMonitor)
.openSharedDatabaseFromParserResult(pr);
} catch (SQLException | DatabaseNotSupportedException | InvalidDBMSConnectionPropertiesException |
OpenDatabaseAction.openSharedDatabase(
parserResult,
mainFrame,
mainFrame.getDialogService(),
preferencesService,
Globals.stateManager,
Globals.entryTypesManager,
fileUpdateMonitor,
mainFrame.getUndoManager());
} catch (SQLException |
DatabaseNotSupportedException |
InvalidDBMSConnectionPropertiesException |
NotASharedDatabaseException e) {
pr.getDatabaseContext().clearDatabasePath(); // do not open the original file
pr.getDatabase().clearSharedDatabaseID();

LOGGER.error("Connection error", e);
mainFrame.getDialogService().showErrorDialogAndWait(
Localization.lang("Connection error"),
Localization.lang("A local copy will be opened."),
e);
toOpenTab.add(parserResult);
}
toOpenTab.add(pr);
} else if (pr.toOpenTab()) {
} else if (parserResult.toOpenTab()) {
// things to be appended to an opened tab should be done after opening all tabs
// add them to the list
toOpenTab.add(pr);
toOpenTab.add(parserResult);
} else {
mainFrame.addParserResult(pr, first);
mainFrame.addTab(parserResult, first);
first = false;
}
}

// finally add things to the currently opened tab
for (ParserResult pr : toOpenTab) {
mainFrame.addParserResult(pr, first);
for (ParserResult parserResult : toOpenTab) {
mainFrame.addTab(parserResult, first);
first = false;
}

Expand All @@ -250,7 +259,7 @@ private void openDatabases() {

// Display warnings, if any
int tabNumber = 0;
for (ParserResult pr : bibDatabases) {
for (ParserResult pr : parserResults) {
ParserResultWarningDialog.showParserResultWarningDialog(pr, mainFrame, tabNumber++);
}

Expand All @@ -263,8 +272,8 @@ private void openDatabases() {
// This is because importToOpen might have been used, which adds to
// loadedDatabases, but not to getBasePanelCount()

for (int i = 0; (i < bibDatabases.size()) && (i < mainFrame.getBasePanelCount()); i++) {
ParserResult pr = bibDatabases.get(i);
for (int i = 0; (i < parserResults.size()) && (i < mainFrame.getBasePanelCount()); i++) {
ParserResult pr = parserResults.get(i);
LibraryTab libraryTab = mainFrame.getLibraryTabAt(i);

OpenDatabaseAction.performPostOpenActions(libraryTab, pr);
Expand Down
57 changes: 48 additions & 9 deletions src/main/java/org/jabref/gui/LibraryTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.util.Optional;
import java.util.Random;

import javax.swing.undo.UndoManager;

import javafx.animation.PauseTransition;
import javafx.application.Platform;
import javafx.beans.property.BooleanProperty;
Expand Down Expand Up @@ -41,6 +43,7 @@
import org.jabref.gui.undo.UndoableRemoveEntries;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.gui.util.DefaultTaskExecutor;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.citationstyle.CitationStyleCache;
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.importer.util.FileFieldParser;
Expand Down Expand Up @@ -122,14 +125,16 @@ public class LibraryTab extends Tab {

public LibraryTab(BibDatabaseContext bibDatabaseContext,
JabRefFrame frame,
DialogService dialogService,
PreferencesService preferencesService,
StateManager stateManager,
FileUpdateMonitor fileUpdateMonitor,
BibEntryTypesManager entryTypesManager) {
BibEntryTypesManager entryTypesManager,
CountingUndoManager undoManager) {
this.frame = Objects.requireNonNull(frame);
this.bibDatabaseContext = Objects.requireNonNull(bibDatabaseContext);
this.undoManager = frame.getUndoManager();
this.dialogService = frame.getDialogService();
this.undoManager = undoManager;
this.dialogService = dialogService;
this.preferencesService = Objects.requireNonNull(preferencesService);
this.stateManager = Objects.requireNonNull(stateManager);
this.fileUpdateMonitor = fileUpdateMonitor;
Expand Down Expand Up @@ -291,7 +296,7 @@ public void feedData(BibDatabaseContext bibDatabaseContextFromParserResult) {
public void installAutosaveManagerAndBackupManager() {
if (isDatabaseReadyForAutoSave(bibDatabaseContext)) {
AutosaveManager autosaveManager = AutosaveManager.start(bibDatabaseContext);
autosaveManager.registerListener(new AutosaveUiManager(this, preferencesService, entryTypesManager));
autosaveManager.registerListener(new AutosaveUiManager(this, dialogService, preferencesService, entryTypesManager));
}
if (isDatabaseReadyForBackup(bibDatabaseContext) && preferencesService.getFilePreferences().shouldCreateBackup()) {
BackupManager.start(this, bibDatabaseContext, Globals.entryTypesManager, preferencesService);
Expand Down Expand Up @@ -657,7 +662,7 @@ public void updateEntryEditorIfShowing() {
*/

public synchronized void markChangedOrUnChanged() {
if (getUndoManager().hasChanged()) {
if (undoManager.hasChanged()) {
this.changedProperty.setValue(true);
} else if (changedProperty.getValue() && !nonUndoableChangeProperty.getValue()) {
this.changedProperty.setValue(false);
Expand Down Expand Up @@ -821,30 +826,64 @@ public void resetChangedProperties() {
/**
* Creates a new library tab. Contents are loaded by the {@code dataLoadingTask}. Most of the other parameters are required by {@code resetChangeMonitor()}.
*
* @param dataLoadingTask The task to execute to load the data. It is executed using {@link org.jabref.gui.Globals.TASK_EXECUTOR}.
* @param dataLoadingTask The task to execute to load the data asynchronously.
* @param file the path to the file (loaded by the dataLoadingTask)
*/
public static LibraryTab createLibraryTab(BackgroundTask<ParserResult> dataLoadingTask,
Path file,
DialogService dialogService,
PreferencesService preferencesService,
StateManager stateManager,
JabRefFrame frame,
FileUpdateMonitor fileUpdateMonitor,
BibEntryTypesManager entryTypesManager) {
BibEntryTypesManager entryTypesManager,
CountingUndoManager undoManager,
TaskExecutor taskExecutor) {
BibDatabaseContext context = new BibDatabaseContext();
context.setDatabasePath(file);

LibraryTab newTab = new LibraryTab(context, frame, preferencesService, stateManager, fileUpdateMonitor, entryTypesManager);
LibraryTab newTab = new LibraryTab(
context,
frame,
dialogService,
preferencesService,
stateManager,
fileUpdateMonitor,
entryTypesManager,
undoManager);

newTab.setDataLoadingTask(dataLoadingTask);
dataLoadingTask.onRunning(newTab::onDatabaseLoadingStarted)
.onSuccess(newTab::onDatabaseLoadingSucceed)
.onFailure(newTab::onDatabaseLoadingFailed)
.executeWith(Globals.TASK_EXECUTOR);
.executeWith(taskExecutor);

return newTab;
}

public static LibraryTab createLibraryTab(BibDatabaseContext databaseContext,
JabRefFrame frame,
DialogService dialogService,
PreferencesService preferencesService,
StateManager stateManager,
FileUpdateMonitor fileUpdateMonitor,
BibEntryTypesManager entryTypesManager,
UndoManager undoManager) {
Objects.requireNonNull(databaseContext);

LibraryTab libraryTab = new LibraryTab(
databaseContext,
frame,
dialogService,
preferencesService,
stateManager,
fileUpdateMonitor,
entryTypesManager,
(CountingUndoManager) undoManager);

return libraryTab;
}

private class GroupTreeListener {

@Subscribe
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/jabref/gui/LibraryTabContainer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.jabref.gui;

import java.util.List;

public interface LibraryTabContainer {
LibraryTab getLibraryTabAt(int i);

List<LibraryTab> getLibraryTabs();

LibraryTab getCurrentLibraryTab();

void showLibraryTab(LibraryTab libraryTab);

void addTab(LibraryTab libraryTab, boolean raisePanel);

void closeTab(LibraryTab libraryTab);

void closeCurrentTab();
}
Loading

0 comments on commit c951dd7

Please sign in to comment.