diff --git a/CHANGELOG.md b/CHANGELOG.md index 62cb1e5e539..ed83bb94f8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,7 +45,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# - We fixed the import of MS-Office XML files, when the `month` field contained an invalid value. - ArXiV fetcher now checks similarity of entry when using DOI retrieval to avoid false positives [#2575](https://github.com/JabRef/jabref/issues/2575) - Sciencedirect/Elsevier fetcher is now able to scrape new HTML structure [#2576](https://github.com/JabRef/jabref/issues/2576) - + - Fixed the synchronization logic of keywords and special fields and vice versa [#2580](https://github.com/JabRef/jabref/issues/2580) ### Removed diff --git a/src/main/java/org/jabref/gui/FileDialog.java b/src/main/java/org/jabref/gui/FileDialog.java index 5b22f554534..c1e3e0d9359 100644 --- a/src/main/java/org/jabref/gui/FileDialog.java +++ b/src/main/java/org/jabref/gui/FileDialog.java @@ -35,7 +35,6 @@ */ @Deprecated public class FileDialog { - private static final Log LOGGER = LogFactory.getLog(FileDialog.class); private final FileChooser fileChooser; diff --git a/src/main/java/org/jabref/gui/specialfields/SpecialFieldDatabaseChangeListener.java b/src/main/java/org/jabref/gui/specialfields/SpecialFieldDatabaseChangeListener.java index 0c49f90a3d4..1ede5e8007e 100644 --- a/src/main/java/org/jabref/gui/specialfields/SpecialFieldDatabaseChangeListener.java +++ b/src/main/java/org/jabref/gui/specialfields/SpecialFieldDatabaseChangeListener.java @@ -26,19 +26,19 @@ public static SpecialFieldDatabaseChangeListener getInstance() { @Subscribe public void listen(EntryAddedEvent event) { - if (Globals.prefs.isKeywordSyncEnabled()) { - final BibEntry entry = event.getBibEntry(); - // NamedCompount code similar to SpecialFieldUpdateListener - NamedCompound nc = new NamedCompound(Localization.lang("Synchronized special fields based on keywords")); - List changes = SpecialFieldsUtils.syncSpecialFieldsFromKeywords(entry, Globals.prefs.getKeywordDelimiter()); - for(FieldChange change: changes) { - nc.addEdit(new UndoableFieldChange(change)); - } - - // Don't insert the compound into the undoManager, - // it would be added before the component which undoes the insertion of the entry and creates heavy problems - // (which prohibits the undo the deleting multiple entries) + if (!Globals.prefs.isKeywordSyncEnabled()) { + return; } - } + final BibEntry entry = event.getBibEntry(); + // NamedCompount code similar to SpecialFieldUpdateListener + NamedCompound nc = new NamedCompound(Localization.lang("Synchronized special fields based on keywords")); + List changes = SpecialFieldsUtils.syncSpecialFieldsFromKeywords(entry, Globals.prefs.getKeywordDelimiter()); + for(FieldChange change: changes) { + nc.addEdit(new UndoableFieldChange(change)); + } + // Don't insert the compound into the undoManager, + // it would be added before the component which undoes the insertion of the entry and creates heavy problems + // (which prohibits the undo the deleting multiple entries) + } } diff --git a/src/main/java/org/jabref/gui/specialfields/SpecialFieldUpdateListener.java b/src/main/java/org/jabref/gui/specialfields/SpecialFieldUpdateListener.java index 46f3fb5c540..30abd646262 100644 --- a/src/main/java/org/jabref/gui/specialfields/SpecialFieldUpdateListener.java +++ b/src/main/java/org/jabref/gui/specialfields/SpecialFieldUpdateListener.java @@ -23,6 +23,11 @@ public class SpecialFieldUpdateListener { @Subscribe public void listen(FieldChangedEvent fieldChangedEvent) { + // only sync if keyword sync is enabled + if (!Globals.prefs.isKeywordSyncEnabled()) { + return; + } + final BibEntry entry = fieldChangedEvent.getBibEntry(); final String fieldName = fieldChangedEvent.getFieldName(); // Source editor cycles through all entries @@ -32,15 +37,10 @@ public void listen(FieldChangedEvent fieldChangedEvent) { SwingUtilities.invokeLater(() -> { if (FieldName.KEYWORDS.equals(fieldName)) { SpecialFieldsUtils.syncSpecialFieldsFromKeywords(entry, Globals.prefs.getKeywordDelimiter()); - SwingUtilities - .invokeLater(() -> JabRefGUI.getMainFrame().getCurrentBasePanel().updateEntryEditorIfShowing()); - } else { - if (SpecialField.isSpecialField(fieldName)) { - SpecialFieldsUtils.syncKeywordsFromSpecialFields(entry, Globals.prefs.isKeywordSyncEnabled(), Globals.prefs.getKeywordDelimiter()); - SwingUtilities.invokeLater( - () -> JabRefGUI.getMainFrame().getCurrentBasePanel().updateEntryEditorIfShowing()); - } + } else if (SpecialField.isSpecialField(fieldName)) { + SpecialFieldsUtils.syncKeywordsFromSpecialFields(entry, Globals.prefs.getKeywordDelimiter()); } + SwingUtilities.invokeLater(() -> JabRefGUI.getMainFrame().getCurrentBasePanel().updateEntryEditorIfShowing()); }); } diff --git a/src/main/java/org/jabref/logic/specialfields/SpecialFieldsUtils.java b/src/main/java/org/jabref/logic/specialfields/SpecialFieldsUtils.java index bb6870c5b25..125a153c710 100644 --- a/src/main/java/org/jabref/logic/specialfields/SpecialFieldsUtils.java +++ b/src/main/java/org/jabref/logic/specialfields/SpecialFieldsUtils.java @@ -35,18 +35,16 @@ public static List updateField(SpecialField field, String value, Bi UpdateField.updateField(entry, field.getFieldName(), value, nullFieldIfValueIsTheSame) .ifPresent(fieldChange -> fieldChanges.add(fieldChange)); // we cannot use "value" here as updateField has side effects: "nullFieldIfValueIsTheSame" nulls the field if value is the same - fieldChanges.addAll(SpecialFieldsUtils.exportFieldToKeywords(field, entry, isKeywordSyncEnabled, keywordDelimiter)); + if (isKeywordSyncEnabled) { + fieldChanges.addAll(SpecialFieldsUtils.exportFieldToKeywords(field, entry, keywordDelimiter)); + } return fieldChanges; } - private static List exportFieldToKeywords(SpecialField specialField, BibEntry entry, boolean isKeywordSyncEnabled, Character keywordDelimiter) { + private static List exportFieldToKeywords(SpecialField specialField, BibEntry entry, Character keywordDelimiter) { List fieldChanges = new ArrayList<>(); - if (!isKeywordSyncEnabled) { - return fieldChanges; - } - Optional newValue = entry.getField(specialField.getFieldName()).map(Keyword::new); KeywordList keyWords = specialField.getKeyWords(); @@ -59,11 +57,11 @@ private static List exportFieldToKeywords(SpecialField specialField /** * Update keywords according to values of special fields */ - public static List syncKeywordsFromSpecialFields(BibEntry entry, boolean isKeywordSyncEnabled, Character keywordDelimiter) { + public static List syncKeywordsFromSpecialFields(BibEntry entry, Character keywordDelimiter) { List fieldChanges = new ArrayList<>(); for(SpecialField field: SpecialField.values()) { - fieldChanges.addAll(SpecialFieldsUtils.exportFieldToKeywords(field, entry, isKeywordSyncEnabled, keywordDelimiter)); + fieldChanges.addAll(SpecialFieldsUtils.exportFieldToKeywords(field, entry, keywordDelimiter)); } return fieldChanges; @@ -71,6 +69,7 @@ public static List syncKeywordsFromSpecialFields(BibEntry entry, bo private static List importKeywordsForField(KeywordList keywordList, SpecialField field, BibEntry entry) { List fieldChanges = new ArrayList<>(); + KeywordList values = field.getKeyWords(); Optional newValue = Optional.empty(); for (Keyword keyword : values) { @@ -88,7 +87,7 @@ private static List importKeywordsForField(KeywordList keywordList, } /** - * updates field values according to keywords + * Updates special field values according to keywords */ public static List syncSpecialFieldsFromKeywords(BibEntry entry, Character keywordDelimiter) { List fieldChanges = new ArrayList<>(); diff --git a/src/test/java/org/jabref/logic/specialfields/SpecialFieldsUtilsTest.java b/src/test/java/org/jabref/logic/specialfields/SpecialFieldsUtilsTest.java index d99c1e0219e..26ea98db537 100644 --- a/src/test/java/org/jabref/logic/specialfields/SpecialFieldsUtilsTest.java +++ b/src/test/java/org/jabref/logic/specialfields/SpecialFieldsUtilsTest.java @@ -19,7 +19,7 @@ public class SpecialFieldsUtilsTest { public void syncKeywordsFromSpecialFieldsWritesToKeywords() { BibEntry entry = new BibEntry(); entry.setField("ranking", "rank2"); - SpecialFieldsUtils.syncKeywordsFromSpecialFields(entry, true, ','); + SpecialFieldsUtils.syncKeywordsFromSpecialFields(entry, ','); assertEquals(Optional.of("rank2"), entry.getField("keywords")); } @@ -27,7 +27,7 @@ public void syncKeywordsFromSpecialFieldsWritesToKeywords() { public void syncKeywordsFromSpecialFieldsCausesChange() { BibEntry entry = new BibEntry(); entry.setField("ranking", "rank2"); - List changes = SpecialFieldsUtils.syncKeywordsFromSpecialFields(entry, true, ','); + List changes = SpecialFieldsUtils.syncKeywordsFromSpecialFields(entry, ','); assertTrue(changes.size() > 0); } @@ -36,14 +36,14 @@ public void syncKeywordsFromSpecialFieldsOverwritesKeywords() { BibEntry entry = new BibEntry(); entry.setField("ranking", "rank2"); entry.setField("keywords", "rank3"); - SpecialFieldsUtils.syncKeywordsFromSpecialFields(entry, true, ','); + SpecialFieldsUtils.syncKeywordsFromSpecialFields(entry, ','); assertEquals(Optional.of("rank2"), entry.getField("keywords")); } @Test public void syncKeywordsFromSpecialFieldsForEmptyFieldCausesNoChange() { BibEntry entry = new BibEntry(); - List changes = SpecialFieldsUtils.syncKeywordsFromSpecialFields(entry, true, ','); + List changes = SpecialFieldsUtils.syncKeywordsFromSpecialFields(entry, ','); assertFalse(changes.size() > 0); }