use of java.awt.datatransfer.DataFlavor in project intellij-community by JetBrains.
the class SimpleTransferable method getDataFlavor.
private static <T> DataFlavor getDataFlavor(final Class<T> dataClass) {
DataFlavor result = ourDataFlavorMap.get(dataClass.getName());
if (result == null) {
try {
result = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType);
ourDataFlavorMap.put(dataClass.getName(), result);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
return result;
}
use of java.awt.datatransfer.DataFlavor in project intellij-community by JetBrains.
the class SimpleTransferable method getData.
@Nullable
public static <T> T getData(Transferable transferable, Class<T> dataClass) {
try {
final DataFlavor dataFlavor = getDataFlavor(dataClass);
if (!transferable.isDataFlavorSupported(dataFlavor)) {
return null;
}
final Object transferData = transferable.getTransferData(dataFlavor);
if (!dataClass.isInstance(transferData)) {
return null;
}
//noinspection unchecked
return (T) transferData;
} catch (IOException e) {
return null;
} catch (Exception e) {
LOG.error(e);
return null;
}
}
use of java.awt.datatransfer.DataFlavor in project bytecode-viewer by Konloch.
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
final 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;
}
use of java.awt.datatransfer.DataFlavor in project jmeter by apache.
the class GuiUtils method getPastedText.
/**
* Get pasted text from clipboard
*
* @return String Pasted text
* @throws UnsupportedFlavorException
* if the clipboard data can not be get as a {@link String}
* @throws IOException
* if the clipboard data is no longer available
*/
public static String getPastedText() throws UnsupportedFlavorException, IOException {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable trans = clipboard.getContents(null);
DataFlavor[] flavourList = trans.getTransferDataFlavors();
Collection<DataFlavor> flavours = new ArrayList<>(flavourList.length);
if (Collections.addAll(flavours, flavourList) && flavours.contains(DataFlavor.stringFlavor)) {
return (String) trans.getTransferData(DataFlavor.stringFlavor);
} else {
return null;
}
}
use of java.awt.datatransfer.DataFlavor in project jmeter by apache.
the class JMeterTreeTransferHandler method importData.
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (!canImport(support)) {
return false;
}
// deal with the jmx files
GuiPackage guiInstance = GuiPackage.getInstance();
DataFlavor[] flavors = support.getDataFlavors();
Transferable t = support.getTransferable();
for (DataFlavor flavor : flavors) {
// Check for file lists specifically
if (flavor.isFlavorJavaFileListType()) {
try {
return guiInstance.getMainFrame().openJmxFilesFromDragAndDrop(t);
} catch (Exception e) {
log.error("Drop file failed", e);
}
return false;
}
}
// Extract transfer data.
JMeterTreeNode[] nodes = getDraggedNodes(t);
if (nodes == null || nodes.length == 0) {
return false;
}
// Get drop location and mode
JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation();
TreePath dest = dl.getPath();
JMeterTreeNode target = (JMeterTreeNode) dest.getLastPathComponent();
nodesForRemoval = new ArrayList<>();
int index = dl.getChildIndex();
TreePath[] pathsToSelect = new TreePath[nodes.length];
int pathPosition = 0;
JMeterTreeModel treeModel = guiInstance.getTreeModel();
for (JMeterTreeNode node : nodes) {
if (index == -1) {
// drop mode == DropMode.ON
index = target.getChildCount();
}
// Insert a clone of the node, the original one will be removed by the exportDone method
// the children are not cloned but moved to the cloned node
// working on the original node would be harder as
// you'll have to deal with the insertion index offset if you re-order a node inside a parent
JMeterTreeNode copy = (JMeterTreeNode) node.clone();
// first copy the children as the call to copy.add will modify the collection we're iterating on
Enumeration<?> enumFrom = node.children();
List<JMeterTreeNode> tmp = new ArrayList<>();
while (enumFrom.hasMoreElements()) {
JMeterTreeNode child = (JMeterTreeNode) enumFrom.nextElement();
tmp.add(child);
}
for (JMeterTreeNode jMeterTreeNode : tmp) {
copy.add(jMeterTreeNode);
}
treeModel.insertNodeInto(copy, target, index++);
nodesForRemoval.add(node);
pathsToSelect[pathPosition++] = new TreePath(treeModel.getPathToRoot(copy));
}
TreePath treePath = new TreePath(target.getPath());
// expand the destination node
JTree tree = (JTree) support.getComponent();
tree.expandPath(treePath);
tree.setSelectionPaths(pathsToSelect);
return true;
}
Aggregations