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;
}
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));
}
}
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;
}
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));
}
}
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);
}
}
Aggregations