From f85c3935b1643f60eeb79951fb84ce47371fdebd Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Thu, 21 Dec 2023 20:13:52 +0100 Subject: [PATCH 01/13] Fix Copy cite command should respect preferences Fixes #10615 --- .../jabref/gui/actions/StandardActions.java | 2 +- .../org/jabref/gui/edit/CopyMoreAction.java | 50 +++++++++++-------- .../external/ExternalTabViewModel.java | 13 +++-- .../gui/push/AbstractPushToApplication.java | 25 ++-------- .../logic/push/CitationCommandString.java | 28 +++++++++++ .../ExternalApplicationsPreferences.java | 24 +++++---- .../jabref/preferences/JabRefPreferences.java | 24 +++++---- src/main/resources/l10n/JabRef_en.properties | 1 + .../org/jabref/gui/push/PushToEmacsTest.java | 2 +- .../CitationCommandStringTest.java | 21 ++++++++ 10 files changed, 122 insertions(+), 68 deletions(-) create mode 100644 src/main/java/org/jabref/logic/push/CitationCommandString.java create mode 100644 src/test/java/org/jabref/preferences/CitationCommandStringTest.java diff --git a/src/main/java/org/jabref/gui/actions/StandardActions.java b/src/main/java/org/jabref/gui/actions/StandardActions.java index eb0c94f866d..546d7c95a3e 100644 --- a/src/main/java/org/jabref/gui/actions/StandardActions.java +++ b/src/main/java/org/jabref/gui/actions/StandardActions.java @@ -13,7 +13,7 @@ public enum StandardActions implements Action { COPY_MORE(Localization.lang("Copy") + "..."), COPY_TITLE(Localization.lang("Copy title"), KeyBinding.COPY_TITLE), COPY_KEY(Localization.lang("Copy citation key"), KeyBinding.COPY_CITATION_KEY), - COPY_CITE_KEY(Localization.lang("Copy \\cite{citation key}"), KeyBinding.COPY_CITE_CITATION_KEY), + COPY_CITE_KEY(Localization.lang("Copy key with configured cite command"), KeyBinding.COPY_CITE_CITATION_KEY), COPY_KEY_AND_TITLE(Localization.lang("Copy citation key and title"), KeyBinding.COPY_CITATION_KEY_AND_TITLE), COPY_KEY_AND_LINK(Localization.lang("Copy citation key and link"), KeyBinding.COPY_CITATION_KEY_AND_LINK), COPY_CITATION_HTML(Localization.lang("Copy citation (html)"), KeyBinding.COPY_PREVIEW), diff --git a/src/main/java/org/jabref/gui/edit/CopyMoreAction.java b/src/main/java/org/jabref/gui/edit/CopyMoreAction.java index 0f9bbe3c103..c885f9888af 100644 --- a/src/main/java/org/jabref/gui/edit/CopyMoreAction.java +++ b/src/main/java/org/jabref/gui/edit/CopyMoreAction.java @@ -3,7 +3,6 @@ import java.io.IOException; import java.io.StringReader; import java.util.List; -import java.util.Optional; import java.util.stream.Collectors; import org.jabref.gui.ClipBoardManager; @@ -17,7 +16,9 @@ import org.jabref.logic.l10n.Localization; import org.jabref.logic.layout.Layout; import org.jabref.logic.layout.LayoutHelper; +import org.jabref.logic.push.CitationCommandString; import org.jabref.logic.util.OS; +import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.field.StandardField; import org.jabref.preferences.PreferencesService; @@ -58,13 +59,20 @@ public void execute() { } switch (action) { - case COPY_TITLE -> copyTitle(); - case COPY_KEY -> copyKey(); - case COPY_CITE_KEY -> copyCiteKey(); - case COPY_KEY_AND_TITLE -> copyKeyAndTitle(); - case COPY_KEY_AND_LINK -> copyKeyAndLink(); - case COPY_DOI, COPY_DOI_URL -> copyDoi(); - default -> LOGGER.info("Unknown copy command."); + case COPY_TITLE -> + copyTitle(); + case COPY_KEY -> + copyKey(); + case COPY_CITE_KEY -> + copyCiteKey(); + case COPY_KEY_AND_TITLE -> + copyKeyAndTitle(); + case COPY_KEY_AND_LINK -> + copyKeyAndLink(); + case COPY_DOI, COPY_DOI_URL -> + copyDoi(); + default -> + LOGGER.info("Unknown copy command."); } } @@ -127,14 +135,14 @@ private void copyDoi() { // 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()); + .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()); + .filter(entry -> entry.getDOI().isPresent()) + .map(entry -> entry.getDOI().get().getDOI()) + .collect(Collectors.toList()), entries.size()); } } @@ -171,11 +179,9 @@ private void copyCiteKey() { return; } - String citeCommand = Optional.ofNullable(preferencesService.getExternalApplicationsPreferences().getCiteCommand()) - .filter(cite -> cite.contains("\\")) // must contain \ - .orElse("\\cite"); + CitationCommandString citeCommand = preferencesService.getExternalApplicationsPreferences().getCiteCommand(); - final String copiedCiteCommand = citeCommand + "{" + String.join(",", keys) + '}'; + final String copiedCiteCommand = citeCommand.prefix() + String.join(citeCommand.delimiter(), keys) + citeCommand.suffix(); clipBoardManager.setContent(copiedCiteCommand); if (keys.size() == entries.size()) { @@ -208,7 +214,9 @@ private void copyKeyAndTitle() { for (BibEntry entry : entries) { if (entry.hasCitationKey()) { entriesWithKeys++; - keyAndTitle.append(layout.doLayout(entry, stateManager.getActiveDatabase().get().getDatabase())); + stateManager.getActiveDatabase() + .map(BibDatabaseContext::getDatabase) + .ifPresent(bibDatabase -> keyAndTitle.append(layout.doLayout(entry, bibDatabase))); } } @@ -242,7 +250,7 @@ private void copyKeyAndLink() { List entriesWithKey = entries.stream() .filter(BibEntry::hasCitationKey) - .collect(Collectors.toList()); + .toList(); if (entriesWithKey.isEmpty()) { dialogService.notify(Localization.lang("None of the selected entries have citation keys.")); @@ -250,7 +258,7 @@ private void copyKeyAndLink() { } for (BibEntry entry : entriesWithKey) { - String key = entry.getCitationKey().get(); + String key = entry.getCitationKey().orElse(""); String url = entry.getField(StandardField.URL).orElse(""); keyAndLink.append(url.isEmpty() ? key : String.format("%s", url, key)); keyAndLink.append(OS.NEWLINE); diff --git a/src/main/java/org/jabref/gui/preferences/external/ExternalTabViewModel.java b/src/main/java/org/jabref/gui/preferences/external/ExternalTabViewModel.java index f8e7f96993c..6a118522c9a 100644 --- a/src/main/java/org/jabref/gui/preferences/external/ExternalTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/external/ExternalTabViewModel.java @@ -22,6 +22,7 @@ import org.jabref.gui.push.PushToEmacs; import org.jabref.gui.util.FileDialogConfiguration; import org.jabref.logic.l10n.Localization; +import org.jabref.logic.push.CitationCommandString; import org.jabref.model.strings.StringUtil; import org.jabref.preferences.ExternalApplicationsPreferences; import org.jabref.preferences.PreferencesService; @@ -39,7 +40,7 @@ public class ExternalTabViewModel implements PreferenceTabViewModel { private final BooleanProperty autoOpenAttachedFoldersProperty = new SimpleBooleanProperty(); private final ListProperty pushToApplicationsListProperty = new SimpleListProperty<>(); private final ObjectProperty selectedPushToApplicationProperty = new SimpleObjectProperty<>(); - private final StringProperty citeCommandProperty = new SimpleStringProperty(""); + private final StringProperty citeCommandProperty = new SimpleStringProperty(); private final BooleanProperty useCustomTerminalProperty = new SimpleBooleanProperty(); private final StringProperty customTerminalCommandProperty = new SimpleStringProperty(""); private final BooleanProperty useCustomFileBrowserProperty = new SimpleBooleanProperty(); @@ -97,7 +98,8 @@ public void setValues() { PushToApplications.getApplicationByName(initialPushToApplicationPreferences.getActiveApplicationName(), dialogService, preferences) .orElse(new PushToEmacs(dialogService, preferences))); - citeCommandProperty.setValue(initialExternalApplicationPreferences.getCiteCommand()); + citeCommandProperty.setValue(initialExternalApplicationPreferences.getCiteCommand().toString()); + useCustomTerminalProperty.setValue(initialExternalApplicationPreferences.useCustomTerminal()); customTerminalCommandProperty.setValue(initialExternalApplicationPreferences.getCustomTerminalCommand()); useCustomFileBrowserProperty.setValue(initialExternalApplicationPreferences.useCustomFileBrowser()); @@ -110,7 +112,10 @@ public void storeSettings() { ExternalApplicationsPreferences externalPreferences = preferences.getExternalApplicationsPreferences(); externalPreferences.setEMailSubject(eMailReferenceSubjectProperty.getValue()); externalPreferences.setAutoOpenEmailAttachmentsFolder(autoOpenAttachedFoldersProperty.getValue()); - externalPreferences.setCiteCommand(citeCommandProperty.getValue()); + + + externalPreferences.setCiteCommand(CitationCommandString.from(citeCommandProperty.getValue())); + externalPreferences.setUseCustomTerminal(useCustomTerminalProperty.getValue()); externalPreferences.setCustomTerminalCommand(customTerminalCommandProperty.getValue()); externalPreferences.setUseCustomFileBrowser(useCustomFileBrowserProperty.getValue()); @@ -229,6 +234,6 @@ public StringProperty customFileBrowserCommandProperty() { } public void resetCiteCommandToDefault() { - this.citeCommandProperty.setValue(preferences.getExternalApplicationsPreferences().getDefaultCiteCommand()); + this.citeCommandProperty.setValue(preferences.getExternalApplicationsPreferences().getDefaultCiteCommand().toString()); } } diff --git a/src/main/java/org/jabref/gui/push/AbstractPushToApplication.java b/src/main/java/org/jabref/gui/push/AbstractPushToApplication.java index aa84c74d3e9..0928ff7b3e8 100644 --- a/src/main/java/org/jabref/gui/push/AbstractPushToApplication.java +++ b/src/main/java/org/jabref/gui/push/AbstractPushToApplication.java @@ -10,6 +10,7 @@ import org.jabref.gui.icon.JabRefIcon; import org.jabref.gui.keyboard.KeyBinding; import org.jabref.logic.l10n.Localization; +import org.jabref.logic.push.CitationCommandString; import org.jabref.logic.util.OS; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; @@ -26,9 +27,6 @@ public abstract class AbstractPushToApplication implements PushToApplication { private static final Logger LOGGER = LoggerFactory.getLogger(AbstractPushToApplication.class); - private static final String CITE_KEY1 = "key1"; - private static final String CITE_KEY2 = "key2"; - protected boolean couldNotCall; // Set to true in case the command could not be executed, e.g., if the file is not found protected boolean couldNotPush; // Set to true in case the tunnel to the program (if one is used) does not operate protected boolean notDefined; // Set to true if the corresponding path is not defined in the preferences @@ -38,7 +36,6 @@ public abstract class AbstractPushToApplication implements PushToApplication { protected final DialogService dialogService; protected final PreferencesService preferencesService; - private String cachedCiteCommand; private String cachedCitePrefix; private String cachedCiteSuffix; private String cachedCiteDelimiter; @@ -49,23 +46,11 @@ public AbstractPushToApplication(DialogService dialogService, PreferencesService } private void dissectCiteCommand() { - String preferencesCiteCommand = preferencesService.getExternalApplicationsPreferences().getCiteCommand(); - - if (preferencesCiteCommand != null && preferencesCiteCommand.equals(cachedCiteCommand)) { - return; - } - - cachedCiteCommand = preferencesCiteCommand; - - int indexKey1 = cachedCiteCommand.indexOf(CITE_KEY1); - int indexKey2 = cachedCiteCommand.indexOf(CITE_KEY2); - if (indexKey1 < 0 || indexKey2 < 0 || indexKey2 < (indexKey1 + CITE_KEY1.length())) { - return; - } + CitationCommandString preferencesCiteCommand = preferencesService.getExternalApplicationsPreferences().getCiteCommand(); - cachedCitePrefix = preferencesCiteCommand.substring(0, indexKey1); - cachedCiteDelimiter = preferencesCiteCommand.substring(preferencesCiteCommand.lastIndexOf(CITE_KEY1) + CITE_KEY1.length(), indexKey2); - cachedCiteSuffix = preferencesCiteCommand.substring(preferencesCiteCommand.lastIndexOf(CITE_KEY2) + CITE_KEY2.length()); + cachedCitePrefix = preferencesCiteCommand.prefix(); + cachedCiteDelimiter = preferencesCiteCommand.delimiter(); + cachedCiteSuffix = preferencesCiteCommand.suffix(); } @Override diff --git a/src/main/java/org/jabref/logic/push/CitationCommandString.java b/src/main/java/org/jabref/logic/push/CitationCommandString.java new file mode 100644 index 00000000000..2544bb41fcc --- /dev/null +++ b/src/main/java/org/jabref/logic/push/CitationCommandString.java @@ -0,0 +1,28 @@ +package org.jabref.logic.push; + +public record CitationCommandString( + String prefix, + String delimiter, + String suffix) { + private static final String CITE_KEY1 = "key1"; + private static final String CITE_KEY2 = "key2"; + + @Override + public String toString() { + return prefix + CITE_KEY1 + delimiter + CITE_KEY2 + suffix; + } + + public static CitationCommandString from(String completeCiteCommand) { + + int indexKey1 = completeCiteCommand.indexOf(CITE_KEY1); + int indexKey2 = completeCiteCommand.indexOf(CITE_KEY2); + if (indexKey1 < 0 || indexKey2 < 0 || indexKey2 < (indexKey1 + CITE_KEY1.length())) { + return new CitationCommandString("", "", ""); + } + + String prefix = completeCiteCommand.substring(0, indexKey1); + String delim = completeCiteCommand.substring(completeCiteCommand.lastIndexOf(CITE_KEY1) + CITE_KEY1.length(), indexKey2); + String suffix = completeCiteCommand.substring(completeCiteCommand.lastIndexOf(CITE_KEY2) + CITE_KEY2.length()); + return new CitationCommandString(prefix, delim, suffix); + } +} diff --git a/src/main/java/org/jabref/preferences/ExternalApplicationsPreferences.java b/src/main/java/org/jabref/preferences/ExternalApplicationsPreferences.java index 45670b68a28..992038c80f3 100644 --- a/src/main/java/org/jabref/preferences/ExternalApplicationsPreferences.java +++ b/src/main/java/org/jabref/preferences/ExternalApplicationsPreferences.java @@ -1,17 +1,21 @@ package org.jabref.preferences; import javafx.beans.property.BooleanProperty; +import javafx.beans.property.ObjectProperty; import javafx.beans.property.SimpleBooleanProperty; +import javafx.beans.property.SimpleObjectProperty; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; +import org.jabref.logic.push.CitationCommandString; + public class ExternalApplicationsPreferences { private final StringProperty eMailSubject; private final BooleanProperty shouldAutoOpenEmailAttachmentsFolder; - private final StringProperty citeCommand; + private final ObjectProperty citeCommand; - private final StringProperty defaultCiteCommand; + private final ObjectProperty defaultCiteCommand; private final BooleanProperty useCustomTerminal; private final StringProperty customTerminalCommand; @@ -21,8 +25,8 @@ public class ExternalApplicationsPreferences { public ExternalApplicationsPreferences(String eMailSubject, boolean shouldAutoOpenEmailAttachmentsFolder, - String citeCommand, - String defaultCiteCommand, + CitationCommandString citeCommand, + CitationCommandString defaultCiteCommand, boolean useCustomTerminal, String customTerminalCommand, boolean useCustomFileBrowser, @@ -31,8 +35,8 @@ public ExternalApplicationsPreferences(String eMailSubject, this.eMailSubject = new SimpleStringProperty(eMailSubject); this.shouldAutoOpenEmailAttachmentsFolder = new SimpleBooleanProperty(shouldAutoOpenEmailAttachmentsFolder); - this.citeCommand = new SimpleStringProperty(citeCommand); - this.defaultCiteCommand = new SimpleStringProperty(defaultCiteCommand); + this.citeCommand = new SimpleObjectProperty<>(citeCommand); + this.defaultCiteCommand = new SimpleObjectProperty<>(defaultCiteCommand); this.useCustomTerminal = new SimpleBooleanProperty(useCustomTerminal); this.customTerminalCommand = new SimpleStringProperty(customTerminalCommand); this.useCustomFileBrowser = new SimpleBooleanProperty(useCustomFileBrowser); @@ -64,15 +68,15 @@ public void setAutoOpenEmailAttachmentsFolder(boolean shouldAutoOpenEmailAttachm this.shouldAutoOpenEmailAttachmentsFolder.set(shouldAutoOpenEmailAttachmentsFolder); } - public String getCiteCommand() { + public CitationCommandString getCiteCommand() { return citeCommand.get(); } - public StringProperty citeCommandProperty() { + public ObjectProperty citeCommandProperty() { return citeCommand; } - public void setCiteCommand(String citeCommand) { + public void setCiteCommand(CitationCommandString citeCommand) { this.citeCommand.set(citeCommand); } @@ -136,7 +140,7 @@ public void setKindleEmail(String kindleEmail) { this.kindleEmail.set(kindleEmail); } - public String getDefaultCiteCommand() { + public CitationCommandString getDefaultCiteCommand() { return defaultCiteCommand.getValue(); } } diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index 28070ab4ed7..90c6bd7b87f 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -97,6 +97,7 @@ import org.jabref.logic.preview.PreviewLayout; import org.jabref.logic.protectedterms.ProtectedTermsLoader; import org.jabref.logic.protectedterms.ProtectedTermsPreferences; +import org.jabref.logic.push.CitationCommandString; import org.jabref.logic.remote.RemotePreferences; import org.jabref.logic.shared.prefs.SharedDatabasePreferences; import org.jabref.logic.shared.security.Password; @@ -1670,7 +1671,7 @@ public void storeGlobalCitationKeyPattern(GlobalCitationKeyPattern pattern) { || pattern.getDefaultValue().isEmpty()) { put(DEFAULT_CITATION_KEY_PATTERN, ""); } else { - put(DEFAULT_CITATION_KEY_PATTERN, pattern.getDefaultValue().get(0)); + put(DEFAULT_CITATION_KEY_PATTERN, pattern.getDefaultValue().getFirst()); } // Store overridden definitions to Preferences. @@ -1685,7 +1686,7 @@ public void storeGlobalCitationKeyPattern(GlobalCitationKeyPattern pattern) { for (EntryType entryType : pattern.getAllKeys()) { if (!pattern.isDefaultValue(entryType)) { // first entry in the map is the full pattern - preferences.put(entryType.getName(), pattern.getValue(entryType).get(0)); + preferences.put(entryType.getName(), pattern.getValue(entryType).getFirst()); } } } @@ -1810,8 +1811,8 @@ public ExternalApplicationsPreferences getExternalApplicationsPreferences() { externalApplicationsPreferences = new ExternalApplicationsPreferences( get(EMAIL_SUBJECT), getBoolean(OPEN_FOLDERS_OF_ATTACHED_FILES), - get(CITE_COMMAND), - (String) defaults.get(CITE_COMMAND), + CitationCommandString.from(get(CITE_COMMAND)), + CitationCommandString.from((String) defaults.get(CITE_COMMAND)), !getBoolean(USE_DEFAULT_CONSOLE_APPLICATION), // mind the ! get(CONSOLE_COMMAND), !getBoolean(USE_DEFAULT_FILE_BROWSER_APPLICATION), // mind the ! @@ -1823,7 +1824,7 @@ public ExternalApplicationsPreferences getExternalApplicationsPreferences() { EasyBind.listen(externalApplicationsPreferences.autoOpenEmailAttachmentsFolderProperty(), (obs, oldValue, newValue) -> putBoolean(OPEN_FOLDERS_OF_ATTACHED_FILES, newValue)); EasyBind.listen(externalApplicationsPreferences.citeCommandProperty(), - (obs, oldValue, newValue) -> put(CITE_COMMAND, newValue)); + (obs, oldValue, newValue) -> put(CITE_COMMAND, newValue.toString())); EasyBind.listen(externalApplicationsPreferences.useCustomTerminalProperty(), (obs, oldValue, newValue) -> putBoolean(USE_DEFAULT_CONSOLE_APPLICATION, !newValue)); // mind the ! EasyBind.listen(externalApplicationsPreferences.customTerminalCommandProperty(), @@ -1838,6 +1839,7 @@ public ExternalApplicationsPreferences getExternalApplicationsPreferences() { return externalApplicationsPreferences; } + //************************************************************************************************************* // Main table and search dialog preferences //************************************************************************************************************* @@ -2283,8 +2285,8 @@ private void storeExportSaveOrder(SaveOrder saveOrder) { long saveOrderCount = saveOrder.getSortCriteria().size(); if (saveOrderCount >= 1) { - put(EXPORT_PRIMARY_SORT_FIELD, saveOrder.getSortCriteria().get(0).field.getName()); - putBoolean(EXPORT_PRIMARY_SORT_DESCENDING, saveOrder.getSortCriteria().get(0).descending); + put(EXPORT_PRIMARY_SORT_FIELD, saveOrder.getSortCriteria().getFirst().field.getName()); + putBoolean(EXPORT_PRIMARY_SORT_DESCENDING, saveOrder.getSortCriteria().getFirst().descending); } else { put(EXPORT_PRIMARY_SORT_FIELD, ""); putBoolean(EXPORT_PRIMARY_SORT_DESCENDING, false); @@ -2337,7 +2339,7 @@ private List getCustomExportFormats() { for (String toImport : getSeries(CUSTOM_EXPORT_FORMAT)) { List formatData = convertStringToList(toImport); TemplateExporter format = new TemplateExporter( - formatData.get(EXPORTER_NAME_INDEX), + formatData.getFirst(), formatData.get(EXPORTER_FILENAME_INDEX), formatData.get(EXPORTER_EXTENSION_INDEX), layoutPreferences, @@ -2354,10 +2356,10 @@ private void storeCustomExportFormats(List exporters) { } else { for (int i = 0; i < exporters.size(); i++) { List exporterData = new ArrayList<>(); - exporterData.add(EXPORTER_NAME_INDEX, exporters.get(i).getName()); + exporterData.addFirst(exporters.get(i).getName()); exporterData.add(EXPORTER_FILENAME_INDEX, exporters.get(i).getLayoutFileName()); // Only stores the first extension associated with FileType - exporterData.add(EXPORTER_EXTENSION_INDEX, exporters.get(i).getFileType().getExtensions().get(0)); + exporterData.add(EXPORTER_EXTENSION_INDEX, exporters.get(i).getFileType().getExtensions().getFirst()); putStringList(CUSTOM_EXPORT_FORMAT + i, exporterData); } purgeSeries(CUSTOM_EXPORT_FORMAT, exporters.size()); @@ -2861,7 +2863,7 @@ private Set getCustomImportFormats() { importers.add(new CustomImporter(importerString.get(3), importerString.get(2))); } } catch (Exception e) { - LOGGER.warn("Could not load {} from preferences. Will ignore.", importerString.get(0), e); + LOGGER.warn("Could not load {} from preferences. Will ignore.", importerString.getFirst(), e); } } diff --git a/src/main/resources/l10n/JabRef_en.properties b/src/main/resources/l10n/JabRef_en.properties index 019dcfcd8ae..db6189357eb 100644 --- a/src/main/resources/l10n/JabRef_en.properties +++ b/src/main/resources/l10n/JabRef_en.properties @@ -176,6 +176,7 @@ Copy\ citation\ (text)=Copy citation (text) Copy\ citation\ key=Copy citation key Copy\ citation\ key\ and\ link=Copy citation key and link Copy\ citation\ key\ and\ title=Copy citation key and title +Copy\ key\ with\ configured\ cite\ command=Copy key with configured cite command Copy\ to\ clipboard=Copy to clipboard diff --git a/src/test/java/org/jabref/gui/push/PushToEmacsTest.java b/src/test/java/org/jabref/gui/push/PushToEmacsTest.java index 830cfff46b5..7e298a2ec6e 100644 --- a/src/test/java/org/jabref/gui/push/PushToEmacsTest.java +++ b/src/test/java/org/jabref/gui/push/PushToEmacsTest.java @@ -41,7 +41,7 @@ public void setup() { when(preferencesService.getPushToApplicationPreferences()).thenReturn(pushToApplicationPreferences); ExternalApplicationsPreferences externalApplicationsPreferences = mock(ExternalApplicationsPreferences.class); - when(externalApplicationsPreferences.getCiteCommand()).thenReturn("\\cite{key1,key2}"); + when(externalApplicationsPreferences.getCiteCommand().toString()).thenReturn("\\cite{key1,key2}"); when(preferencesService.getExternalApplicationsPreferences()).thenReturn(externalApplicationsPreferences); pushToEmacs = new PushToEmacs(dialogService, preferencesService); diff --git a/src/test/java/org/jabref/preferences/CitationCommandStringTest.java b/src/test/java/org/jabref/preferences/CitationCommandStringTest.java new file mode 100644 index 00000000000..cad7bb4129c --- /dev/null +++ b/src/test/java/org/jabref/preferences/CitationCommandStringTest.java @@ -0,0 +1,21 @@ +package org.jabref.preferences; + +import org.jabref.logic.push.CitationCommandString; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class CitationCommandStringTest { + + @Test + void testToString() { + assertEquals("\\cite{key1,key2}", new CitationCommandString("\\cite{", ",", "}").toString()); + } + + @Test + void from() { + CitationCommandString expected = new CitationCommandString("\\cite{", ",", "}"); + assertEquals(expected, CitationCommandString.from("\\cite{key1,key2}")); + } +} From a4ef04f1c00f1fb4385012d9630e0d74c03d2d4e Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Thu, 21 Dec 2023 20:19:23 +0100 Subject: [PATCH 02/13] add changelog fix checsktyle --- CHANGELOG.md | 1 + .../gui/preferences/external/ExternalTabViewModel.java | 3 --- .../java/org/jabref/logic/push/CitationCommandString.java | 6 +----- 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50053b9c270..bad968181c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv - We fixed some small inconsistencies in the user interface. [#10507](https://github.com/JabRef/jabref/issues/10507) [#10458](https://github.com/JabRef/jabref/issues/10458) [#10660](https://github.com/JabRef/jabref/issues/10660) - We fixed the issue where the Hayagriva YAML exporter would not include a parent field for the publisher/series. [#10596](https://github.com/JabRef/jabref/issues/10596) - We fixed issues in the external file type dialog w.r.t. duplicate entries in the case of a language switch. [#10271](https://github.com/JabRef/jabref/issues/10271) +- We fixe an issue where the right-click action "Copy cite..." did not respect the configured citation command under "External Programs" -> "Push Appplications" [#10615](https://github.com/JabRef/jabref/issues/10615) ### Removed diff --git a/src/main/java/org/jabref/gui/preferences/external/ExternalTabViewModel.java b/src/main/java/org/jabref/gui/preferences/external/ExternalTabViewModel.java index 6a118522c9a..b4688a6af90 100644 --- a/src/main/java/org/jabref/gui/preferences/external/ExternalTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/external/ExternalTabViewModel.java @@ -112,10 +112,7 @@ public void storeSettings() { ExternalApplicationsPreferences externalPreferences = preferences.getExternalApplicationsPreferences(); externalPreferences.setEMailSubject(eMailReferenceSubjectProperty.getValue()); externalPreferences.setAutoOpenEmailAttachmentsFolder(autoOpenAttachedFoldersProperty.getValue()); - - externalPreferences.setCiteCommand(CitationCommandString.from(citeCommandProperty.getValue())); - externalPreferences.setUseCustomTerminal(useCustomTerminalProperty.getValue()); externalPreferences.setCustomTerminalCommand(customTerminalCommandProperty.getValue()); externalPreferences.setUseCustomFileBrowser(useCustomFileBrowserProperty.getValue()); diff --git a/src/main/java/org/jabref/logic/push/CitationCommandString.java b/src/main/java/org/jabref/logic/push/CitationCommandString.java index 2544bb41fcc..da45ea3f065 100644 --- a/src/main/java/org/jabref/logic/push/CitationCommandString.java +++ b/src/main/java/org/jabref/logic/push/CitationCommandString.java @@ -1,9 +1,6 @@ package org.jabref.logic.push; -public record CitationCommandString( - String prefix, - String delimiter, - String suffix) { +public record CitationCommandString(String prefix, String delimiter, String suffix) { private static final String CITE_KEY1 = "key1"; private static final String CITE_KEY2 = "key2"; @@ -13,7 +10,6 @@ public String toString() { } public static CitationCommandString from(String completeCiteCommand) { - int indexKey1 = completeCiteCommand.indexOf(CITE_KEY1); int indexKey2 = completeCiteCommand.indexOf(CITE_KEY2); if (indexKey1 < 0 || indexKey2 < 0 || indexKey2 < (indexKey1 + CITE_KEY1.length())) { From 10f748b62d3b21fff4a7c1b33fe15d02c5409802 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Thu, 21 Dec 2023 21:40:30 +0100 Subject: [PATCH 03/13] Fix CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bad968181c5..034fcdfc0b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,7 +37,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv - We fixed some small inconsistencies in the user interface. [#10507](https://github.com/JabRef/jabref/issues/10507) [#10458](https://github.com/JabRef/jabref/issues/10458) [#10660](https://github.com/JabRef/jabref/issues/10660) - We fixed the issue where the Hayagriva YAML exporter would not include a parent field for the publisher/series. [#10596](https://github.com/JabRef/jabref/issues/10596) - We fixed issues in the external file type dialog w.r.t. duplicate entries in the case of a language switch. [#10271](https://github.com/JabRef/jabref/issues/10271) -- We fixe an issue where the right-click action "Copy cite..." did not respect the configured citation command under "External Programs" -> "Push Appplications" [#10615](https://github.com/JabRef/jabref/issues/10615) +- We fixed an issue where the right-click action "Copy cite..." did not respect the configured citation command under "External Programs" -> "[Push Applications](https://docs.jabref.org/cite/pushtoapplications)" [#10615](https://github.com/JabRef/jabref/issues/10615) ### Removed From ed5e3a24899335fc95ce79ebf1e427b82d8266ca Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Thu, 21 Dec 2023 21:47:02 +0100 Subject: [PATCH 04/13] Add missing "citation" to "key" --- src/main/java/org/jabref/gui/actions/StandardActions.java | 2 +- src/main/resources/l10n/JabRef_en.properties | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jabref/gui/actions/StandardActions.java b/src/main/java/org/jabref/gui/actions/StandardActions.java index 546d7c95a3e..b53ecee6cc0 100644 --- a/src/main/java/org/jabref/gui/actions/StandardActions.java +++ b/src/main/java/org/jabref/gui/actions/StandardActions.java @@ -13,7 +13,7 @@ public enum StandardActions implements Action { COPY_MORE(Localization.lang("Copy") + "..."), COPY_TITLE(Localization.lang("Copy title"), KeyBinding.COPY_TITLE), COPY_KEY(Localization.lang("Copy citation key"), KeyBinding.COPY_CITATION_KEY), - COPY_CITE_KEY(Localization.lang("Copy key with configured cite command"), KeyBinding.COPY_CITE_CITATION_KEY), + COPY_CITE_KEY(Localization.lang("Copy citation key with configured cite command"), KeyBinding.COPY_CITE_CITATION_KEY), COPY_KEY_AND_TITLE(Localization.lang("Copy citation key and title"), KeyBinding.COPY_CITATION_KEY_AND_TITLE), COPY_KEY_AND_LINK(Localization.lang("Copy citation key and link"), KeyBinding.COPY_CITATION_KEY_AND_LINK), COPY_CITATION_HTML(Localization.lang("Copy citation (html)"), KeyBinding.COPY_PREVIEW), diff --git a/src/main/resources/l10n/JabRef_en.properties b/src/main/resources/l10n/JabRef_en.properties index db6189357eb..d3bb165d290 100644 --- a/src/main/resources/l10n/JabRef_en.properties +++ b/src/main/resources/l10n/JabRef_en.properties @@ -176,7 +176,7 @@ Copy\ citation\ (text)=Copy citation (text) Copy\ citation\ key=Copy citation key Copy\ citation\ key\ and\ link=Copy citation key and link Copy\ citation\ key\ and\ title=Copy citation key and title -Copy\ key\ with\ configured\ cite\ command=Copy key with configured cite command +Copy\ citation\ key\ with\ configured\ cite\ command=Copy citation key with configured cite command Copy\ to\ clipboard=Copy to clipboard From bac658bc74994289858f3e315dcb0087cbcbaf21 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Thu, 21 Dec 2023 21:48:14 +0100 Subject: [PATCH 05/13] Add debug message at error in logic in CopyMoreAction --- src/main/java/org/jabref/gui/edit/CopyMoreAction.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/org/jabref/gui/edit/CopyMoreAction.java b/src/main/java/org/jabref/gui/edit/CopyMoreAction.java index c885f9888af..1c325c75a95 100644 --- a/src/main/java/org/jabref/gui/edit/CopyMoreAction.java +++ b/src/main/java/org/jabref/gui/edit/CopyMoreAction.java @@ -259,6 +259,9 @@ private void copyKeyAndLink() { for (BibEntry entry : entriesWithKey) { String key = entry.getCitationKey().orElse(""); + if (LOGGER.isDebugEnabled() && key.isEmpty()) { + LOGGER.debug("entry {} had no citation key, but it should have had one", entry); + } String url = entry.getField(StandardField.URL).orElse(""); keyAndLink.append(url.isEmpty() ? key : String.format("%s", url, key)); keyAndLink.append(OS.NEWLINE); From f3ee1848f685d2231032144a7ca02e6af62022d2 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Thu, 21 Dec 2023 21:48:41 +0100 Subject: [PATCH 06/13] Add fallback if preference could not be parsed --- .../java/org/jabref/logic/push/CitationCommandString.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/jabref/logic/push/CitationCommandString.java b/src/main/java/org/jabref/logic/push/CitationCommandString.java index da45ea3f065..5e8a35e4ea5 100644 --- a/src/main/java/org/jabref/logic/push/CitationCommandString.java +++ b/src/main/java/org/jabref/logic/push/CitationCommandString.java @@ -1,6 +1,10 @@ package org.jabref.logic.push; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public record CitationCommandString(String prefix, String delimiter, String suffix) { + private static final Logger LOGGER = LoggerFactory.getLogger(CitationCommandString.class); private static final String CITE_KEY1 = "key1"; private static final String CITE_KEY2 = "key2"; @@ -13,7 +17,8 @@ public static CitationCommandString from(String completeCiteCommand) { int indexKey1 = completeCiteCommand.indexOf(CITE_KEY1); int indexKey2 = completeCiteCommand.indexOf(CITE_KEY2); if (indexKey1 < 0 || indexKey2 < 0 || indexKey2 < (indexKey1 + CITE_KEY1.length())) { - return new CitationCommandString("", "", ""); + LOGGER.error("Wrong indexes {} {} for completeCiteCommand {}. Using default.", indexKey1, indexKey2, completeCiteCommand); + return new CitationCommandString("\\cite{", ",", "}"); } String prefix = completeCiteCommand.substring(0, indexKey1); From bc64e5f410d50aaa3e2853acc32788d16cd913db Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Thu, 21 Dec 2023 21:52:12 +0100 Subject: [PATCH 07/13] Fix key binding setting --- src/main/java/org/jabref/gui/keyboard/KeyBinding.java | 5 ++++- src/main/resources/l10n/JabRef_en.properties | 1 - 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jabref/gui/keyboard/KeyBinding.java b/src/main/java/org/jabref/gui/keyboard/KeyBinding.java index 5706d55cc7b..506d3fc40e4 100644 --- a/src/main/java/org/jabref/gui/keyboard/KeyBinding.java +++ b/src/main/java/org/jabref/gui/keyboard/KeyBinding.java @@ -32,7 +32,10 @@ public enum KeyBinding { CLOSE("Close dialog", Localization.lang("Close dialog"), "Esc", KeyBindingCategory.VIEW), COPY("Copy", Localization.lang("Copy"), "ctrl+C", KeyBindingCategory.EDIT), COPY_TITLE("Copy title", Localization.lang("Copy title"), "ctrl+shift+alt+T", KeyBindingCategory.EDIT), - COPY_CITE_CITATION_KEY("Copy \\cite{citation key}", Localization.lang("Copy \\cite{citation key}"), "ctrl+K", KeyBindingCategory.EDIT), + + // We migrated from "Copy \\cite{citation key}" to "Copy citation key with configured cite command", therefore we keep the "old string" for backwards comppatibility + COPY_CITE_CITATION_KEY("Copy \\cite{citation key}", Localization.lang("Copy citation key with configured cite command"), "ctrl+K", KeyBindingCategory.EDIT), + COPY_CITATION_KEY("Copy citation key", Localization.lang("Copy citation key"), "ctrl+shift+K", KeyBindingCategory.EDIT), COPY_CITATION_KEY_AND_TITLE("Copy citation key and title", Localization.lang("Copy citation key and title"), "ctrl+shift+alt+K", KeyBindingCategory.EDIT), COPY_CITATION_KEY_AND_LINK("Copy citation key and link", Localization.lang("Copy citation key and link"), "ctrl+alt+K", KeyBindingCategory.EDIT), diff --git a/src/main/resources/l10n/JabRef_en.properties b/src/main/resources/l10n/JabRef_en.properties index d3bb165d290..25dde6cd490 100644 --- a/src/main/resources/l10n/JabRef_en.properties +++ b/src/main/resources/l10n/JabRef_en.properties @@ -170,7 +170,6 @@ Copied=Copied Copy=Copy Copy\ title=Copy title -Copy\ \\cite{citation\ key}=Copy \\cite{citation key} Copy\ citation\ (html)=Copy citation (html) Copy\ citation\ (text)=Copy citation (text) Copy\ citation\ key=Copy citation key From ff3d6c30e0db14f70bc4f7e50164859fb33c6808 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Thu, 21 Dec 2023 22:12:41 +0100 Subject: [PATCH 08/13] More relaxed parsing --- .../org/jabref/logic/push/CitationCommandString.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jabref/logic/push/CitationCommandString.java b/src/main/java/org/jabref/logic/push/CitationCommandString.java index 5e8a35e4ea5..8a645e51fba 100644 --- a/src/main/java/org/jabref/logic/push/CitationCommandString.java +++ b/src/main/java/org/jabref/logic/push/CitationCommandString.java @@ -17,8 +17,13 @@ public static CitationCommandString from(String completeCiteCommand) { int indexKey1 = completeCiteCommand.indexOf(CITE_KEY1); int indexKey2 = completeCiteCommand.indexOf(CITE_KEY2); if (indexKey1 < 0 || indexKey2 < 0 || indexKey2 < (indexKey1 + CITE_KEY1.length())) { - LOGGER.error("Wrong indexes {} {} for completeCiteCommand {}. Using default.", indexKey1, indexKey2, completeCiteCommand); - return new CitationCommandString("\\cite{", ",", "}"); + LOGGER.info("Wrong indexes {} {} for completeCiteCommand {}. Using default delimiter and suffix.", indexKey1, indexKey2, completeCiteCommand); + if (completeCiteCommand.isEmpty()) { + completeCiteCommand = "\\cite{"; + } else if (!completeCiteCommand.endsWith("{")) { + completeCiteCommand += "{"; + } + return new CitationCommandString(completeCiteCommand, ",", "}"); } String prefix = completeCiteCommand.substring(0, indexKey1); From 0b1fc6b26acba8d377b2282dd0533e57b7cbba57 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Thu, 21 Dec 2023 22:51:51 +0100 Subject: [PATCH 09/13] Streamline code in AbstractPushToApplication --- .../gui/push/AbstractPushToApplication.java | 22 +++---------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/jabref/gui/push/AbstractPushToApplication.java b/src/main/java/org/jabref/gui/push/AbstractPushToApplication.java index 0928ff7b3e8..3de3e190432 100644 --- a/src/main/java/org/jabref/gui/push/AbstractPushToApplication.java +++ b/src/main/java/org/jabref/gui/push/AbstractPushToApplication.java @@ -10,7 +10,6 @@ import org.jabref.gui.icon.JabRefIcon; import org.jabref.gui.keyboard.KeyBinding; import org.jabref.logic.l10n.Localization; -import org.jabref.logic.push.CitationCommandString; import org.jabref.logic.util.OS; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; @@ -36,23 +35,11 @@ public abstract class AbstractPushToApplication implements PushToApplication { protected final DialogService dialogService; protected final PreferencesService preferencesService; - private String cachedCitePrefix; - private String cachedCiteSuffix; - private String cachedCiteDelimiter; - public AbstractPushToApplication(DialogService dialogService, PreferencesService preferencesService) { this.dialogService = dialogService; this.preferencesService = preferencesService; } - private void dissectCiteCommand() { - CitationCommandString preferencesCiteCommand = preferencesService.getExternalApplicationsPreferences().getCiteCommand(); - - cachedCitePrefix = preferencesCiteCommand.prefix(); - cachedCiteDelimiter = preferencesCiteCommand.delimiter(); - cachedCiteSuffix = preferencesCiteCommand.suffix(); - } - @Override public JabRefIcon getApplicationIcon() { return IconTheme.JabRefIcons.APPLICATION_GENERIC; @@ -155,18 +142,15 @@ protected String getCommandName() { } protected String getCitePrefix() { - dissectCiteCommand(); - return cachedCitePrefix; + return preferencesService.getExternalApplicationsPreferences().getCiteCommand().prefix(); } public String getDelimiter() { - dissectCiteCommand(); - return cachedCiteDelimiter; + return preferencesService.getExternalApplicationsPreferences().getCiteCommand().delimiter(); } protected String getCiteSuffix() { - dissectCiteCommand(); - return cachedCiteSuffix; + return preferencesService.getExternalApplicationsPreferences().getCiteCommand().suffix(); } @Override From b501709f01c1e6ce0f6de7f09c0ba074d380b71c Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Thu, 21 Dec 2023 22:52:09 +0100 Subject: [PATCH 10/13] Add more test cases for configured citation command --- .../CitationCommandStringTest.java | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/jabref/preferences/CitationCommandStringTest.java b/src/test/java/org/jabref/preferences/CitationCommandStringTest.java index cad7bb4129c..0bca17e7eed 100644 --- a/src/test/java/org/jabref/preferences/CitationCommandStringTest.java +++ b/src/test/java/org/jabref/preferences/CitationCommandStringTest.java @@ -1,8 +1,13 @@ package org.jabref.preferences; +import java.util.stream.Stream; + import org.jabref.logic.push.CitationCommandString; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -13,9 +18,27 @@ void testToString() { assertEquals("\\cite{key1,key2}", new CitationCommandString("\\cite{", ",", "}").toString()); } - @Test - void from() { - CitationCommandString expected = new CitationCommandString("\\cite{", ",", "}"); - assertEquals(expected, CitationCommandString.from("\\cite{key1,key2}")); + public static Stream from() { + return Stream.of( + Arguments.of( + new CitationCommandString("\\cite{", ",", "}"), + "\\cite{key1,key2}" + ), + Arguments.of( + new CitationCommandString("\\cite{", ",", "}"), + "\\cite" + ), + // We could do better, but this is very seldom + Arguments.of( + new CitationCommandString("\\cite[key1,key]{", ",", "}"), + "\\cite[key1,key]" + ) + ); + } + + @ParameterizedTest + @MethodSource + void from(CitationCommandString expected, String input) { + assertEquals(expected, CitationCommandString.from(input)); } } From 2a4fe1b0a8bbb7187d1b3152d75ff0b68feffbce Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Thu, 21 Dec 2023 22:52:29 +0100 Subject: [PATCH 11/13] Streamline code for copy action --- CHANGELOG.md | 1 + .../org/jabref/gui/edit/CopyMoreAction.java | 48 +++++++------------ 2 files changed, 17 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 034fcdfc0b5..606bf9aa981 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv - We upgraded to JavaFX 21.0.1. As a consequence JabRef requires now macOS 11 or later and GTK 3.8 or later on Linux [10627](https://github.com/JabRef/jabref/pull/10627). - A user-specific comment fields is not enabled by default, but can be enabled using the "Add" button. [#10424](https://github.com/JabRef/jabref/issues/10424) - We upgraded to Lucene 9.9 for the fulltext search. The search index will be rebuild. [#10686](https://github.com/JabRef/jabref/pull/10686) +- When using "Copy..." -> "Copy citation key", the delimiter configured at "Push applications" is respected. [#10707](https://github.com/JabRef/jabref/pull/10707) ### Fixed diff --git a/src/main/java/org/jabref/gui/edit/CopyMoreAction.java b/src/main/java/org/jabref/gui/edit/CopyMoreAction.java index 1c325c75a95..36a770b4422 100644 --- a/src/main/java/org/jabref/gui/edit/CopyMoreAction.java +++ b/src/main/java/org/jabref/gui/edit/CopyMoreAction.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.io.StringReader; import java.util.List; +import java.util.function.Function; import java.util.stream.Collectors; import org.jabref.gui.ClipBoardManager; @@ -102,33 +103,6 @@ private void copyTitle() { } } - private void copyKey() { - List entries = stateManager.getSelectedEntries(); - - // Collect all non-null keys. - List keys = entries.stream() - .filter(entry -> entry.getCitationKey().isPresent()) - .map(entry -> entry.getCitationKey().get()) - .collect(Collectors.toList()); - - if (keys.isEmpty()) { - dialogService.notify(Localization.lang("None of the selected entries have citation keys.")); - return; - } - - final String copiedKeys = String.join(",", keys); - clipBoardManager.setContent(copiedKeys); - - if (keys.size() == entries.size()) { - // All entries had keys. - dialogService.notify(Localization.lang("Copied '%0' to clipboard.", - JabRefDialogService.shortenDialogMessage(copiedKeys))); - } else { - dialogService.notify(Localization.lang("Warning: %0 out of %1 entries have undefined citation key.", - Integer.toString(entries.size() - keys.size()), Integer.toString(entries.size()))); - } - } - private void copyDoi() { List entries = stateManager.getSelectedEntries(); @@ -165,7 +139,7 @@ private void copyDoiList(List dois, int size) { } } - private void copyCiteKey() { + private void doCopyKey(Function, String> mapKeyList) { List entries = stateManager.getSelectedEntries(); // Collect all non-null keys. @@ -179,21 +153,31 @@ private void copyCiteKey() { return; } - CitationCommandString citeCommand = preferencesService.getExternalApplicationsPreferences().getCiteCommand(); + String clipBoardContent = mapKeyList.apply(keys); - final String copiedCiteCommand = citeCommand.prefix() + String.join(citeCommand.delimiter(), keys) + citeCommand.suffix(); - clipBoardManager.setContent(copiedCiteCommand); + clipBoardManager.setContent(clipBoardContent); if (keys.size() == entries.size()) { // All entries had keys. dialogService.notify(Localization.lang("Copied '%0' to clipboard.", - JabRefDialogService.shortenDialogMessage(copiedCiteCommand))); + JabRefDialogService.shortenDialogMessage(clipBoardContent))); } else { dialogService.notify(Localization.lang("Warning: %0 out of %1 entries have undefined citation key.", Integer.toString(entries.size() - keys.size()), Integer.toString(entries.size()))); } } + private void copyCiteKey() { + doCopyKey(keys -> { + CitationCommandString citeCommand = preferencesService.getExternalApplicationsPreferences().getCiteCommand(); + return citeCommand.prefix() + String.join(citeCommand.delimiter(), keys) + citeCommand.suffix(); + }); + } + + private void copyKey() { + doCopyKey(keys -> String.join(preferencesService.getExternalApplicationsPreferences().getCiteCommand().delimiter(), keys)); + } + private void copyKeyAndTitle() { List entries = stateManager.getSelectedEntries(); From 28d003d868ae1246129e95f41f1c66fbc30e067c Mon Sep 17 00:00:00 2001 From: Christoph Date: Thu, 21 Dec 2023 23:26:00 +0100 Subject: [PATCH 12/13] Update src/main/java/org/jabref/gui/preferences/external/ExternalTabViewModel.java --- .../jabref/gui/preferences/external/ExternalTabViewModel.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/jabref/gui/preferences/external/ExternalTabViewModel.java b/src/main/java/org/jabref/gui/preferences/external/ExternalTabViewModel.java index b4688a6af90..365b7b694d0 100644 --- a/src/main/java/org/jabref/gui/preferences/external/ExternalTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/external/ExternalTabViewModel.java @@ -40,7 +40,7 @@ public class ExternalTabViewModel implements PreferenceTabViewModel { private final BooleanProperty autoOpenAttachedFoldersProperty = new SimpleBooleanProperty(); private final ListProperty pushToApplicationsListProperty = new SimpleListProperty<>(); private final ObjectProperty selectedPushToApplicationProperty = new SimpleObjectProperty<>(); - private final StringProperty citeCommandProperty = new SimpleStringProperty(); + private final StringProperty citeCommandProperty = new SimpleStringProperty(""); private final BooleanProperty useCustomTerminalProperty = new SimpleBooleanProperty(); private final StringProperty customTerminalCommandProperty = new SimpleStringProperty(""); private final BooleanProperty useCustomFileBrowserProperty = new SimpleBooleanProperty(); From d6b1de1c93196af06a5c713e9604eb9d9c52ef69 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Fri, 22 Dec 2023 03:14:18 +0100 Subject: [PATCH 13/13] Fix test --- src/test/java/org/jabref/gui/edit/CopyMoreActionTest.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/java/org/jabref/gui/edit/CopyMoreActionTest.java b/src/test/java/org/jabref/gui/edit/CopyMoreActionTest.java index d5fe209a3a0..ba3c8465be1 100644 --- a/src/test/java/org/jabref/gui/edit/CopyMoreActionTest.java +++ b/src/test/java/org/jabref/gui/edit/CopyMoreActionTest.java @@ -14,11 +14,13 @@ import org.jabref.gui.actions.StandardActions; import org.jabref.logic.journals.JournalAbbreviationRepository; import org.jabref.logic.l10n.Localization; +import org.jabref.logic.push.CitationCommandString; import org.jabref.model.database.BibDatabase; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.field.StandardField; import org.jabref.model.entry.types.StandardEntryType; +import org.jabref.preferences.ExternalApplicationsPreferences; import org.jabref.preferences.PreferencesService; import org.junit.jupiter.api.BeforeEach; @@ -58,6 +60,10 @@ public void setUp() { titles.add(title); keys.add("abc"); dois.add("10.1145/3377811.3380330"); + + ExternalApplicationsPreferences externalApplicationsPreferences = mock(ExternalApplicationsPreferences.class); + when(externalApplicationsPreferences.getCiteCommand()).thenReturn(new CitationCommandString("\\cite{", ",", "}")); + when(preferencesService.getExternalApplicationsPreferences()).thenReturn(externalApplicationsPreferences); } @Test