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

Make it easier to rename and move files #4200

Merged
merged 2 commits into from
Jul 19, 2018
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- We changed the default keyboard shortcuts for moving between entries when the entry editor is active to ̀<kbd>alt</kbd> + <kbd>up/down</kbd>.
- Opening a new file now prompts the directory of the currently selected file, instead of the directory of the last opened file.
- Window state is saved on close and restored on start.

- We streamlined the process to rename and move files by removing the confirmation dialogs.



Expand Down
32 changes: 7 additions & 25 deletions src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,49 +201,35 @@ public void rename() {
// Cannot rename remote links
return;
}
Optional<Path> fileDir = databaseContext.getFirstExistingFileDir(fileDirectoryPreferences);
Copy link
Member

Choose a reason for hiding this comment

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

Why remove this?

Copy link
Member Author

Choose a reason for hiding this comment

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

This checks whether a certain file directory exists (the one where we usually put files). This is unnecessary if we just want to rename the file...

if (!fileDir.isPresent()) {
dialogService.showErrorDialogAndWait(Localization.lang("Rename file"), Localization.lang("File directory is not set or does not exist!"));
return;
}

Optional<Path> file = linkedFile.findIn(databaseContext, fileDirectoryPreferences);
if ((file.isPresent()) && Files.exists(file.get())) {
RenamePdfCleanup pdfCleanup = new RenamePdfCleanup(false, databaseContext, fileDirPattern, fileDirectoryPreferences, linkedFile);

String targetFileName = pdfCleanup.getTargetFileName(linkedFile, entry);

boolean confirm = dialogService.showConfirmationDialogAndWait(Localization.lang("Rename file"),
Localization.lang("Rename file to") + " " + targetFileName,
Localization.lang("Rename file"),
Localization.lang("Cancel"));

if (confirm) {
Optional<Path> fileConflictCheck = pdfCleanup.findExistingFile(linkedFile, entry);
performRenameWithConflictCheck(file, pdfCleanup, targetFileName, fileConflictCheck);
}
performRenameWithConflictCheck(file.get(), pdfCleanup);
} else {
dialogService.showErrorDialogAndWait(Localization.lang("File not found"), Localization.lang("Could not find file '%0'.", linkedFile.getLink()));
}
}

private void performRenameWithConflictCheck(Optional<Path> file, RenamePdfCleanup pdfCleanup, String targetFileName, Optional<Path> fileConflictCheck) {
private void performRenameWithConflictCheck(Path file, RenamePdfCleanup pdfCleanup) {
boolean confirm;
Optional<Path> fileConflictCheck = pdfCleanup.findExistingFile(linkedFile, entry);
if (!fileConflictCheck.isPresent()) {
try {
pdfCleanup.cleanupWithException(entry);
} catch (IOException e) {
dialogService.showErrorDialogAndWait(Localization.lang("Rename failed"), Localization.lang("JabRef cannot access the file because it is being used by another process."));
}
} else {
String targetFileName = pdfCleanup.getTargetFileName(linkedFile, entry);
confirm = dialogService.showConfirmationDialogAndWait(Localization.lang("File exists"),
Localization.lang("'%0' exists. Overwrite file?", targetFileName),
Localization.lang("Overwrite"),
Localization.lang("Cancel"));

if (confirm) {
try {
FileUtil.renameFileWithException(fileConflictCheck.get(), file.get(), true);
FileUtil.renameFileWithException(fileConflictCheck.get(), file, true);
pdfCleanup.cleanupWithException(entry);
} catch (IOException e) {
dialogService.showErrorDialogAndWait(Localization.lang("Rename failed"),
Expand All @@ -268,13 +254,9 @@ public void moveToDefaultDirectory() {

Optional<Path> file = linkedFile.findIn(databaseContext, fileDirectoryPreferences);
if ((file.isPresent()) && Files.exists(file.get())) {
// Linked file exists, so move it
// Found the linked file, so move it
MoveFilesCleanup moveFiles = new MoveFilesCleanup(databaseContext, fileDirPattern, fileDirectoryPreferences, linkedFile);

boolean confirm = dialogService.showConfirmationDialogAndWait(Localization.lang("Move file"), Localization.lang("Move file to file directory?") + " " + fileDir.get(), Localization.lang("Move file"), Localization.lang("Cancel"));
if (confirm) {
moveFiles.cleanup(entry);
}
moveFiles.cleanup(entry);
} else {
// File doesn't exist, so we can't move it.
dialogService.showErrorDialogAndWait(Localization.lang("File not found"), Localization.lang("Could not find file '%0'.", linkedFile.getLink()));
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/org/jabref/logic/util/io/FileBasedLock.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@
import org.slf4j.LoggerFactory;

public class FileBasedLock {
private static final Logger LOGGER = LoggerFactory.getLogger(FileBasedLock.class);

/**
* The age in ms of a lockfile before JabRef will offer to "steal" the locked file.
* The age in ms of a lock file before JabRef will offer to "steal" the locked file.
*/
public static final long LOCKFILE_CRITICAL_AGE = 60000;

private static final Logger LOGGER = LoggerFactory.getLogger(FileBasedLock.class);
private static final String LOCKFILE_SUFFIX = ".lock";

// default retry count for acquiring file lock
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,6 @@ Error\ while\ fetching\ from\ %0=Error while fetching from %0
Show\ search\ results\ in\ a\ window=Show search results in a window
Show\ global\ search\ results\ in\ a\ window=Show global search results in a window
Search\ in\ all\ open\ libraries=Search in all open libraries
Move\ file\ to\ file\ directory?=Move file to file directory?

Library\ is\ protected.\ Cannot\ save\ until\ external\ changes\ have\ been\ reviewed.=Library is protected. Cannot save until external changes have been reviewed.
Protected\ library=Protected library
Expand Down