Skip to content
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

Keyword - Special field synchronization #2583

Merged
merged 4 commits into from
Feb 22, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/jabref/gui/FileDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
*/
@Deprecated
public class FileDialog {

private static final Log LOGGER = LogFactory.getLog(FileDialog.class);

private final FileChooser fileChooser;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FieldChange> 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<FieldChange> 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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,16 @@ public static List<FieldChange> 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<FieldChange> exportFieldToKeywords(SpecialField specialField, BibEntry entry, boolean isKeywordSyncEnabled, Character keywordDelimiter) {
private static List<FieldChange> exportFieldToKeywords(SpecialField specialField, BibEntry entry, Character keywordDelimiter) {
List<FieldChange> fieldChanges = new ArrayList<>();

if (!isKeywordSyncEnabled) {
return fieldChanges;
}

Optional<Keyword> newValue = entry.getField(specialField.getFieldName()).map(Keyword::new);
KeywordList keyWords = specialField.getKeyWords();

Expand All @@ -59,18 +57,19 @@ private static List<FieldChange> exportFieldToKeywords(SpecialField specialField
/**
* Update keywords according to values of special fields
*/
public static List<FieldChange> syncKeywordsFromSpecialFields(BibEntry entry, boolean isKeywordSyncEnabled, Character keywordDelimiter) {
public static List<FieldChange> syncKeywordsFromSpecialFields(BibEntry entry, Character keywordDelimiter) {
List<FieldChange> 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;
}

private static List<FieldChange> importKeywordsForField(KeywordList keywordList, SpecialField field, BibEntry entry) {
List<FieldChange> fieldChanges = new ArrayList<>();

KeywordList values = field.getKeyWords();
Optional<Keyword> newValue = Optional.empty();
for (Keyword keyword : values) {
Expand All @@ -88,7 +87,7 @@ private static List<FieldChange> importKeywordsForField(KeywordList keywordList,
}

/**
* updates field values according to keywords
* Updates special field values according to keywords
*/
public static List<FieldChange> syncSpecialFieldsFromKeywords(BibEntry entry, Character keywordDelimiter) {
List<FieldChange> fieldChanges = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ 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"));
}

@Test
public void syncKeywordsFromSpecialFieldsCausesChange() {
BibEntry entry = new BibEntry();
entry.setField("ranking", "rank2");
List<FieldChange> changes = SpecialFieldsUtils.syncKeywordsFromSpecialFields(entry, true, ',');
List<FieldChange> changes = SpecialFieldsUtils.syncKeywordsFromSpecialFields(entry, ',');
assertTrue(changes.size() > 0);
}

Expand All @@ -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<FieldChange> changes = SpecialFieldsUtils.syncKeywordsFromSpecialFields(entry, true, ',');
List<FieldChange> changes = SpecialFieldsUtils.syncKeywordsFromSpecialFields(entry, ',');
assertFalse(changes.size() > 0);
}

Expand Down