diff --git a/src/main/java/org/jabref/gui/JabRefFrame.java b/src/main/java/org/jabref/gui/JabRefFrame.java index 9da1e8fd77f..08a03176ec2 100644 --- a/src/main/java/org/jabref/gui/JabRefFrame.java +++ b/src/main/java/org/jabref/gui/JabRefFrame.java @@ -659,6 +659,15 @@ public void init() { Platform.runLater(() -> stateManager.focusOwnerProperty().bind( EasyBind.map(mainStage.getScene().focusOwnerProperty(), Optional::ofNullable))); + 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 * 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..8357479185d 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) { @@ -247,6 +239,11 @@ 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); 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()); 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) {