Search in sources :

Example 1 with FieldChange

use of org.jabref.model.FieldChange in project jabref by JabRef.

the class ManageKeywordsAction method updateKeywords.

private NamedCompound updateKeywords(List<BibEntry> entries, KeywordList keywordsToAdd, KeywordList keywordsToRemove) {
    NamedCompound ce = new NamedCompound(Localization.lang("Update keywords"));
    for (BibEntry entry : entries) {
        KeywordList keywords = entry.getKeywords(Globals.prefs.getKeywordDelimiter());
        // update keywords
        keywords.removeAll(keywordsToRemove);
        keywords.addAll(keywordsToAdd);
        // put keywords back
        Optional<FieldChange> change = entry.putKeywords(keywords, Globals.prefs.getKeywordDelimiter());
        if (change.isPresent()) {
            ce.addEdit(new UndoableFieldChange(change.get()));
        }
        if (Globals.prefs.isKeywordSyncEnabled()) {
            SpecialFieldsUtils.syncSpecialFieldsFromKeywords(entry, Globals.prefs.getKeywordDelimiter());
        }
    }
    ce.end();
    return ce;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) NamedCompound(org.jabref.gui.undo.NamedCompound) KeywordList(org.jabref.model.entry.KeywordList) FieldChange(org.jabref.model.FieldChange) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange)

Example 2 with FieldChange

use of org.jabref.model.FieldChange in project jabref by JabRef.

the class GroupTreeNodeViewModel method changeEntriesTo.

public void changeEntriesTo(List<BibEntry> entries, UndoManager undoManager) {
    AbstractGroup group = node.getGroup();
    List<FieldChange> changesRemove = new ArrayList<>();
    List<FieldChange> changesAdd = new ArrayList<>();
    // Sort entries into current members and non-members of the group
    // Current members will be removed
    // Current non-members will be added
    List<BibEntry> toRemove = new ArrayList<>(entries.size());
    List<BibEntry> toAdd = new ArrayList<>(entries.size());
    for (BibEntry entry : entries) {
        // Sort according to current state of the entries
        if (group.contains(entry)) {
            toRemove.add(entry);
        } else {
            toAdd.add(entry);
        }
    }
    // If there are entries to remove
    if (!toRemove.isEmpty()) {
        changesRemove = removeEntriesFromGroup(toRemove);
    }
    // If there are entries to add
    if (!toAdd.isEmpty()) {
        changesAdd = addEntriesToGroup(toAdd);
    }
    // Remember undo information
    if (!changesRemove.isEmpty()) {
        AbstractUndoableEdit undoRemove = UndoableChangeEntriesOfGroup.getUndoableEdit(this, changesRemove);
        if (!changesAdd.isEmpty() && (undoRemove != null)) {
            // we removed and added entries
            undoRemove.addEdit(UndoableChangeEntriesOfGroup.getUndoableEdit(this, changesAdd));
        }
        undoManager.addEdit(undoRemove);
    } else if (!changesAdd.isEmpty()) {
        undoManager.addEdit(UndoableChangeEntriesOfGroup.getUndoableEdit(this, changesAdd));
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) AbstractUndoableEdit(javax.swing.undo.AbstractUndoableEdit) AbstractGroup(org.jabref.model.groups.AbstractGroup) ArrayList(java.util.ArrayList) FieldChange(org.jabref.model.FieldChange)

Example 3 with FieldChange

use of org.jabref.model.FieldChange in project jabref by JabRef.

the class UndoableChangeEntriesOfGroup method getUndoableEdit.

public static AbstractUndoableEdit getUndoableEdit(GroupTreeNodeViewModel node, List<FieldChange> changes) {
    boolean hasEntryChanges = false;
    NamedCompound entryChangeCompound = new NamedCompound(Localization.lang("change entries of group"));
    for (FieldChange fieldChange : changes) {
        hasEntryChanges = true;
        entryChangeCompound.addEdit(new UndoableFieldChange(fieldChange));
    }
    if (hasEntryChanges) {
        entryChangeCompound.end();
        return entryChangeCompound;
    }
    return null;
}
Also used : NamedCompound(org.jabref.gui.undo.NamedCompound) FieldChange(org.jabref.model.FieldChange) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange)

Example 4 with FieldChange

use of org.jabref.model.FieldChange in project jabref by JabRef.

the class LookupIdentifiersWorker method run.

@Override
public void run() {
    BasePanel basePanel = Objects.requireNonNull(frame.getCurrentBasePanel());
    List<BibEntry> bibEntries = basePanel.getSelectedEntries();
    if (!bibEntries.isEmpty()) {
        String totalCount = Integer.toString(bibEntries.size());
        NamedCompound namedCompound = new NamedCompound(Localization.lang("Look up %0", fetcher.getIdentifierName()));
        int count = 0;
        int foundCount = 0;
        for (BibEntry bibEntry : bibEntries) {
            count++;
            frame.output(Localization.lang("Looking up %0... - entry %1 out of %2 - found %3", fetcher.getIdentifierName(), Integer.toString(count), totalCount, Integer.toString(foundCount)));
            Optional<T> identifier = Optional.empty();
            try {
                identifier = fetcher.findIdentifier(bibEntry);
            } catch (FetcherException e) {
                LOGGER.error("Could not fetch " + fetcher.getIdentifierName(), e);
            }
            if (identifier.isPresent() && !bibEntry.hasField(identifier.get().getDefaultField())) {
                Optional<FieldChange> fieldChange = bibEntry.setField(identifier.get().getDefaultField(), identifier.get().getNormalized());
                if (fieldChange.isPresent()) {
                    namedCompound.addEdit(new UndoableFieldChange(fieldChange.get()));
                    foundCount++;
                    frame.output(Localization.lang("Looking up %0... - entry %1 out of %2 - found %3", Integer.toString(count), totalCount, Integer.toString(foundCount)));
                }
            }
        }
        namedCompound.end();
        if (foundCount > 0) {
            basePanel.getUndoManager().addEdit(namedCompound);
            basePanel.markBaseChanged();
        }
        message = Localization.lang("Determined %0 for %1 entries", fetcher.getIdentifierName(), Integer.toString(foundCount));
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) BasePanel(org.jabref.gui.BasePanel) FetcherException(org.jabref.logic.importer.FetcherException) NamedCompound(org.jabref.gui.undo.NamedCompound) FieldChange(org.jabref.model.FieldChange) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange)

Example 5 with FieldChange

use of org.jabref.model.FieldChange in project jabref by JabRef.

the class SpecialFieldAction method action.

@Override
public void action() {
    try {
        List<BibEntry> bes = frame.getCurrentBasePanel().getSelectedEntries();
        if ((bes == null) || bes.isEmpty()) {
            return;
        }
        NamedCompound ce = new NamedCompound(undoText);
        for (BibEntry be : bes) {
            // if (value==null) and then call nullField has been omitted as updatefield also handles value==null
            List<FieldChange> changes = SpecialFieldsUtils.updateField(specialField, value, be, nullFieldIfValueIsTheSame, Globals.prefs.isKeywordSyncEnabled(), Globals.prefs.getKeywordDelimiter());
            for (FieldChange change : changes) {
                ce.addEdit(new UndoableFieldChange(change));
            }
        }
        ce.end();
        if (ce.hasEdits()) {
            frame.getCurrentBasePanel().getUndoManager().addEdit(ce);
            frame.getCurrentBasePanel().markBaseChanged();
            frame.getCurrentBasePanel().updateEntryEditorIfShowing();
            String outText;
            if (nullFieldIfValueIsTheSame || value == null) {
                outText = getTextDone(specialField, Integer.toString(bes.size()));
            } else {
                outText = getTextDone(specialField, value, Integer.toString(bes.size()));
            }
            frame.output(outText);
        } else {
        // if user does not change anything with his action, we do not do anything either
        // even no output message
        }
    } catch (Throwable ex) {
        LOGGER.error("Problem setting special fields", ex);
    }
}
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)

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