Skip to content

Commit

Permalink
Add compare button to duplicates in Citation relations tab (#11915)
Browse files Browse the repository at this point in the history
* - Added a "Compare" button for duplicate entries in the CitationRelationsTab.
- Added localization key for 'Compare with duplicate entries'.

* - Implemented functionality to open a "Possible duplicate entries" for comparing and merging duplicate entries.
- Updated changelog for compare button feature.

* Updated changelog

* - Updated localization key for compare button tooltip.
- Fixed bug that when adding an entry to the library from Citation Relations Tab by adding the entry's clone instead of itself.
- Optimised openPossibleDuplicateEntriesWindow function in CitationRelationsTab.java to show result of undo and redo.
- Implemented that After clicking "merge", the current entry will be kept selected - and not the merged entry be selected.

* Modified CitationsRelationsTabViewModel and ImportHandler to pass CitationsRelationsTabViewModelTest with clone entries.

* - Deleted old code in CitationRelationTab.java which to implement that original entry be selected after citation item merge.
- Modified MainTable#clearAndSelect to ensure original entry be selected after citation item merge.
- Added some comments.

* Optimised citation relations item merge: Added a citation merge flag in MainTable.java to let original entry get selected after merge instead of selecting new merged entry.

* Optimised MainTable#clearAndSelect: make the code look more concise.

* Fixed bug: As original local entry of citation relation item has been changed to new merged entry, need to refresh selected citation relation item to ensure that the item link to current local entry instead of the old one.

* Adapted tool tip of compare button.

* - renamed some variables to make them more understandable
- fixed wrong position codes: codes that updating citation relation item and setting state of citationMergeMode now be moved into CitationRelationsTab#openPossibleDuplicateEntriesWindow
- modified some comments and Java doc

* Move statement closer to intended use

* Modified the column headings of the merge entries dialog to make them more self-explanatory.

---------

Co-authored-by: Oliver Kopp <[email protected]>
  • Loading branch information
KumaLuo and koppor authored Oct 16, 2024
1 parent e68783b commit ec2059a
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We added a different background color to the search bar to indicate when the search syntax is wrong. [#11658](https://github.com/JabRef/jabref/pull/11658)
- We added a setting which always adds the literal "Cited on pages" text before each JStyle citation. [#11691](https://github.com/JabRef/jabref/pull/11732)
- We added a new plain citation parser that uses LLMs. [#11825](https://github.com/JabRef/jabref/issues/11825)
- We added a compare button to the duplicates in the citation relations tab to open the "Possible duplicate entries" window. [#11192](https://github.com/JabRef/jabref/issues/11192)
- We added automatic browser extension install on Windows for Chrome and Edge. [#6076](https://github.com/JabRef/jabref/issues/6076)
- We added a search bar for filtering keyboard shortcuts. [#11686](https://github.com/JabRef/jabref/issues/11686)
- By double clicking on a local citation in the Citation Relations Tab you can now jump the the linked entry. [#11955](https://github.com/JabRef/jabref/pull/11955)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import javax.swing.undo.UndoManager;

Expand Down Expand Up @@ -39,7 +40,12 @@
import org.jabref.gui.entryeditor.citationrelationtab.semanticscholar.CitationFetcher;
import org.jabref.gui.entryeditor.citationrelationtab.semanticscholar.SemanticScholarFetcher;
import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.mergeentries.EntriesMergeResult;
import org.jabref.gui.mergeentries.MergeEntriesDialog;
import org.jabref.gui.preferences.GuiPreferences;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableInsertEntries;
import org.jabref.gui.undo.UndoableRemoveEntries;
import org.jabref.gui.util.NoSelectionModel;
import org.jabref.gui.util.ViewModelListCellFactory;
import org.jabref.logic.bibtex.BibEntryWriter;
Expand All @@ -51,6 +57,7 @@
import org.jabref.logic.os.OS;
import org.jabref.logic.util.BackgroundTask;
import org.jabref.logic.util.TaskExecutor;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.database.BibDatabaseModeDetection;
Expand Down Expand Up @@ -89,6 +96,8 @@ public class CitationRelationsTab extends EntryEditorTab {
private final CitationsRelationsTabViewModel citationsRelationsTabViewModel;
private final DuplicateCheck duplicateCheck;
private final BibEntryTypesManager entryTypesManager;
private final StateManager stateManager;
private final UndoManager undoManager;

public CitationRelationsTab(DialogService dialogService,
BibDatabaseContext databaseContext,
Expand All @@ -104,6 +113,8 @@ public CitationRelationsTab(DialogService dialogService,
this.preferences = preferences;
this.libraryTab = libraryTab;
this.taskExecutor = taskExecutor;
this.undoManager = undoManager;
this.stateManager = stateManager;
setText(Localization.lang("Citation relations"));
setTooltip(new Tooltip(Localization.lang("Show articles related by citation")));

Expand Down Expand Up @@ -238,6 +249,13 @@ private void styleFetchedListView(CheckListView<CitationRelationItem> listView)
}
});
vContainer.getChildren().add(jumpTo);

Button compareButton = IconTheme.JabRefIcons.MERGE_ENTRIES.asButton();
compareButton.setTooltip(new Tooltip(Localization.lang("Compare with existing entry")));
compareButton.setOnMouseClicked(event -> {
openPossibleDuplicateEntriesWindow(entry, listView);
});
vContainer.getChildren().add(compareButton);
} else {
ToggleButton addToggle = IconTheme.JabRefIcons.ADD.asToggleButton();
addToggle.setTooltip(new Tooltip(Localization.lang("Select entry")));
Expand Down Expand Up @@ -511,4 +529,43 @@ private void importEntries(List<CitationRelationItem> entriesToImport, CitationF

dialogService.notify(Localization.lang("Number of entries successfully imported") + ": " + entriesToImport.size());
}

/**
* Function to open possible duplicate entries window to compare duplicate entries
*
* @param citationRelationItem duplicate in the citation relations tab
* @param listView CheckListView to display citations
*/
private void openPossibleDuplicateEntriesWindow(CitationRelationItem citationRelationItem, CheckListView<CitationRelationItem> listView) {
BibEntry libraryEntry = citationRelationItem.localEntry();
BibEntry citationEntry = citationRelationItem.entry();
String leftHeader = Localization.lang("Library Entry");
String rightHeader = Localization.lang("Citation Entry");

MergeEntriesDialog dialog = new MergeEntriesDialog(libraryEntry, citationEntry, leftHeader, rightHeader, preferences);
dialog.setTitle(Localization.lang("Possible duplicate entries"));

Optional<EntriesMergeResult> entriesMergeResult = dialogService.showCustomDialogAndWait(dialog);
entriesMergeResult.ifPresentOrElse(mergeResult -> {

BibEntry mergedEntry = mergeResult.mergedEntry();
// update local entry of selected citation relation item
listView.getItems().set(listView.getItems().indexOf(citationRelationItem), new CitationRelationItem(citationRelationItem.entry(), mergedEntry, true));

// Merge method is similar to MergeTwoEntriesAction#execute
BibDatabase database = stateManager.getActiveDatabase().get().getDatabase();
database.removeEntry(mergeResult.originalLeftEntry());
libraryTab.getMainTable().setCitationMergeMode(true);
database.insertEntry(mergedEntry);

NamedCompound ce = new NamedCompound(Localization.lang("Merge entries"));
ce.addEdit(new UndoableRemoveEntries(database, mergeResult.originalLeftEntry()));
ce.addEdit(new UndoableInsertEntries(stateManager.getActiveDatabase().get().getDatabase(), mergedEntry));
ce.end();

undoManager.addEdit(ce);

dialogService.notify(Localization.lang("Merged entries"));
}, () -> dialogService.notify(Localization.lang("Canceled merging entries")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ private void importCites(List<BibEntry> entries, BibEntry existingEntry, ImportH

List<String> citeKeys = getExistingEntriesFromCiteField(existingEntry);
citeKeys.removeIf(String::isEmpty);

for (BibEntry entryToCite : entries) {
if (generateNewKeyOnImport || entryToCite.getCitationKey().isEmpty()) {
String key = generator.generateKey(entryToCite);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public void importEntries(List<BibEntry> entries) {
}

public void importCleanedEntries(List<BibEntry> entries) {
entries = entries.stream().map(entry -> (BibEntry) entry.clone()).toList();
bibDatabaseContext.getDatabase().insertEntries(entries);
generateKeys(entries);
setAutomaticFields(entries);
Expand Down
22 changes: 17 additions & 5 deletions src/main/java/org/jabref/gui/maintable/MainTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public class MainTable extends TableView<BibEntryTableViewModel> {

private long lastKeyPressTime;
private String columnSearchTerm;
private boolean citationMergeMode = false;

public MainTable(MainTableDataModel model,
LibraryTab libraryTab,
Expand Down Expand Up @@ -249,11 +250,18 @@ public void listen(EntriesAddedEvent event) {
}

public void clearAndSelect(BibEntry bibEntry) {
getSelectionModel().clearSelection();
findEntry(bibEntry).ifPresent(entry -> {
getSelectionModel().select(entry);
scrollTo(entry);
});
// check if entries merged from citation relations tab
if (citationMergeMode) {
// keep original entry selected and reset citation merge mode
this.citationMergeMode = false;
} else {
// select new entry
getSelectionModel().clearSelection();
findEntry(bibEntry).ifPresent(entry -> {
getSelectionModel().select(entry);
scrollTo(entry);
});
}
}

private void scrollToNextMatchCategory() {
Expand Down Expand Up @@ -492,4 +500,8 @@ private Optional<BibEntryTableViewModel> findEntry(BibEntry entry) {
.filter(viewModel -> viewModel.getEntry().equals(entry))
.findFirst();
}

public void setCitationMergeMode(boolean citationMerge) {
this.citationMergeMode = citationMerge;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ public MergeEntriesDialog(BibEntry one, BibEntry two, GuiPreferences preferences
init();
}

public MergeEntriesDialog(BibEntry one, BibEntry two, String leftHeader, String rightHeader, GuiPreferences preferences) {
threeWayMergeView = new ThreeWayMergeView(one, two, leftHeader, rightHeader, preferences);
this.one = one;
this.two = two;

init();
}

/**
* Sets up the dialog
*/
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2797,3 +2797,7 @@ Warning\:\ The\ selected\ directory\ is\ not\ a\ valid\ directory.=Warning: The
Currently\ selected\ JStyle\:\ '%0' = Currently selected JStyle: '%0'
Currently\ selected\ CSL\ Style\:\ '%0' = Currently selected CSL Style: '%0'
Store\ url\ for\ downloaded\ file=Store url for downloaded file
Compare\ with\ existing\ entry=Compare with existing entry
Library\ Entry=Library Entry
Citation\ Entry=Citation Entry

0 comments on commit ec2059a

Please sign in to comment.