From 48cdcda31c0451788ef6c7db72e4ac017e87951b Mon Sep 17 00:00:00 2001 From: Stefan Kolb Date: Mon, 23 May 2016 15:23:11 +0200 Subject: [PATCH] Fix #1420 Auto downloader should respect file pattern and propose correct filename --- .../sf/jabref/external/DownloadExternalFile.java | 4 +++- .../sf/jabref/external/DroppedFileHandler.java | 2 +- .../net/sf/jabref/external/MoveFileAction.java | 2 +- .../sf/jabref/logic/cleanup/RenamePdfCleanup.java | 2 +- .../net/sf/jabref/logic/util/io/FileUtil.java | 3 +-- .../net/sf/jabref/logic/util/io/FileUtilTest.java | 15 +++++++++++++++ 6 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/sf/jabref/external/DownloadExternalFile.java b/src/main/java/net/sf/jabref/external/DownloadExternalFile.java index 140502dd670a..c0d0e6b0782d 100644 --- a/src/main/java/net/sf/jabref/external/DownloadExternalFile.java +++ b/src/main/java/net/sf/jabref/external/DownloadExternalFile.java @@ -26,6 +26,7 @@ import javax.swing.SwingUtilities; import net.sf.jabref.BibDatabaseContext; +import net.sf.jabref.Globals; import net.sf.jabref.JabRefExecutorService; import net.sf.jabref.gui.FileListEntry; import net.sf.jabref.gui.FileListEntryEditor; @@ -267,7 +268,8 @@ private void downloadFinished() { } // FIXME: will break download if no bibtexkey is present! private String getSuggestedFileName(String suffix) { - String plannedName = bibtexKey == null ? "set-filename" : bibtexKey; + String plannedName = FileUtil.createFileNameFromPattern(databaseContext.getDatabase(), frame.getCurrentBasePanel().getSelectedEntries().get(0), Globals.journalAbbreviationLoader.getRepository()); + if (!suffix.isEmpty()) { plannedName += "." + suffix; } diff --git a/src/main/java/net/sf/jabref/external/DroppedFileHandler.java b/src/main/java/net/sf/jabref/external/DroppedFileHandler.java index 217e3ca9d88a..c8f09083c516 100644 --- a/src/main/java/net/sf/jabref/external/DroppedFileHandler.java +++ b/src/main/java/net/sf/jabref/external/DroppedFileHandler.java @@ -350,7 +350,7 @@ private boolean showLinkMoveCopyRenameDialog(String linkFileName, ExternalFileTy renameCheckBox.setText(Localization.lang("Rename file to").concat(": ")); // Determine which name to suggest: - String targetName = FileUtil.getLinkedFileName(database, entry, Globals.journalAbbreviationLoader.getRepository()); + String targetName = FileUtil.createFileNameFromPattern(database, entry, Globals.journalAbbreviationLoader.getRepository()); renameToTextBox.setText(targetName.concat(".").concat(fileType.getExtension())); diff --git a/src/main/java/net/sf/jabref/external/MoveFileAction.java b/src/main/java/net/sf/jabref/external/MoveFileAction.java index 37766f28c3fd..23caa58e7ef0 100644 --- a/src/main/java/net/sf/jabref/external/MoveFileAction.java +++ b/src/main/java/net/sf/jabref/external/MoveFileAction.java @@ -110,7 +110,7 @@ public void actionPerformed(ActionEvent event) { if (toFileDir) { // Determine which name to suggest: String suggName = FileUtil - .getLinkedFileName(eEditor.getDatabase(), eEditor.getEntry(), + .createFileNameFromPattern(eEditor.getDatabase(), eEditor.getEntry(), Globals.journalAbbreviationLoader.getRepository()) .concat(entry.type.isPresent() ? "." + entry.type.get().getExtension() : ""); CheckBoxMessage cbm = new CheckBoxMessage(Localization.lang("Move file to file directory?"), diff --git a/src/main/java/net/sf/jabref/logic/cleanup/RenamePdfCleanup.java b/src/main/java/net/sf/jabref/logic/cleanup/RenamePdfCleanup.java index 7f511b4ace70..b8bf7331429b 100644 --- a/src/main/java/net/sf/jabref/logic/cleanup/RenamePdfCleanup.java +++ b/src/main/java/net/sf/jabref/logic/cleanup/RenamePdfCleanup.java @@ -59,7 +59,7 @@ public List cleanup(BibEntry entry) { } StringBuilder newFilename = new StringBuilder( - FileUtil.getLinkedFileName(databaseContext.getDatabase(), entry, repository)); + FileUtil.createFileNameFromPattern(databaseContext.getDatabase(), entry, repository)); //Add extension to newFilename newFilename.append('.').append(FileUtil.getFileExtension(realOldFilename).orElse("pdf")); diff --git a/src/main/java/net/sf/jabref/logic/util/io/FileUtil.java b/src/main/java/net/sf/jabref/logic/util/io/FileUtil.java index 4ba686095dbf..bc165632dcfb 100644 --- a/src/main/java/net/sf/jabref/logic/util/io/FileUtil.java +++ b/src/main/java/net/sf/jabref/logic/util/io/FileUtil.java @@ -389,8 +389,7 @@ public static List getListOfLinkedFiles(List bes, List f * @param repository * @return a suggested fileName */ - public static String getLinkedFileName(BibDatabase database, BibEntry entry, - JournalAbbreviationRepository repository) { + public static String createFileNameFromPattern(BibDatabase database, BibEntry entry, JournalAbbreviationRepository repository) { String targetName = entry.getCiteKey() == null ? "default" : entry.getCiteKey(); StringReader sr = new StringReader(Globals.prefs.get(JabRefPreferences.PREF_IMPORT_FILENAMEPATTERN)); Layout layout = null; diff --git a/src/test/java/net/sf/jabref/logic/util/io/FileUtilTest.java b/src/test/java/net/sf/jabref/logic/util/io/FileUtilTest.java index 8ca96d10b666..86a16c8b5188 100644 --- a/src/test/java/net/sf/jabref/logic/util/io/FileUtilTest.java +++ b/src/test/java/net/sf/jabref/logic/util/io/FileUtilTest.java @@ -5,12 +5,27 @@ import java.util.Arrays; import java.util.List; +import net.sf.jabref.Globals; +import net.sf.jabref.JabRefPreferences; +import net.sf.jabref.logic.journals.JournalAbbreviationLoader; +import net.sf.jabref.model.entry.BibEntry; + import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; public class FileUtilTest { + @Test + public void testGetLinkedFileName() { + Globals.prefs = JabRefPreferences.getInstance(); + Globals.journalAbbreviationLoader = new JournalAbbreviationLoader(Globals.prefs); + BibEntry entry = new BibEntry(); + entry.setCiteKey("1234"); + entry.setField("title", "mytitle"); + + assertEquals("1234 - mytitle", FileUtil.createFileNameFromPattern(null, entry, Globals.journalAbbreviationLoader.getRepository())); + } @Test public void testGetFileExtensionSimpleFile() {