Search in sources :

Example 16 with UndoableFieldChange

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

the class FileLinksUpgradeWarning method upgradePdfPsToFile.

/**
     * Collect file links from the given set of fields, and add them to the list contained in the field
     * GUIGlobals.FILE_FIELD.
     *
     * @param database The database to modify.
     * @param fields   The fields to find links in.
     * @return A CompoundEdit specifying the undo operation for the whole operation.
     */
private static NamedCompound upgradePdfPsToFile(BibDatabase database) {
    NamedCompound ce = new NamedCompound(Localization.lang("Move external links to 'file' field"));
    UpgradePdfPsToFileCleanup cleanupJob = new UpgradePdfPsToFileCleanup();
    for (BibEntry entry : database.getEntries()) {
        List<FieldChange> changes = cleanupJob.cleanup(entry);
        for (FieldChange change : changes) {
            ce.addEdit(new UndoableFieldChange(change));
        }
    }
    ce.end();
    return ce;
}
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) UpgradePdfPsToFileCleanup(org.jabref.logic.cleanup.UpgradePdfPsToFileCleanup)

Example 17 with UndoableFieldChange

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

the class EntryMarker method markEntry.

/**
     * @param increment whether the given increment should be added to the current one. Currently never used in JabRef. Could be used to increase marking color ("Mark in specific color").
     */
public static void markEntry(BibEntry be, int markIncrement, boolean increment, NamedCompound ce) {
    int prevMarkLevel;
    String newValue = null;
    if (be.hasField(FieldName.MARKED_INTERNAL)) {
        String markerString = be.getField(FieldName.MARKED_INTERNAL).get();
        int index = markerString.indexOf(Globals.prefs.getWrappedUsername());
        if (index >= 0) {
            // Already marked 1 for this user.
            prevMarkLevel = 1;
            newValue = markerString.substring(0, index) + markerString.substring(index + Globals.prefs.getWrappedUsername().length()) + Globals.prefs.getWrappedUsername().substring(0, Globals.prefs.getWrappedUsername().length() - 1) + ":" + (increment ? Math.min(MAX_MARKING_LEVEL, prevMarkLevel + markIncrement) : markIncrement) + "]";
        } else {
            Matcher m = MARK_NUMBER_PATTERN.matcher(markerString);
            if (m.find()) {
                try {
                    prevMarkLevel = Integer.parseInt(m.group(1));
                    newValue = markerString.substring(0, m.start(1)) + (increment ? Math.min(MAX_MARKING_LEVEL, prevMarkLevel + markIncrement) : markIncrement) + markerString.substring(m.end(1));
                } catch (NumberFormatException ex) {
                // Do nothing.
                }
            }
        }
    }
    if (newValue == null) {
        newValue = Globals.prefs.getWrappedUsername().substring(0, Globals.prefs.getWrappedUsername().length() - 1) + ":" + markIncrement + "]";
    }
    ce.addEdit(new UndoableFieldChange(be, FieldName.MARKED_INTERNAL, be.getField(FieldName.MARKED_INTERNAL).orElse(null), newValue));
    be.setField(FieldName.MARKED_INTERNAL, newValue);
}
Also used : Matcher(java.util.regex.Matcher) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange)

Example 18 with UndoableFieldChange

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

the class MassSetFieldAction method massRenameField.

/**
     * Move contents from one field to another for a Collection of entries.
     *
     * @param entries         The entries to do this operation for.
     * @param field           The field to move contents from.
     * @param newField        The field to move contents into.
     * @param overwriteValues If true, overwrites any existing values in the new field. If false, makes no change for
     *                        entries with existing value in the new field.
     * @return A CompoundEdit for the entire operation.
     */
private static UndoableEdit massRenameField(Collection<BibEntry> entries, String field, String newField, boolean overwriteValues) {
    NamedCompound ce = new NamedCompound(Localization.lang("Rename field"));
    for (BibEntry entry : entries) {
        Optional<String> valToMove = entry.getField(field);
        // If there is no value, do nothing:
        if ((!valToMove.isPresent()) || valToMove.get().isEmpty()) {
            continue;
        }
        // If we are not allowed to overwrite values, check if there is a
        // non-empty value already for this entry for the new field:
        Optional<String> valInNewField = entry.getField(newField);
        if (!overwriteValues && (valInNewField.isPresent()) && !valInNewField.get().isEmpty()) {
            continue;
        }
        entry.setField(newField, valToMove.get());
        ce.addEdit(new UndoableFieldChange(entry, newField, valInNewField.orElse(null), valToMove.get()));
        entry.clearField(field);
        ce.addEdit(new UndoableFieldChange(entry, field, valToMove.get(), null));
    }
    ce.end();
    return ce;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) NamedCompound(org.jabref.gui.undo.NamedCompound) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange)

Example 19 with UndoableFieldChange

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

the class MassSetFieldAction method massSetField.

/**
     * Set a given field to a given value for all entries in a Collection. This method DOES NOT update any UndoManager,
     * but returns a relevant CompoundEdit that should be registered by the caller.
     *
     * @param entries         The entries to set the field for.
     * @param field           The name of the field to set.
     * @param text            The value to set. This value can be null, indicating that the field should be cleared.
     * @param overwriteValues Indicate whether the value should be set even if an entry already has the field set.
     * @return A CompoundEdit for the entire operation.
     */
private static UndoableEdit massSetField(Collection<BibEntry> entries, String field, String text, boolean overwriteValues) {
    NamedCompound ce = new NamedCompound(Localization.lang("Set field"));
    for (BibEntry entry : entries) {
        Optional<String> oldVal = entry.getField(field);
        // value already for this entry:
        if (!overwriteValues && (oldVal.isPresent()) && !oldVal.get().isEmpty()) {
            continue;
        }
        if (text == null) {
            entry.clearField(field);
        } else {
            entry.setField(field, text);
        }
        ce.addEdit(new UndoableFieldChange(entry, field, oldVal.orElse(null), text));
    }
    ce.end();
    return ce;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) NamedCompound(org.jabref.gui.undo.NamedCompound) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange)

Example 20 with UndoableFieldChange

use of org.jabref.gui.undo.UndoableFieldChange 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)

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