Skip to content

Commit

Permalink
Improve MainTable#findEntry (#12126)
Browse files Browse the repository at this point in the history
* Improve `MainTable#findEntry`

* Return findEntry method
  • Loading branch information
LoayGhreeb authored Oct 30, 2024
1 parent d586102 commit aaff6f5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
5 changes: 1 addition & 4 deletions src/main/java/org/jabref/gui/maintable/MainTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -490,10 +490,7 @@ public List<BibEntry> getSelectedEntries() {
}

private Optional<BibEntryTableViewModel> 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) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/jabref/gui/maintable/MainTableDataModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<BibEntryTableViewModel> entriesViewModel;
private final FilteredList<BibEntryTableViewModel> entriesFiltered;
Expand Down Expand Up @@ -195,6 +198,14 @@ public SortedList<BibEntryTableViewModel> getEntriesFilteredAndSorted() {
return entriesFilteredAndSorted;
}

public Optional<BibEntryTableViewModel> 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));
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/jabref/model/database/BibDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit aaff6f5

Please sign in to comment.