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 2 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
18 changes: 15 additions & 3 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 Expand Up @@ -1184,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);
}
Expand Down Expand Up @@ -1226,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);
}
Expand Down Expand Up @@ -1370,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);
}
});
Expand Down
9 changes: 1 addition & 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,7 @@ public void feedData(BibDatabaseContext bibDatabaseContext) {
cleanUp();

this.bibDatabaseContext = Objects.requireNonNull(bibDatabaseContext);
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