Search in sources :

Example 16 with LinkedFile

use of org.jabref.model.entry.LinkedFile in project jabref by JabRef.

the class MoveFilesCleanupTest method movesFileFromSubfolderWithFileDirPattern.

@Test
public void movesFileFromSubfolderWithFileDirPattern() throws IOException {
    File subfolder = bibFolder.newFolder();
    File fileBefore = new File(subfolder, "test.pdf");
    assertTrue(fileBefore.createNewFile());
    assertTrue(new File(subfolder, "test.pdf").exists());
    LinkedFile fileField = new LinkedFile("", fileBefore.getAbsolutePath(), "");
    entry.setField("file", FileFieldWriter.getStringRepresentation(fileField));
    cleanup = new MoveFilesCleanup(databaseContext, "\\EntryType", fileDirPrefs, mock(LayoutFormatterPreferences.class));
    cleanup.cleanup(entry);
    assertFalse(fileBefore.exists());
    Path after = pdfFolder.toPath().resolve("Misc").resolve("test.pdf");
    Path relativefileDir = pdfFolder.toPath().relativize(after);
    assertTrue(Files.exists(after));
    assertEquals(Optional.of(FileFieldWriter.getStringRepresentation(new LinkedFile("", relativefileDir.toString(), ""))), entry.getField("file"));
}
Also used : Path(java.nio.file.Path) LinkedFile(org.jabref.model.entry.LinkedFile) File(java.io.File) LinkedFile(org.jabref.model.entry.LinkedFile) Test(org.junit.Test)

Example 17 with LinkedFile

use of org.jabref.model.entry.LinkedFile in project jabref by JabRef.

the class RenamePdfCleanupTest method cleanupRenamePdfRenamesFileEvenIfOnlyDifferenceIsCase.

/**
     * Test for #466
     */
@Test
public void cleanupRenamePdfRenamesFileEvenIfOnlyDifferenceIsCase() throws IOException {
    String fileNamePattern = "\\bibtexkey";
    File tempFile = testFolder.newFile("toot.tmp");
    LinkedFile fileField = new LinkedFile("", tempFile.getAbsolutePath(), "");
    entry.setField("file", FileFieldWriter.getStringRepresentation(fileField));
    RenamePdfCleanup cleanup = new RenamePdfCleanup(false, context, fileNamePattern, mock(LayoutFormatterPreferences.class), fileDirPrefs);
    cleanup.cleanup(entry);
    LinkedFile newFileField = new LinkedFile("", "Toot.tmp", "");
    assertEquals(Optional.of(FileFieldWriter.getStringRepresentation(newFileField)), entry.getField("file"));
}
Also used : LinkedFile(org.jabref.model.entry.LinkedFile) LayoutFormatterPreferences(org.jabref.logic.layout.LayoutFormatterPreferences) File(java.io.File) LinkedFile(org.jabref.model.entry.LinkedFile) Test(org.junit.Test)

Example 18 with LinkedFile

use of org.jabref.model.entry.LinkedFile in project jabref by JabRef.

the class FileListTableModel method setContent.

private FileListEntry setContent(String val, boolean firstOnly, boolean deduceUnknownTypes) {
    String value = val;
    if (value == null) {
        value = "";
    }
    List<LinkedFile> fields = FileFieldParser.parse(value);
    List<FileListEntry> files = new ArrayList<>();
    for (LinkedFile entry : fields) {
        if (entry.isEmpty()) {
            continue;
        }
        if (firstOnly) {
            return decodeEntry(entry, deduceUnknownTypes);
        } else {
            files.add(decodeEntry(entry, deduceUnknownTypes));
        }
    }
    synchronized (list) {
        list.clear();
        list.addAll(files);
    }
    fireTableChanged(new TableModelEvent(this));
    return null;
}
Also used : LinkedFile(org.jabref.model.entry.LinkedFile) TableModelEvent(javax.swing.event.TableModelEvent) ArrayList(java.util.ArrayList)

Example 19 with LinkedFile

use of org.jabref.model.entry.LinkedFile in project jabref by JabRef.

the class LinkedFilesEditorViewModel method addFromURL.

private void addFromURL(URL url) {
    URLDownload urlDownload = new URLDownload(url);
    Optional<ExternalFileType> suggestedType = inferFileType(urlDownload);
    String suggestedTypeName = suggestedType.map(ExternalFileType::getName).orElse("");
    List<Path> fileDirectories = databaseContext.getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences());
    Path destination = constructSuggestedPath(suggestedType, fileDirectories);
    LinkedFileViewModel temporaryDownloadFile = new LinkedFileViewModel(new LinkedFile("", url, suggestedTypeName));
    files.add(temporaryDownloadFile);
    FileDownloadTask downloadTask = new FileDownloadTask(url, destination);
    temporaryDownloadFile.downloadProgressProperty().bind(downloadTask.progressProperty());
    downloadTask.setOnSucceeded(event -> {
        files.remove(temporaryDownloadFile);
        LinkedFile newLinkedFile = fromFile(destination, fileDirectories);
        files.add(new LinkedFileViewModel(newLinkedFile));
    });
    downloadTask.setOnFailed(event -> dialogService.showErrorDialogAndWait("", downloadTask.getException()));
    taskExecutor.execute(downloadTask);
}
Also used : UnknownExternalFileType(org.jabref.gui.externalfiletype.UnknownExternalFileType) ExternalFileType(org.jabref.gui.externalfiletype.ExternalFileType) Path(java.nio.file.Path) LinkedFile(org.jabref.model.entry.LinkedFile) FileDownloadTask(org.jabref.gui.externalfiles.FileDownloadTask) URLDownload(org.jabref.logic.net.URLDownload)

Example 20 with LinkedFile

use of org.jabref.model.entry.LinkedFile in project jabref by JabRef.

the class LinkedFilesEditorViewModel method addNewFile.

public void addNewFile() {
    Path workingDirectory = databaseContext.getFirstExistingFileDir(Globals.prefs.getFileDirectoryPreferences()).orElse(Paths.get(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)));
    FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder().withInitialDirectory(workingDirectory).build();
    List<Path> fileDirectories = databaseContext.getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences());
    dialogService.showFileOpenDialog(fileDialogConfiguration).ifPresent(newFile -> {
        LinkedFile newLinkedFile = fromFile(newFile, fileDirectories);
        files.add(new LinkedFileViewModel(newLinkedFile));
    });
}
Also used : Path(java.nio.file.Path) LinkedFile(org.jabref.model.entry.LinkedFile) FileDialogConfiguration(org.jabref.gui.util.FileDialogConfiguration)

Aggregations

LinkedFile (org.jabref.model.entry.LinkedFile)28 Test (org.junit.Test)13 File (java.io.File)11 Path (java.nio.file.Path)8 FieldChange (org.jabref.model.FieldChange)6 ArrayList (java.util.ArrayList)5 BibEntry (org.jabref.model.entry.BibEntry)5 HashMap (java.util.HashMap)3 List (java.util.List)3 ExternalFileType (org.jabref.gui.externalfiletype.ExternalFileType)3 UnknownExternalFileType (org.jabref.gui.externalfiletype.UnknownExternalFileType)3 FileListEntry (org.jabref.gui.filelist.FileListEntry)3 LayoutFormatterPreferences (org.jabref.logic.layout.LayoutFormatterPreferences)3 IOException (java.io.IOException)2 Paths (java.nio.file.Paths)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Log (org.apache.commons.logging.Log)2 LogFactory (org.apache.commons.logging.LogFactory)2 FileFieldParser (org.jabref.model.entry.FileFieldParser)2