use of java.awt.datatransfer.DataFlavor in project jdk8u_jdk by JetBrains.
the class SunDragSourceContextPeer method startDrag.
/**
* initiate a DnD operation ...
*/
public void startDrag(DragSourceContext dsc, Cursor c, Image di, Point p) throws InvalidDnDOperationException {
/* Fix for 4354044: don't initiate a drag if event sequence provided by
* DragGestureRecognizer is empty */
if (getTrigger().getTriggerEvent() == null) {
throw new InvalidDnDOperationException("DragGestureEvent has a null trigger");
}
dragSourceContext = dsc;
cursor = c;
sourceActions = getDragSourceContext().getSourceActions();
dragImage = di;
dragImageOffset = p;
Transferable transferable = getDragSourceContext().getTransferable();
SortedMap<Long, DataFlavor> formatMap = DataTransferer.getInstance().getFormatsForTransferable(transferable, DataTransferer.adaptFlavorMap(getTrigger().getDragSource().getFlavorMap()));
long[] formats = DataTransferer.getInstance().keysToLongArray(formatMap);
startDrag(transferable, formats, formatMap);
/*
* Fix for 4613903.
* Filter out all mouse events that are currently on the event queue.
*/
discardingMouseEvents = true;
EventQueue.invokeLater(new Runnable() {
public void run() {
discardingMouseEvents = false;
}
});
}
use of java.awt.datatransfer.DataFlavor in project jdk8u_jdk by JetBrains.
the class XDataTransferer method getPlatformMappingsForNative.
/*
* The XDnD protocol prescribes that the Atoms used as targets for data
* transfer should have string names that represent the corresponding MIME
* types.
* To meet this requirement we check if the passed native format constitutes
* a valid MIME and return a list of flavors to which the data in this MIME
* type can be translated by the Data Transfer subsystem.
*/
public LinkedHashSet<DataFlavor> getPlatformMappingsForNative(String nat) {
LinkedHashSet<DataFlavor> flavors = new LinkedHashSet<>();
if (nat == null) {
return flavors;
}
DataFlavor df = null;
try {
df = new DataFlavor(nat);
} catch (Exception e) {
// The string doesn't constitute a valid MIME type.
return flavors;
}
DataFlavor value = df;
final String primaryType = df.getPrimaryType();
final String baseType = primaryType + "/" + df.getSubType();
// See SystemFlavorMap.getFlavorsForNative() for details.
if ("image".equals(primaryType)) {
Iterator readers = ImageIO.getImageReadersByMIMEType(baseType);
if (readers.hasNext()) {
flavors.add(DataFlavor.imageFlavor);
}
}
flavors.add(value);
return flavors;
}
use of java.awt.datatransfer.DataFlavor in project jdk8u_jdk by JetBrains.
the class WClipboard method setContentsNative.
@Override
protected void setContentsNative(Transferable contents) {
// Don't use delayed Clipboard rendering for the Transferable's data.
// If we did that, we would call Transferable.getTransferData on
// the Toolkit thread, which is a security hole.
//
// Get all of the target formats into which the Transferable can be
// translated. Then, for each format, translate the data and post
// it to the Clipboard.
Map<Long, DataFlavor> formatMap = WDataTransferer.getInstance().getFormatsForTransferable(contents, getDefaultFlavorTable());
openClipboard(this);
try {
for (Long format : formatMap.keySet()) {
DataFlavor flavor = formatMap.get(format);
try {
byte[] bytes = WDataTransferer.getInstance().translateTransferable(contents, flavor, format);
publishClipboardData(format, bytes);
} catch (IOException e) {
// May remove this if-check when 5078787 is fixed.
if (!(flavor.isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType) && e instanceof java.io.NotSerializableException)) {
e.printStackTrace();
}
}
}
} finally {
closeClipboard();
}
}
use of java.awt.datatransfer.DataFlavor 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 java.awt.datatransfer.DataFlavor in project beast-mcmc by beast-dev.
the class FileDrop method isDragOk.
// end dropListener
/** Determine if the dragged data is a file list. */
private boolean isDragOk(final java.io.PrintStream out, final java.awt.dnd.DropTargetDragEvent evt) {
boolean ok = false;
// Get data flavors being dragged
java.awt.datatransfer.DataFlavor[] flavors = evt.getCurrentDataFlavors();
// See if any of the flavors are a file list
int i = 0;
while (!ok && i < flavors.length) {
// BEGIN 2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added.
// Is the flavor a file list?
final DataFlavor curFlavor = flavors[i];
if (curFlavor.equals(java.awt.datatransfer.DataFlavor.javaFileListFlavor) || curFlavor.isRepresentationClassReader()) {
ok = true;
}
// END 2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added.
i++;
}
// If logging is enabled, show data flavors
if (out != null) {
if (flavors.length == 0)
log(out, "FileDrop: no data flavors.");
for (i = 0; i < flavors.length; i++) log(out, flavors[i].toString());
}
return ok;
}
Aggregations