Example usage for java.awt.dnd DropTargetDropEvent getCurrentDataFlavors

List of usage examples for java.awt.dnd DropTargetDropEvent getCurrentDataFlavors

Introduction

In this page you can find the example usage for java.awt.dnd DropTargetDropEvent getCurrentDataFlavors.

Prototype


public DataFlavor[] getCurrentDataFlavors() 

Source Link

Document

This method returns the current DataFlavors.

Usage

From source file:javazoom.jlgui.player.amp.skin.DropTargetAdapter.java

public void drop(DropTargetDropEvent e) {
    // Check DataFlavor
    DataFlavor[] dfs = e.getCurrentDataFlavors();
    DataFlavor tdf = null;//from  w  w w .  j  a  v a  2s  .co m
    for (int i = 0; i < dfs.length; i++) {
        if (DataFlavor.javaFileListFlavor.equals(dfs[i])) {
            tdf = dfs[i];
            break;
        } else if (DataFlavor.stringFlavor.equals(dfs[i])) {
            tdf = dfs[i];
            break;
        }
    }
    // Data Flavor available ?
    if (tdf != null) {
        // Accept COPY DnD only.
        if ((e.getSourceActions() & DnDConstants.ACTION_COPY) != 0) {
            e.acceptDrop(DnDConstants.ACTION_COPY);
        } else
            return;
        try {
            Transferable t = e.getTransferable();
            Object data = t.getTransferData(tdf);
            processDrop(data);
        } catch (IOException ioe) {
            log.info("Drop error", ioe);
            e.dropComplete(false);
            return;
        } catch (UnsupportedFlavorException ufe) {
            log.info("Drop error", ufe);
            e.dropComplete(false);
            return;
        } catch (Exception ex) {
            log.info("Drop error", ex);
            e.dropComplete(false);
            return;
        }
        e.dropComplete(true);
    }
}

From source file:EditorDropTarget2.java

protected boolean dropContent(Transferable transferable, DropTargetDropEvent dtde) {
    if (!pane.isEditable()) {
        // Can't drop content on a read-only text control
        return false;
    }// www. j av  a 2 s  .  co  m

    try {
        // Check for a match with the current content type
        DataFlavor[] flavors = dtde.getCurrentDataFlavors();

        DataFlavor selectedFlavor = null;

        // Look for either plain text or a String.
        for (int i = 0; i < flavors.length; i++) {
            DataFlavor flavor = flavors[i];

            if (flavor.equals(DataFlavor.plainTextFlavor) || flavor.equals(DataFlavor.stringFlavor)) {
                selectedFlavor = flavor;
                break;
            }
        }

        if (selectedFlavor == null) {
            // No compatible flavor - should never happen
            return false;

        }

        DnDUtils.debugPrintln("Selected flavor is " + selectedFlavor.getHumanPresentableName());

        // Get the transferable and then obtain the data
        Object data = transferable.getTransferData(selectedFlavor);

        DnDUtils.debugPrintln("Transfer data type is " + data.getClass().getName());

        String insertData = null;
        if (data instanceof InputStream) {
            // Plain text flavor
            String charSet = selectedFlavor.getParameter("charset");
            InputStream is = (InputStream) data;
            byte[] bytes = new byte[is.available()];
            is.read(bytes);
            try {
                insertData = new String(bytes, charSet);
            } catch (UnsupportedEncodingException e) {
                // Use the platform default encoding
                insertData = new String(bytes);
            }
        } else if (data instanceof String) {
            // String flavor
            insertData = (String) data;
        }

        if (insertData != null) {
            int selectionStart = pane.getCaretPosition();
            pane.replaceSelection(insertData);
            pane.select(selectionStart, selectionStart + insertData.length());
            return true;
        }
        return false;
    } catch (Exception e) {
        return false;
    }
}

From source file:EditorDropTarget3.java

protected boolean dropContent(Transferable transferable, DropTargetDropEvent dtde) {
    if (!pane.isEditable()) {
        // Can't drop content on a read-only text control
        return false;
    }//  w  w w  .  j  a v  a2 s  .  co m

    try {
        // Check for a match with the current content type
        DataFlavor[] flavors = dtde.getCurrentDataFlavors();

        DataFlavor selectedFlavor = null;

        // Look for either plain text or a String.
        for (int i = 0; i < flavors.length; i++) {
            DataFlavor flavor = flavors[i];
            DnDUtils.debugPrintln("Drop MIME type " + flavor.getMimeType() + " is available");
            if (flavor.equals(DataFlavor.plainTextFlavor) || flavor.equals(DataFlavor.stringFlavor)) {
                selectedFlavor = flavor;
                break;
            }
        }

        if (selectedFlavor == null) {
            // No compatible flavor - should never happen
            return false;

        }

        DnDUtils.debugPrintln("Selected flavor is " + selectedFlavor.getHumanPresentableName());

        // Get the transferable and then obtain the data
        Object data = transferable.getTransferData(selectedFlavor);

        DnDUtils.debugPrintln("Transfer data type is " + data.getClass().getName());

        String insertData = null;
        if (data instanceof InputStream) {
            // Plain text flavor
            String charSet = selectedFlavor.getParameter("charset");
            InputStream is = (InputStream) data;
            byte[] bytes = new byte[is.available()];
            is.read(bytes);
            try {
                insertData = new String(bytes, charSet);
            } catch (UnsupportedEncodingException e) {
                // Use the platform default encoding
                insertData = new String(bytes);
            }
        } else if (data instanceof String) {
            // String flavor
            insertData = (String) data;
        }

        if (insertData != null) {
            int selectionStart = pane.getCaretPosition();
            pane.replaceSelection(insertData);
            pane.select(selectionStart, selectionStart + insertData.length());
            return true;
        }
        return false;
    } catch (Exception e) {
        return false;
    }
}

From source file:EditorDropTarget4.java

protected boolean dropContent(Transferable transferable, DropTargetDropEvent dtde) {
    if (!pane.isEditable()) {
        // Can't drop content on a read-only text control
        return false;
    }//from w  w w.j  ava 2 s.  c om

    try {
        // Check for a match with the current content type
        DataFlavor[] flavors = dtde.getCurrentDataFlavors();

        DataFlavor selectedFlavor = null;

        // Look for either plain text or a String.
        for (int i = 0; i < flavors.length; i++) {
            DataFlavor flavor = flavors[i];
            DnDUtils.debugPrintln("Drop MIME type " + flavor.getMimeType() + " is available");
            if (flavor.equals(DataFlavor.plainTextFlavor) || flavor.equals(DataFlavor.stringFlavor)) {
                selectedFlavor = flavor;
                break;
            }
        }

        if (selectedFlavor == null) {
            // No compatible flavor - should never happen
            return false;
        }

        DnDUtils.debugPrintln("Selected flavor is " + selectedFlavor.getHumanPresentableName());

        // Get the transferable and then obtain the data
        Object data = transferable.getTransferData(selectedFlavor);

        DnDUtils.debugPrintln("Transfer data type is " + data.getClass().getName());

        String insertData = null;
        if (data instanceof InputStream) {
            // Plain text flavor
            String charSet = selectedFlavor.getParameter("charset");
            InputStream is = (InputStream) data;
            byte[] bytes = new byte[is.available()];
            is.read(bytes);
            try {
                insertData = new String(bytes, charSet);
            } catch (UnsupportedEncodingException e) {
                // Use the platform default encoding
                insertData = new String(bytes);
            }
        } else if (data instanceof String) {
            // String flavor
            insertData = (String) data;
        }

        if (insertData != null) {
            int selectionStart = pane.getCaretPosition();
            pane.replaceSelection(insertData);
            pane.select(selectionStart, selectionStart + insertData.length());
            return true;
        }
        return false;
    } catch (Exception e) {
        return false;
    }
}

From source file:net.sf.nmedit.nordmodular.NMSynthDeviceContext.java

protected void dropTransfer(SlotObject<NordModular> s, DropTargetDropEvent dtde) {
    if (!acceptsDropData(s, dtde.getCurrentDataFlavors())) {
        dtde.rejectDrop();/* w  w w . ja  va 2 s  .  c  o  m*/
        return;
    }

    if (!dtde.isLocalTransfer()) {
        dtde.rejectDrop();
        return;
    }

    try {
        NMPatch patch = (NMPatch) dtde.getTransferable().getTransferData(JTNMPatch.nmPatchFlavor);

        if (patch.getSlot() != null)
            patch.setSlot(null);

        (new StorePatchInSlotWorker((NmSlot) s.getSlot(), patch)).store();
    } catch (IOException e) {
        dtde.rejectDrop();
    } catch (UnsupportedFlavorException e) {
        dtde.rejectDrop();
    }
}

From source file:com.igormaznitsa.sciareto.ui.editors.MMDEditor.java

@Override
public void drop(@Nonnull final DropTargetDropEvent dtde) {

    dtde.acceptDrop(DnDConstants.ACTION_MOVE);
    File detectedFileObject = null;

    for (final DataFlavor df : dtde.getCurrentDataFlavors()) {
        final Class<?> representation = df.getRepresentationClass();
        if (FileTransferable.class.isAssignableFrom(representation)) {
            final FileTransferable t = (FileTransferable) dtde.getTransferable();
            final List<File> listOfFiles = t.getFiles();
            detectedFileObject = listOfFiles.isEmpty() ? null : listOfFiles.get(0);
            break;
        } else if (df.isFlavorJavaFileListType()) {
            try {
                final List list = (List) dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
                if (list != null && !list.isEmpty()) {
                    detectedFileObject = (File) list.get(0);
                }//from   www  . ja  va2s  .  com
                break;
            } catch (Exception ex) {
                LOGGER.error("Can't extract file from DnD", ex);
            }
        }
    }

    if (detectedFileObject != null) {
        addFileToElement(detectedFileObject, this.mindMapPanel.findTopicUnderPoint(dtde.getLocation()));
    }
}

From source file:javazoom.jlgui.player.amp.Player.java

/**
 * DnD : Drop implementation./*from w ww . j  a v  a2s. c o m*/
 * Adds all dropped files to the playlist.
 */
public void drop(DropTargetDropEvent e) {
    // Check DataFlavor
    DataFlavor[] dfs = e.getCurrentDataFlavors();
    DataFlavor tdf = null;
    for (int i = 0; i < dfs.length; i++) {
        if (DataFlavor.javaFileListFlavor.equals(dfs[i])) {
            tdf = dfs[i];
            break;
        }
    }
    // Is file list ?
    if (tdf != null) {
        // Accept COPY DnD only.
        if ((e.getSourceActions() & DnDConstants.ACTION_COPY) != 0) {
            e.acceptDrop(DnDConstants.ACTION_COPY);
        } else
            return;
        try {
            Transferable t = e.getTransferable();
            Object data = t.getTransferData(tdf);
            // How many files ?
            if (data instanceof java.util.List) {
                java.util.List al = (java.util.List) data;
                // Read the first File.
                if (al.size() > 0) {
                    File file = null;
                    // Stops the player if needed.
                    if ((playerState == PLAY) || (playerState == PAUSE)) {
                        theSoundPlayer.stop();
                        playerState = STOP;
                    }
                    // Clean the playlist.
                    playlist.removeAllItems();
                    // Add all dropped files to playlist.
                    ListIterator li = al.listIterator();
                    while (li.hasNext()) {
                        file = (File) li.next();
                        PlaylistItem pli = null;
                        if (file != null) {

                            pli = new PlaylistItem(file.getName(), file.getAbsolutePath(), -1, true);
                            if (pli != null)
                                playlist.appendItem(pli);
                        }
                    }
                    // Start the playlist from the top.
                    playlist.nextCursor();
                    fileList.initPlayList();
                    this.setCurrentSong(playlist.getCursor());
                }
            } else {
                log.info("Unknown dropped objects");
            }
        } catch (IOException ioe) {
            log.info("Drop error", ioe);
            e.dropComplete(false);
            return;
        } catch (UnsupportedFlavorException ufe) {
            log.info("Drop error", ufe);
            e.dropComplete(false);
            return;
        } catch (Exception ex) {
            log.info("Drop error", ex);
            e.dropComplete(false);
            return;
        }
        e.dropComplete(true);
    }
}

From source file:tvbrowser.ui.mainframe.MainFrame.java

@Override
public void drop(DropTargetDropEvent dtde) {
    dtde.acceptDrop(dtde.getDropAction());
    File[] files = getDragDropPlugins(dtde.getCurrentDataFlavors(), dtde.getTransferable());

    try {//  w  ww .ja  v a 2  s.co m
        File tmpFile = File.createTempFile("plugins", ".txt");
        StringBuilder alreadyInstalled = new StringBuilder();
        StringBuilder notCompatiblePlugins = new StringBuilder();

        for (File jarFile : files) {
            ClassLoader classLoader = null;

            try {
                URL[] urls = new URL[] { jarFile.toURI().toURL() };
                classLoader = URLClassLoader.newInstance(urls, ClassLoader.getSystemClassLoader());
            } catch (MalformedURLException exc) {

            }

            if (classLoader != null) {
                // Get the plugin name
                String pluginName = jarFile.getName();
                pluginName = pluginName.substring(0, pluginName.length() - 4);

                try {
                    String pluginId = "java." + pluginName.toLowerCase() + "." + pluginName;

                    PluginProxy installedPlugin = PluginProxyManager.getInstance().getPluginForId(pluginId);
                    TvDataServiceProxy service = TvDataServiceProxyManager.getInstance()
                            .findDataServiceById(pluginName.toLowerCase() + '.' + pluginName);

                    Class<?> pluginClass = classLoader.loadClass(pluginName.toLowerCase() + '.' + pluginName);

                    Method getVersion = pluginClass.getMethod("getVersion", new Class[0]);

                    Version version1 = null;
                    try {
                        version1 = (Version) getVersion.invoke(pluginClass, new Object[0]);
                    } catch (Throwable t1) {
                        t1.printStackTrace();
                    }

                    if (installedPlugin != null
                            && (installedPlugin.getInfo().getVersion().compareTo(version1) > 0
                                    || (installedPlugin.getInfo().getVersion().compareTo(version1) == 0
                                            && version1.isStable()))) {
                        alreadyInstalled.append(installedPlugin.getInfo().getName()).append('\n');
                    } else if (service != null && (service.getInfo().getVersion().compareTo(version1) > 0
                            || (service.getInfo().getVersion().compareTo(version1) == 0
                                    && version1.isStable()))) {
                        alreadyInstalled.append(service.getInfo().getName()).append('\n');
                    } else {
                        RandomAccessFile write = new RandomAccessFile(tmpFile, "rw");

                        String versionString = Integer.toString(version1.getMajor()) + '.'
                                + (version1.getMinor() / 10) + (version1.getMinor() % 10) + '.'
                                + version1.getSubMinor();

                        write.seek(write.length());

                        write.writeBytes("[plugin:" + pluginName + "]\n");
                        write.writeBytes("name_en=" + pluginName + "\n");
                        write.writeBytes("filename=" + jarFile.getName() + "\n");
                        write.writeBytes("version=" + versionString + "\n");
                        write.writeBytes("stable=" + version1.isStable() + "\n");
                        write.writeBytes("download=" + jarFile.toURI().toURL() + "\n");
                        write.writeBytes("category=unknown\n");

                        write.close();

                    }
                } catch (Exception e) {
                    notCompatiblePlugins.append(jarFile.getName()).append("\n");
                }
            }
        }

        if (alreadyInstalled.length() > 0) {
            showInfoTextMessage(
                    mLocalizer.msg("update.alreadyInstalled",
                            "The following Plugin in current version are already installed:"),
                    alreadyInstalled.toString(), 400);
        }

        if (notCompatiblePlugins.length() > 0) {
            showInfoTextMessage(
                    mLocalizer.msg("update.noTVBPlugin", "This following files are not TV-Browser Plugins:"),
                    notCompatiblePlugins.toString(), 400);
        }

        if (tmpFile.length() > 0) {
            java.net.URL url = tmpFile.toURI().toURL();
            SoftwareUpdater softwareUpdater = new SoftwareUpdater(url, false, true);
            mSoftwareUpdateItems = softwareUpdater.getAvailableSoftwareUpdateItems();
            dtde.dropComplete(true);

            SoftwareUpdateDlg updateDlg = new SoftwareUpdateDlg(this, false, mSoftwareUpdateItems);
            updateDlg.setVisible(true);
        } else {
            dtde.rejectDrop();
            dtde.dropComplete(false);
        }

        if (!tmpFile.delete()) {
            tmpFile.deleteOnExit();
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}