Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We fixed an issue where pressing "+" in the "File" field with an automatically found file selected opened the "Add file link" dialog instead of linking the selected file. [#16938](https://github.com/JabRef/jabref/pull/16938)
- We fixed an issue where closing a library did not free its memory, so a session that opened and closed several libraries kept all of them in memory. [#16948](https://github.com/JabRef/jabref/pull/16948)
- We fixed an issue where case-sensitive search (`=!`, `==!`, `=~!`) in linked files ignored the casing and matched text in any casing. [#13048](https://github.com/JabRef/jabref/issues/13048)
- We fixed linked-file full-text search results missing from the entry editor. [#17367](https://github.com/JabRef/jabref/pull/17367)
- We fixed an issue where accepting a backup that adds groups to a library without groups failed with an error. [#17165](https://github.com/JabRef/jabref/pull/17165)
- We fixed an issue where JabRef closed when a client of the integrated language server disconnected. [#17303](https://github.com/JabRef/jabref/pull/17303)
- We fixed menu items not showing why they are disabled when a menu was opened for the first time. [#17200](https://github.com/JabRef/jabref/pull/17200)
Expand Down
12 changes: 12 additions & 0 deletions jabgui/src/main/java/org/jabref/gui/LibraryTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ListProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleListProperty;
Expand Down Expand Up @@ -111,6 +113,7 @@
import org.jabref.model.metadata.event.MetaDataChangeSource;
import org.jabref.model.metadata.event.MetaDataChangedEvent;
import org.jabref.model.search.query.SearchQuery;
import org.jabref.model.search.query.SearchResults;
import org.jabref.model.undo.UndoableRemoveEntries;
import org.jabref.model.util.DummyFileUpdateMonitor;
import org.jabref.model.util.FileUpdateMonitor;
Expand Down Expand Up @@ -180,6 +183,7 @@ public class LibraryTab extends Tab implements CommandSelectionTab {

private ListProperty<GroupTreeNode> selectedGroupsProperty;
private final OptionalObjectProperty<SearchQuery> searchQueryProperty = OptionalObjectProperty.empty();
private final ReadOnlyObjectWrapper<Optional<SearchResults>> searchResults = new ReadOnlyObjectWrapper<>(Optional.empty());
private final IntegerProperty resultSize = new SimpleIntegerProperty(0);

private Optional<DatabaseChangeMonitor> changeMonitor = Optional.empty();
Expand Down Expand Up @@ -341,6 +345,8 @@ private void initializeComponentsAndListeners(boolean isDummyContext) {

this.selectedGroupsProperty = new SimpleListProperty<>(stateManager.getSelectedGroups(bibDatabaseContext));
this.tableModel = new MainTableDataModel(getBibDatabaseContext(), preferences, taskExecutor, getSearchContext(), selectedGroupsProperty(), searchQueryProperty, resultSizeProperty());
searchResults.unbind();
searchResults.bind(tableModel.searchResultsProperty());

new CitationStyleCache(bibDatabaseContext);
annotationCache = new FileAnnotationCache(bibDatabaseContext, preferences.getFilePreferences());
Expand Down Expand Up @@ -989,6 +995,8 @@ private void onClosed(Event event) {
tableModel.unbind();
}

searchResults.unbind();

if (autoRenameFileOnEntryChange != null) {
coarseChangeFilter.unregisterListener(autoRenameFileOnEntryChange);
}
Expand Down Expand Up @@ -1054,6 +1062,10 @@ public OptionalObjectProperty<SearchQuery> searchQueryProperty() {
return searchQueryProperty;
}

public ReadOnlyObjectProperty<Optional<SearchResults>> searchResultsProperty() {
return searchResults.getReadOnlyProperty();
}

public IntegerProperty resultSizeProperty() {
return resultSize;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javafx.beans.binding.Bindings;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
Expand All @@ -18,6 +20,7 @@
import javafx.scene.text.TextFlow;

import org.jabref.gui.DialogService;
import org.jabref.gui.LibraryTab;
import org.jabref.gui.StateManager;
import org.jabref.gui.actions.ActionFactory;
import org.jabref.gui.actions.StandardActions;
Expand All @@ -36,16 +39,22 @@
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.LinkedFile;
import org.jabref.model.search.SearchFlags;
import org.jabref.model.search.query.SearchQuery;
import org.jabref.model.search.query.SearchResult;
import org.jabref.model.search.query.SearchResults;

import org.jspecify.annotations.NullMarked;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FulltextSearchResultsTab extends EntryEditorTab {

private static final Logger LOGGER = LoggerFactory.getLogger(FulltextSearchResultsTab.class);

@NullMarked
private record SearchDisplay(BibEntry entry, SearchQuery query, SearchResults results) {
}

private final StateManager stateManager;
private final GuiPreferences preferences;
private final DialogService dialogService;
Expand All @@ -58,6 +67,13 @@ public class FulltextSearchResultsTab extends EntryEditorTab {

/// Content available only while an active, valid fulltext search query exists.
private final ObservableValue<Boolean> contentVisibility;
private final ChangeListener<Optional<SearchResults>> searchResultsListener = (_, _, _) -> updateSearch();
private final ChangeListener<Optional<SearchQuery>> searchQueryListener;
private final ChangeListener<Optional<LibraryTab>> activeTabListener = (_, previous, current) -> {
previous.ifPresent(tab -> tab.searchResultsProperty().removeListener(searchResultsListener));
current.ifPresent(tab -> tab.searchResultsProperty().addListener(searchResultsListener));
updateSearch();
};

public FulltextSearchResultsTab(StateManager stateManager,
GuiPreferences preferences,
Expand All @@ -77,14 +93,25 @@ public FulltextSearchResultsTab(StateManager stateManager,
setContentDrivenVisibility(contentVisibility);

content = new TextFlow();
searchQueryListener = (_, _, _) -> content.getChildren().clear();
ScrollPane scrollPane = new ScrollPane(content);
scrollPane.setFitToWidth(true);
content.setPadding(new Insets(10));
setContent(scrollPane);
setText(EntryEditorTabModel.BuiltIn.FULLTEXT_SEARCH_RESULTS.displayName());

// Rebinding is necessary because of re-rendering of highlighting of matched text
stateManager.activeSearchQuery(SearchType.NORMAL_SEARCH).addListener((_, _, _) -> updateSearch());
stateManager.activeTabProperty().get()
.ifPresent(tab -> tab.searchResultsProperty().addListener(searchResultsListener));
stateManager.activeTabProperty().addListener(activeTabListener);
stateManager.activeSearchQuery(SearchType.NORMAL_SEARCH).addListener(searchQueryListener);
}

@Override
protected void dispose() {
stateManager.activeSearchQuery(SearchType.NORMAL_SEARCH).removeListener(searchQueryListener);
stateManager.activeTabProperty().removeListener(activeTabListener);
stateManager.activeTabProperty().get()
.ifPresent(tab -> tab.searchResultsProperty().removeListener(searchResultsListener));
}

@Override
Expand All @@ -97,40 +124,44 @@ protected void bindToEntry(BibEntry entry) {
}

private void updateSearch() {
stateManager.activeSearchQuery(SearchType.NORMAL_SEARCH).get().ifPresent(searchQuery -> {
SearchResults searchResults = searchQuery.getSearchResults();
if (searchResults != null && entry != null) {
Map<String, List<SearchResult>> searchResultsForEntry = searchResults.getFileSearchResultsForEntry(entry);
content.getChildren().clear();
if (searchResultsForEntry.isEmpty()) {
content.getChildren().add(new Text(Localization.lang("No search matches.")));
} else {
// Iterate through files with search hits
for (Map.Entry<String, List<SearchResult>> iterator : searchResultsForEntry.entrySet()) {
entry.getFiles().stream().filter(file -> file.getLink().equals(iterator.getKey())).findFirst().ifPresent(linkedFile -> {
content.getChildren().addAll(createFileLink(linkedFile), lineSeparator());
// Iterate through pages (within file) with search hits
for (SearchResult searchResult : iterator.getValue()) {
for (String resultTextHtml : searchResult.getContentResultStringsHtml()) {
content.getChildren().addAll(TooltipTextUtil.createTextsFromHtml(resultTextHtml.replace("</b> <b>", " ")));
content.getChildren().addAll(new Text(System.lineSeparator()), lineSeparator(0.8), createPageLink(linkedFile, searchResult.getPageNumber(), searchQuery.getSearchExpression()));
}
if (!searchResult.getAnnotationsResultStringsHtml().isEmpty()) {
Text annotationsText = new Text(System.lineSeparator() + Localization.lang("Found matches in annotations:") + System.lineSeparator() + System.lineSeparator());
annotationsText.getStyleClass().add("italic");
content.getChildren().add(annotationsText);

for (String resultTextHtml : searchResult.getAnnotationsResultStringsHtml()) {
content.getChildren().addAll(TooltipTextUtil.createTextsFromHtml(resultTextHtml.replace("</b> <b>", " ")));
content.getChildren().addAll(new Text(System.lineSeparator()), lineSeparator(0.8), createPageLink(linkedFile, searchResult.getPageNumber(), searchQuery.getSearchExpression()));
}
}
content.getChildren().clear();
Optional.ofNullable(entry)
.flatMap(selectedEntry -> stateManager.activeSearchQuery(SearchType.NORMAL_SEARCH).get()
.flatMap(searchQuery -> stateManager.activeTabProperty().get()
.flatMap(tab -> tab.searchResultsProperty().get())
.map(results -> new SearchDisplay(selectedEntry, searchQuery, results))))
.ifPresent(display -> renderResults(display.entry(), display.query(), display.results()));
}

private void renderResults(BibEntry selectedEntry, SearchQuery searchQuery, SearchResults searchResults) {
Map<String, List<SearchResult>> searchResultsForEntry = searchResults.getFileSearchResultsForEntry(selectedEntry);
if (searchResultsForEntry.isEmpty()) {
content.getChildren().add(new Text(Localization.lang("No search matches.")));
} else {
// Iterate through files with search hits
for (Map.Entry<String, List<SearchResult>> iterator : searchResultsForEntry.entrySet()) {
selectedEntry.getFiles().stream().filter(file -> file.getLink().equals(iterator.getKey())).findFirst().ifPresent(linkedFile -> {
content.getChildren().addAll(createFileLink(linkedFile), lineSeparator());
// Iterate through pages (within file) with search hits
for (SearchResult searchResult : iterator.getValue()) {
for (String resultTextHtml : searchResult.getContentResultStringsHtml()) {
content.getChildren().addAll(TooltipTextUtil.createTextsFromHtml(resultTextHtml.replace("</b> <b>", " ")));
content.getChildren().addAll(new Text(System.lineSeparator()), lineSeparator(0.8), createPageLink(linkedFile, searchResult.getPageNumber(), searchQuery.getSearchExpression()));
}
if (!searchResult.getAnnotationsResultStringsHtml().isEmpty()) {
Text annotationsText = new Text(System.lineSeparator() + Localization.lang("Found matches in annotations:") + System.lineSeparator() + System.lineSeparator());
annotationsText.getStyleClass().add("italic");
content.getChildren().add(annotationsText);

for (String resultTextHtml : searchResult.getAnnotationsResultStringsHtml()) {
content.getChildren().addAll(TooltipTextUtil.createTextsFromHtml(resultTextHtml.replace("</b> <b>", " ")));
content.getChildren().addAll(new Text(System.lineSeparator()), lineSeparator(0.8), createPageLink(linkedFile, searchResult.getPageNumber(), searchQuery.getSearchExpression()));
}
});
}
}
}
});
}
});
}
}

private Text createFileLink(LinkedFile linkedFile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ListProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
Expand All @@ -22,6 +24,7 @@
import org.jabref.gui.search.MatchCategory;
import org.jabref.gui.util.BindingsHelper;
import org.jabref.gui.util.FilteredListProxy;
import org.jabref.gui.util.UiTaskExecutor;
import org.jabref.logic.search.SearchContext;
import org.jabref.logic.search.SearchPreferences;
import org.jabref.logic.util.BackgroundTask;
Expand Down Expand Up @@ -55,6 +58,7 @@ public class MainTableDataModel {
private final FilteredList<BibEntryTableViewModel> entriesFiltered;
private final SortedList<BibEntryTableViewModel> entriesFilteredAndSorted;
private final ObjectProperty<MainTableFieldValueFormatter> fieldValueFormatter = new SimpleObjectProperty<>();
private final ReadOnlyObjectWrapper<Optional<SearchResults>> searchResults = new ReadOnlyObjectWrapper<>(Optional.empty());
private final GroupsPreferences groupsPreferences;
private final SearchPreferences searchPreferences;
private final NameDisplayPreferences nameDisplayPreferences;
Expand Down Expand Up @@ -108,6 +112,11 @@ public MainTableDataModel(BibDatabaseContext context,

private void updateSearchMatches(Optional<SearchQuery> query) {
long updateSequence = searchUpdateSequence.incrementAndGet();
UiTaskExecutor.runNowOrInJavaFXThread(() -> {
if (updateSequence == searchUpdateSequence.get()) {
searchResults.set(Optional.empty());
}
Comment on lines +118 to +120

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please a small Java comment on the why.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

added comment

});
Optional<SearchQuery> querySnapshot = query.map(searchQuery -> new SearchQuery(
searchQuery.getSearchExpression(),
EnumSet.copyOf(searchQuery.getSearchFlags())));
Expand All @@ -118,14 +127,16 @@ private void updateSearchMatches(Optional<SearchQuery> query) {
if (updateSequence != searchUpdateSequence.get()) {
return;
}
results.ifPresentOrElse(
this::setSearchMatches,
this::clearSearchMatches
);
searchResults.set(results);
results.ifPresentOrElse(this::setSearchMatches, this::clearSearchMatches);
Comment on lines +133 to +134

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Strange that both (.set and .setSearchMatchees) is needed - maybe small Java comment why searchResults and searchMatches need to be updated? IDK.

Should we creae/link requirements on the various places?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

added a comment and requirements

FilteredListProxy.refilterListReflection(entriesFiltered);
}).executeWith(taskExecutor);
}

public ReadOnlyObjectProperty<Optional<SearchResults>> searchResultsProperty() {
return searchResults.getReadOnlyProperty();
}

/// Refresh the current search
///
/// We need to call this when the database is switched during a fulltext search since
Expand Down
Loading