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);
}
}
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));
}
}
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();
}
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;
}
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;
}
Aggregations