Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Drastically! Improve performance #4578

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 64 additions & 57 deletions src/main/java/org/jabref/JabRefGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javafx.scene.Scene;
Expand All @@ -20,6 +19,7 @@
import org.jabref.gui.importer.ParserResultWarningDialog;
import org.jabref.gui.importer.actions.OpenDatabaseAction;
import org.jabref.gui.shared.SharedDatabaseUIManager;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.logic.autosaveandbackup.BackupManager;
import org.jabref.logic.importer.OpenDatabase;
import org.jabref.logic.importer.ParserResult;
Expand Down Expand Up @@ -54,14 +54,14 @@ public JabRefGUI(Stage mainStage, List<ParserResult> argsDatabases, boolean isBl

// passed file (we take the first one) should be focused
focusedFile = argsDatabases.stream()
.findFirst()
.flatMap(ParserResult::getFile)
.map(File::getAbsolutePath)
.orElse(Globals.prefs.get(JabRefPreferences.LAST_FOCUSED));
.findFirst()
.flatMap(ParserResult::getFile)
.map(File::getAbsolutePath)
.orElse(Globals.prefs.get(JabRefPreferences.LAST_FOCUSED));

openWindow(mainStage);
new VersionWorker(Globals.BUILD_INFO.getVersion(), Globals.prefs.getVersionPreferences().getIgnoredVersion(), JabRefGUI.getMainFrame().getDialogService(), Globals.TASK_EXECUTOR)
.checkForNewVersionAsync(false);
.checkForNewVersionAsync(false);
}

private void openWindow(Stage mainStage) {
Expand All @@ -88,49 +88,6 @@ private void openWindow(Stage mainStage) {
JabRefGUI.mainFrame = new JabRefFrame(mainStage);

// Add all bibDatabases databases to the frame:
boolean first = false;
if (!bibDatabases.isEmpty()) {
for (Iterator<ParserResult> parserResultIterator = bibDatabases.iterator(); parserResultIterator.hasNext();) {
ParserResult pr = parserResultIterator.next();
// Define focused tab
if (pr.getFile().filter(path -> path.getAbsolutePath().equals(focusedFile)).isPresent()) {
first = true;
}

if (pr.isInvalid()) {
failed.add(pr);
parserResultIterator.remove();
} else if (pr.getDatabase().isShared()) {
try {
new SharedDatabaseUIManager(mainFrame).openSharedDatabaseFromParserResult(pr);
} catch (SQLException | DatabaseNotSupportedException | InvalidDBMSConnectionPropertiesException |
NotASharedDatabaseException e) {
pr.getDatabaseContext().clearDatabaseFile(); // do not open the original file
pr.getDatabase().clearSharedDatabaseID();

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

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

// If we are set to remember the window location, we also remember the maximised
// state. This needs to be set after the window has been made visible, so we
Expand Down Expand Up @@ -166,7 +123,7 @@ private void openWindow(Stage mainStage) {

for (ParserResult pr : failed) {
String message = Localization.lang("Error opening file '%0'.", pr.getFile().get().getName()) + "\n"
+ pr.getErrorMessage();
+ pr.getErrorMessage();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest of the code should also be moved to run async because it relies on opening the last opened files.


dialogService.showErrorDialogAndWait(Localization.lang("Error opening file"), message);

Expand Down Expand Up @@ -196,6 +153,45 @@ private void openWindow(Stage mainStage) {
LOGGER.debug("Finished adding panels");
}

private void addTabs(ParserResult pr) {
boolean first = false;

// Define focused tab
if (pr.getFile().filter(path -> path.getAbsolutePath().equals(focusedFile)).isPresent()) {
first = true;
}

if (pr.isInvalid()) {
failed.add(pr);
} else if (pr.getDatabase().isShared()) {
try {
new SharedDatabaseUIManager(mainFrame).openSharedDatabaseFromParserResult(pr);
} catch (SQLException | DatabaseNotSupportedException | InvalidDBMSConnectionPropertiesException |
NotASharedDatabaseException e) {
pr.getDatabaseContext().clearDatabaseFile(); // do not open the original file
pr.getDatabase().clearSharedDatabaseID();

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

// finally add things to the currently opened tab
JabRefGUI.getMainFrame().addParserResult(pr, first);

}

private void saveWindowState(Stage mainStage) {
Globals.prefs.putBoolean(JabRefPreferences.WINDOW_MAXIMISED, mainStage.isMaximized());
Globals.prefs.putDouble(JabRefPreferences.POS_X, mainStage.getX());
Expand All @@ -222,15 +218,26 @@ private void openLastEditedDatabases() {
BackupUIManager.showRestoreBackupDialog(dialogService, dbFile.toPath());
}

ParserResult parsedDatabase = OpenDatabase.loadDatabase(fileName,
Globals.prefs.getImportFormatPreferences(), Globals.getFileUpdateMonitor());
BackgroundTask.wrap(() -> {
return loadDB(fileName);

}).onSuccess(parsedDatabase -> {
if (parsedDatabase.isEmpty()) {
LOGGER.error(Localization.lang("Error opening file") + " '" + dbFile.getPath() + "'");
} else {
bibDatabases.add(parsedDatabase);
addTabs(parsedDatabase);
}

}).executeWith(Globals.TASK_EXECUTOR);

if (parsedDatabase.isEmpty()) {
LOGGER.error(Localization.lang("Error opening file") + " '" + dbFile.getPath() + "'");
} else {
bibDatabases.add(parsedDatabase);
}
}

}

private ParserResult loadDB(String fileName) {
return OpenDatabase.loadDatabase(fileName, Globals.prefs.getImportFormatPreferences(), Globals.getFileUpdateMonitor());

}

private boolean isLoaded(File fileToOpen) {
Expand Down