Skip to content

fixes remaining issues in #4844 #4950

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.jabref.logic.externalfiles.LinkedFileHandler;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.net.URLDownload;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.logic.xmp.XmpPreferences;
import org.jabref.logic.xmp.XmpUtilWriter;
import org.jabref.model.database.BibDatabaseContext;
Expand Down Expand Up @@ -194,24 +193,34 @@ public void openFolder() {
}
}

public void rename() {
public void renameToSuggestion() {
renameFileToName(linkedFileHandler.getSuggestedFileName());
}

public void askForNameAndRename() {
String oldFile = this.linkedFile.getLink();
Path oldFilePath = Paths.get(oldFile);
Optional<String> askedFileName = dialogService.showInputDialogWithDefaultAndWait(Localization.lang("Rename file"), Localization.lang("New Filename"), oldFilePath.getFileName().toString());
askedFileName.ifPresent(this::renameFileToName);
}

public void renameFileToName(String targetFileName) {
if (linkedFile.isOnlineLink()) {
// Cannot rename remote links
return;
}

Optional<Path> file = linkedFile.findIn(databaseContext, filePreferences);
if (file.isPresent()) {
performRenameWithConflictCheck();
performRenameWithConflictCheck(targetFileName);
} else {
dialogService.showErrorDialogAndWait(Localization.lang("File not found"), Localization.lang("Could not find file '%0'.", linkedFile.getLink()));
}
}

private void performRenameWithConflictCheck() {
Optional<Path> fileConflictCheck = linkedFileHandler.findExistingFile(linkedFile, entry);
private void performRenameWithConflictCheck(String targetFileName) {
Optional<Path> fileConflictCheck = linkedFileHandler.findExistingFile(linkedFile, entry, targetFileName);
if (fileConflictCheck.isPresent()) {
String targetFileName = linkedFileHandler.getSuggestedFileName();
boolean confirmOverwrite = dialogService.showConfirmationDialogAndWait(
Localization.lang("File exists"),
Localization.lang("'%0' exists. Overwrite file?", targetFileName),
Expand All @@ -233,7 +242,7 @@ private void performRenameWithConflictCheck() {
}

try {
linkedFileHandler.renameToSuggestedName();
linkedFileHandler.renameToName(targetFileName);
} catch (IOException e) {
dialogService.showErrorDialogAndWait(Localization.lang("Rename failed"), Localization.lang("JabRef cannot access the file because it is being used by another process."));
}
Expand Down Expand Up @@ -302,7 +311,7 @@ public boolean isGeneratedPathSameAsOriginal() {

public void moveToDefaultDirectoryAndRename() {
moveToDefaultDirectory();
rename();
renameToSuggestion();
}

/**
Expand Down Expand Up @@ -355,17 +364,6 @@ public void edit() {
});
}

public void renameFile() {
String oldFile = this.linkedFile.getLink();
Path oldFilePath = Paths.get(oldFile);
Optional<String> editedFile = dialogService.showInputDialogWithDefaultAndWait(Localization.lang("Rename file"), Localization.lang("New Filename"), oldFilePath.getFileName().toString());
editedFile.ifPresent(file -> {
Path newFile = Paths.get(oldFile).resolveSibling(file);
this.linkedFile.setLink(newFile.toString());
FileUtil.renameFile(Paths.get(oldFile), newFile);
});
}

public void writeXMPMetadata() {
// Localization.lang("Writing XMP-metadata...")
BackgroundTask<Void> writeTask = BackgroundTask.wrap(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,12 @@ private ContextMenu createContextMenuForFile(LinkedFileViewModel linkedFile) {
download.setOnAction(event -> linkedFile.download());

MenuItem renameFile = new MenuItem(Localization.lang("Rename file to defined pattern"));
renameFile.setOnAction(event -> linkedFile.rename());
renameFile.setOnAction(event -> linkedFile.renameToSuggestion());
renameFile.setDisable(linkedFile.getFile().isOnlineLink() || linkedFile.isGeneratedNameSameAsOriginal());

MenuItem renameFileName = new MenuItem(Localization.lang("Rename file to a given name"));
renameFileName.setOnAction(event -> linkedFile.renameFile());
renameFileName.setOnAction(event -> linkedFile.askForNameAndRename());
renameFileName.setDisable(linkedFile.getFile().isOnlineLink());

MenuItem moveFile = new MenuItem(Localization.lang("Move file to file directory"));
moveFile.setOnAction(event -> linkedFile.moveToDefaultDirectory());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,16 @@ public boolean moveToDefaultDirectory() throws IOException {
}

public boolean renameToSuggestedName() throws IOException {
return renameToName(getSuggestedFileName());
}

public boolean renameToName(String targetFileName) throws IOException {
Optional<Path> oldFile = fileEntry.findIn(databaseContext, filePreferences);
if (!oldFile.isPresent()) {
// Could not find file
return false;
}

String targetFileName = getSuggestedFileName();
Path newPath = oldFile.get().resolveSibling(targetFileName);

String expandedOldFilePath = oldFile.get().toString();
Expand Down Expand Up @@ -124,8 +127,7 @@ public String getSuggestedFileName() {
* @return First identified path that matches an existing file. This name can be used in subsequent calls to
* override the existing file.
*/
public Optional<Path> findExistingFile(LinkedFile flEntry, BibEntry entry) {
String targetFileName = getSuggestedFileName();
public Optional<Path> findExistingFile(LinkedFile flEntry, BibEntry entry, String targetFileName) {
// The .get() is legal without check because the method will always return a value.
Path targetFilePath = flEntry.findIn(databaseContext, filePreferences)
.get().getParent().resolve(targetFileName);
Expand Down