use of org.jabref.model.FieldChange in project jabref by JabRef.
the class FieldFormatterCleanup method cleanupSingleField.
/**
* Runs the formatter on the specified field in the given entry.
*
* If the formatter returns an empty string, then the field is removed.
* @param fieldKey the field on which to run the formatter
* @param entry the entry to be cleaned up
* @return a list of changes of the entry
*/
private List<FieldChange> cleanupSingleField(String fieldKey, BibEntry entry) {
if (!entry.hasField(fieldKey)) {
// Not set -> nothing to do
return new ArrayList<>();
}
String oldValue = entry.getField(fieldKey).orElse(null);
// Run formatter
String newValue = formatter.format(oldValue);
if (oldValue.equals(newValue)) {
return new ArrayList<>();
} else {
if (newValue.isEmpty()) {
entry.clearField(fieldKey);
newValue = null;
} else {
entry.setField(fieldKey, newValue, EntryEventSource.SAVE_ACTION);
}
FieldChange change = new FieldChange(entry, fieldKey, oldValue, newValue);
return Collections.singletonList(change);
}
}
use of org.jabref.model.FieldChange in project jabref by JabRef.
the class BibEntry method setField.
/**
* Set a field, and notify listeners about the change.
*
* @param name The field to set
* @param value The value to set
* @param eventSource Source the event is sent from
*/
public Optional<FieldChange> setField(String name, String value, EntryEventSource eventSource) {
Objects.requireNonNull(name, "field name must not be null");
Objects.requireNonNull(value, "field value must not be null");
String fieldName = toLowerCase(name);
if (value.isEmpty()) {
return clearField(fieldName);
}
String oldValue = getField(fieldName).orElse(null);
if (value.equals(oldValue)) {
return Optional.empty();
}
if (BibEntry.ID_FIELD.equals(fieldName)) {
throw new IllegalArgumentException("The field name '" + name + "' is reserved");
}
changed = true;
fields.put(fieldName, value.intern());
invalidateFieldCache(fieldName);
FieldChange change = new FieldChange(this, fieldName, oldValue, value);
eventBus.post(new FieldChangedEvent(change, eventSource));
return Optional.of(change);
}
use of org.jabref.model.FieldChange in project jabref by JabRef.
the class WordKeywordGroup method add.
@Override
public List<FieldChange> add(List<BibEntry> entriesToAdd) {
Objects.requireNonNull(entriesToAdd);
List<FieldChange> changes = new ArrayList<>();
for (BibEntry entry : entriesToAdd) {
if (!contains(entry)) {
String oldContent = entry.getField(searchField).orElse("");
KeywordList wordlist = KeywordList.parse(oldContent, keywordSeparator);
wordlist.add(searchExpression);
String newContent = wordlist.getAsString(keywordSeparator);
entry.setField(searchField, newContent).ifPresent(changes::add);
}
}
return changes;
}
use of org.jabref.model.FieldChange in project jabref by JabRef.
the class SpecialFieldsUtilsTest method syncKeywordsFromSpecialFieldsCausesChange.
@Test
public void syncKeywordsFromSpecialFieldsCausesChange() {
BibEntry entry = new BibEntry();
entry.setField("ranking", "rank2");
List<FieldChange> changes = SpecialFieldsUtils.syncKeywordsFromSpecialFields(entry, ',');
assertTrue(changes.size() > 0);
}
use of org.jabref.model.FieldChange in project jabref by JabRef.
the class SpecialFieldsUtilsTest method syncSpecialFieldsFromKeywordCausesChange.
@Test
public void syncSpecialFieldsFromKeywordCausesChange() {
BibEntry entry = new BibEntry();
entry.setField("keywords", "rank2");
List<FieldChange> changes = SpecialFieldsUtils.syncSpecialFieldsFromKeywords(entry, ',');
assertTrue(changes.size() > 0);
}
Aggregations