Search in sources :

Example 56 with Transferable

use of java.awt.datatransfer.Transferable in project libgdx by libgdx.

the class LwjglClipboard method getContents.

@Override
public String getContents() {
    String result = "";
    try {
        java.awt.datatransfer.Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable contents = clipboard.getContents(null);
        boolean hasTransferableText = (contents != null) && contents.isDataFlavorSupported(DataFlavor.stringFlavor);
        if (hasTransferableText) {
            try {
                result = (String) contents.getTransferData(DataFlavor.stringFlavor);
            } catch (Exception ex) {
            }
        }
    } catch (Exception ignored) {
    // Ignore JDK crashes sorting data flavors.
    }
    return result;
}
Also used : Transferable(java.awt.datatransfer.Transferable) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) IOException(java.io.IOException)

Example 57 with Transferable

use of java.awt.datatransfer.Transferable in project GCViewer by chewiebug.

the class FileDropTargetListener method drop.

@Override
public void drop(DropTargetDropEvent event) {
    try {
        Transferable tr = event.getTransferable();
        if (event.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            event.acceptDrop(DnDConstants.ACTION_COPY);
            @SuppressWarnings("unchecked") List<File> list = (List<File>) tr.getTransferData(DataFlavor.javaFileListFlavor);
            File[] fileArray = list.toArray(new File[list.size()]);
            if (flavor.equals(DropFlavor.ADD)) {
                controller.add(fileArray);
            } else {
                controller.open(fileArray);
            }
            event.dropComplete(true);
        } else {
            event.rejectDrop();
        }
    } catch (IOException | UnsupportedFlavorException e) {
        event.rejectDrop();
        e.printStackTrace();
    }
}
Also used : Transferable(java.awt.datatransfer.Transferable) List(java.util.List) IOException(java.io.IOException) File(java.io.File) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException)

Example 58 with Transferable

use of java.awt.datatransfer.Transferable in project GCViewer by chewiebug.

the class TestGCModelLoaderController method dropOnDesktopPane.

/**
     * Test drag and drop action on GCViewerGui.
     */
@Test
public void dropOnDesktopPane() throws Exception {
    // TODO SWINGWORKER: test drag and drop on GCDocument
    assertThat("number of gcdocuments before", controller.getGCViewerGui().getDesktopPane().getAllFrames().length, is(0));
    List<File> fileList = new ArrayList<File>();
    fileList.add(new File(UnittestHelper.getResource(UnittestHelper.FOLDER_OPENJDK, "SampleSun1_6_0G1_MarkStackFull.txt").getPath()));
    Transferable tr = Mockito.mock(Transferable.class);
    Mockito.when(tr.getTransferData(DataFlavor.javaFileListFlavor)).thenReturn(fileList);
    DropTargetDropEvent dte = Mockito.mock(DropTargetDropEvent.class);
    Mockito.when(dte.isDataFlavorSupported(DataFlavor.javaFileListFlavor)).thenReturn(true);
    Mockito.when(dte.getTransferable()).thenReturn(tr);
    DropTarget target = controller.getGCViewerGui().getDesktopPane().getDropTarget();
    target.drop(dte);
    assertThat("number of gcdocuments after drop", controller.getGCViewerGui().getDesktopPane().getAllFrames().length, is(1));
}
Also used : ArrayList(java.util.ArrayList) Transferable(java.awt.datatransfer.Transferable) DropTarget(java.awt.dnd.DropTarget) File(java.io.File) GcResourceFile(com.tagtraum.perf.gcviewer.model.GcResourceFile) DropTargetDropEvent(java.awt.dnd.DropTargetDropEvent) Test(org.junit.Test)

Example 59 with Transferable

use of java.awt.datatransfer.Transferable in project Openfire by igniterealtime.

the class DroppableTextPane method drop.

@Override
public void drop(DropTargetDropEvent dropTargetDropEvent) {
    try {
        Transferable transferable = dropTargetDropEvent.getTransferable();
        if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            dropTargetDropEvent.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
            List fileList = (List) transferable.getTransferData(DataFlavor.javaFileListFlavor);
            Iterator iterator = fileList.iterator();
            while (iterator.hasNext()) {
                File file = (File) iterator.next();
                if (file.isFile()) {
                    fileDropped(file);
                }
                if (file.isDirectory()) {
                    directoryDropped(file);
                }
            }
            dropTargetDropEvent.getDropTargetContext().dropComplete(true);
        } else {
            dropTargetDropEvent.rejectDrop();
        }
    } catch (IOException | UnsupportedFlavorException io) {
        io.printStackTrace();
        dropTargetDropEvent.rejectDrop();
    }
}
Also used : Transferable(java.awt.datatransfer.Transferable) Iterator(java.util.Iterator) List(java.util.List) IOException(java.io.IOException) File(java.io.File) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException)

Example 60 with Transferable

use of java.awt.datatransfer.Transferable in project jna by java-native-access.

the class AlphaMaskDemo method run.

public void run() {
    // Must find a graphics configuration with a depth of 32 bits
    GraphicsConfiguration gconfig = WindowUtils.getAlphaCompatibleGraphicsConfiguration();
    frame = new JFrame("Alpha Mask Demo");
    alphaWindow = new JWindow(frame, gconfig);
    MouseInputAdapter handler = new MouseInputAdapter() {

        private Point offset;

        public void mousePressed(MouseEvent e) {
            if (SwingUtilities.isLeftMouseButton(e))
                offset = e.getPoint();
        }

        public void mouseReleased(MouseEvent e) {
            offset = null;
            // hit testing; not sure why it gets disabled
            if (System.getProperty("os.name").startsWith("Windows"))
                update(true, true);
        }

        public void mouseDragged(MouseEvent e) {
            if (offset != null) {
                Window w = (Window) e.getSource();
                Point where = e.getPoint();
                where.translate(-offset.x, -offset.y);
                Point loc = w.getLocationOnScreen();
                loc.translate(where.x, where.y);
                w.setLocation(loc.x, loc.y);
            }
        }
    };
    alphaWindow.addMouseListener(handler);
    alphaWindow.addMouseMotionListener(handler);
    JPanel p = new JPanel(new BorderLayout(8, 8));
    p.setBorder(new EmptyBorder(8, 8, 8, 8));
    p.setTransferHandler(new TransferHandler() {

        private static final long serialVersionUID = 1L;

        public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
            List<DataFlavor> list = Arrays.asList(transferFlavors);
            if (list.contains(URL_FLAVOR) || list.contains(URI_LIST_FLAVOR) || list.contains(DataFlavor.imageFlavor) || list.contains(DataFlavor.javaFileListFlavor)) {
                return true;
            }
            if (DataFlavor.selectBestTextFlavor(transferFlavors) != null) {
                return true;
            }
            System.out.println("No acceptable flavor found in " + Arrays.asList(transferFlavors));
            return false;
        }

        public boolean importData(JComponent comp, Transferable t) {
            try {
                if (t.isDataFlavorSupported(URL_FLAVOR)) {
                    URL url = (URL) t.getTransferData(URL_FLAVOR);
                    setImage(Toolkit.getDefaultToolkit().getImage(url));
                    return true;
                }
                if (t.isDataFlavorSupported(URI_LIST_FLAVOR)) {
                    String s = (String) t.getTransferData(URI_LIST_FLAVOR);
                    String[] uris = s.split("[\r\n]");
                    if (uris.length > 0) {
                        URL url = new URL(uris[0]);
                        setImage(Toolkit.getDefaultToolkit().getImage(url));
                        return true;
                    }
                    return false;
                }
                if (t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
                    Image image = (Image) t.getTransferData(DataFlavor.imageFlavor);
                    setImage(image);
                    return true;
                }
                if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                    List<File> files = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor);
                    File f = files.get(0);
                    URL url = new URL("file://" + f.toURI().toURL().getPath());
                    Image image = Toolkit.getDefaultToolkit().getImage(url);
                    setImage(image);
                    return true;
                }
                DataFlavor flavor = DataFlavor.selectBestTextFlavor(t.getTransferDataFlavors());
                if (flavor != null) {
                    Reader reader = flavor.getReaderForText(t);
                    char[] buf = new char[512];
                    StringBuilder b = new StringBuilder();
                    int count;
                    // encoding wrong
                    while ((count = reader.read(buf)) > 0) {
                        for (int i = 0; i < count; i++) {
                            if (buf[i] != 0)
                                b.append(buf, i, 1);
                        }
                    }
                    String html = b.toString();
                    Pattern p = Pattern.compile("<img.*src=\"([^\\\"\">]+)\"", Pattern.CANON_EQ | Pattern.UNICODE_CASE);
                    Matcher m = p.matcher(html);
                    if (m.find()) {
                        URL url = new URL(m.group(1));
                        System.out.println("Load image from " + url);
                        Image image = Toolkit.getDefaultToolkit().getImage(url);
                        setImage(image);
                        return true;
                    }
                    System.out.println("Can't parse text: " + html);
                    return false;
                }
                System.out.println("No flavor available: " + Arrays.asList(t.getTransferDataFlavors()));
            } catch (UnsupportedFlavorException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Throwable e) {
                e.printStackTrace();
            }
            return false;
        }
    });
    p.add(new JLabel("<html><center>Drop an image with an alpha channel onto this window<br>" + "You may also adjust the overall transparency with the slider</center></html>"), BorderLayout.NORTH);
    final JSlider slider = new JSlider(0, 255, 255);
    slider.addChangeListener(new ChangeListener() {

        public void stateChanged(ChangeEvent e) {
            int value = slider.getValue();
            setAlpha(value / 255f);
        }
    });
    p.add(slider, BorderLayout.SOUTH);
    frame.getContentPane().add(p);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    centerOnScreen(frame);
    frame.setVisible(true);
    p.addMouseListener(new MouseAdapter() {

        public void mousePressed(MouseEvent e) {
            update();
        }
    });
    try {
        URL url = getClass().getResource("tardis.png");
        if (url != null) {
            setImage(Toolkit.getDefaultToolkit().getImage(url));
        }
    } catch (Exception e) {
    }
}
Also used : JPanel(javax.swing.JPanel) Matcher(java.util.regex.Matcher) Reader(java.io.Reader) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) URL(java.net.URL) GraphicsConfiguration(java.awt.GraphicsConfiguration) DataFlavor(java.awt.datatransfer.DataFlavor) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) JSlider(javax.swing.JSlider) List(java.util.List) ChangeListener(javax.swing.event.ChangeListener) EmptyBorder(javax.swing.border.EmptyBorder) Window(java.awt.Window) JWindow(javax.swing.JWindow) Pattern(java.util.regex.Pattern) MouseEvent(java.awt.event.MouseEvent) JWindow(javax.swing.JWindow) JComponent(javax.swing.JComponent) Transferable(java.awt.datatransfer.Transferable) MouseAdapter(java.awt.event.MouseAdapter) JLabel(javax.swing.JLabel) Point(java.awt.Point) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) IOException(java.io.IOException) ChangeEvent(javax.swing.event.ChangeEvent) TransferHandler(javax.swing.TransferHandler) File(java.io.File) MouseInputAdapter(javax.swing.event.MouseInputAdapter)

Aggregations

Transferable (java.awt.datatransfer.Transferable)96 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)30 IOException (java.io.IOException)30 List (java.util.List)18 DataFlavor (java.awt.datatransfer.DataFlavor)14 ArrayList (java.util.ArrayList)14 File (java.io.File)13 Clipboard (java.awt.datatransfer.Clipboard)12 Point (java.awt.Point)9 StringSelection (java.awt.datatransfer.StringSelection)5 Project (com.intellij.openapi.project.Project)4 ClipboardOwner (java.awt.datatransfer.ClipboardOwner)4 TransferHandler (javax.swing.TransferHandler)4 Editor (com.intellij.openapi.editor.Editor)3 ActionEvent (java.awt.event.ActionEvent)3 JComponent (javax.swing.JComponent)3 TreePath (javax.swing.tree.TreePath)3 AsynchClientTask (cbit.vcell.client.task.AsynchClientTask)2 DataContext (com.intellij.openapi.actionSystem.DataContext)2 PsiElement (com.intellij.psi.PsiElement)2