Search in sources :

Example 1 with UndoableInsertString

use of org.jabref.gui.undo.UndoableInsertString in project jabref by JabRef.

the class StringAddChange method makeChange.

@Override
public boolean makeChange(BasePanel panel, BibDatabase secondary, NamedCompound undoEdit) {
    if (panel.getDatabase().hasStringLabel(string.getName())) {
        // The name to change to is already in the database, so we can't comply.
        LOGGER.info("Cannot add string '" + string.getName() + "' because the name " + "is already in use.");
    }
    try {
        panel.getDatabase().addString(string);
        undoEdit.addEdit(new UndoableInsertString(panel, panel.getDatabase(), string));
    } catch (KeyCollisionException ex) {
        LOGGER.info("Error: could not add string '" + string.getName() + "': " + ex.getMessage(), ex);
    }
    try {
        secondary.addString(new BibtexString(string.getName(), string.getContent()));
    } catch (KeyCollisionException ex) {
        LOGGER.info("Error: could not add string '" + string.getName() + "' to tmp database: " + ex.getMessage(), ex);
    }
    return true;
}
Also used : KeyCollisionException(org.jabref.model.database.KeyCollisionException) UndoableInsertString(org.jabref.gui.undo.UndoableInsertString) BibtexString(org.jabref.model.entry.BibtexString)

Example 2 with UndoableInsertString

use of org.jabref.gui.undo.UndoableInsertString in project jabref by JabRef.

the class AppendDatabaseAction method mergeFromBibtex.

private static void mergeFromBibtex(BasePanel panel, ParserResult parserResult, boolean importEntries, boolean importStrings, boolean importGroups, boolean importSelectorWords) throws KeyCollisionException {
    BibDatabase fromDatabase = parserResult.getDatabase();
    List<BibEntry> appendedEntries = new ArrayList<>();
    List<BibEntry> originalEntries = new ArrayList<>();
    BibDatabase database = panel.getDatabase();
    NamedCompound ce = new NamedCompound(Localization.lang("Append library"));
    MetaData meta = parserResult.getMetaData();
    if (importEntries) {
        // Add entries
        boolean overwriteOwner = Globals.prefs.getBoolean(JabRefPreferences.OVERWRITE_OWNER);
        boolean overwriteTimeStamp = Globals.prefs.getBoolean(JabRefPreferences.OVERWRITE_TIME_STAMP);
        for (BibEntry originalEntry : fromDatabase.getEntries()) {
            BibEntry entry = (BibEntry) originalEntry.clone();
            UpdateField.setAutomaticFields(entry, overwriteOwner, overwriteTimeStamp, Globals.prefs.getUpdateFieldPreferences());
            database.insertEntry(entry);
            appendedEntries.add(entry);
            originalEntries.add(originalEntry);
            ce.addEdit(new UndoableInsertEntry(database, entry, panel));
        }
    }
    if (importStrings) {
        for (BibtexString bs : fromDatabase.getStringValues()) {
            if (!database.hasStringLabel(bs.getName())) {
                database.addString(bs);
                ce.addEdit(new UndoableInsertString(panel, database, bs));
            }
        }
    }
    if (importGroups) {
        meta.getGroups().ifPresent(newGroups -> {
            if (newGroups.getGroup() instanceof AllEntriesGroup) {
                try {
                    ExplicitGroup group = new ExplicitGroup("Imported", GroupHierarchyType.INDEPENDENT, Globals.prefs.getKeywordDelimiter());
                    newGroups.setGroup(group);
                    group.add(appendedEntries);
                } catch (IllegalArgumentException e) {
                    LOGGER.error(e);
                }
            }
            addGroups(newGroups, ce);
        });
    }
    if (importSelectorWords) {
        for (ContentSelector selector : meta.getContentSelectorList()) {
            panel.getBibDatabaseContext().getMetaData().addContentSelector(selector);
        }
    }
    ce.end();
    panel.getUndoManager().addEdit(ce);
    panel.markBaseChanged();
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) ArrayList(java.util.ArrayList) BibtexString(org.jabref.model.entry.BibtexString) UndoableInsertEntry(org.jabref.gui.undo.UndoableInsertEntry) ExplicitGroup(org.jabref.model.groups.ExplicitGroup) AllEntriesGroup(org.jabref.model.groups.AllEntriesGroup) UndoableInsertString(org.jabref.gui.undo.UndoableInsertString) NamedCompound(org.jabref.gui.undo.NamedCompound) MetaData(org.jabref.model.metadata.MetaData) ContentSelector(org.jabref.model.metadata.ContentSelector) BibDatabase(org.jabref.model.database.BibDatabase)

Example 3 with UndoableInsertString

use of org.jabref.gui.undo.UndoableInsertString in project jabref by JabRef.

the class StringChange method makeChange.

@Override
public boolean makeChange(BasePanel panel, BibDatabase secondary, NamedCompound undoEdit) {
    if (string == null) {
        // The string was removed or renamed locally. We guess that it was removed.
        BibtexString bs = new BibtexString(label, disk);
        try {
            panel.getDatabase().addString(bs);
            undoEdit.addEdit(new UndoableInsertString(panel, panel.getDatabase(), bs));
        } catch (KeyCollisionException ex) {
            LOGGER.info("Error: could not add string '" + bs.getName() + "': " + ex.getMessage(), ex);
        }
    } else {
        string.setContent(disk);
        undoEdit.addEdit(new UndoableStringChange(panel, string, false, mem, disk));
    }
    // Update tmp database:
    if (tmpString == null) {
        BibtexString bs = new BibtexString(label, disk);
        secondary.addString(bs);
    } else {
        tmpString.setContent(disk);
    }
    return true;
}
Also used : KeyCollisionException(org.jabref.model.database.KeyCollisionException) UndoableInsertString(org.jabref.gui.undo.UndoableInsertString) UndoableStringChange(org.jabref.gui.undo.UndoableStringChange) BibtexString(org.jabref.model.entry.BibtexString)

Example 4 with UndoableInsertString

use of org.jabref.gui.undo.UndoableInsertString in project jabref by JabRef.

the class StringNameChange method makeChange.

@Override
public boolean makeChange(BasePanel panel, BibDatabase secondary, NamedCompound undoEdit) {
    if (panel.getDatabase().hasStringLabel(disk)) {
        // The name to change to is already in the database, so we can't comply.
        LOGGER.info("Cannot rename string '" + mem + "' to '" + disk + "' because the name " + "is already in use.");
    }
    if (string == null) {
        // The string was removed or renamed locally. We guess that it was removed.
        BibtexString bs = new BibtexString(disk, content);
        try {
            panel.getDatabase().addString(bs);
            undoEdit.addEdit(new UndoableInsertString(panel, panel.getDatabase(), bs));
        } catch (KeyCollisionException ex) {
            LOGGER.info("Error: could not add string '" + bs.getName() + "': " + ex.getMessage(), ex);
        }
    } else {
        string.setName(disk);
        undoEdit.addEdit(new UndoableStringChange(panel, string, true, mem, disk));
    }
    // Update tmp database:
    if (tmpString == null) {
        BibtexString bs = new BibtexString(disk, content);
        secondary.addString(bs);
    } else {
        tmpString.setName(disk);
    }
    return true;
}
Also used : KeyCollisionException(org.jabref.model.database.KeyCollisionException) UndoableInsertString(org.jabref.gui.undo.UndoableInsertString) UndoableStringChange(org.jabref.gui.undo.UndoableStringChange) BibtexString(org.jabref.model.entry.BibtexString)

Aggregations

UndoableInsertString (org.jabref.gui.undo.UndoableInsertString)4 BibtexString (org.jabref.model.entry.BibtexString)4 KeyCollisionException (org.jabref.model.database.KeyCollisionException)3 UndoableStringChange (org.jabref.gui.undo.UndoableStringChange)2 ArrayList (java.util.ArrayList)1 NamedCompound (org.jabref.gui.undo.NamedCompound)1 UndoableInsertEntry (org.jabref.gui.undo.UndoableInsertEntry)1 BibDatabase (org.jabref.model.database.BibDatabase)1 BibEntry (org.jabref.model.entry.BibEntry)1 AllEntriesGroup (org.jabref.model.groups.AllEntriesGroup)1 ExplicitGroup (org.jabref.model.groups.ExplicitGroup)1 ContentSelector (org.jabref.model.metadata.ContentSelector)1 MetaData (org.jabref.model.metadata.MetaData)1