Search in sources :

Example 6 with FieldChange

use of org.jabref.model.FieldChange 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 7 with FieldChange

use of org.jabref.model.FieldChange 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)

Example 8 with FieldChange

use of org.jabref.model.FieldChange in project jabref by JabRef.

the class RenamePdfCleanup method cleanup.

@Override
public List<FieldChange> cleanup(BibEntry entry) {
    List<LinkedFile> newFileList;
    List<LinkedFile> fileList;
    if (singleFieldCleanup != null) {
        fileList = Arrays.asList(singleFieldCleanup);
        newFileList = entry.getFiles().stream().filter(x -> !x.equals(singleFieldCleanup)).collect(Collectors.toList());
    } else {
        newFileList = new ArrayList<>();
        fileList = entry.getFiles();
    }
    boolean changed = false;
    for (LinkedFile flEntry : fileList) {
        String realOldFilename = flEntry.getLink();
        if (onlyRelativePaths && Paths.get(realOldFilename).isAbsolute()) {
            newFileList.add(flEntry);
            continue;
        }
        //old path and old filename
        Optional<Path> expandedOldFile = flEntry.findIn(databaseContext, fileDirectoryPreferences);
        if ((!expandedOldFile.isPresent()) || (expandedOldFile.get().getParent() == null)) {
            // something went wrong. Just skip this entry
            newFileList.add(flEntry);
            continue;
        }
        String targetFileName = getTargetFileName(flEntry, entry);
        Path newPath = expandedOldFile.get().getParent().resolve(targetFileName);
        String expandedOldFilePath = expandedOldFile.get().toString();
        boolean pathsDifferOnlyByCase = newPath.toString().equalsIgnoreCase(expandedOldFilePath) && !newPath.toString().equals(expandedOldFilePath);
        if (Files.exists(newPath) && !pathsDifferOnlyByCase) {
            // we do not overwrite files
            // Since File.exists is sometimes not case-sensitive, the check pathsDifferOnlyByCase ensures that we
            // nonetheless rename files to a new name which just differs by case.
            // TODO: we could check here if the newPath file is linked with the current entry. And if not, we could add a link
            LOGGER.debug("There already exists a file with that name " + newPath.getFileName() + " so I won't rename it");
            newFileList.add(flEntry);
            continue;
        }
        try {
            if (!Files.exists(newPath)) {
                Files.createDirectories(newPath);
            }
        } catch (IOException e) {
            LOGGER.error("Could not create necessary target directoires for renaming", e);
        }
        boolean renameSuccessful = FileUtil.renameFile(Paths.get(expandedOldFilePath), newPath, true);
        if (renameSuccessful) {
            changed = true;
            //Change the path for this entry
            String description = flEntry.getDescription();
            String type = flEntry.getFileType();
            //We use the file directory (if none is set - then bib file) to create relative file links
            Optional<Path> settingsDir = databaseContext.getFirstExistingFileDir(fileDirectoryPreferences);
            if (settingsDir.isPresent()) {
                Path parent = settingsDir.get();
                String newFileEntryFileName;
                if (parent == null) {
                    newFileEntryFileName = targetFileName;
                } else {
                    newFileEntryFileName = parent.relativize(newPath).toString();
                }
                newFileList.add(new LinkedFile(description, newFileEntryFileName, type));
            }
        } else {
            unsuccessfulRenames++;
        }
    }
    if (changed) {
        Optional<FieldChange> change = entry.setFiles(newFileList);
        //if we put a null undo object here, the change by "doMakePathsRelative" would overwrite the field value nevertheless.
        if (change.isPresent()) {
            return Collections.singletonList(change.get());
        } else {
            return Collections.emptyList();
        }
    }
    return Collections.emptyList();
}
Also used : Path(java.nio.file.Path) LinkedFile(org.jabref.model.entry.LinkedFile) FieldChange(org.jabref.model.FieldChange) IOException(java.io.IOException)

Example 9 with FieldChange

use of org.jabref.model.FieldChange in project jabref by JabRef.

the class UpgradePdfPsToFileCleanup method cleanup.

@Override
public List<FieldChange> cleanup(BibEntry entry) {
    List<FieldChange> changes = new ArrayList<>();
    // If there are already links in the file field, keep those on top:
    String oldFileContent = entry.getField(FieldName.FILE).orElse(null);
    List<LinkedFile> fileList = new ArrayList<>(entry.getFiles());
    int oldItemCount = fileList.size();
    for (Map.Entry<String, String> field : fields.entrySet()) {
        entry.getField(field.getKey()).ifPresent(o -> {
            if (o.trim().isEmpty()) {
                return;
            }
            File f = new File(o);
            LinkedFile flEntry = new LinkedFile(f.getName(), o, field.getValue());
            fileList.add(flEntry);
            entry.clearField(field.getKey());
            changes.add(new FieldChange(entry, field.getKey(), o, null));
        });
    }
    if (fileList.size() != oldItemCount) {
        String newValue = FileFieldWriter.getStringRepresentation(fileList);
        entry.setField(FieldName.FILE, newValue);
        changes.add(new FieldChange(entry, FieldName.FILE, oldFileContent, newValue));
    }
    return changes;
}
Also used : LinkedFile(org.jabref.model.entry.LinkedFile) ArrayList(java.util.ArrayList) FieldChange(org.jabref.model.FieldChange) Map(java.util.Map) HashMap(java.util.HashMap) File(java.io.File) LinkedFile(org.jabref.model.entry.LinkedFile)

Example 10 with FieldChange

use of org.jabref.model.FieldChange in project jabref by JabRef.

the class BibDatabaseWriter method applySaveActions.

private static List<FieldChange> applySaveActions(List<BibEntry> toChange, MetaData metaData) {
    List<FieldChange> changes = new ArrayList<>();
    Optional<FieldFormatterCleanups> saveActions = metaData.getSaveActions();
    saveActions.ifPresent(actions -> {
        for (BibEntry entry : toChange) {
            changes.addAll(actions.applySaveActions(entry));
        }
    });
    return changes;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) ArrayList(java.util.ArrayList) FieldChange(org.jabref.model.FieldChange) FieldFormatterCleanups(org.jabref.model.cleanup.FieldFormatterCleanups)

Aggregations

FieldChange (org.jabref.model.FieldChange)35 BibEntry (org.jabref.model.entry.BibEntry)17 ArrayList (java.util.ArrayList)13 UndoableFieldChange (org.jabref.gui.undo.UndoableFieldChange)8 NamedCompound (org.jabref.gui.undo.NamedCompound)7 Test (org.junit.Test)7 KeywordList (org.jabref.model.entry.KeywordList)6 LinkedFile (org.jabref.model.entry.LinkedFile)6 IOException (java.io.IOException)3 File (java.io.File)2 Path (java.nio.file.Path)2 Keyword (org.jabref.model.entry.Keyword)2 FieldChangedEvent (org.jabref.model.entry.event.FieldChangedEvent)2 Subscribe (com.google.common.eventbus.Subscribe)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 AbstractUndoableEdit (javax.swing.undo.AbstractUndoableEdit)1 BasePanel (org.jabref.gui.BasePanel)1 CleanupWorker (org.jabref.logic.cleanup.CleanupWorker)1