Skip to content

Commit

Permalink
Try to relocate listener binding (#9238)
Browse files Browse the repository at this point in the history
* Try to relocate listener binding

Co-authored-by: Christoph <[email protected]>
Co-authored-by: Carl Christian Snethlage <[email protected]>
Co-authored-by: Houssem Nasri <[email protected]>
Co-authored-by: ThiloteE <[email protected]>

* Update the side pane when a library is opened asynchronously

* Add a comment

* Remove weird catch statement formatting

- I must have mistakenly changed IntelliJ code style settings

Co-authored-by: Christoph <[email protected]>
Co-authored-by: Carl Christian Snethlage <[email protected]>
Co-authored-by: Houssem Nasri <[email protected]>
Co-authored-by: ThiloteE <[email protected]>
  • Loading branch information
5 people authored Oct 28, 2022
1 parent 3ca07bf commit 25b6858
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
9 changes: 9 additions & 0 deletions src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/org/jabref/gui/LibraryTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/jabref/gui/StateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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<BibDatabaseContext> openDatabases = FXCollections.observableArrayList();
private final OptionalObjectProperty<BibDatabaseContext> activeDatabase = OptionalObjectProperty.empty();
Expand Down Expand Up @@ -129,6 +132,15 @@ public Optional<BibDatabaseContext> 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<BibEntry> getEntriesInCurrentDatabase() {
return OptionalUtil.flatMap(activeDatabase.get(), BibDatabaseContext::getEntries)
.collect(Collectors.toList());
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/jabref/gui/importer/NewEntryAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<EntryType> type;

private final DialogService dialogService;

private final PreferencesService preferences;

public NewEntryAction(JabRefFrame jabRefFrame, DialogService dialogService, PreferencesService preferences, StateManager stateManager) {
Expand Down

0 comments on commit 25b6858

Please sign in to comment.