From aaff6f5e56cd6c1ece463da95dd9acb516cb19f5 Mon Sep 17 00:00:00 2001 From: Loay Ghreeb Date: Wed, 30 Oct 2024 11:59:12 +0300 Subject: [PATCH] Improve `MainTable#findEntry` (#12126) * Improve `MainTable#findEntry` * Return findEntry method --- src/main/java/org/jabref/gui/maintable/MainTable.java | 5 +---- .../org/jabref/gui/maintable/MainTableDataModel.java | 11 +++++++++++ .../java/org/jabref/model/database/BibDatabase.java | 6 +++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/jabref/gui/maintable/MainTable.java b/src/main/java/org/jabref/gui/maintable/MainTable.java index 4d66704856f..b0bcb2c70a9 100644 --- a/src/main/java/org/jabref/gui/maintable/MainTable.java +++ b/src/main/java/org/jabref/gui/maintable/MainTable.java @@ -490,10 +490,7 @@ public List getSelectedEntries() { } private Optional findEntry(BibEntry entry) { - return model.getEntriesFilteredAndSorted() - .stream() - .filter(viewModel -> viewModel.getEntry().equals(entry)) - .findFirst(); + return model.getViewModelByIndex(database.getDatabase().indexOf(entry)); } public void setCitationMergeMode(boolean citationMerge) { diff --git a/src/main/java/org/jabref/gui/maintable/MainTableDataModel.java b/src/main/java/org/jabref/gui/maintable/MainTableDataModel.java index 4c4d6a11c75..e758516ade9 100644 --- a/src/main/java/org/jabref/gui/maintable/MainTableDataModel.java +++ b/src/main/java/org/jabref/gui/maintable/MainTableDataModel.java @@ -38,10 +38,13 @@ import com.tobiasdiez.easybind.EasyBind; import com.tobiasdiez.easybind.Subscription; import org.jspecify.annotations.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import static org.jabref.model.search.PostgreConstants.ENTRY_ID; public class MainTableDataModel { + private final Logger LOGGER = LoggerFactory.getLogger(MainTableDataModel.class); private final ObservableList entriesViewModel; private final FilteredList entriesFiltered; @@ -195,6 +198,14 @@ public SortedList getEntriesFilteredAndSorted() { return entriesFilteredAndSorted; } + public Optional getViewModelByIndex(int index) { + if (index < 0 || index >= entriesViewModel.size()) { + LOGGER.warn("Tried to access out of bounds index {} in entriesViewModel", index); + return Optional.empty(); + } + return Optional.of(entriesViewModel.get(index)); + } + public void resetFieldFormatter() { this.fieldValueFormatter.setValue(new MainTableFieldValueFormatter(nameDisplayPreferences, bibDatabaseContext)); } diff --git a/src/main/java/org/jabref/model/database/BibDatabase.java b/src/main/java/org/jabref/model/database/BibDatabase.java index 56f650e8893..911b5bed2a0 100644 --- a/src/main/java/org/jabref/model/database/BibDatabase.java +++ b/src/main/java/org/jabref/model/database/BibDatabase.java @@ -637,7 +637,11 @@ public String getNewLineSeparator() { */ public int indexOf(BibEntry bibEntry) { int index = Collections.binarySearch(entries, bibEntry, Comparator.comparing(BibEntry::getId)); - return index >= 0 ? index : -1; + if (index >= 0) { + return index; + } + LOGGER.warn("Could not find entry with ID {} in the database", bibEntry.getId()); + return -1; } public BibEntry getEntryById(String id) {