Search in sources :

Example 6 with UndoableFieldChange

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;
}
Also used : UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange)

Example 7 with UndoableFieldChange

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));
    }
}
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 8 with UndoableFieldChange

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

Example 9 with UndoableFieldChange

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

Example 10 with UndoableFieldChange

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

Aggregations

UndoableFieldChange (org.jabref.gui.undo.UndoableFieldChange)23 BibEntry (org.jabref.model.entry.BibEntry)13 NamedCompound (org.jabref.gui.undo.NamedCompound)11 FieldChange (org.jabref.model.FieldChange)8 FileListTableModel (org.jabref.gui.filelist.FileListTableModel)5 FileListEntry (org.jabref.gui.filelist.FileListEntry)4 ArrayList (java.util.ArrayList)3 Optional (java.util.Optional)3 ExternalFileType (org.jabref.gui.externalfiletype.ExternalFileType)3 UnknownExternalFileType (org.jabref.gui.externalfiletype.UnknownExternalFileType)3 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 List (java.util.List)2 Matcher (java.util.regex.Matcher)2 ExternalFileTypeEntryEditor (org.jabref.gui.externalfiletype.ExternalFileTypeEntryEditor)2 FileListEntryEditor (org.jabref.gui.filelist.FileListEntryEditor)2 Subscribe (com.google.common.eventbus.Subscribe)1 ActionEvent (java.awt.event.ActionEvent)1 File (java.io.File)1 StringReader (java.io.StringReader)1