Search in sources :

Example 6 with LatexFieldFormatter

use of org.jabref.logic.bibtex.LatexFieldFormatter in project jabref by JabRef.

the class EntryEditor method storeSource.

private boolean storeSource() {
    BibtexParser bibtexParser = new BibtexParser(Globals.prefs.getImportFormatPreferences());
    try {
        ParserResult parserResult = bibtexParser.parse(new StringReader(source.getText()));
        BibDatabase database = parserResult.getDatabase();
        if (database.getEntryCount() > 1) {
            throw new IllegalStateException("More than one entry found.");
        }
        if (!database.hasEntries()) {
            if (parserResult.hasWarnings()) {
                // put the warning into as exception text -> it will be displayed to the user
                throw new IllegalStateException(parserResult.warnings().get(0));
            } else {
                throw new IllegalStateException("No entries found.");
            }
        }
        NamedCompound compound = new NamedCompound(Localization.lang("source edit"));
        BibEntry newEntry = database.getEntries().get(0);
        String newKey = newEntry.getCiteKeyOptional().orElse(null);
        boolean entryChanged = false;
        boolean emptyWarning = (newKey == null) || newKey.isEmpty();
        if (newKey != null) {
            entry.setCiteKey(newKey);
        } else {
            entry.clearCiteKey();
        }
        // First, remove fields that the user has removed.
        for (Entry<String, String> field : entry.getFieldMap().entrySet()) {
            String fieldName = field.getKey();
            String fieldValue = field.getValue();
            if (InternalBibtexFields.isDisplayableField(fieldName) && !newEntry.hasField(fieldName)) {
                compound.addEdit(new UndoableFieldChange(entry, fieldName, fieldValue, null));
                entry.clearField(fieldName);
                entryChanged = true;
            }
        }
        // Then set all fields that have been set by the user.
        for (Entry<String, String> field : newEntry.getFieldMap().entrySet()) {
            String fieldName = field.getKey();
            String oldValue = entry.getField(fieldName).orElse(null);
            String newValue = field.getValue();
            if (!Objects.equals(oldValue, newValue)) {
                // Test if the field is legally set.
                new LatexFieldFormatter(Globals.prefs.getLatexFieldFormatterPreferences()).format(newValue, fieldName);
                compound.addEdit(new UndoableFieldChange(entry, fieldName, oldValue, newValue));
                entry.setField(fieldName, newValue);
                entryChanged = true;
            }
        }
        // See if the user has changed the entry type:
        if (!Objects.equals(newEntry.getType(), entry.getType())) {
            compound.addEdit(new UndoableChangeType(entry, entry.getType(), newEntry.getType()));
            entry.setType(newEntry.getType());
            entryChanged = true;
        }
        compound.end();
        if (!entryChanged) {
            return true;
        }
        panel.getUndoManager().addEdit(compound);
        if (panel.getDatabase().getDuplicationChecker().isDuplicateCiteKeyExisting(entry)) {
            warnDuplicateBibtexkey();
        } else if (emptyWarning) {
            warnEmptyBibtexkey();
        } else {
            panel.output(Localization.lang("Stored entry") + '.');
        }
        lastSourceStringAccepted = source.getText();
        // Update UI
        // TODO: we need to repaint the entryeditor if fields that are not displayed have been added
        panel.updateEntryEditorIfShowing();
        lastSourceAccepted = true;
        updateSource = true;
        // TODO: does updating work properly after source stored?
        panel.markBaseChanged();
        panel.highlightEntry(entry);
        return true;
    } catch (InvalidFieldValueException | IOException ex) {
        // The source couldn't be parsed, so the user is given an
        // error message, and the choice to keep or revert the contents
        // of the source text field.
        updateSource = false;
        lastSourceAccepted = false;
        tabbed.setSelectedComponent(srcPanel);
        Object[] options = { Localization.lang("Edit"), Localization.lang("Revert to original source") };
        if (!SwingUtilities.isEventDispatchThread()) {
            int answer = JOptionPane.showOptionDialog(frame, Localization.lang("Error") + ": " + ex.getMessage(), Localization.lang("Problem with parsing entry"), JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null, options, options[0]);
            if (answer != 0) {
                updateSource = true;
                lastSourceAccepted = true;
                updateSource();
            }
        }
        LOGGER.debug("Incorrect source", ex);
        return false;
    }
}
Also used : UndoableChangeType(org.jabref.gui.undo.UndoableChangeType) TypedBibEntry(org.jabref.logic.TypedBibEntry) BibEntry(org.jabref.model.entry.BibEntry) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) InvalidFieldValueException(org.jabref.logic.bibtex.InvalidFieldValueException) IOException(java.io.IOException) LatexFieldFormatter(org.jabref.logic.bibtex.LatexFieldFormatter) ParserResult(org.jabref.logic.importer.ParserResult) NamedCompound(org.jabref.gui.undo.NamedCompound) StringReader(java.io.StringReader) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange) BibDatabase(org.jabref.model.database.BibDatabase)

Example 7 with LatexFieldFormatter

use of org.jabref.logic.bibtex.LatexFieldFormatter in project jabref by JabRef.

the class MergeEntries method updateAll.

/**
     * Update the merged BibEntry with source and preview panel every time something is changed
     */
private void updateAll() {
    if (!doneBuilding) {
        // If we are not done adding everything, do not do anything...
        return;
    }
    // Check if the type has changed
    if (!identicalTypes && typeRadioButtons.get(0).isSelected()) {
        mergedEntry.setType(leftEntry.getType());
    } else {
        mergedEntry.setType(rightEntry.getType());
    }
    // Check the potentially different fields
    for (String field : differentFields) {
        if (radioButtons.get(field).get(0).isSelected()) {
            // Will only happen if field exists
            mergedEntry.setField(field, leftEntry.getField(field).get());
        } else if (radioButtons.get(field).get(2).isSelected()) {
            // Will only happen if field exists
            mergedEntry.setField(field, rightEntry.getField(field).get());
        } else {
            mergedEntry.clearField(field);
        }
    }
    // Update the PreviewPanel
    entryPreview.setEntry(mergedEntry);
    // Update the BibTeX source view
    StringWriter writer = new StringWriter();
    try {
        new BibEntryWriter(new LatexFieldFormatter(Globals.prefs.getLatexFieldFormatterPreferences()), false).write(mergedEntry, writer, databaseType);
    } catch (IOException ex) {
        LOGGER.error("Error in entry", ex);
    }
    sourceView.setText(writer.getBuffer().toString());
    sourceView.setCaretPosition(0);
}
Also used : StringWriter(java.io.StringWriter) IOException(java.io.IOException) BibEntryWriter(org.jabref.logic.bibtex.BibEntryWriter) LatexFieldFormatter(org.jabref.logic.bibtex.LatexFieldFormatter)

Example 8 with LatexFieldFormatter

use of org.jabref.logic.bibtex.LatexFieldFormatter in project jabref by JabRef.

the class BasicAction method updateSourceView.

// update the bibtex source view and available List
private void updateSourceView() {
    StringWriter sw = new StringWriter(200);
    try {
        new BibEntryWriter(new LatexFieldFormatter(Globals.prefs.getLatexFieldFormatterPreferences()), false).write(entry, sw, frame.getCurrentBasePanel().getBibDatabaseContext().getMode());
        sourcePreview.setText(sw.getBuffer().toString());
    } catch (IOException ex) {
        LOGGER.error("Error in entry" + ": " + ex.getMessage(), ex);
    }
    fieldList.clearSelection();
}
Also used : StringWriter(java.io.StringWriter) IOException(java.io.IOException) BibEntryWriter(org.jabref.logic.bibtex.BibEntryWriter) LatexFieldFormatter(org.jabref.logic.bibtex.LatexFieldFormatter)

Aggregations

LatexFieldFormatter (org.jabref.logic.bibtex.LatexFieldFormatter)8 StringWriter (java.io.StringWriter)6 BibEntryWriter (org.jabref.logic.bibtex.BibEntryWriter)6 IOException (java.io.IOException)5 BibEntry (org.jabref.model.entry.BibEntry)3 InvalidFieldValueException (org.jabref.logic.bibtex.InvalidFieldValueException)2 ParserResult (org.jabref.logic.importer.ParserResult)2 BibtexParser (org.jabref.logic.importer.fileformat.BibtexParser)2 Desktop (java.awt.Desktop)1 File (java.io.File)1 FileReader (java.io.FileReader)1 StringReader (java.io.StringReader)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 XMPMetadata (org.apache.jempbox.xmp.XMPMetadata)1 BasePanel (org.jabref.gui.BasePanel)1 JabRefDesktop (org.jabref.gui.desktop.JabRefDesktop)1