Search in sources :

Example 6 with DataFlavor

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;
}
Also used : DataFlavor(java.awt.datatransfer.DataFlavor)

Example 7 with DataFlavor

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;
    }
}
Also used : IOException(java.io.IOException) IOException(java.io.IOException) DataFlavor(java.awt.datatransfer.DataFlavor) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with DataFlavor

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;
}
Also used : DataFlavor(java.awt.datatransfer.DataFlavor)

Example 9 with DataFlavor

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;
    }
}
Also used : Transferable(java.awt.datatransfer.Transferable) ArrayList(java.util.ArrayList) Clipboard(java.awt.datatransfer.Clipboard) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 10 with DataFlavor

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;
}
Also used : Transferable(java.awt.datatransfer.Transferable) ArrayList(java.util.ArrayList) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) DataFlavor(java.awt.datatransfer.DataFlavor) JTree(javax.swing.JTree) TreePath(javax.swing.tree.TreePath) GuiPackage(org.apache.jmeter.gui.GuiPackage)

Aggregations

DataFlavor (java.awt.datatransfer.DataFlavor)50 IOException (java.io.IOException)12 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)11 JPanel (javax.swing.JPanel)11 Transferable (java.awt.datatransfer.Transferable)10 JLabel (javax.swing.JLabel)9 List (java.util.List)8 Point (java.awt.Point)7 ArrayList (java.util.ArrayList)7 Clipboard (java.awt.datatransfer.Clipboard)6 NamedIcon (jmri.jmrit.catalog.NamedIcon)6 URL (java.net.URL)4 DragJLabel (jmri.jmrit.catalog.DragJLabel)4 File (java.io.File)3 BorderLayout (java.awt.BorderLayout)2 GraphicsConfiguration (java.awt.GraphicsConfiguration)2 GridBagConstraints (java.awt.GridBagConstraints)2 GridBagLayout (java.awt.GridBagLayout)2 Image (java.awt.Image)2 Insets (java.awt.Insets)2