Skip to content
29 changes: 29 additions & 0 deletions src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,35 @@ public void openFolder() {
}
}

public boolean performNameConflictCheck() {
/*
Get the filepath for current linkedFile and truncate it to only have the filename and
not the filepath. Compare to new suggested filename and return boolean result.
*/
Path file = Paths.get(this.linkedFile.getLink());
String currentFileName = file.getFileName().toString();
String suggestedFileName = this.linkedFileHandler.getSuggestedFileName();

return currentFileName.equals(suggestedFileName);
}

public boolean performDirectoryConflictCheck() {
/*
Get the suggested filepath for current linkedFile and compare it to the existing filepath.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of code documentation is somewhat contrary to the usual style used in the rest of the code. Either move these comments before the method as JavaDoc comments, or use normal inline comments as done below.

Return boolean result.
*/
//Get the new path for the file
Optional<Path> newDir = databaseContext.getFirstExistingFileDir(filePreferences);
Path newDirectory = newDir.get();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The get method throws an exception if the optional is empty. I would propose to rewrite the code as return OptionalUtil.equals(newDir, currentDirectory, Files::isSameFile); and add the following method to OptionalUtil

public <T,U> boolean equals(Optional<T> left, Optional<U> right, BiPredicate<T,U> equality) {
   if(!left.isPresent()) {
       return !right.isPresent();
   } else {
       if (right.isPresent()) {
           return equality.test(left.get(), right.get());
      } else {
           return false;
      }
   }
}

(there might be syntax errors in the above code)


//Get the current path for the file
Optional<Path> currentDir = linkedFile.findIn(databaseContext, filePreferences);
Path currentDirectory = currentDir.get();

//Compare the two paths
return newDirectory.toString().equals(currentDirectory.getParent().toString());
}

public void rename() {
if (linkedFile.isOnlineLink()) {
// Cannot rename remote links
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,15 @@ private ContextMenu createContextMenuForFile(LinkedFileViewModel linkedFile) {

MenuItem renameFile = new MenuItem(Localization.lang("Rename file"));
renameFile.setOnAction(event -> linkedFile.rename());
renameFile.setDisable(linkedFile.getFile().isOnlineLink());
renameFile.setDisable(linkedFile.getFile().isOnlineLink() || linkedFile.performNameConflictCheck());

MenuItem moveFile = new MenuItem(Localization.lang("Move file to file directory"));
moveFile.setOnAction(event -> linkedFile.moveToDefaultDirectory());
moveFile.setDisable(linkedFile.getFile().isOnlineLink());
moveFile.setDisable(linkedFile.getFile().isOnlineLink() || linkedFile.performDirectoryConflictCheck());

MenuItem renameAndMoveFile = new MenuItem(Localization.lang("Move file to file directory and rename file"));
renameAndMoveFile.setOnAction(event -> linkedFile.moveToDefaultDirectoryAndRename());
renameAndMoveFile.setDisable(linkedFile.getFile().isOnlineLink());
renameAndMoveFile.setDisable(linkedFile.getFile().isOnlineLink() || linkedFile.performDirectoryConflictCheck());

MenuItem deleteFile = new MenuItem(Localization.lang("Permanently delete local file"));
deleteFile.setOnAction(event -> viewModel.deleteFile(linkedFile));
Expand Down