Skip to content

Commit

Permalink
fixes remaining issues in #4844
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasgeiger committed May 4, 2019
1 parent 99d99cf commit c0746a1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
38 changes: 19 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,36 @@ 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 +244,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 +313,7 @@ public boolean isGeneratedPathSameAsOriginal() {

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

/**
Expand Down Expand Up @@ -355,17 +366,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

0 comments on commit c0746a1

Please sign in to comment.