Skip to content

Feature: Add "Copy Field Content" submenu to entry context menu #13280

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
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 support for import of a Refer/BibIX file format. [#13069](https://github.com/JabRef/jabref/issues/13069)
- We added a new `jabkit` command `pseudonymize` to pseudonymize the library. [#13109](https://github.com/JabRef/jabref/issues/13109)
- We added functionality to focus running instance when trying to start a second instance. [#13129](https://github.com/JabRef/jabref/issues/13129)
- We added a "Copy Field Content" submenu to the entry context menu, allowing users to quickly copy specific field contents including Author, Journal, Date, Keywords, and Abstract fields from selected entries.

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public enum StandardActions implements Action {
COPY_CITATION_HTML(Localization.lang("Copy citation (html)"), KeyBinding.COPY_PREVIEW),
COPY_CITATION_TEXT(Localization.lang("Copy citation (text)")),
COPY_CITATION_PREVIEW(Localization.lang("Copy preview"), KeyBinding.COPY_PREVIEW),
COPY_FIELD_CONTENT(Localization.lang("Copy field content")),
COPY_FIELD_AUTHOR(Localization.lang("Author")),
COPY_FIELD_JOURNAL(Localization.lang("Journal")),
COPY_FIELD_DATE(Localization.lang("Date")),
COPY_FIELD_KEYWORDS(Localization.lang("Keywords")),
COPY_FIELD_ABSTRACT(Localization.lang("Abstract")),
EXPORT_TO_CLIPBOARD(Localization.lang("Export to clipboard"), IconTheme.JabRefIcons.EXPORT_TO_CLIPBOARD),
EXPORT_SELECTED_TO_CLIPBOARD(Localization.lang("Export selected entries to clipboard"), IconTheme.JabRefIcons.EXPORT_TO_CLIPBOARD),
COPY(Localization.lang("Copy"), IconTheme.JabRefIcons.COPY, KeyBinding.COPY),
Expand Down
39 changes: 39 additions & 0 deletions jabgui/src/main/java/org/jabref/gui/edit/CopyMoreAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ public void execute() {
copyKeyAndLink();
case COPY_DOI, COPY_DOI_URL ->
copyDoi();
case COPY_FIELD_AUTHOR ->
copyField(StandardField.AUTHOR, Localization.lang("Author"));
case COPY_FIELD_JOURNAL ->
copyField(StandardField.JOURNAL, Localization.lang("Journal"));
case COPY_FIELD_DATE ->
copyField(StandardField.DATE, Localization.lang("Date"));
case COPY_FIELD_KEYWORDS ->
copyField(StandardField.KEYWORDS, Localization.lang("Keywords"));
case COPY_FIELD_ABSTRACT ->
copyField(StandardField.ABSTRACT, Localization.lang("Abstract"));
default ->
LOGGER.info("Unknown copy command.");
}
Expand Down Expand Up @@ -264,4 +274,33 @@ private void copyKeyAndLink() {
Long.toString(entries.size() - entriesWithKey.size()), Integer.toString(entries.size())));
}
}

private void copyField(StandardField field, String fieldDisplayName) {
List<BibEntry> selectedBibEntries = stateManager.getSelectedEntries();

List<String> fieldValues = selectedBibEntries.stream()
.filter(bibEntry -> bibEntry.getFieldOrAlias(field).isPresent())
.map(bibEntry -> bibEntry.getFieldOrAlias(field).orElse(""))
.filter(value -> !value.isEmpty())
.collect(Collectors.toList());
Copy link
Member

Choose a reason for hiding this comment

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

Use Collectors.joining instead of String.join("\n", fieldValues); below.


if (fieldValues.isEmpty()) {
dialogService.notify(Localization.lang("None of the selected entries have %0.", fieldDisplayName));
return;
}

final String copiedContent = fieldValues.stream().collect(Collectors.joining("\n"));
clipBoardManager.setContent(copiedContent);

if (fieldValues.size() == selectedBibEntries.size()) {
// All entries had the field.
dialogService.notify(Localization.lang("Copied '%0' to clipboard.",
JabRefDialogService.shortenDialogMessage(copiedContent)));
} else {
dialogService.notify(Localization.lang("Warning: %0 out of %1 entries have undefined %2.",
Integer.toString(selectedBibEntries.size() - fieldValues.size()),
Integer.toString(selectedBibEntries.size()),
fieldDisplayName));
}
}
}
21 changes: 21 additions & 0 deletions jabgui/src/main/java/org/jabref/gui/maintable/RightClickMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ private static Menu createCopySubMenu(ActionFactory factory,

copySpecialMenu.getItems().addAll(
factory.createMenuItem(StandardActions.COPY_TITLE, new CopyMoreAction(StandardActions.COPY_TITLE, dialogService, stateManager, clipBoardManager, preferences, abbreviationRepository)),
createCopyFieldContentSubMenu(factory, dialogService, stateManager, clipBoardManager, preferences, abbreviationRepository),
Copy link

Choose a reason for hiding this comment

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

The method createCopyFieldContentSubMenu is added but lacks any null checks or use of Optional for its parameters, which could lead to NullPointerExceptions if any parameter is null.

new SeparatorMenuItem(),
factory.createMenuItem(StandardActions.COPY_KEY, new CopyMoreAction(StandardActions.COPY_KEY, dialogService, stateManager, clipBoardManager, preferences, abbreviationRepository)),
factory.createMenuItem(StandardActions.COPY_CITE_KEY, new CopyMoreAction(StandardActions.COPY_CITE_KEY, dialogService, stateManager, clipBoardManager, preferences, abbreviationRepository)),
factory.createMenuItem(StandardActions.COPY_KEY_AND_TITLE, new CopyMoreAction(StandardActions.COPY_KEY_AND_TITLE, dialogService, stateManager, clipBoardManager, preferences, abbreviationRepository)),
Expand All @@ -204,6 +206,25 @@ private static Menu createCopySubMenu(ActionFactory factory,
return copySpecialMenu;
}

private static Menu createCopyFieldContentSubMenu(ActionFactory factory,
DialogService dialogService,
StateManager stateManager,
ClipBoardManager clipBoardManager,
GuiPreferences preferences,
JournalAbbreviationRepository abbreviationRepository) {
Menu copyFieldContentMenu = factory.createMenu(StandardActions.COPY_FIELD_CONTENT);

copyFieldContentMenu.getItems().addAll(
factory.createMenuItem(StandardActions.COPY_FIELD_AUTHOR, new CopyMoreAction(StandardActions.COPY_FIELD_AUTHOR, dialogService, stateManager, clipBoardManager, preferences, abbreviationRepository)),
factory.createMenuItem(StandardActions.COPY_FIELD_JOURNAL, new CopyMoreAction(StandardActions.COPY_FIELD_JOURNAL, dialogService, stateManager, clipBoardManager, preferences, abbreviationRepository)),
factory.createMenuItem(StandardActions.COPY_FIELD_DATE, new CopyMoreAction(StandardActions.COPY_FIELD_DATE, dialogService, stateManager, clipBoardManager, preferences, abbreviationRepository)),
factory.createMenuItem(StandardActions.COPY_FIELD_KEYWORDS, new CopyMoreAction(StandardActions.COPY_FIELD_KEYWORDS, dialogService, stateManager, clipBoardManager, preferences, abbreviationRepository)),
factory.createMenuItem(StandardActions.COPY_FIELD_ABSTRACT, new CopyMoreAction(StandardActions.COPY_FIELD_ABSTRACT, dialogService, stateManager, clipBoardManager, preferences, abbreviationRepository))
);

return copyFieldContentMenu;
}

private static Menu createSendSubMenu(ActionFactory factory,
DialogService dialogService,
StateManager stateManager,
Expand Down
Loading
Loading