Skip to content
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

[#4306] Disable renaming #4727

Merged
merged 2 commits into from
Mar 16, 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
33 changes: 33 additions & 0 deletions src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.BiPredicate;

import javax.xml.transform.TransformerException;

Expand Down Expand Up @@ -44,6 +45,7 @@
import org.jabref.model.entry.LinkedFile;
import org.jabref.model.metadata.FilePreferences;
import org.jabref.model.strings.StringUtil;
import org.jabref.model.util.OptionalUtil;
import org.jabref.preferences.JabRefPreferences;

import org.slf4j.Logger;
Expand Down Expand Up @@ -267,6 +269,37 @@ public void moveToDefaultDirectory() {
}
}

/**
* Gets the filename for the current linked file and compares it to the new suggested filename.
* @return true if the suggested filename is same as current filename.
*/
public boolean isGeneratedNameSameAsOriginal() {
Path file = Paths.get(this.linkedFile.getLink());
String currentFileName = file.getFileName().toString();
String suggestedFileName = this.linkedFileHandler.getSuggestedFileName();

return currentFileName.equals(suggestedFileName);
}

/**
* Compares suggested filepath of current linkedFile with existing filepath.
* @return true if suggested filepath is same as existing filepath.
*/
public boolean isGeneratedPathSameAsOriginal() {
Optional<Path> newDir = databaseContext.getFirstExistingFileDir(filePreferences);

Optional<Path> currentDir = linkedFile.findIn(databaseContext, filePreferences);

BiPredicate<Path, Path> equality = (fileA, fileB) -> {
try {
return Files.isSameFile(fileA, fileB);
} catch (IOException e) {
return false;
}
};
return OptionalUtil.equals(newDir, currentDir, equality);
}

public void moveToDefaultDirectoryAndRename() {
moveToDefaultDirectory();
rename();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,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.isGeneratedNameSameAsOriginal());

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.isGeneratedPathSameAsOriginal());

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.isGeneratedPathSameAsOriginal());

MenuItem deleteFile = new MenuItem(Localization.lang("Permanently delete local file"));
deleteFile.setOnAction(event -> viewModel.deleteFile(linkedFile));
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/jabref/model/util/OptionalUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand All @@ -20,6 +21,18 @@ public static <T> List<T> toList(Optional<T> value) {
}
}

public static <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;
}
}
}

/**
* No longer needed in Java 9 where {@code Optional<T>.stream()} is added.
*/
Expand Down