use of org.jabref.model.FieldChange in project jabref by JabRef.
the class SpecialFieldsUtils method exportFieldToKeywords.
private static List<FieldChange> exportFieldToKeywords(SpecialField specialField, BibEntry entry, Character keywordDelimiter) {
List<FieldChange> fieldChanges = new ArrayList<>();
Optional<Keyword> newValue = entry.getField(specialField.getFieldName()).map(Keyword::new);
KeywordList keyWords = specialField.getKeyWords();
Optional<FieldChange> change = entry.replaceKeywords(keyWords, newValue, keywordDelimiter);
change.ifPresent(changeValue -> fieldChanges.add(changeValue));
return fieldChanges;
}
use of org.jabref.model.FieldChange in project jabref by JabRef.
the class UpdateField method updateField.
/**
* Undoable change of field value
*
* @param be BibEntry
* @param field Field name
* @param newValue New field value
* @param nullFieldIfValueIsTheSame If true the field value is removed when the current value is equals to newValue
*/
public static Optional<FieldChange> updateField(BibEntry be, String field, String newValue, Boolean nullFieldIfValueIsTheSame) {
String writtenValue = null;
String oldValue = null;
if (be.hasField(field)) {
oldValue = be.getField(field).get();
if ((newValue == null) || (oldValue.equals(newValue) && nullFieldIfValueIsTheSame)) {
// If the new field value is null or the old and the new value are the same and flag is set
// Clear the field
be.clearField(field);
} else if (!oldValue.equals(newValue)) {
// Update
writtenValue = newValue;
be.setField(field, newValue);
} else {
// Values are the same, do nothing
return Optional.empty();
}
} else {
// old field value not set
if (newValue == null) {
// Do nothing
return Optional.empty();
} else {
// Set new value
writtenValue = newValue;
be.setField(field, newValue);
}
}
return Optional.of(new FieldChange(be, field, oldValue, writtenValue));
}
use of org.jabref.model.FieldChange in project jabref by JabRef.
the class WordKeywordGroup method remove.
@Override
public List<FieldChange> remove(List<BibEntry> entriesToRemove) {
Objects.requireNonNull(entriesToRemove);
List<FieldChange> changes = new ArrayList<>();
for (BibEntry entry : entriesToRemove) {
if (contains(entry)) {
String oldContent = entry.getField(searchField).orElse("");
KeywordList wordlist = KeywordList.parse(oldContent, keywordSeparator);
wordlist.remove(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 BibEntry method clearField.
/**
* Remove the mapping for the field name, and notify listeners about
* the change including the {@link EntryEventSource}.
*
* @param name The field to clear.
* @param eventSource the source a new {@link FieldChangedEvent} should be posten from.
*/
public Optional<FieldChange> clearField(String name, EntryEventSource eventSource) {
String fieldName = toLowerCase(name);
if (BibEntry.ID_FIELD.equals(fieldName)) {
throw new IllegalArgumentException("The field name '" + name + "' is reserved");
}
Optional<String> oldValue = getField(fieldName);
if (!oldValue.isPresent()) {
return Optional.empty();
}
changed = true;
fields.remove(fieldName);
invalidateFieldCache(fieldName);
FieldChange change = new FieldChange(this, fieldName, oldValue.get(), null);
eventBus.post(new FieldChangedEvent(change, eventSource));
return Optional.of(change);
}
use of org.jabref.model.FieldChange in project jabref by JabRef.
the class FileLinksUpgradeWarning method upgradePdfPsToFile.
/**
* Collect file links from the given set of fields, and add them to the list contained in the field
* GUIGlobals.FILE_FIELD.
*
* @param database The database to modify.
* @param fields The fields to find links in.
* @return A CompoundEdit specifying the undo operation for the whole operation.
*/
private static NamedCompound upgradePdfPsToFile(BibDatabase database) {
NamedCompound ce = new NamedCompound(Localization.lang("Move external links to 'file' field"));
UpgradePdfPsToFileCleanup cleanupJob = new UpgradePdfPsToFileCleanup();
for (BibEntry entry : database.getEntries()) {
List<FieldChange> changes = cleanupJob.cleanup(entry);
for (FieldChange change : changes) {
ce.addEdit(new UndoableFieldChange(change));
}
}
ce.end();
return ce;
}
Aggregations