Search in sources :

Example 1 with KeyCollisionException

use of org.jabref.model.database.KeyCollisionException in project jabref by JabRef.

the class BasePanel method newEntry.

/**
     * This method is called from JabRefFrame when the user wants to create a new entry. If the argument is null, the
     * user is prompted for an entry type.
     *
     * @param type The type of the entry to create.
     * @return The newly created BibEntry or null the operation was canceled by the user.
     */
public BibEntry newEntry(EntryType type) {
    EntryType actualType = type;
    if (actualType == null) {
        // Find out what type is wanted.
        final EntryTypeDialog etd = new EntryTypeDialog(frame);
        // We want to center the dialog, to make it look nicer.
        etd.setLocationRelativeTo(frame);
        etd.setVisible(true);
        actualType = etd.getChoice();
    }
    if (actualType != null) {
        // Only if the dialog was not canceled.
        final BibEntry be = new BibEntry(actualType.getName());
        try {
            bibDatabaseContext.getDatabase().insertEntry(be);
            // Set owner/timestamp if options are enabled:
            List<BibEntry> list = new ArrayList<>();
            list.add(be);
            UpdateField.setAutomaticFields(list, true, true, Globals.prefs.getUpdateFieldPreferences());
            // Create an UndoableInsertEntry object.
            getUndoManager().addEdit(new UndoableInsertEntry(bibDatabaseContext.getDatabase(), be, BasePanel.this));
            output(Localization.lang("Added new '%0' entry.", actualType.getName().toLowerCase(Locale.ROOT)));
            // and adjustment of the splitter.
            if (mode != BasePanelMode.SHOWING_EDITOR) {
                mode = BasePanelMode.WILL_SHOW_EDITOR;
            }
            highlightEntry(be);
            // The database just changed.
            markBaseChanged();
            final EntryEditor entryEditor = getEntryEditor(be);
            this.showEntryEditor(entryEditor);
            entryEditor.requestFocus();
            return be;
        } catch (KeyCollisionException ex) {
            LOGGER.info(ex.getMessage(), ex);
        }
    }
    return null;
}
Also used : KeyCollisionException(org.jabref.model.database.KeyCollisionException) BibEntry(org.jabref.model.entry.BibEntry) EntryEditor(org.jabref.gui.entryeditor.EntryEditor) EntryType(org.jabref.model.entry.EntryType) ArrayList(java.util.ArrayList) UndoableInsertEntry(org.jabref.gui.undo.UndoableInsertEntry)

Example 2 with KeyCollisionException

use of org.jabref.model.database.KeyCollisionException 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 3 with KeyCollisionException

use of org.jabref.model.database.KeyCollisionException in project jabref by JabRef.

the class AppendDatabaseAction method openIt.

private void openIt(boolean importEntries, boolean importStrings, boolean importGroups, boolean importSelectorWords) {
    if (filesToOpen.isEmpty()) {
        return;
    }
    for (Path file : filesToOpen) {
        try {
            Globals.prefs.put(JabRefPreferences.WORKING_DIRECTORY, file.getParent().toString());
            // Should this be done _after_ we know it was successfully opened?
            ParserResult parserResult = OpenDatabase.loadDatabase(file.toFile(), Globals.prefs.getImportFormatPreferences());
            AppendDatabaseAction.mergeFromBibtex(panel, parserResult, importEntries, importStrings, importGroups, importSelectorWords);
            panel.output(Localization.lang("Imported from library") + " '" + file + "'");
        } catch (IOException | KeyCollisionException ex) {
            LOGGER.warn("Could not open database", ex);
            JOptionPane.showMessageDialog(panel, ex.getMessage(), Localization.lang("Open library"), JOptionPane.ERROR_MESSAGE);
        }
    }
}
Also used : Path(java.nio.file.Path) KeyCollisionException(org.jabref.model.database.KeyCollisionException) ParserResult(org.jabref.logic.importer.ParserResult) IOException(java.io.IOException)

Example 4 with KeyCollisionException

use of org.jabref.model.database.KeyCollisionException in project jabref by JabRef.

the class BibtexParser method parseBibtexString.

private void parseBibtexString() throws IOException {
    BibtexString bibtexString = parseString();
    bibtexString.setParsedSerialization(dumpTextReadSoFarToString());
    try {
        database.addString(bibtexString);
    } catch (KeyCollisionException ex) {
        parserResult.addWarning(Localization.lang("Duplicate string name") + ": " + bibtexString.getName());
    }
}
Also used : KeyCollisionException(org.jabref.model.database.KeyCollisionException) BibtexString(org.jabref.model.entry.BibtexString)

Example 5 with KeyCollisionException

use of org.jabref.model.database.KeyCollisionException in project jabref by JabRef.

the class PdfImporter method createNewEntry.

private Optional<BibEntry> createNewEntry() {
    // Find out what type is desired
    EntryTypeDialog etd = new EntryTypeDialog(frame);
    // We want to center the dialog, to make it look nicer.
    etd.setLocationRelativeTo(frame);
    etd.setVisible(true);
    EntryType type = etd.getChoice();
    if (type != null) {
        // Only if the dialog was not canceled.
        final BibEntry bibEntry = new BibEntry(type.getName());
        try {
            panel.getDatabase().insertEntry(bibEntry);
            // Set owner/timestamp if options are enabled:
            List<BibEntry> list = new ArrayList<>();
            list.add(bibEntry);
            UpdateField.setAutomaticFields(list, true, true, Globals.prefs.getUpdateFieldPreferences());
            // Create an UndoableInsertEntry object.
            panel.getUndoManager().addEdit(new UndoableInsertEntry(panel.getDatabase(), bibEntry, panel));
            panel.output(Localization.lang("Added new") + " '" + type.getName().toLowerCase(Locale.ROOT) + "' " + Localization.lang("entry") + ".");
            // and adjustment of the splitter.
            if (panel.getMode() != BasePanelMode.SHOWING_EDITOR) {
                panel.setMode(BasePanelMode.WILL_SHOW_EDITOR);
            }
            SwingUtilities.invokeLater(() -> panel.showEntry(bibEntry));
            // The database just changed.
            panel.markBaseChanged();
            return Optional.of(bibEntry);
        } catch (KeyCollisionException ex) {
            LOGGER.info("Key collision occurred", ex);
        }
    }
    return Optional.empty();
}
Also used : KeyCollisionException(org.jabref.model.database.KeyCollisionException) BibEntry(org.jabref.model.entry.BibEntry) EntryType(org.jabref.model.entry.EntryType) EntryTypeDialog(org.jabref.gui.EntryTypeDialog) ArrayList(java.util.ArrayList) UndoableInsertEntry(org.jabref.gui.undo.UndoableInsertEntry)

Aggregations

KeyCollisionException (org.jabref.model.database.KeyCollisionException)10 BibtexString (org.jabref.model.entry.BibtexString)5 UndoableInsertEntry (org.jabref.gui.undo.UndoableInsertEntry)3 UndoableInsertString (org.jabref.gui.undo.UndoableInsertString)3 BibEntry (org.jabref.model.entry.BibEntry)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 UndoableStringChange (org.jabref.gui.undo.UndoableStringChange)2 ParserResult (org.jabref.logic.importer.ParserResult)2 EntryType (org.jabref.model.entry.EntryType)2 PushbackReader (java.io.PushbackReader)1 Path (java.nio.file.Path)1 EntryTypeDialog (org.jabref.gui.EntryTypeDialog)1 EntryEditor (org.jabref.gui.entryeditor.EntryEditor)1 NamedCompound (org.jabref.gui.undo.NamedCompound)1 ImportFormatReader (org.jabref.logic.importer.ImportFormatReader)1 BibDatabase (org.jabref.model.database.BibDatabase)1