Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to copy the DOI to the copy sub-menu #7894

Merged
merged 6 commits into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve

### Added

- We added the option to copy the DOI of an entry directly from the context menu copy submenu. [#7826](https://github.com/JabRef/jabref/issues/7826)

### Changed

### Fixed
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/org/jabref/gui/edit/CopyMoreAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void execute() {
case COPY_CITE_KEY -> copyCiteKey();
case COPY_KEY_AND_TITLE -> copyKeyAndTitle();
case COPY_KEY_AND_LINK -> copyKeyAndLink();
case COPY_DOI -> copyDOI();
default -> LOGGER.info("Unknown copy command.");
}
}
Expand Down Expand Up @@ -113,6 +114,33 @@ private void copyKey() {
}
}

private void copyDOI() {
List<BibEntry> entries = stateManager.getSelectedEntries();

// Collect all non-null DOIs.
List<String> DOIs = entries.stream()
AidanM11 marked this conversation as resolved.
Show resolved Hide resolved
.filter(entry -> entry.getDOI().isPresent())
.map(entry -> entry.getDOI().get().getDOI())
.collect(Collectors.toList());

if (DOIs.isEmpty()) {
dialogService.notify(Localization.lang("None of the selected entries have DOIs."));
return;
}

final String copiedDOIs = String.join(",", DOIs);
clipBoardManager.setContent(copiedDOIs);

if (DOIs.size() == entries.size()) {
// All entries had DOIs.
dialogService.notify(Localization.lang("Copied '%0' to clipboard.",
JabRefDialogService.shortenDialogMessage(copiedDOIs)));
} else {
dialogService.notify(Localization.lang("Warning: %0 out of %1 entries have undefined DOIs.",
Integer.toString(entries.size() - DOIs.size()), Integer.toString(entries.size())));
}
}

private void copyCiteKey() {
List<BibEntry> entries = stateManager.getSelectedEntries();

Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/jabref/gui/maintable/RightClickMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ private static Menu createCopySubMenu(LibraryTab libraryTab,
copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_CITE_KEY, new CopyMoreAction(StandardActions.COPY_CITE_KEY, dialogService, stateManager, clipBoardManager, preferencesService)));
copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_KEY_AND_TITLE, new CopyMoreAction(StandardActions.COPY_KEY_AND_TITLE, dialogService, stateManager, clipBoardManager, preferencesService)));
copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_KEY_AND_LINK, new CopyMoreAction(StandardActions.COPY_KEY_AND_LINK, dialogService, stateManager, clipBoardManager, preferencesService)));
copySpecialMenu.getItems().add(factory.createMenuItem(StandardActions.COPY_DOI, new CopyMoreAction(StandardActions.COPY_DOI, dialogService, stateManager, clipBoardManager, preferencesService)));

// the submenu will behave dependent on what style is currently selected (citation/preview)
PreviewPreferences previewPreferences = preferencesService.getPreviewPreferences();
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,7 @@ Return\ to\ JabRef=Return to JabRef
Could\ not\ connect\ to\ %0=Could not connect to %0
Warning\:\ %0\ out\ of\ %1\ entries\ have\ undefined\ title.=Warning: %0 out of %1 entries have undefined title.
Warning\:\ %0\ out\ of\ %1\ entries\ have\ undefined\ citation\ key.=Warning: %0 out of %1 entries have undefined citation key.
Warning\:\ %0\ out\ of\ %1\ entries\ have\ undefined\ DOIs.=Warning: %0 out of %1 entries have undefined DOIs.
Really\ delete\ the\ selected\ entry?=Really delete the selected entry?
Really\ delete\ the\ %0\ selected\ entries?=Really delete the %0 selected entries?
Keep\ merged\ entry\ only=Keep merged entry only
Expand Down Expand Up @@ -1365,6 +1366,7 @@ Display\ keywords\ appearing\ in\ ALL\ entries=Display keywords appearing in ALL
Display\ keywords\ appearing\ in\ ANY\ entry=Display keywords appearing in ANY entry
None\ of\ the\ selected\ entries\ have\ titles.=None of the selected entries have titles.
None\ of\ the\ selected\ entries\ have\ citation\ keys.=None of the selected entries have citation keys.
None\ of\ the\ selected\ entries\ have\ DOIs.=None of the selected entries have DOIs.
Unabbreviate\ journal\ names=Unabbreviate journal names
Unabbreviating...=Unabbreviating...
Usage=Usage
Expand Down
52 changes: 52 additions & 0 deletions src/test/java/org/jabref/gui/edit/CopyMoreActionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class CopyMoreActionTest {
private BibEntry entry;
private List<String> titles = new ArrayList<String>();
private List<String> keys = new ArrayList<String>();
private List<String> DOIs = new ArrayList<String>();
Copy link
Member

Choose a reason for hiding this comment

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

Variabl names should be all lowerCamelCase, e.g dois


@BeforeEach
public void setUp() {
Expand All @@ -53,6 +54,7 @@ public void setUp() {
.withCitationKey("abc");
titles.add(title);
keys.add("abc");
DOIs.add("10.1145/3377811.3380330");
}

@Test
Expand Down Expand Up @@ -165,4 +167,54 @@ public void testExecuteCopyKeyOnSuccess() {
verify(dialogService, times(1)).notify(Localization.lang("Copied '%0' to clipboard.",
JabRefDialogService.shortenDialogMessage(copiedKeys)));
}

@Test
public void testExecuteCopyDOIWithNoDOI() {
BibEntry entryWithNoDOI = (BibEntry) entry.clone();
entryWithNoDOI.clearField(StandardField.DOI);
ObservableList<BibEntry> entriesWithNoDOIs = FXCollections.observableArrayList(entryWithNoDOI);
BibDatabaseContext databaseContext = new BibDatabaseContext(new BibDatabase(entriesWithNoDOIs));

when(stateManager.getActiveDatabase()).thenReturn(Optional.ofNullable(databaseContext));
when(stateManager.getSelectedEntries()).thenReturn(entriesWithNoDOIs);
copyMoreAction = new CopyMoreAction(StandardActions.COPY_DOI, dialogService, stateManager, clipBoardManager, preferencesService);
copyMoreAction.execute();

verify(clipBoardManager, times(0)).setContent(any(String.class));
verify(dialogService, times(1)).notify(Localization.lang("None of the selected entries have DOIs."));
}

@Test
public void testExecuteCopyDOIOnPartialSuccess() {
BibEntry entryWithNoDOI = (BibEntry) entry.clone();
entryWithNoDOI.clearField(StandardField.DOI);
ObservableList<BibEntry> mixedEntries = FXCollections.observableArrayList(entryWithNoDOI, entry);
BibDatabaseContext databaseContext = new BibDatabaseContext(new BibDatabase(mixedEntries));

when(stateManager.getActiveDatabase()).thenReturn(Optional.ofNullable(databaseContext));
when(stateManager.getSelectedEntries()).thenReturn(mixedEntries);
copyMoreAction = new CopyMoreAction(StandardActions.COPY_DOI, dialogService, stateManager, clipBoardManager, preferencesService);
copyMoreAction.execute();

String copiedDOIs = String.join("\n", DOIs);
verify(clipBoardManager, times(1)).setContent(copiedDOIs);
verify(dialogService, times(1)).notify(Localization.lang("Warning: %0 out of %1 entries have undefined DOIs.",
Integer.toString(mixedEntries.size() - titles.size()), Integer.toString(mixedEntries.size())));
}

@Test
public void testExecuteCopyDOIOnSuccess() {
ObservableList<BibEntry> entriesWithDOIs = FXCollections.observableArrayList(entry);
BibDatabaseContext databaseContext = new BibDatabaseContext(new BibDatabase(entriesWithDOIs));

when(stateManager.getActiveDatabase()).thenReturn(Optional.ofNullable(databaseContext));
when(stateManager.getSelectedEntries()).thenReturn(entriesWithDOIs);
copyMoreAction = new CopyMoreAction(StandardActions.COPY_DOI, dialogService, stateManager, clipBoardManager, preferencesService);
copyMoreAction.execute();

String copiedDOIs = String.join("\n", DOIs);
verify(clipBoardManager, times(1)).setContent(copiedDOIs);
verify(dialogService, times(1)).notify(Localization.lang("Copied '%0' to clipboard.",
JabRefDialogService.shortenDialogMessage(copiedDOIs)));
}
}