Skip to content

Commit

Permalink
Fix #1420 Auto downloader should respect file pattern and propose cor…
Browse files Browse the repository at this point in the history
…rect filename
  • Loading branch information
stefan-kolb committed May 23, 2016
1 parent 9021933 commit 48cdcda
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/sf/jabref/external/MoveFileAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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?"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public List<FieldChange> 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"));
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/net/sf/jabref/logic/util/io/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,7 @@ public static List<File> getListOfLinkedFiles(List<BibEntry> bes, List<String> 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;
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/net/sf/jabref/logic/util/io/FileUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 48cdcda

Please sign in to comment.