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 2 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 link

Choose a reason for hiding this comment

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

The label 'author' should be in sentence case, but it is already in lowercase. Ensure consistency with other labels which are in sentence case.

Copy link
Member

Choose a reason for hiding this comment

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

Is this ever displayed, the screenshots don't contain lower cased.

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
76 changes: 76 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("authors"));
case COPY_FIELD_JOURNAL ->
copyJournalField();
case COPY_FIELD_DATE ->
copyField(StandardField.DATE, Localization.lang("dates"));
case COPY_FIELD_KEYWORDS ->
copyField(StandardField.KEYWORDS, Localization.lang("keywords"));
case COPY_FIELD_ABSTRACT ->
copyField(StandardField.ABSTRACT, Localization.lang("abstracts"));
default ->
LOGGER.info("Unknown copy command.");
}
Expand Down Expand Up @@ -264,4 +274,70 @@ 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.getField(field).isPresent())
Copy link
Member

Choose a reason for hiding this comment

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

You need to du getFieldOrAlias IMHO.

Did you check for date handling? You know, there is year, month, day, and also date. If only year is present, what does date copy do? (It should copy the year -and I hope that getFieldOrAlias supports that)

.map(bibEntry -> bibEntry.getField(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 = String.join("\n", fieldValues);
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));
}
}

private void copyJournalField() {
List<BibEntry> selectedBibEntries = stateManager.getSelectedEntries();

List<String> journalValues = selectedBibEntries.stream()
.filter(bibEntry ->
bibEntry.getField(StandardField.JOURNAL).isPresent() ||
bibEntry.getField(StandardField.JOURNALTITLE).isPresent())
Copy link
Member

Choose a reason for hiding this comment

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

Use getFieldOrAlias instead of this hack. - I think, the method can be removed completely

.map(bibEntry -> {
// Prefer journal over journaltitle for consistency
if (bibEntry.getField(StandardField.JOURNAL).isPresent()) {
return bibEntry.getField(StandardField.JOURNAL).orElse("");
} else {
return bibEntry.getField(StandardField.JOURNALTITLE).orElse("");
}
})
.filter(value -> !value.isEmpty())
.collect(Collectors.toList());

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

final String copiedContent = String.join("\n", journalValues);
clipBoardManager.setContent(copiedContent);

if (journalValues.size() == selectedBibEntries.size()) {
// All entries had journal fields.
dialogService.notify(Localization.lang("Copied '%0' to clipboard.",
JabRefDialogService.shortenDialogMessage(copiedContent)));
} else {
dialogService.notify(Localization.lang("Warning: %0 out of %1 entries have undefined journal names.",
Integer.toString(selectedBibEntries.size() - journalValues.size()),
Integer.toString(selectedBibEntries.size())));
}
}
}
26 changes: 26 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,30 @@ 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);

// Ensure we never return null
if (copyFieldContentMenu == null) {
Copy link

Choose a reason for hiding this comment

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

Using exceptions for normal control flow is not recommended. Instead of checking for null and throwing an exception, ensure that the method creating the menu never returns null.

Copy link
Member

Choose a reason for hiding this comment

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

What is this? Just remove the whole if block.

throw new IllegalStateException("Failed to create Copy Field Content menu");
}

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