use of org.jabref.gui.undo.UndoableFieldChange in project jabref by JabRef.
the class UndoableAbbreviator method abbreviate.
/**
* Abbreviate the journal name of the given entry.
*
* @param database The database the entry belongs to, or null if no database.
* @param entry The entry to be treated.
* @param fieldName The field name (e.g. "journal")
* @param ce If the entry is changed, add an edit to this compound.
* @return true if the entry was changed, false otherwise.
*/
public boolean abbreviate(BibDatabase database, BibEntry entry, String fieldName, CompoundEdit ce) {
if (!entry.hasField(fieldName)) {
return false;
}
String text = entry.getField(fieldName).get();
String origText = text;
if (database != null) {
text = database.resolveForStrings(text);
}
if (!journalAbbreviationRepository.isKnownName(text)) {
// unknown, cannot un/abbreviate anything
return false;
}
String newText = getAbbreviatedName(journalAbbreviationRepository.getAbbreviation(text).get());
if (newText.equals(origText)) {
return false;
}
entry.setField(fieldName, newText);
ce.addEdit(new UndoableFieldChange(entry, fieldName, origText, newText));
return true;
}
use of org.jabref.gui.undo.UndoableFieldChange 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.gui.undo.UndoableFieldChange 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);
}
}
use of org.jabref.gui.undo.UndoableFieldChange in project jabref by JabRef.
the class BasePanel method registerUndoableChanges.
public void registerUndoableChanges(SaveSession session) {
NamedCompound ce = new NamedCompound(Localization.lang("Save actions"));
for (FieldChange change : session.getFieldChanges()) {
ce.addEdit(new UndoableFieldChange(change));
}
ce.end();
if (ce.hasEdits()) {
getUndoManager().addEdit(ce);
}
}
use of org.jabref.gui.undo.UndoableFieldChange in project jabref by JabRef.
the class CleanupAction method doCleanup.
/**
* Runs the cleanup on the entry and records the change.
*/
private void doCleanup(CleanupPreset preset, BibEntry entry, NamedCompound ce) {
// Create and run cleaner
CleanupWorker cleaner = new CleanupWorker(panel.getBibDatabaseContext(), preferences.getCleanupPreferences(Globals.journalAbbreviationLoader));
List<FieldChange> changes = cleaner.cleanup(preset, entry);
unsuccessfulRenames = cleaner.getUnsuccessfulRenames();
if (changes.isEmpty()) {
return;
}
// Register undo action
for (FieldChange change : changes) {
ce.addEdit(new UndoableFieldChange(change));
}
}
Aggregations