Search in sources :

Example 1 with DroppedFileHandler

use of org.jabref.gui.externalfiles.DroppedFileHandler in project jabref by JabRef.

the class EntryTableTransferHandler method loadOrImportFiles.

/**
     * Take a set of filenames. Those with names indicating BIB files are opened as such if possible. All other files we
     * will attempt to import into the current library.
     *
     * @param fileNames The names of the files to open.
     * @param dropRow success status for the operation
     */
private void loadOrImportFiles(List<String> fileNames, int dropRow) {
    OpenDatabaseAction openAction = new OpenDatabaseAction(frame, false);
    List<String> notBibFiles = new ArrayList<>();
    List<String> bibFiles = new ArrayList<>();
    for (String fileName : fileNames) {
        // Find the file's extension, if any:
        Optional<String> extension = FileHelper.getFileExtension(fileName);
        Optional<ExternalFileType> fileType;
        if (extension.isPresent() && "bib".equals(extension.get())) {
            // we assume that it is a BibTeX file.
            // When a user wants to import something with file extension "bib", but which is not a BibTeX file, he should use "file -> import"
            bibFiles.add(fileName);
            continue;
        }
        fileType = ExternalFileTypes.getInstance().getExternalFileTypeByExt(extension.orElse(""));
        /*
             * This is a linkable file. If the user dropped it on an entry, we
             * should offer options for autolinking to this files:
             *
             * TODO we should offer an option to highlight the row the user is on too.
             */
        if ((fileType.isPresent()) && (dropRow >= 0)) {
            /*
                 * TODO: make this an instance variable?
                 */
            DroppedFileHandler dfh = new DroppedFileHandler(frame, panel);
            dfh.handleDroppedfile(fileName, fileType.get(), entryTable, dropRow);
            continue;
        }
        notBibFiles.add(fileName);
    }
    openAction.openFilesAsStringList(bibFiles, true);
    if (!notBibFiles.isEmpty()) {
        // Import into new if entryTable==null, otherwise into current
        // database:
        ImportMenuItem importer = new ImportMenuItem(frame, entryTable == null);
        importer.automatedImport(notBibFiles);
    }
}
Also used : ExternalFileType(org.jabref.gui.externalfiletype.ExternalFileType) OpenDatabaseAction(org.jabref.gui.importer.actions.OpenDatabaseAction) ArrayList(java.util.ArrayList) DroppedFileHandler(org.jabref.gui.externalfiles.DroppedFileHandler) ImportMenuItem(org.jabref.gui.importer.ImportMenuItem)

Example 2 with DroppedFileHandler

use of org.jabref.gui.externalfiles.DroppedFileHandler in project jabref by JabRef.

the class FileListEditorTransferHandler method importData.

@Override
public boolean importData(JComponent comp, Transferable t) {
    try {
        List<Path> files = new ArrayList<>();
        // This flavor is used for dragged file links in Windows:
        if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            @SuppressWarnings("unchecked") List<Path> transferedFiles = (List<Path>) t.getTransferData(DataFlavor.javaFileListFlavor);
            files.addAll(transferedFiles);
        }
        if (t.isDataFlavorSupported(urlFlavor)) {
            URL dropLink = (URL) t.getTransferData(urlFlavor);
            LOGGER.debug("URL: " + dropLink);
        }
        // under Gnome. The data consists of the file paths, one file per line:
        if (t.isDataFlavorSupported(stringFlavor)) {
            String dropStr = (String) t.getTransferData(stringFlavor);
            files.addAll(EntryTableTransferHandler.getFilesFromDraggedFilesString(dropStr));
        }
        SwingUtilities.invokeLater(() -> {
            for (Path file : files) {
                // Find the file's extension, if any:
                String name = file.toAbsolutePath().toString();
                FileHelper.getFileExtension(name).ifPresent(extension -> ExternalFileTypes.getInstance().getExternalFileTypeByExt(extension).ifPresent(fileType -> {
                    if (droppedFileHandler == null) {
                        droppedFileHandler = new DroppedFileHandler(frame, frame.getCurrentBasePanel());
                    }
                    droppedFileHandler.handleDroppedfile(name, fileType, entryContainer.getEntry());
                }));
            }
        });
        if (!files.isEmpty()) {
            // Found some files, return
            return true;
        }
    } catch (IOException ioe) {
        LOGGER.warn("Failed to read dropped data. ", ioe);
    } catch (UnsupportedFlavorException | ClassCastException ufe) {
        LOGGER.warn("Drop type error. ", ufe);
    }
    // all supported flavors failed
    StringBuilder logMessage = new StringBuilder("Cannot transfer input:");
    DataFlavor[] inflavs = t.getTransferDataFlavors();
    for (DataFlavor inflav : inflavs) {
        logMessage.append(' ').append(inflav);
    }
    LOGGER.warn(logMessage.toString());
    return false;
}
Also used : Path(java.nio.file.Path) Clipboard(java.awt.datatransfer.Clipboard) JComponent(javax.swing.JComponent) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) DataFlavor(java.awt.datatransfer.DataFlavor) URL(java.net.URL) Transferable(java.awt.datatransfer.Transferable) DnDConstants(java.awt.dnd.DnDConstants) IOException(java.io.IOException) EntryTableTransferHandler(org.jabref.gui.groups.EntryTableTransferHandler) ArrayList(java.util.ArrayList) FileHelper(org.jabref.model.util.FileHelper) List(java.util.List) SwingUtilities(javax.swing.SwingUtilities) DroppedFileHandler(org.jabref.gui.externalfiles.DroppedFileHandler) JabRefFrame(org.jabref.gui.JabRefFrame) TransferHandler(javax.swing.TransferHandler) Log(org.apache.commons.logging.Log) ExternalFileTypes(org.jabref.gui.externalfiletype.ExternalFileTypes) LogFactory(org.apache.commons.logging.LogFactory) Path(java.nio.file.Path) EntryContainer(org.jabref.gui.EntryContainer) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DroppedFileHandler(org.jabref.gui.externalfiles.DroppedFileHandler) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) URL(java.net.URL) DataFlavor(java.awt.datatransfer.DataFlavor) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with DroppedFileHandler

use of org.jabref.gui.externalfiles.DroppedFileHandler in project jabref by JabRef.

the class PdfImporter method createNewBlankEntry.

private Optional<BibEntry> createNewBlankEntry(String fileName) {
    Optional<BibEntry> newEntry = createNewEntry();
    newEntry.ifPresent(bibEntry -> {
        DroppedFileHandler dfh = new DroppedFileHandler(frame, panel);
        dfh.linkPdfToEntry(fileName, bibEntry);
    });
    return newEntry;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) DroppedFileHandler(org.jabref.gui.externalfiles.DroppedFileHandler)

Example 4 with DroppedFileHandler

use of org.jabref.gui.externalfiles.DroppedFileHandler in project jabref by JabRef.

the class PdfImporter method importPdfFilesInternal.

/**
     * @param fileNames - PDF files to import
     * @return true if the import succeeded, false otherwise
     */
private List<BibEntry> importPdfFilesInternal(List<String> fileNames) {
    if (panel == null) {
        return Collections.emptyList();
    }
    ImportDialog importDialog = null;
    boolean doNotShowAgain = false;
    boolean neverShow = Globals.prefs.getBoolean(JabRefPreferences.IMPORT_ALWAYSUSE);
    int globalChoice = Globals.prefs.getInt(JabRefPreferences.IMPORT_DEFAULT_PDF_IMPORT_STYLE);
    List<BibEntry> res = new ArrayList<>();
    for (String fileName : fileNames) {
        if (!neverShow && !doNotShowAgain) {
            importDialog = new ImportDialog(dropRow >= 0, fileName);
            if (!XMPUtil.hasMetadata(Paths.get(fileName), Globals.prefs.getXMPPreferences())) {
                importDialog.disableXMPChoice();
            }
            importDialog.setLocationRelativeTo(frame);
            importDialog.showDialog();
            doNotShowAgain = importDialog.isDoNotShowAgain();
        }
        if (neverShow || (importDialog.getResult() == JOptionPane.OK_OPTION)) {
            int choice = neverShow ? globalChoice : importDialog.getChoice();
            switch(choice) {
                case ImportDialog.XMP:
                    doXMPImport(fileName, res);
                    break;
                case ImportDialog.CONTENT:
                    doContentImport(fileName, res);
                    break;
                case ImportDialog.NOMETA:
                    createNewBlankEntry(fileName).ifPresent(res::add);
                    break;
                case ImportDialog.ONLYATTACH:
                    DroppedFileHandler dfh = new DroppedFileHandler(frame, panel);
                    if (dropRow >= 0) {
                        dfh.linkPdfToEntry(fileName, entryTable, dropRow);
                    } else {
                        dfh.linkPdfToEntry(fileName, entryTable, entryTable.getSelectedRow());
                    }
                    break;
                default:
                    break;
            }
        }
    }
    return res;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) ArrayList(java.util.ArrayList) DroppedFileHandler(org.jabref.gui.externalfiles.DroppedFileHandler)

Example 5 with DroppedFileHandler

use of org.jabref.gui.externalfiles.DroppedFileHandler in project jabref by JabRef.

the class PdfImporter method doContentImport.

private void doContentImport(String fileName, List<BibEntry> res) {
    PdfContentImporter contentImporter = new PdfContentImporter(Globals.prefs.getImportFormatPreferences());
    Path filePath = Paths.get(fileName);
    ParserResult result = contentImporter.importDatabase(filePath, Globals.prefs.getDefaultEncoding());
    if (result.hasWarnings()) {
        frame.showMessage(result.getErrorMessage());
    }
    if (!result.getDatabase().hasEntries()) {
        // import failed -> generate default entry
        createNewBlankEntry(fileName).ifPresent(res::add);
        return;
    }
    // only one entry is imported
    BibEntry entry = result.getDatabase().getEntries().get(0);
    // insert entry to database and link file
    panel.getDatabase().insertEntry(entry);
    panel.markBaseChanged();
    BibtexKeyPatternUtil.makeAndSetLabel(panel.getBibDatabaseContext().getMetaData().getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()), panel.getDatabase(), entry, Globals.prefs.getBibtexKeyPatternPreferences());
    DroppedFileHandler dfh = new DroppedFileHandler(frame, panel);
    dfh.linkPdfToEntry(fileName, entry);
    SwingUtilities.invokeLater(() -> panel.highlightEntry(entry));
    if (Globals.prefs.getBoolean(JabRefPreferences.AUTO_OPEN_FORM)) {
        EntryEditor editor = panel.getEntryEditor(entry);
        panel.showEntryEditor(editor);
    }
    res.add(entry);
}
Also used : Path(java.nio.file.Path) ParserResult(org.jabref.logic.importer.ParserResult) BibEntry(org.jabref.model.entry.BibEntry) EntryEditor(org.jabref.gui.entryeditor.EntryEditor) PdfContentImporter(org.jabref.logic.importer.fileformat.PdfContentImporter) DroppedFileHandler(org.jabref.gui.externalfiles.DroppedFileHandler)

Aggregations

DroppedFileHandler (org.jabref.gui.externalfiles.DroppedFileHandler)5 ArrayList (java.util.ArrayList)3 BibEntry (org.jabref.model.entry.BibEntry)3 Path (java.nio.file.Path)2 Clipboard (java.awt.datatransfer.Clipboard)1 DataFlavor (java.awt.datatransfer.DataFlavor)1 Transferable (java.awt.datatransfer.Transferable)1 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)1 DnDConstants (java.awt.dnd.DnDConstants)1 IOException (java.io.IOException)1 URL (java.net.URL)1 List (java.util.List)1 JComponent (javax.swing.JComponent)1 SwingUtilities (javax.swing.SwingUtilities)1 TransferHandler (javax.swing.TransferHandler)1 Log (org.apache.commons.logging.Log)1 LogFactory (org.apache.commons.logging.LogFactory)1 EntryContainer (org.jabref.gui.EntryContainer)1 JabRefFrame (org.jabref.gui.JabRefFrame)1 EntryEditor (org.jabref.gui.entryeditor.EntryEditor)1