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

Try to relocate listener binding #9238

Merged
merged 7 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
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
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 -> {
Copy link
Member

Choose a reason for hiding this comment

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

Here, I used EasyBind#subscribe instead of binding because a bound value cannot be set, but I'm setting it in another place. See the other comment.

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);
HoussemNasri marked this conversation as resolved.
Show resolved Hide resolved

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