Skip to content

Commit

Permalink
Return findEntry method
Browse files Browse the repository at this point in the history
  • Loading branch information
LoayGhreeb committed Oct 30, 2024
1 parent 875ec14 commit c480bce
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/main/java/org/jabref/gui/maintable/MainTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public void clearAndSelect(BibEntry bibEntry) {
} else {
// select new entry
getSelectionModel().clearSelection();
model.getViewModelByIndex(database.getDatabase().indexOf(bibEntry)).ifPresent(entry -> {
findEntry(bibEntry).ifPresent(entry -> {
getSelectionModel().select(entry);
scrollTo(entry);
});
Expand Down Expand Up @@ -489,6 +489,10 @@ public List<BibEntry> getSelectedEntries() {
.collect(Collectors.toList());
}

private Optional<BibEntryTableViewModel> findEntry(BibEntry entry) {
return model.getViewModelByIndex(database.getDatabase().indexOf(entry));
}

public void setCitationMergeMode(boolean citationMerge) {
this.citationMergeMode = citationMerge;
}
Expand Down
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 @@ -197,6 +200,7 @@ public SortedList<BibEntryTableViewModel> getEntriesFilteredAndSorted() {

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));
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 c480bce

Please sign in to comment.