package org.jabref.gui.externalfiles; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.io.IOException; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import org.jabref.Globals; import org.jabref.gui.BasePanel; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.LinkedFile; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * */ public class TransferableFileLinkSelection implements Transferable { private static final Log LOGGER = LogFactory.getLog(TransferableFileLinkSelection.class); private final List<Path> fileList = new ArrayList<>(); public TransferableFileLinkSelection(BasePanel panel, List<BibEntry> selection) { BibEntry entry = selection.get(0); List<LinkedFile> files = entry.getFiles(); if (!files.isEmpty()) { // Find the default directory for this field type, if any: LinkedFile firstFile = files.get(0); firstFile.findIn(panel.getDatabaseContext(), Globals.prefs.getFileDirectoryPreferences()) .ifPresent(fileList::add); } } @Override public DataFlavor[] getTransferDataFlavors() { return new DataFlavor[] {DataFlavor.javaFileListFlavor};//, DataFlavor.stringFlavor}; } @Override public boolean isDataFlavorSupported(DataFlavor dataFlavor) { LOGGER.debug("Query: " + dataFlavor.getHumanPresentableName() + " , " + dataFlavor.getDefaultRepresentationClass() + " , " + dataFlavor.getMimeType()); return dataFlavor.equals(DataFlavor.javaFileListFlavor) || dataFlavor.equals(DataFlavor.stringFlavor); } @Override public Object getTransferData(DataFlavor dataFlavor) throws UnsupportedFlavorException, IOException { //if (dataFlavor.equals(DataFlavor.javaFileListFlavor)) return fileList; //else // return "test"; } /* private StringSelection ss; public TransferableFileLinkSelection(BasePanel panel, BibEntry[] selection) { String s = selection[0].getField(GUIGlobals.FILE_FIELD); FileListTableModel tm = new FileListTableModel(); if (s != null) tm.setContent(s); if (tm.getRowCount() > 0) { // Find the default directory for this field type, if any: String dir = panel.metaData().getFileDirectory(GUIGlobals.FILE_FIELD); // Include the standard "file" directory: String fileDir = panel.metaData().getFileDirectory(GUIGlobals.FILE_FIELD); // Include the directory of the BIB file: String[] dirs; if (panel.metaData().getDatabaseFile() != null) { String databaseDir = panel.metaData().getDatabaseFile().getParent(); dirs = new String[] { dir, fileDir, databaseDir }; } else dirs = new String[] { dir, fileDir }; System.out.println(tm.getEntry(0).getLink()); for (int i = 0; i < dirs.length; i++) { String dir1 = dirs[i]; System.out.println("dir:"+dir1); } File expLink = Util.expandFilename(tm.getEntry(0).getLink(), dirs); try { System.out.println(expLink.toURI().toURL().toString()); ss = new StringSelection(expLink.toURI().toURL().toString()); } catch (MalformedURLException ex) { ss = new StringSelection(""); } } else ss = new StringSelection(""); } public Transferable getTransferable() { return ss; } */ }