Skip to content

Commit

Permalink
Fix that "Rename and move file" throws file not found exception
Browse files Browse the repository at this point in the history
Fixes #4307. Moreover, I refactored a bit how files are renamed/moved to the default file directory. This should make it easier to reuse the code.
  • Loading branch information
tobiasdiez committed Sep 4, 2018
1 parent b39ed7b commit dafd614
Show file tree
Hide file tree
Showing 64 changed files with 544 additions and 728 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ private void exportFile(List<ParserResult> loaded, String[] data) {
BibDatabaseContext databaseContext = pr.getDatabaseContext();
databaseContext.setDatabaseFile(theFile);
Globals.prefs.fileDirForDatabase = databaseContext
.getFileDirectories(Globals.prefs.getFileDirectoryPreferences());
.getFileDirectories(Globals.prefs.getFilePreferences());
System.out.println(Localization.lang("Exporting") + ": " + data[0]);
Optional<Exporter> exporter = Globals.exportFactory.getExporterByName(data[1]);
if (!exporter.isPresent()) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public BasePanel(JabRefFrame frame, BasePanelPreferences preferences, BibDatabas
this.tableModel = new MainTableDataModel(getBibDatabaseContext());

citationStyleCache = new CitationStyleCache(bibDatabaseContext);
annotationCache = new FileAnnotationCache(bibDatabaseContext, Globals.prefs.getFileDirectoryPreferences());
annotationCache = new FileAnnotationCache(bibDatabaseContext, Globals.prefs.getFilePreferences());

setupMainPanel();

Expand Down Expand Up @@ -370,7 +370,7 @@ private void setupActions() {
actions.put(Actions.OPEN_EXTERNAL_FILE, this::openExternalFile);

actions.put(Actions.OPEN_FOLDER, () -> JabRefExecutorService.INSTANCE.execute(() -> {
final List<Path> files = FileUtil.getListOfLinkedFiles(mainTable.getSelectedEntries(), bibDatabaseContext.getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences()));
final List<Path> files = FileUtil.getListOfLinkedFiles(mainTable.getSelectedEntries(), bibDatabaseContext.getFileDirectoriesAsPaths(Globals.prefs.getFilePreferences()));
for (final Path f : files) {
try {
JabRefDesktop.openFolderAndSelectFile(f.toAbsolutePath());
Expand Down Expand Up @@ -1458,7 +1458,7 @@ public void searchAndOpen() {
}

final Set<ExternalFileType> types = ExternalFileTypes.getInstance().getExternalFileTypeSelection();
final List<Path> dirs = basePanel.getBibDatabaseContext().getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences());
final List<Path> dirs = basePanel.getBibDatabaseContext().getFileDirectoriesAsPaths(Globals.prefs.getFilePreferences());
final List<String> extensions = types.stream().map(ExternalFileType::getExtension).collect(Collectors.toList());

// Run the search operation:
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/org/jabref/gui/PreviewPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.event.FieldChangedEvent;
import org.jabref.preferences.JabRefPreferences;
import org.jabref.preferences.PreviewPreferences;

import com.google.common.eventbus.Subscribe;
Expand Down Expand Up @@ -89,12 +88,10 @@ public PreviewPanel(BasePanel panel, BibDatabaseContext databaseContext, KeyBind
this.keyBindingRepository = keyBindingRepository;

fileHandler = new NewDroppedFileHandler(dialogService, databaseContext, externalFileTypes,
Globals.prefs.getFileDirectoryPreferences(),
Globals.prefs.getCleanupPreferences(Globals.journalAbbreviationLoader).getFileDirPattern(),
Globals.prefs.getFilePreferences(),
Globals.prefs.getImportFormatPreferences(),
Globals.prefs.getUpdateFieldPreferences(),
Globals.getFileUpdateMonitor(),
Globals.prefs.get(JabRefPreferences.IMPORT_FILENAMEPATTERN));
Globals.getFileUpdateMonitor());

// Set up scroll pane for preview pane
setFitToHeight(true);
Expand Down
8 changes: 1 addition & 7 deletions src/main/java/org/jabref/gui/actions/CleanupAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ private void doCleanup(CleanupPreset preset, BibEntry entry, NamedCompound ce) {
Globals.journalAbbreviationLoader));
List<FieldChange> changes = cleaner.cleanup(preset, entry);

unsuccessfulRenames = cleaner.getUnsuccessfulRenames();

if (changes.isEmpty()) {
return;
}
Expand All @@ -90,11 +88,7 @@ private void showResults() {
if (isCanceled) {
return;
}
if (unsuccessfulRenames > 0) { //Rename failed for at least one entry
dialogService.showErrorDialogAndWait(
Localization.lang("Autogenerate PDF Names"),
Localization.lang("File rename failed for %0 entries.", Integer.toString(unsuccessfulRenames)));
}

if (modifiedEntriesCount > 0) {
panel.updateEntryEditorIfShowing();
panel.markBaseChanged();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public IntegrityCheckAction(JabRefFrame frame) {
@Override
public void execute() {
IntegrityCheck check = new IntegrityCheck(frame.getCurrentBasePanel().getBibDatabaseContext(),
Globals.prefs.getFileDirectoryPreferences(),
Globals.prefs.getFilePreferences(),
Globals.prefs.getBibtexKeyPatternPreferences(),
Globals.journalAbbreviationLoader.getRepository(Globals.prefs.getJournalAbbreviationPreferences()),
Globals.prefs.getBoolean(JabRefPreferences.ENFORCE_LEGAL_BIBTEX_KEY));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/actions/WriteXMPAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private void writeXMP() {
// Make a list of all PDFs linked from this entry:
List<Path> files = entry.getFiles().stream()
.filter(file -> file.getFileType().equalsIgnoreCase("pdf"))
.map(file -> file.findIn(basePanel.getBibDatabaseContext(), Globals.prefs.getFileDirectoryPreferences()))
.map(file -> file.findIn(basePanel.getBibDatabaseContext(), Globals.prefs.getFilePreferences()))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private void init() {
cleanUpISSN = new JCheckBox(Localization.lang("Reformat ISSN"));

Optional<Path> firstExistingDir = databaseContext
.getFirstExistingFileDir(JabRefPreferences.getInstance().getFileDirectoryPreferences());
.getFirstExistingFileDir(JabRefPreferences.getInstance().getFilePreferences());
if (firstExistingDir.isPresent()) {
cleanUpMovePDF = new JCheckBox(Localization.lang("Move linked files to default file directory %0",
firstExistingDir.get().toString()));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/copyfiles/CopyFilesTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected List<CopyFilesResultItemViewModel> call() throws InterruptedException,

LinkedFile fileName = files.get(j);

Optional<Path> fileToExport = fileName.findIn(databaseContext, Globals.prefs.getFileDirectoryPreferences());
Optional<Path> fileToExport = fileName.findIn(databaseContext, Globals.prefs.getFilePreferences());

newPath = OptionalUtil.combine(Optional.of(exportPath), fileToExport, resolvePathFilename);

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/desktop/JabRefDesktop.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static void openExternalViewer(BibDatabaseContext databaseContext, String
String fieldName = initialFieldName;
if (FieldName.PS.equals(fieldName) || FieldName.PDF.equals(fieldName)) {
// Find the default directory for this field type:
List<String> dir = databaseContext.getFileDirectories(fieldName, Globals.prefs.getFileDirectoryPreferences());
List<String> dir = databaseContext.getFileDirectories(fieldName, Globals.prefs.getFilePreferences());

Optional<Path> file = FileHelper.expandFilename(link, dir);

Expand Down Expand Up @@ -128,7 +128,7 @@ public static boolean openExternalFileAnyFormat(final BibDatabaseContext databas
return true;
}

Optional<Path> file = FileHelper.expandFilename(databaseContext, link, Globals.prefs.getFileDirectoryPreferences());
Optional<Path> file = FileHelper.expandFilename(databaseContext, link, Globals.prefs.getFilePreferences());
if (file.isPresent() && Files.exists(file.get())) {
// Open the file:
String filePath = file.get().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ private void setCurrentDocument(Path path) {
public void switchToFile(LinkedFile file) {
if (file != null) {
stateManager.getActiveDatabase().ifPresent(database ->
file.findIn(database, Globals.prefs.getFileDirectoryPreferences())
.ifPresent(this::setCurrentDocument));
file.findIn(database, Globals.prefs.getFilePreferences())
.ifPresent(this::setCurrentDocument));
}
}

Expand Down
7 changes: 2 additions & 5 deletions src/main/java/org/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.util.FileUpdateMonitor;
import org.jabref.preferences.JabRefPreferences;

import com.airhacks.afterburner.views.ViewLoader;
import org.fxmisc.easybind.EasyBind;
Expand Down Expand Up @@ -102,12 +101,10 @@ public EntryEditor(BasePanel panel, EntryEditorPreferences preferences, FileUpda
this.dialogService = dialogService;

fileHandler = new NewDroppedFileHandler(dialogService, databaseContext, externalFileTypes,
Globals.prefs.getFileDirectoryPreferences(),
Globals.prefs.getCleanupPreferences(Globals.journalAbbreviationLoader).getFileDirPattern(),
Globals.prefs.getFilePreferences(),
Globals.prefs.getImportFormatPreferences(),
Globals.prefs.getUpdateFieldPreferences(),
Globals.getFileUpdateMonitor(),
Globals.prefs.get(JabRefPreferences.IMPORT_FILENAMEPATTERN));
Globals.getFileUpdateMonitor());

ViewLoader.view(this)
.root(this)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/exporter/ExportCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ private void export(Path file, FileChooser.ExtensionFilter selectedExtensionFilt
// so formatters can resolve linked files correctly.
// (This is an ugly hack!)
Globals.prefs.fileDirForDatabase = frame.getCurrentBasePanel()
.getBibDatabaseContext()
.getFileDirectories(Globals.prefs.getFileDirectoryPreferences());
.getBibDatabaseContext()
.getFileDirectories(Globals.prefs.getFilePreferences());

// Make sure we remember which filter was used, to set
// the default for next time:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private String exportToClipboard(Exporter exporter) {
// Set the global variable for this database's file directory before exporting,
// so formatters can resolve linked files correctly.
// (This is an ugly hack!)
Globals.prefs.fileDirForDatabase = panel.getBibDatabaseContext().getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences()).stream().map(Path::toString).collect(Collectors.toList());
Globals.prefs.fileDirForDatabase = panel.getBibDatabaseContext().getFileDirectoriesAsPaths(Globals.prefs.getFilePreferences()).stream().map(Path::toString).collect(Collectors.toList());

Path tmp = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.LinkedFile;
import org.jabref.model.metadata.FileDirectoryPreferences;
import org.jabref.model.metadata.FilePreferences;
import org.jabref.model.util.FileHelper;

import org.slf4j.Logger;
Expand All @@ -31,8 +31,8 @@ public class AutoSetFileLinksUtil {
private AutoLinkPreferences autoLinkPreferences;
private ExternalFileTypes externalFileTypes;

public AutoSetFileLinksUtil(BibDatabaseContext databaseContext, FileDirectoryPreferences fileDirectoryPreferences, AutoLinkPreferences autoLinkPreferences, ExternalFileTypes externalFileTypes) {
this(databaseContext.getFileDirectoriesAsPaths(fileDirectoryPreferences), autoLinkPreferences, externalFileTypes);
public AutoSetFileLinksUtil(BibDatabaseContext databaseContext, FilePreferences filePreferences, AutoLinkPreferences autoLinkPreferences, ExternalFileTypes externalFileTypes) {
this(databaseContext.getFileDirectoriesAsPaths(filePreferences), autoLinkPreferences, externalFileTypes);
}

public AutoSetFileLinksUtil(List<Path> directories, AutoLinkPreferences autoLinkPreferences, ExternalFileTypes externalFileTypes) {
Expand Down Expand Up @@ -68,7 +68,7 @@ public List<LinkedFile> findAssociatedNotLinkedFiles(BibEntry entry) throws IOEx
.orElse(Optional.of(new UnknownExternalFileType("")));

String strType = type.isPresent() ? type.get().getName() : "";
String relativeFilePath = FileUtil.shortenFileName(foundFile, directories).toString();
String relativeFilePath = FileUtil.relativize(foundFile, directories).toString();
LinkedFile linkedFile = new LinkedFile("", relativeFilePath, strType);
linkedFiles.add(linkedFile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static Runnable autoSetLinks(final List<BibEntry> entries, final NamedCom

Runnable r = () -> {
boolean foundAny = false;
AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(databaseContext, Globals.prefs.getFileDirectoryPreferences(), Globals.prefs.getAutoLinkPreferences(), ExternalFileTypes.getInstance());
AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(databaseContext, Globals.prefs.getFilePreferences(), Globals.prefs.getAutoLinkPreferences(), ExternalFileTypes.getInstance());

for (BibEntry entry : entries) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public void download(URL url, String mimeType, final DownloadCallback callback)
fileType = ExternalFileTypes.getInstance().getExternalFileTypeByExt(suffix);
}
String suggestedName = getSuggestedFileName(suffix);
List<String> fDirectory = databaseContext.getFileDirectories(Globals.prefs.getFileDirectoryPreferences());
List<String> fDirectory = databaseContext.getFileDirectories(Globals.prefs.getFilePreferences());
String directory;
if (fDirectory.isEmpty()) {
directory = null;
Expand Down
26 changes: 13 additions & 13 deletions src/main/java/org/jabref/gui/externalfiles/DroppedFileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ public void handleDroppedfile(String fileName, ExternalFileType fileType, BibEnt
String destFilename;

if (linkInPlace.isSelected()) {
destFilename = FileUtil.shortenFileName(Paths.get(fileName),
panel.getBibDatabaseContext().getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences()))
.toString();
destFilename = FileUtil.relativize(Paths.get(fileName),
panel.getBibDatabaseContext().getFileDirectoriesAsPaths(Globals.prefs.getFilePreferences()))
.toString();
} else {
destFilename = renameCheckBox.isSelected() ? renameToTextBox.getText() : Paths.get(fileName).toString();
if (copyRadioButton.isSelected()) {
Expand Down Expand Up @@ -199,9 +199,9 @@ public void linkPdfToEntry(String fileName, BibEntry entry) {
NamedCompound edits = new NamedCompound(Localization.lang("Drop %0", fileType.getExtension()));

if (linkInPlace.isSelected()) {
destFilename = FileUtil.shortenFileName(Paths.get(fileName),
panel.getBibDatabaseContext().getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences()))
.toString();
destFilename = FileUtil.relativize(Paths.get(fileName),
panel.getBibDatabaseContext().getFileDirectoriesAsPaths(Globals.prefs.getFilePreferences()))
.toString();
} else {
destFilename = renameCheckBox.isSelected() ? renameToTextBox.getText() : new File(fileName).getName();
if (copyRadioButton.isSelected()) {
Expand Down Expand Up @@ -284,9 +284,9 @@ private boolean tryXmpImport(String fileName, ExternalFileType fileType, NamedCo
String destFilename;

if (linkInPlace.isSelected()) {
destFilename = FileUtil.shortenFileName(Paths.get(fileName),
panel.getBibDatabaseContext().getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences()))
.toString();
destFilename = FileUtil.relativize(Paths.get(fileName),
panel.getBibDatabaseContext().getFileDirectoriesAsPaths(Globals.prefs.getFilePreferences()))
.toString();
} else {
if (renameCheckBox.isSelected() || (single == null)) {
destFilename = fileName;
Expand Down Expand Up @@ -325,7 +325,7 @@ private boolean showLinkMoveCopyRenameDialog(String linkFileName, ExternalFileTy
String dialogTitle = Localization.lang("Link to file %0", linkFileName);

Optional<Path> dir = panel.getBibDatabaseContext()
.getFirstExistingFileDir(Globals.prefs.getFileDirectoryPreferences());
.getFirstExistingFileDir(Globals.prefs.getFilePreferences());

if (!dir.isPresent()) {
destDirLabel.setText(Localization.lang("File directory is not set or does not exist!"));
Expand Down Expand Up @@ -416,7 +416,7 @@ private void doLink(BibEntry entry, ExternalFileType fileType, String filename,
if (avoidDuplicate) {
// For comparison, find the absolute filename:
List<Path> dirs = panel.getBibDatabaseContext()
.getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences());
.getFileDirectoriesAsPaths(Globals.prefs.getFilePreferences());
String absFilename;
if (new File(filename).isAbsolute() || dirs.isEmpty()) {
absFilename = filename;
Expand Down Expand Up @@ -475,7 +475,7 @@ private void doLink(BibEntry entry, ExternalFileType fileType, String filename,
*/
private boolean doMove(String fileName, String destFilename, NamedCompound edits) {
Optional<Path> dir = panel.getBibDatabaseContext()
.getFirstExistingFileDir(Globals.prefs.getFileDirectoryPreferences());
.getFirstExistingFileDir(Globals.prefs.getFilePreferences());

if (dir.isPresent()) {
Path destFile = dir.get().resolve(destFilename);
Expand Down Expand Up @@ -522,7 +522,7 @@ private boolean doMove(String fileName, String destFilename, NamedCompound edits
private boolean doCopy(String fileName, String toFile, NamedCompound edits) {

List<String> dirs = panel.getBibDatabaseContext()
.getFileDirectories(Globals.prefs.getFileDirectoryPreferences());
.getFileDirectories(Globals.prefs.getFilePreferences());
int found = -1;
for (int i = 0; i < dirs.size(); i++) {
if (new File(dirs.get(i)).exists()) {
Expand Down
Loading

0 comments on commit dafd614

Please sign in to comment.