Search in sources :

Example 16 with FieldChange

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);
    }
}
Also used : ArrayList(java.util.ArrayList) FieldChange(org.jabref.model.FieldChange)

Example 17 with FieldChange

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);
}
Also used : FieldChangedEvent(org.jabref.model.entry.event.FieldChangedEvent) FieldChange(org.jabref.model.FieldChange)

Example 18 with FieldChange

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;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) ArrayList(java.util.ArrayList) FieldChange(org.jabref.model.FieldChange) KeywordList(org.jabref.model.entry.KeywordList)

Example 19 with FieldChange

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);
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) FieldChange(org.jabref.model.FieldChange) Test(org.junit.Test)

Example 20 with FieldChange

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);
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) FieldChange(org.jabref.model.FieldChange) Test(org.junit.Test)

Aggregations

FieldChange (org.jabref.model.FieldChange)35 BibEntry (org.jabref.model.entry.BibEntry)17 ArrayList (java.util.ArrayList)13 UndoableFieldChange (org.jabref.gui.undo.UndoableFieldChange)8 NamedCompound (org.jabref.gui.undo.NamedCompound)7 Test (org.junit.Test)7 KeywordList (org.jabref.model.entry.KeywordList)6 LinkedFile (org.jabref.model.entry.LinkedFile)6 IOException (java.io.IOException)3 File (java.io.File)2 Path (java.nio.file.Path)2 Keyword (org.jabref.model.entry.Keyword)2 FieldChangedEvent (org.jabref.model.entry.event.FieldChangedEvent)2 Subscribe (com.google.common.eventbus.Subscribe)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 AbstractUndoableEdit (javax.swing.undo.AbstractUndoableEdit)1 BasePanel (org.jabref.gui.BasePanel)1 CleanupWorker (org.jabref.logic.cleanup.CleanupWorker)1