From fb482586a9f53009b3d6f18b9a760af9367775d2 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Mon, 12 Dec 2022 23:28:46 +0100 Subject: [PATCH 1/2] Update database context in state manager after loading Fixes #9440 --- src/main/java/org/jabref/gui/LibraryTab.java | 23 ++++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/jabref/gui/LibraryTab.java b/src/main/java/org/jabref/gui/LibraryTab.java index 8357479185d..cc135213fad 100644 --- a/src/main/java/org/jabref/gui/LibraryTab.java +++ b/src/main/java/org/jabref/gui/LibraryTab.java @@ -235,22 +235,31 @@ public void onDatabaseLoadingFailed(Exception ex) { dialogService.showErrorDialogAndWait(title, content, ex); } - public void feedData(BibDatabaseContext bibDatabaseContext) { + public void feedData(BibDatabaseContext bibDatabaseContextFromParserResult) { cleanUp(); - this.bibDatabaseContext = Objects.requireNonNull(bibDatabaseContext); + // this.bibDatabaseContext.getDatabase().insertEntries(bibDatabaseContextFromParserResult.getEntries()); + // When you open an existing library, a library tab with a loading animation is added immediately. // At that point, the library tab is given a temporary bibDatabaseContext with no entries. // This line is necessary because, while there is already a binding that updates the active database when a new tab is added, // it doesn't handle the case when a library is loaded asynchronously. - stateManager.setActiveDatabase(bibDatabaseContext); + stateManager.setActiveDatabase(bibDatabaseContextFromParserResult); - bibDatabaseContext.getDatabase().registerListener(this); - bibDatabaseContext.getMetaData().registerListener(this); + Optional x = stateManager.getOpenDatabases().stream().filter(ctx -> ctx.equals(this.bibDatabaseContext)).findFirst(); + + x.ifPresent(s -> stateManager.getOpenDatabases().remove(s)); + + this.bibDatabaseContext = Objects.requireNonNull(bibDatabaseContextFromParserResult); + + stateManager.getOpenDatabases().add(bibDatabaseContextFromParserResult); + + bibDatabaseContextFromParserResult.getDatabase().registerListener(this); + bibDatabaseContextFromParserResult.getMetaData().registerListener(this); this.tableModel = new MainTableDataModel(getBibDatabaseContext(), preferencesService, stateManager); - citationStyleCache = new CitationStyleCache(bibDatabaseContext); - annotationCache = new FileAnnotationCache(bibDatabaseContext, preferencesService.getFilePreferences()); + citationStyleCache = new CitationStyleCache(bibDatabaseContextFromParserResult); + annotationCache = new FileAnnotationCache(bibDatabaseContextFromParserResult, preferencesService.getFilePreferences()); setupMainPanel(); setupAutoCompletion(); From dee188050d3d6d0d2e7e28ad0d5bb252315b875b Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Tue, 13 Dec 2022 21:06:14 +0100 Subject: [PATCH 2/2] rename variables and add conmment --- src/main/java/org/jabref/gui/LibraryTab.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/jabref/gui/LibraryTab.java b/src/main/java/org/jabref/gui/LibraryTab.java index cc135213fad..960a3b8ae5b 100644 --- a/src/main/java/org/jabref/gui/LibraryTab.java +++ b/src/main/java/org/jabref/gui/LibraryTab.java @@ -238,17 +238,15 @@ public void onDatabaseLoadingFailed(Exception ex) { public void feedData(BibDatabaseContext bibDatabaseContextFromParserResult) { cleanUp(); - // this.bibDatabaseContext.getDatabase().insertEntries(bibDatabaseContextFromParserResult.getEntries()); - // When you open an existing library, a library tab with a loading animation is added immediately. // At that point, the library tab is given a temporary bibDatabaseContext with no entries. // This line is necessary because, while there is already a binding that updates the active database when a new tab is added, // it doesn't handle the case when a library is loaded asynchronously. stateManager.setActiveDatabase(bibDatabaseContextFromParserResult); - Optional x = stateManager.getOpenDatabases().stream().filter(ctx -> ctx.equals(this.bibDatabaseContext)).findFirst(); - - x.ifPresent(s -> stateManager.getOpenDatabases().remove(s)); + // Remove existing dummy BibDatabaseContext and add correct BibDatabaseContext from ParserResult to trigger changes in the openDatabases list in the stateManager + Optional foundExistingBibDatabase = stateManager.getOpenDatabases().stream().filter(databaseContext -> databaseContext.equals(this.bibDatabaseContext)).findFirst(); + foundExistingBibDatabase.ifPresent(databaseContext -> stateManager.getOpenDatabases().remove(databaseContext)); this.bibDatabaseContext = Objects.requireNonNull(bibDatabaseContextFromParserResult);