Skip to content

Commit

Permalink
Fix selecting group triggers in all open libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
LoayGhreeb committed Jun 29, 2024
1 parent 48da22e commit b0017fb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 45 deletions.
38 changes: 22 additions & 16 deletions src/main/java/org/jabref/gui/LibraryTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import javafx.animation.PauseTransition;
import javafx.application.Platform;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.value.ObservableBooleanValue;
import javafx.collections.ListChangeListener;
import javafx.event.Event;
Expand Down Expand Up @@ -85,6 +87,7 @@
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.FieldFactory;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.groups.GroupTreeNode;
import org.jabref.model.util.DirectoryMonitor;
import org.jabref.model.util.DirectoryMonitorManager;
import org.jabref.model.util.FileUpdateMonitor;
Expand Down Expand Up @@ -148,6 +151,7 @@ private enum PanelMode { MAIN_TABLE, MAIN_TABLE_AND_ENTRY_EDITOR }

// the query the user searches when this BasePanel is active
private Optional<SearchQuery> currentSearchQuery = Optional.empty();
private ListProperty<GroupTreeNode> selectedGroups;

private Optional<DatabaseChangeMonitor> changeMonitor = Optional.empty();

Expand All @@ -159,15 +163,15 @@ private enum PanelMode { MAIN_TABLE, MAIN_TABLE_AND_ENTRY_EDITOR }
private final DirectoryMonitorManager directoryMonitorManager;

private LibraryTab(BibDatabaseContext bibDatabaseContext,
LibraryTabContainer tabContainer,
DialogService dialogService,
PreferencesService preferencesService,
StateManager stateManager,
FileUpdateMonitor fileUpdateMonitor,
BibEntryTypesManager entryTypesManager,
CountingUndoManager undoManager,
ClipBoardManager clipBoardManager,
TaskExecutor taskExecutor) {
LibraryTabContainer tabContainer,
DialogService dialogService,
PreferencesService preferencesService,
StateManager stateManager,
FileUpdateMonitor fileUpdateMonitor,
BibEntryTypesManager entryTypesManager,
CountingUndoManager undoManager,
ClipBoardManager clipBoardManager,
TaskExecutor taskExecutor) {
this.tabContainer = Objects.requireNonNull(tabContainer);
this.bibDatabaseContext = Objects.requireNonNull(bibDatabaseContext);
this.undoManager = undoManager;
Expand All @@ -184,7 +188,8 @@ private LibraryTab(BibDatabaseContext bibDatabaseContext,
bibDatabaseContext.getDatabase().registerListener(this);
bibDatabaseContext.getMetaData().registerListener(this);

this.tableModel = new MainTableDataModel(getBibDatabaseContext(), preferencesService, stateManager);
this.selectedGroups = new SimpleListProperty<>(stateManager.getSelectedGroups(bibDatabaseContext));
this.tableModel = new MainTableDataModel(bibDatabaseContext, preferencesService, stateManager, selectedGroups);

citationStyleCache = new CitationStyleCache(bibDatabaseContext);
annotationCache = new FileAnnotationCache(bibDatabaseContext, preferencesService.getFilePreferences());
Expand Down Expand Up @@ -307,7 +312,9 @@ private void setDatabaseContext(BibDatabaseContext bibDatabaseContext) {
bibDatabaseContext.getDatabase().registerListener(this);
bibDatabaseContext.getMetaData().registerListener(this);

this.tableModel = new MainTableDataModel(getBibDatabaseContext(), preferencesService, stateManager);
tableModel.removeBinding();
this.selectedGroups = new SimpleListProperty<>(stateManager.getSelectedGroups(bibDatabaseContext));
this.tableModel = new MainTableDataModel(bibDatabaseContext, preferencesService, stateManager, selectedGroups);
citationStyleCache = new CitationStyleCache(bibDatabaseContext);
annotationCache = new FileAnnotationCache(bibDatabaseContext, preferencesService.getFilePreferences());

Expand Down Expand Up @@ -385,7 +392,7 @@ public void updateTabTitle(boolean isChanged) {
toolTipText.append(databasePath.toAbsolutePath());

if (databaseLocation == DatabaseLocation.SHARED) {
tabTitle.append(" \u2013 ");
tabTitle.append(" ");
addSharedDbInformation(tabTitle, bibDatabaseContext);
toolTipText.append(' ');
addSharedDbInformation(toolTipText, bibDatabaseContext);
Expand Down Expand Up @@ -870,6 +877,9 @@ private void onClosed(Event event) {
LOGGER.error("Problem when shutting down backup manager", e);
}

if (tableModel != null) {
tableModel.removeBinding();
}
// clean up the groups map
stateManager.clearSelectedGroups(bibDatabaseContext);
}
Expand Down Expand Up @@ -922,10 +932,6 @@ public void setCurrentSearchQuery(Optional<SearchQuery> currentSearchQuery) {
this.currentSearchQuery = currentSearchQuery;
}

public CitationStyleCache getCitationStyleCache() {
return citationStyleCache;
}

public FileAnnotationCache getAnnotationCache() {
return annotationCache;
}
Expand Down
25 changes: 5 additions & 20 deletions src/main/java/org/jabref/gui/StateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
import java.util.Optional;

import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyListProperty;
import javafx.beans.property.ReadOnlyListWrapper;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
Expand Down Expand Up @@ -55,7 +52,6 @@ public class StateManager {
private final ObservableList<BibDatabaseContext> openDatabases = FXCollections.observableArrayList();
private final OptionalObjectProperty<BibDatabaseContext> activeDatabase = OptionalObjectProperty.empty();
private final OptionalObjectProperty<LibraryTab> activeTab = OptionalObjectProperty.empty();
private final ReadOnlyListWrapper<GroupTreeNode> activeGroups = new ReadOnlyListWrapper<>(FXCollections.observableArrayList());
private final ObservableList<BibEntry> selectedEntries = FXCollections.observableArrayList();
private final ObservableMap<String, ObservableList<GroupTreeNode>> selectedGroups = FXCollections.observableHashMap();
private final OptionalObjectProperty<SearchQuery> activeSearchQuery = OptionalObjectProperty.empty();
Expand All @@ -69,15 +65,9 @@ public class StateManager {
private final EasyBinding<Double> tasksProgress = EasyBind.reduce(backgroundTasks, tasks -> tasks.map(Pair::getValue).filter(Task::isRunning).mapToDouble(Task::getProgress).average().orElse(1));
private final ObservableMap<String, DialogWindowState> dialogWindowStates = FXCollections.observableHashMap();
private final ObservableList<SidePaneType> visibleSidePanes = FXCollections.observableArrayList();

private final ObjectProperty<LastAutomaticFieldEditorEdit> lastAutomaticFieldEditorEdit = new SimpleObjectProperty<>();

private final ObservableList<String> searchHistory = FXCollections.observableArrayList();

public StateManager() {
activeGroups.bind(Bindings.valueAt(selectedGroups, activeDatabase.orElseOpt(null).map(BibDatabaseContext::getUid)));
}

public ObservableList<SidePaneType> getVisibleSidePaneComponents() {
return visibleSidePanes;
}
Expand Down Expand Up @@ -126,10 +116,6 @@ public IntegerProperty getSearchResultSize(OptionalObjectProperty<SearchQuery> s
}
}

public ReadOnlyListProperty<GroupTreeNode> activeGroupProperty() {
return activeGroups.getReadOnlyProperty();
}

public ObservableList<BibEntry> getSelectedEntries() {
return selectedEntries;
}
Expand All @@ -138,18 +124,17 @@ public void setSelectedEntries(List<BibEntry> newSelectedEntries) {
selectedEntries.setAll(newSelectedEntries);
}

public void setSelectedGroups(BibDatabaseContext database, List<GroupTreeNode> newSelectedGroups) {
public void setSelectedGroups(BibDatabaseContext context, List<GroupTreeNode> newSelectedGroups) {
Objects.requireNonNull(newSelectedGroups);
selectedGroups.put(database.getUid(), FXCollections.observableArrayList(newSelectedGroups));
selectedGroups.computeIfAbsent(context.getUid(), k -> FXCollections.observableArrayList()).setAll(newSelectedGroups);
}

public ObservableList<GroupTreeNode> getSelectedGroups(BibDatabaseContext context) {
ObservableList<GroupTreeNode> selectedGroupsForDatabase = selectedGroups.get(context.getUid());
return selectedGroupsForDatabase != null ? selectedGroupsForDatabase : FXCollections.observableArrayList();
return selectedGroups.computeIfAbsent(context.getUid(), k -> FXCollections.observableArrayList());
}

public void clearSelectedGroups(BibDatabaseContext database) {
selectedGroups.remove(database.getUid());
public void clearSelectedGroups(BibDatabaseContext context) {
selectedGroups.computeIfAbsent(context.getUid(), k -> FXCollections.observableArrayList()).clear();
}

public Optional<BibDatabaseContext> getActiveDatabase() {
Expand Down
8 changes: 1 addition & 7 deletions src/main/java/org/jabref/gui/groups/GroupTreeViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ public GroupTreeViewModel(StateManager stateManager, DialogService dialogService

// Set-up bindings
filterPredicate.bind(EasyBind.map(filterText, text -> group -> group.isMatchedBy(text)));

// Init
refresh();
}

private void refresh() {
Expand Down Expand Up @@ -278,11 +275,8 @@ public void editGroup(GroupNodeViewModel oldGroup) {
// we need to check the old name for duplicates. If the new group name occurs more than once, it won't matter
groupsWithSameName = databaseRootGroup.get().findChildrenSatisfying(g -> g.getName().equals(oldGroupName)).size();
}
boolean removePreviousAssignments = true;
boolean removePreviousAssignments = groupsWithSameName < 2;
// We found more than 2 groups, so we cannot simply remove old assignment
if (groupsWithSameName >= 2) {
removePreviousAssignments = false;
}

oldGroup.getGroupNode().setGroup(
group,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ListProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
Expand Down Expand Up @@ -34,7 +35,7 @@ public class MainTableDataModel {
private final NameDisplayPreferences nameDisplayPreferences;
private final BibDatabaseContext bibDatabaseContext;

public MainTableDataModel(BibDatabaseContext context, PreferencesService preferencesService, StateManager stateManager) {
public MainTableDataModel(BibDatabaseContext context, PreferencesService preferencesService, StateManager stateManager, ListProperty<GroupTreeNode> selectedGroupsProperty) {
this.groupsPreferences = preferencesService.getGroupsPreferences();
this.nameDisplayPreferences = preferencesService.getNameDisplayPreferences();
this.bibDatabaseContext = context;
Expand All @@ -47,7 +48,7 @@ public MainTableDataModel(BibDatabaseContext context, PreferencesService prefere

entriesFiltered = new FilteredList<>(entriesViewModel);
entriesFiltered.predicateProperty().bind(
EasyBind.combine(stateManager.activeGroupProperty(),
EasyBind.combine(selectedGroupsProperty,
stateManager.activeSearchQueryProperty(),
groupsPreferences.groupViewModeProperty(),
(groups, query, groupViewMode) -> entry -> isMatched(groups, query, entry))
Expand All @@ -60,6 +61,10 @@ public MainTableDataModel(BibDatabaseContext context, PreferencesService prefere
entriesFilteredAndSorted = new SortedList<>(entriesFiltered);
}

public void removeBinding() {
entriesFiltered.predicateProperty().unbind();
}

private boolean isMatched(ObservableList<GroupTreeNode> groups, Optional<SearchQuery> query, BibEntryTableViewModel entry) {
return isMatchedByGroup(groups, entry) && isMatchedBySearch(query, entry);
}
Expand Down

0 comments on commit b0017fb

Please sign in to comment.