Skip to content

Commit

Permalink
Fix update search query triggers in all open libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
LoayGhreeb committed Jul 1, 2024
1 parent e26dc17 commit d6d455c
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 151 deletions.
22 changes: 11 additions & 11 deletions src/main/java/org/jabref/gui/LibraryTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import javafx.animation.PauseTransition;
import javafx.application.Platform;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.value.ObservableBooleanValue;
import javafx.collections.ListChangeListener;
Expand Down Expand Up @@ -57,6 +59,7 @@
import org.jabref.gui.undo.UndoableInsertEntries;
import org.jabref.gui.undo.UndoableRemoveEntries;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.gui.util.OptionalObjectProperty;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.gui.util.UiTaskExecutor;
import org.jabref.logic.citationstyle.CitationStyleCache;
Expand Down Expand Up @@ -149,8 +152,8 @@ private enum PanelMode { MAIN_TABLE, MAIN_TABLE_AND_ENTRY_EDITOR }
@SuppressWarnings({"FieldCanBeLocal"})
private Subscription dividerPositionSubscription;

// the query the user searches when this BasePanel is active
private Optional<SearchQuery> currentSearchQuery = Optional.empty();
private final OptionalObjectProperty<SearchQuery> searchQueryProperty = OptionalObjectProperty.empty();
private final IntegerProperty resultSize = new SimpleIntegerProperty(0);
private ListProperty<GroupTreeNode> selectedGroups;

private Optional<DatabaseChangeMonitor> changeMonitor = Optional.empty();
Expand Down Expand Up @@ -189,7 +192,7 @@ private LibraryTab(BibDatabaseContext bibDatabaseContext,
bibDatabaseContext.getMetaData().registerListener(this);

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

citationStyleCache = new CitationStyleCache(bibDatabaseContext);
annotationCache = new FileAnnotationCache(bibDatabaseContext, preferencesService.getFilePreferences());
Expand Down Expand Up @@ -314,7 +317,7 @@ private void setDatabaseContext(BibDatabaseContext bibDatabaseContext) {

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

Expand Down Expand Up @@ -921,15 +924,12 @@ public MainTable getMainTable() {
return mainTable;
}

public Optional<SearchQuery> getCurrentSearchQuery() {
return currentSearchQuery;
public OptionalObjectProperty<SearchQuery> searchQueryProperty() {
return searchQueryProperty;
}

/**
* Set the query the user currently searches while this basepanel is active
*/
public void setCurrentSearchQuery(Optional<SearchQuery> currentSearchQuery) {
this.currentSearchQuery = currentSearchQuery;
public IntegerProperty resultSizeProperty() {
return resultSize;
}

public FileAnnotationCache getAnnotationCache() {
Expand Down
39 changes: 6 additions & 33 deletions src/main/java/org/jabref/gui/StateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import javafx.util.Pair;

import org.jabref.gui.edit.automaticfiededitor.LastAutomaticFieldEditorEdit;
import org.jabref.gui.search.SearchType;
import org.jabref.gui.sidepane.SidePaneType;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.gui.util.CustomLocalDragboard;
Expand Down Expand Up @@ -57,7 +58,7 @@ public class StateManager {
private final OptionalObjectProperty<SearchQuery> activeSearchQuery = OptionalObjectProperty.empty();
private final OptionalObjectProperty<SearchQuery> activeGlobalSearchQuery = OptionalObjectProperty.empty();
private final IntegerProperty globalSearchResultSize = new SimpleIntegerProperty(0);
private final ObservableMap<String, IntegerProperty> searchResultMap = FXCollections.observableHashMap();
private final IntegerProperty searchResultSize = new SimpleIntegerProperty(0);
private final OptionalObjectProperty<Node> focusOwner = OptionalObjectProperty.empty();
private final ObservableList<Pair<BackgroundTask<?>, Task<?>>> backgroundTasks = FXCollections.observableArrayList(task -> new Observable[] {task.getValue().progressProperty(), task.getValue().runningProperty()});
private final EasyBinding<Boolean> anyTaskRunning = EasyBind.reduce(backgroundTasks, tasks -> tasks.map(Pair::getValue).anyMatch(Task::isRunning));
Expand Down Expand Up @@ -92,28 +93,16 @@ public OptionalObjectProperty<SearchQuery> activeSearchQueryProperty() {
return activeSearchQuery;
}

public void setActiveSearchResultSize(BibDatabaseContext database, IntegerProperty resultSize) {
searchResultMap.put(database.getUid(), resultSize);
}

public IntegerProperty getSearchResultSize() {
return searchResultMap.getOrDefault(activeDatabase.getValue().orElse(new BibDatabaseContext()).getUid(), new SimpleIntegerProperty(0));
}

public OptionalObjectProperty<SearchQuery> activeGlobalSearchQueryProperty() {
return activeGlobalSearchQuery;
}

public IntegerProperty getGlobalSearchResultSize() {
return globalSearchResultSize;
public OptionalObjectProperty<SearchQuery> activeSearchQuery(SearchType type) {
return type == SearchType.NORMAL_SEARCH ? activeSearchQuery : activeGlobalSearchQuery;
}

public IntegerProperty getSearchResultSize(OptionalObjectProperty<SearchQuery> searchQueryProperty) {
if (searchQueryProperty.equals(activeSearchQuery)) {
return getSearchResultSize();
} else {
return getGlobalSearchResultSize();
}
public IntegerProperty searchResultSize(SearchType type) {
return type == SearchType.NORMAL_SEARCH ? searchResultSize : globalSearchResultSize;
}

public ObservableList<BibEntry> getSelectedEntries() {
Expand Down Expand Up @@ -150,18 +139,6 @@ public void setActiveDatabase(BibDatabaseContext database) {
}
}

public void clearSearchQuery() {
activeSearchQuery.setValue(Optional.empty());
}

public void setSearchQuery(OptionalObjectProperty<SearchQuery> searchQueryProperty, SearchQuery query) {
searchQueryProperty.setValue(Optional.of(query));
}

public void clearSearchQuery(OptionalObjectProperty<SearchQuery> searchQueryProperty) {
searchQueryProperty.setValue(Optional.empty());
}

public OptionalObjectProperty<Node> focusOwnerProperty() {
return focusOwner;
}
Expand Down Expand Up @@ -202,10 +179,6 @@ public ObjectProperty<LastAutomaticFieldEditorEdit> lastAutomaticFieldEditorEdit
return lastAutomaticFieldEditorEdit;
}

public LastAutomaticFieldEditorEdit getLastAutomaticFieldEditorEdit() {
return lastAutomaticFieldEditorEditProperty().get();
}

public void setLastAutomaticFieldEditorEdit(LastAutomaticFieldEditorEdit automaticFieldEditorEdit) {
lastAutomaticFieldEditorEditProperty().set(automaticFieldEditorEdit);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,12 @@ private List<EntryEditorTab> createTabs() {
preferencesService.getImportFormatPreferences(),
fileMonitor,
dialogService,
stateManager,
bibEntryTypesManager,
keyBindingRepository);
keyBindingRepository,
libraryTab.searchQueryProperty());
tabs.add(sourceTab);
tabs.add(new LatexCitationsTab(databaseContext, preferencesService, dialogService, directoryMonitorManager));
tabs.add(new FulltextSearchResultsTab(stateManager, preferencesService, dialogService, taskExecutor));
tabs.add(new FulltextSearchResultsTab(stateManager, preferencesService, dialogService, taskExecutor, libraryTab.searchQueryProperty()));

return tabs;
}
Expand Down
13 changes: 4 additions & 9 deletions src/main/java/org/jabref/gui/entryeditor/SourceTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import javafx.scene.input.KeyEvent;

import org.jabref.gui.DialogService;
import org.jabref.gui.StateManager;
import org.jabref.gui.actions.ActionFactory;
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.actions.StandardActions;
Expand All @@ -32,6 +31,7 @@
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableChangeType;
import org.jabref.gui.undo.UndoableFieldChange;
import org.jabref.gui.util.OptionalObjectProperty;
import org.jabref.gui.util.UiTaskExecutor;
import org.jabref.logic.bibtex.BibEntryWriter;
import org.jabref.logic.bibtex.FieldPreferences;
Expand Down Expand Up @@ -104,9 +104,9 @@ public SourceTab(BibDatabaseContext bibDatabaseContext,
ImportFormatPreferences importFormatPreferences,
FileUpdateMonitor fileMonitor,
DialogService dialogService,
StateManager stateManager,
BibEntryTypesManager entryTypesManager,
KeyBindingRepository keyBindingRepository) {
KeyBindingRepository keyBindingRepository,
OptionalObjectProperty<SearchQuery> searchQuery) {
this.mode = bibDatabaseContext.getMode();
this.setText(Localization.lang("%0 source", mode.getFormattedName()));
this.setTooltip(new Tooltip(Localization.lang("Show/edit %0 source", mode.getFormattedName())));
Expand All @@ -119,12 +119,7 @@ public SourceTab(BibDatabaseContext bibDatabaseContext,
this.entryTypesManager = entryTypesManager;
this.keyBindingRepository = keyBindingRepository;

stateManager.activeSearchQueryProperty().addListener((observable, oldValue, newValue) -> {
searchHighlightPattern = newValue.flatMap(SearchQuery::getPatternForWords);
highlightSearchPattern();
});

stateManager.activeGlobalSearchQueryProperty().addListener((observable, oldValue, newValue) -> {
searchQuery.addListener((observable, oldValue, newValue) -> {
searchHighlightPattern = newValue.flatMap(SearchQuery::getPatternForWords);
highlightSearchPattern();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
import org.jabref.gui.entryeditor.EntryEditorTab;
import org.jabref.gui.maintable.OpenExternalFileAction;
import org.jabref.gui.maintable.OpenFolderAction;
import org.jabref.gui.util.OptionalObjectProperty;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.gui.util.TooltipTextUtil;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.search.SearchQuery;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.LinkedFile;
import org.jabref.model.pdf.search.PdfSearchResults;
Expand All @@ -48,34 +50,37 @@ public class FulltextSearchResultsTab extends EntryEditorTab {
private final ActionFactory actionFactory;
private final TaskExecutor taskExecutor;
private final TextFlow content;
private final OptionalObjectProperty<SearchQuery> searchQuery;
private BibEntry entry;
private DocumentViewerView documentViewerView;

public FulltextSearchResultsTab(StateManager stateManager,
PreferencesService preferencesService,
DialogService dialogService,
TaskExecutor taskExecutor) {
TaskExecutor taskExecutor,
OptionalObjectProperty<SearchQuery> searchQuery) {
this.stateManager = stateManager;
this.preferencesService = preferencesService;
this.dialogService = dialogService;
this.actionFactory = new ActionFactory();
this.taskExecutor = taskExecutor;
this.searchQuery = searchQuery;

content = new TextFlow();
ScrollPane scrollPane = new ScrollPane(content);
scrollPane.setFitToWidth(true);
content.setPadding(new Insets(10));
setContent(scrollPane);
setText(Localization.lang("Search results"));
this.stateManager.activeSearchQueryProperty().addListener((observable, oldValue, newValue) -> bindToEntry(entry));
searchQuery.addListener((observable, oldValue, newValue) -> bindToEntry(entry));
}

@Override
public boolean shouldShow(BibEntry entry) {
return this.stateManager.activeSearchQueryProperty().isPresent().get() &&
this.stateManager.activeSearchQueryProperty().get().isPresent() &&
this.stateManager.activeSearchQueryProperty().get().get().getSearchFlags().contains(SearchRules.SearchFlags.FULLTEXT) &&
!this.stateManager.activeSearchQueryProperty().get().get().getQuery().isEmpty();
return searchQuery.isPresent().get() &&
searchQuery.get().isPresent() &&
searchQuery.get().get().getSearchFlags().contains(SearchRules.SearchFlags.FULLTEXT) &&
!searchQuery.get().get().getQuery().isEmpty();
}

@Override
Expand All @@ -87,7 +92,7 @@ protected void bindToEntry(BibEntry entry) {
documentViewerView = new DocumentViewerView();
}
this.entry = entry;
PdfSearchResults searchResults = stateManager.activeSearchQueryProperty().get().get().getRule().getFulltextResults(stateManager.activeSearchQueryProperty().get().get().getQuery(), entry);
PdfSearchResults searchResults = searchQuery.get().get().getRule().getFulltextResults(searchQuery.get().get().getQuery(), entry);

content.getChildren().clear();

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/gui/entrytype/EntryTypeView.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.jabref.gui.DialogService;
import org.jabref.gui.LibraryTab;
import org.jabref.gui.StateManager;
import org.jabref.gui.search.SearchType;
import org.jabref.gui.util.BaseDialog;
import org.jabref.gui.util.ControlHelper;
import org.jabref.gui.util.IconValidationDecorator;
Expand Down Expand Up @@ -219,7 +220,7 @@ private void focusTextField(Event event) {
private void setEntryTypeForReturnAndClose(Optional<BibEntryType> entryType) {
type = entryType.map(BibEntryType::getType).orElse(null);
viewModel.stopFetching();
this.stateManager.clearSearchQuery();
stateManager.activeSearchQuery(SearchType.NORMAL_SEARCH).set(Optional.empty());
this.close();
}

Expand Down
25 changes: 9 additions & 16 deletions src/main/java/org/jabref/gui/frame/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -330,18 +330,11 @@ private void initBindings() {
// the binding for stateManager.activeDatabaseProperty() is at org.jabref.gui.LibraryTab.onDatabaseLoadingSucceed

// Subscribe to the search
EasyBind.subscribe(stateManager.activeSearchQueryProperty(),
query -> {
if (prefs.getSearchPreferences().shouldKeepSearchString()) {
for (LibraryTab tab : getLibraryTabs()) {
tab.setCurrentSearchQuery(query);
}
} else {
if (getCurrentLibraryTab() != null) {
getCurrentLibraryTab().setCurrentSearchQuery(query);
}
}
});
EasyBind.subscribe(stateManager.activeSearchQuery(SearchType.NORMAL_SEARCH), query -> {
if (getCurrentLibraryTab() != null) {
getCurrentLibraryTab().searchQueryProperty().set(query);
}
});

// Wait for the scene to be created, otherwise focusOwnerProperty is not provided
Platform.runLater(() -> stateManager.focusOwnerProperty().bind(
Expand Down Expand Up @@ -375,12 +368,12 @@ private void initBindings() {
stateManager.setSelectedEntries(libraryTab.getSelectedEntries());

// Update active search query when switching between databases
if (prefs.getSearchPreferences().shouldKeepSearchString() && libraryTab.getCurrentSearchQuery().isEmpty() && stateManager.activeSearchQueryProperty().get().isPresent()) {
// apply search query also when opening a new library and keep search string is activated
libraryTab.setCurrentSearchQuery(stateManager.activeSearchQueryProperty().get());
if (prefs.getSearchPreferences().shouldKeepSearchString()) {
libraryTab.searchQueryProperty().set(stateManager.activeSearchQuery(SearchType.NORMAL_SEARCH).get());
} else {
stateManager.activeSearchQueryProperty().set(libraryTab.getCurrentSearchQuery());
stateManager.activeSearchQuery(SearchType.NORMAL_SEARCH).set(libraryTab.searchQueryProperty().get());
}
stateManager.searchResultSize(SearchType.NORMAL_SEARCH).bind(libraryTab.resultSizeProperty());

// Update search autocompleter with information for the correct database:
globalSearchBar.setAutoCompleter(libraryTab.getAutoCompleter());
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/org/jabref/gui/maintable/MainTableDataModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
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;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;

import org.jabref.gui.StateManager;
import org.jabref.gui.groups.GroupViewMode;
import org.jabref.gui.groups.GroupsPreferences;
import org.jabref.gui.util.BindingsHelper;
import org.jabref.gui.util.OptionalObjectProperty;
import org.jabref.logic.search.SearchQuery;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
Expand All @@ -35,7 +34,11 @@ public class MainTableDataModel {
private final NameDisplayPreferences nameDisplayPreferences;
private final BibDatabaseContext bibDatabaseContext;

public MainTableDataModel(BibDatabaseContext context, PreferencesService preferencesService, StateManager stateManager, ListProperty<GroupTreeNode> selectedGroupsProperty) {
public MainTableDataModel(BibDatabaseContext context,
PreferencesService preferencesService,
ListProperty<GroupTreeNode> selectedGroupsProperty,
OptionalObjectProperty<SearchQuery> searchQueryProperty,
IntegerProperty resultSize) {
this.groupsPreferences = preferencesService.getGroupsPreferences();
this.nameDisplayPreferences = preferencesService.getNameDisplayPreferences();
this.bibDatabaseContext = context;
Expand All @@ -49,14 +52,12 @@ public MainTableDataModel(BibDatabaseContext context, PreferencesService prefere
entriesFiltered = new FilteredList<>(entriesViewModel);
entriesFiltered.predicateProperty().bind(
EasyBind.combine(selectedGroupsProperty,
stateManager.activeSearchQueryProperty(),
searchQueryProperty,
groupsPreferences.groupViewModeProperty(),
(groups, query, groupViewMode) -> entry -> isMatched(groups, query, entry))
);
(groups, query, groupViewMode) -> entry -> isMatched(groups, query, entry)));

IntegerProperty resultSize = new SimpleIntegerProperty();
resultSize.bind(Bindings.size(entriesFiltered));
stateManager.setActiveSearchResultSize(context, resultSize);

// We need to wrap the list since otherwise sorting in the table does not work
entriesFilteredAndSorted = new SortedList<>(entriesFiltered);
}
Expand Down
Loading

0 comments on commit d6d455c

Please sign in to comment.