From d040be5c5acdd86c663d88002f2caae3b080451c Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Mon, 10 Oct 2022 22:10:30 +0200 Subject: [PATCH 1/4] Try to relocate listener binding Co-authored-by: Christoph Co-authored-by: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Co-authored-by: Houssem Nasri Co-authored-by: ThiloteE <73715071+ThiloteE@users.noreply.github.com> --- src/main/java/org/jabref/gui/JabRefFrame.java | 7 +++++++ src/main/java/org/jabref/gui/LibraryTab.java | 8 -------- src/main/java/org/jabref/gui/importer/NewEntryAction.java | 3 +++ 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/jabref/gui/JabRefFrame.java b/src/main/java/org/jabref/gui/JabRefFrame.java index 9da1e8fd77f..028d283d178 100644 --- a/src/main/java/org/jabref/gui/JabRefFrame.java +++ b/src/main/java/org/jabref/gui/JabRefFrame.java @@ -659,6 +659,13 @@ public void init() { Platform.runLater(() -> stateManager.focusOwnerProperty().bind( EasyBind.map(mainStage.getScene().focusOwnerProperty(), Optional::ofNullable))); + stateManager.activeDatabaseProperty().bind( + EasyBind.map(tabbedPane.getSelectionModel().selectedItemProperty(), + selectedTab -> Optional.ofNullable(selectedTab) + .filter(tab -> tab instanceof LibraryTab) + .map(tab -> (LibraryTab) tab) + .map(LibraryTab::getBibDatabaseContext))); + /* * The following state listener makes sure focus is registered with the * correct database when the user switches tabs. Without this, diff --git a/src/main/java/org/jabref/gui/LibraryTab.java b/src/main/java/org/jabref/gui/LibraryTab.java index 607bcb474f7..1fc688d5b81 100644 --- a/src/main/java/org/jabref/gui/LibraryTab.java +++ b/src/main/java/org/jabref/gui/LibraryTab.java @@ -226,14 +226,6 @@ public void onDatabaseLoadingSucceed(ParserResult result) { LOGGER.error("Cannot access lucene index", e); } } - - // a temporary workaround to update groups pane - stateManager.activeDatabaseProperty().bind( - EasyBind.map(frame.getTabbedPane().getSelectionModel().selectedItemProperty(), - selectedTab -> Optional.ofNullable(selectedTab) - .filter(tab -> tab instanceof LibraryTab) - .map(tab -> (LibraryTab) tab) - .map(LibraryTab::getBibDatabaseContext))); } public void onDatabaseLoadingFailed(Exception ex) { diff --git a/src/main/java/org/jabref/gui/importer/NewEntryAction.java b/src/main/java/org/jabref/gui/importer/NewEntryAction.java index ef085920713..fcb25a991bc 100644 --- a/src/main/java/org/jabref/gui/importer/NewEntryAction.java +++ b/src/main/java/org/jabref/gui/importer/NewEntryAction.java @@ -24,11 +24,14 @@ public class NewEntryAction extends SimpleCommand { private static final Logger LOGGER = LoggerFactory.getLogger(NewEntryAction.class); private final JabRefFrame jabRefFrame; + /** * The type of the entry to create. */ private Optional type; + private final DialogService dialogService; + private final PreferencesService preferences; public NewEntryAction(JabRefFrame jabRefFrame, DialogService dialogService, PreferencesService preferences, StateManager stateManager) { From a8c31375c482d3f458832b1ad44d2054050e1593 Mon Sep 17 00:00:00 2001 From: Houssem Nasri Date: Tue, 11 Oct 2022 11:49:16 +0100 Subject: [PATCH 2/4] Update the side pane when a library is opened asynchronously --- src/main/java/org/jabref/gui/JabRefFrame.java | 23 +++++++++++-------- src/main/java/org/jabref/gui/LibraryTab.java | 1 + .../java/org/jabref/gui/StateManager.java | 12 ++++++++++ 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/jabref/gui/JabRefFrame.java b/src/main/java/org/jabref/gui/JabRefFrame.java index 028d283d178..4438070c8f1 100644 --- a/src/main/java/org/jabref/gui/JabRefFrame.java +++ b/src/main/java/org/jabref/gui/JabRefFrame.java @@ -659,12 +659,14 @@ public void init() { Platform.runLater(() -> stateManager.focusOwnerProperty().bind( EasyBind.map(mainStage.getScene().focusOwnerProperty(), Optional::ofNullable))); - stateManager.activeDatabaseProperty().bind( - EasyBind.map(tabbedPane.getSelectionModel().selectedItemProperty(), - selectedTab -> Optional.ofNullable(selectedTab) - .filter(tab -> tab instanceof LibraryTab) - .map(tab -> (LibraryTab) tab) - .map(LibraryTab::getBibDatabaseContext))); + EasyBind.subscribe(tabbedPane.getSelectionModel().selectedItemProperty(), selectedTab -> { + if (selectedTab instanceof LibraryTab libraryTab) { + stateManager.setActiveDatabase(libraryTab.getBibDatabaseContext()); + } else if (selectedTab == null) { + // All databases are closed + stateManager.setActiveDatabase(null); + } + }); /* * The following state listener makes sure focus is registered with the @@ -1191,7 +1193,8 @@ private boolean confirmClose(LibraryTab libraryTab) { } // The action was either canceled or unsuccessful. dialogService.notify(Localization.lang("Unable to save library")); - } catch (Throwable ex) { + } catch ( + Throwable ex) { LOGGER.error("A problem occurred when trying to save the file", ex); dialogService.showErrorDialogAndWait(Localization.lang("Save library"), Localization.lang("Could not save file."), ex); } @@ -1233,7 +1236,8 @@ private Boolean confirmEmptyEntry(LibraryTab libraryTab, BibDatabaseContext cont } // The action was either canceled or unsuccessful. dialogService.notify(Localization.lang("Unable to save library")); - } catch (Throwable ex) { + } catch ( + Throwable ex) { LOGGER.error("A problem occurred when trying to delete the empty entries", ex); dialogService.showErrorDialogAndWait(Localization.lang("Delete empty entries"), Localization.lang("Could not delete empty entries."), ex); } @@ -1377,7 +1381,8 @@ public void execute() { Optional.of(databaseContext.get()).flatMap(BibDatabaseContext::getDatabasePath).ifPresent(path -> { try { JabRefDesktop.openFolderAndSelectFile(path, prefs, dialogService); - } catch (IOException e) { + } catch ( + IOException e) { LOGGER.info("Could not open folder", e); } }); diff --git a/src/main/java/org/jabref/gui/LibraryTab.java b/src/main/java/org/jabref/gui/LibraryTab.java index 1fc688d5b81..20ecac0e659 100644 --- a/src/main/java/org/jabref/gui/LibraryTab.java +++ b/src/main/java/org/jabref/gui/LibraryTab.java @@ -239,6 +239,7 @@ public void feedData(BibDatabaseContext bibDatabaseContext) { cleanUp(); this.bibDatabaseContext = Objects.requireNonNull(bibDatabaseContext); + stateManager.setActiveDatabase(bibDatabaseContext); bibDatabaseContext.getDatabase().registerListener(this); bibDatabaseContext.getMetaData().registerListener(this); diff --git a/src/main/java/org/jabref/gui/StateManager.java b/src/main/java/org/jabref/gui/StateManager.java index 9a27649eb9c..9cab5af0169 100644 --- a/src/main/java/org/jabref/gui/StateManager.java +++ b/src/main/java/org/jabref/gui/StateManager.java @@ -34,6 +34,8 @@ import com.tobiasdiez.easybind.EasyBind; import com.tobiasdiez.easybind.EasyBinding; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * This class manages the GUI-state of JabRef, including: @@ -49,6 +51,7 @@ */ public class StateManager { + private static final Logger LOGGER = LoggerFactory.getLogger(StateManager.class); private final CustomLocalDragboard localDragboard = new CustomLocalDragboard(); private final ObservableList openDatabases = FXCollections.observableArrayList(); private final OptionalObjectProperty activeDatabase = OptionalObjectProperty.empty(); @@ -129,6 +132,15 @@ public Optional getActiveDatabase() { return activeDatabase.get(); } + public void setActiveDatabase(BibDatabaseContext database) { + if (database == null) { + LOGGER.info("No open database detected"); + activeDatabaseProperty().set(Optional.empty()); + } else { + activeDatabaseProperty().set(Optional.of(database)); + } + } + public List getEntriesInCurrentDatabase() { return OptionalUtil.flatMap(activeDatabase.get(), BibDatabaseContext::getEntries) .collect(Collectors.toList()); From e93f93210ff17d166400da1194f2891c6840604a Mon Sep 17 00:00:00 2001 From: Houssem Nasri Date: Fri, 21 Oct 2022 22:55:36 +0100 Subject: [PATCH 3/4] Add a comment --- src/main/java/org/jabref/gui/LibraryTab.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/org/jabref/gui/LibraryTab.java b/src/main/java/org/jabref/gui/LibraryTab.java index 20ecac0e659..8357479185d 100644 --- a/src/main/java/org/jabref/gui/LibraryTab.java +++ b/src/main/java/org/jabref/gui/LibraryTab.java @@ -239,6 +239,10 @@ public void feedData(BibDatabaseContext bibDatabaseContext) { cleanUp(); this.bibDatabaseContext = Objects.requireNonNull(bibDatabaseContext); + // 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); bibDatabaseContext.getDatabase().registerListener(this); From 0bcf9e877c6303a6a82eba41294c2a108268efdd Mon Sep 17 00:00:00 2001 From: Houssem Nasri Date: Fri, 21 Oct 2022 23:00:05 +0100 Subject: [PATCH 4/4] Remove weird catch statement formatting - I must have mistakenly changed IntelliJ code style settings --- src/main/java/org/jabref/gui/JabRefFrame.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/jabref/gui/JabRefFrame.java b/src/main/java/org/jabref/gui/JabRefFrame.java index 4438070c8f1..08a03176ec2 100644 --- a/src/main/java/org/jabref/gui/JabRefFrame.java +++ b/src/main/java/org/jabref/gui/JabRefFrame.java @@ -1193,8 +1193,7 @@ private boolean confirmClose(LibraryTab libraryTab) { } // The action was either canceled or unsuccessful. dialogService.notify(Localization.lang("Unable to save library")); - } catch ( - Throwable ex) { + } catch (Throwable ex) { LOGGER.error("A problem occurred when trying to save the file", ex); dialogService.showErrorDialogAndWait(Localization.lang("Save library"), Localization.lang("Could not save file."), ex); } @@ -1236,8 +1235,7 @@ private Boolean confirmEmptyEntry(LibraryTab libraryTab, BibDatabaseContext cont } // The action was either canceled or unsuccessful. dialogService.notify(Localization.lang("Unable to save library")); - } catch ( - Throwable ex) { + } catch (Throwable ex) { LOGGER.error("A problem occurred when trying to delete the empty entries", ex); dialogService.showErrorDialogAndWait(Localization.lang("Delete empty entries"), Localization.lang("Could not delete empty entries."), ex); } @@ -1381,8 +1379,7 @@ public void execute() { Optional.of(databaseContext.get()).flatMap(BibDatabaseContext::getDatabasePath).ifPresent(path -> { try { JabRefDesktop.openFolderAndSelectFile(path, prefs, dialogService); - } catch ( - IOException e) { + } catch (IOException e) { LOGGER.info("Could not open folder", e); } });