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);
}
}
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;
}
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;
}
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;
}
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);
}
Aggregations