Search in sources :

Example 26 with FieldChange

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

Example 27 with FieldChange

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

Example 28 with FieldChange

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

Example 29 with FieldChange

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

Example 30 with FieldChange

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;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) NamedCompound(org.jabref.gui.undo.NamedCompound) FieldChange(org.jabref.model.FieldChange) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange) UpgradePdfPsToFileCleanup(org.jabref.logic.cleanup.UpgradePdfPsToFileCleanup)

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