Skip to content

Commit

Permalink
Fix for DOI copy issues (#8471)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke authored Jan 31, 2022
1 parent c95cef5 commit 395abea
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 14 deletions.
3 changes: 3 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 an extra option when right-clicking an entry in the Entry List to copy either the DOI or the DOI url.

### Changed

- The CSL preview styles now also support displaying data from cross references entries that are linked via the `crossref` field [#7378](https://github.com/JabRef/jabref/issues/7378)
Expand All @@ -19,6 +21,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
### Fixed

- We fixed an issue where an exception could occur when saving the preferences [#7614](https://github.com/JabRef/jabref/issues/7614)
- We fixed an issue where "Copy DOI url" in the right-click menu of the Entry List would just copy the DOI and not the DOI url. [#8389](https://github.com/JabRef/jabref/issues/8389)

### Removed

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/gui/actions/StandardActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public enum StandardActions implements Action {
OPEN_FILE(Localization.lang("Open file"), Localization.lang("Open file"), IconTheme.JabRefIcons.FILE, KeyBinding.OPEN_FILE),
OPEN_CONSOLE(Localization.lang("Open terminal here"), Localization.lang("Open terminal here"), IconTheme.JabRefIcons.CONSOLE, KeyBinding.OPEN_CONSOLE),
COPY_LINKED_FILES(Localization.lang("Copy linked files to folder...")),
COPY_DOI(Localization.lang("Copy DOI url")),
COPY_DOI(Localization.lang("Copy DOI")),
COPY_DOI_URL(Localization.lang("Copy DOI url")),
ABBREVIATE(Localization.lang("Abbreviate journal names")),
ABBREVIATE_DEFAULT("DEFAULT", Localization.lang("Abbreviate journal names of the selected entries (DEFAULT abbreviation)"), KeyBinding.ABBREVIATE),
ABBREVIATE_MEDLINE("MEDLINE", Localization.lang("Abbreviate journal names of the selected entries (MEDLINE abbreviation)")),
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/org/jabref/gui/edit/CopyDoiUrlAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.jabref.gui.Globals;
import org.jabref.gui.JabRefGUI;
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.actions.StandardActions;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.entry.identifier.DOI;

Expand All @@ -16,16 +17,25 @@
public class CopyDoiUrlAction extends SimpleCommand {

private TextArea component;
private StandardActions action;

public CopyDoiUrlAction(TextArea component) {
public CopyDoiUrlAction(TextArea component, StandardActions action) {
this.component = component;
this.action = action;
}

@Override
public void execute() {
String identifier = component.getText();

Optional<String> urlOptional = DOI.parse(identifier).map(DOI::getURIAsASCIIString);
if (action == StandardActions.COPY_DOI_URL) {
copy(DOI.parse(identifier).map(DOI::getURIAsASCIIString), identifier);
} else {
copy(DOI.parse(identifier).map(DOI::getDOI), identifier);
}
}

private void copy(Optional<String> urlOptional, String identifier) {
if (urlOptional.isPresent()) {
Globals.getClipboardManager().setContent(urlOptional.get());
JabRefGUI.getMainFrame().getDialogService().notify(Localization.lang("The link has been copied to the clipboard."));
Expand Down
25 changes: 17 additions & 8 deletions src/main/java/org/jabref/gui/edit/CopyMoreAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,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();
case COPY_DOI, COPY_DOI_URL -> copyDoi();
default -> LOGGER.info("Unknown copy command.");
}
}
Expand Down Expand Up @@ -121,12 +121,21 @@ private void copyKey() {
private void copyDoi() {
List<BibEntry> entries = stateManager.getSelectedEntries();

// Collect all non-null DOIs.
List<String> dois = entries.stream()
.filter(entry -> entry.getDOI().isPresent())
.map(entry -> entry.getDOI().get().getDOI())
.collect(Collectors.toList());
// Collect all non-null DOI or DOI urls
if (action == StandardActions.COPY_DOI_URL) {
copyDoiList(entries.stream()
.filter(entry -> entry.getDOI().isPresent())
.map(entry -> entry.getDOI().get().getURIAsASCIIString())
.collect(Collectors.toList()), entries.size());
} else {
copyDoiList(entries.stream()
.filter(entry -> entry.getDOI().isPresent())
.map(entry -> entry.getDOI().get().getDOI())
.collect(Collectors.toList()), entries.size());
}
}

private void copyDoiList(List<String> dois, int size) {
if (dois.isEmpty()) {
dialogService.notify(Localization.lang("None of the selected entries have DOIs."));
return;
Expand All @@ -135,13 +144,13 @@ private void copyDoi() {
final String copiedDois = String.join(",", dois);
clipBoardManager.setContent(copiedDois);

if (dois.size() == entries.size()) {
if (dois.size() == 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())));
Integer.toString(size - dois.size()), Integer.toString(size)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,18 @@ public static Supplier<List<MenuItem>> getNameMenu(final TextInputControl textIn
}

/**
* The default context menu with a specific menu copying a DOI URL.
* The default context menu with a specific menu copying a DOI/ DOI URL.
*
* @param textArea text-area that this menu will be connected to
* @return menu containing items of the default menu and an item for copying a DOI URL
* @return menu containing items of the default menu and an item for copying a DOI/DOI URL
*/
public static Supplier<List<MenuItem>> getDOIMenu(TextArea textArea) {
return () -> {
ActionFactory factory = new ActionFactory(Globals.getKeyPrefs());
MenuItem copyDoiUrlMenuItem = factory.createMenuItem(StandardActions.COPY_DOI, new CopyDoiUrlAction(textArea));
MenuItem copyDoiMenuItem = factory.createMenuItem(StandardActions.COPY_DOI, new CopyDoiUrlAction(textArea, StandardActions.COPY_DOI));
MenuItem copyDoiUrlMenuItem = factory.createMenuItem(StandardActions.COPY_DOI_URL, new CopyDoiUrlAction(textArea, StandardActions.COPY_DOI_URL));
List<MenuItem> menuItems = new ArrayList<>();
menuItems.add(copyDoiMenuItem);
menuItems.add(copyDoiUrlMenuItem);
menuItems.add(new SeparatorMenuItem());
menuItems.addAll(new DefaultMenu(textArea).get());
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 @@ -98,6 +98,7 @@ private static Menu createCopySubMenu(ActionFactory factory,
factory.createMenuItem(StandardActions.COPY_KEY_AND_TITLE, new CopyMoreAction(StandardActions.COPY_KEY_AND_TITLE, dialogService, stateManager, clipBoardManager, preferencesService)),
factory.createMenuItem(StandardActions.COPY_KEY_AND_LINK, new CopyMoreAction(StandardActions.COPY_KEY_AND_LINK, dialogService, stateManager, clipBoardManager, preferencesService)),
factory.createMenuItem(StandardActions.COPY_DOI, new CopyMoreAction(StandardActions.COPY_DOI, dialogService, stateManager, clipBoardManager, preferencesService)),
factory.createMenuItem(StandardActions.COPY_DOI_URL, new CopyMoreAction(StandardActions.COPY_DOI_URL, dialogService, stateManager, clipBoardManager, preferencesService)),
new SeparatorMenuItem()
);

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1854,6 +1854,7 @@ Blog=Blog
Check\ integrity=Check integrity
Cleanup\ URL\ link=Cleanup URL link
Cleanup\ URL\ link\ by\ removing\ special\ symbols\ and\ extracting\ simple\ link=Cleanup URL link by removing special symbols and extracting simple link
Copy\ DOI=Copy DOI
Copy\ DOI\ url=Copy DOI url
Development\ version=Development version
Export\ selected\ entries=Export selected entries
Expand Down

0 comments on commit 395abea

Please sign in to comment.