Example usage for java.awt.dnd DropTargetDropEvent getTransferable

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

Introduction

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

Prototype


public Transferable getTransferable() 

Source Link

Document

This method returns the Transferable object associated with the drop.

Usage

From source file:com.ftb2om2.view.MultiplePane.java

private String getDragAndDropPath(DropTargetDropEvent evt) throws IOException, UnsupportedFlavorException {
    evt.acceptDrop(DnDConstants.ACTION_COPY);
    List<File> droppedFile = (List<File>) evt.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
    return droppedFile.get(0).getAbsolutePath();
}

From source file:TreeDragTest.java

public void drop(DropTargetDropEvent dtde) {
    Point pt = dtde.getLocation();
    DropTargetContext dtc = dtde.getDropTargetContext();
    JTree tree = (JTree) dtc.getComponent();
    TreePath parentpath = tree.getClosestPathForLocation(pt.x, pt.y);
    DefaultMutableTreeNode parent = (DefaultMutableTreeNode) parentpath.getLastPathComponent();
    if (parent.isLeaf()) {
        dtde.rejectDrop();/*from   w  ww.  j  ava 2  s. c o  m*/
        return;
    }

    try {
        Transferable tr = dtde.getTransferable();
        DataFlavor[] flavors = tr.getTransferDataFlavors();
        for (int i = 0; i < flavors.length; i++) {
            if (tr.isDataFlavorSupported(flavors[i])) {
                dtde.acceptDrop(dtde.getDropAction());
                TreePath p = (TreePath) tr.getTransferData(flavors[i]);
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) p.getLastPathComponent();
                DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
                model.insertNodeInto(node, parent, 0);
                dtde.dropComplete(true);
                return;
            }
        }
        dtde.rejectDrop();
    } catch (Exception e) {
        e.printStackTrace();
        dtde.rejectDrop();
    }
}

From source file:EditorDropTarget4.java

public void drop(DropTargetDropEvent dtde) {
    DnDUtils.debugPrintln("DropTarget drop, drop action = " + DnDUtils.showActions(dtde.getDropAction()));

    // Check the drop action
    if ((dtde.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0) {
        // Accept the drop and get the transfer data
        dtde.acceptDrop(dtde.getDropAction());
        Transferable transferable = dtde.getTransferable();

        try {//from www .java2 s . c  o m
            boolean result = false;

            if (draggingFile) {
                result = dropFile(transferable);
            } else {
                result = dropContent(transferable, dtde);
            }

            dtde.dropComplete(result);
            DnDUtils.debugPrintln("Drop completed, success: " + result);
        } catch (Exception e) {
            DnDUtils.debugPrintln("Exception while handling drop " + e);
            dtde.rejectDrop();
        }
    } else {
        DnDUtils.debugPrintln("Drop target rejected drop");
        dtde.dropComplete(false);
    }
}

From source file:com.ftb2om2.view.MultiplePane.java

private void difficultyTableDragAndDrop(DropTargetDropEvent evt) {
    try {//from   w  w  w.j  a  v a  2 s  .c  o m
        DefaultTableModel model = (DefaultTableModel) difficultyTable.getModel();
        evt.acceptDrop(DnDConstants.ACTION_COPY);
        List<File> droppedFile = (List<File>) evt.getTransferable()
                .getTransferData(DataFlavor.javaFileListFlavor);
        droppedFile.forEach(file -> model.addRow(
                new Object[] { file.getName(), file.getPath(), FilenameUtils.getBaseName(file.getPath()) }));
    } catch (Exception ex) {
        Logger.getLogger(MainWindow.class.getName()).log(Level.INFO, null, ex);
    }
}

From source file:net.sf.nmedit.jtheme.component.JTModuleContainer.java

public void dropPatchFile(DropTargetDropEvent dtde) {
    Transferable transfer = dtde.getTransferable();
    PPatch patch = getModuleContainer().getPatch();
    DataFlavor fileFlavor = FileDnd.getFileFlavor(transfer.getTransferDataFlavors());
    List<File> files = FileDnd.getTransferableFiles(fileFlavor, transfer);
    if (files.size() == 1) {
        PPatch newPatch;/* w  w  w  .jav  a 2s .co m*/
        try {
            newPatch = patch.createFromFile(files.get(0));
        } catch (Exception e) {
            newPatch = null;
        }
        if (newPatch != null) {
            if (dropPatch(newPatch, dtde.getLocation())) {
                dtde.dropComplete(true);
            } else {
                dtde.rejectDrop();
                dtde.dropComplete(false);
            }
        } else {
            dtde.rejectDrop();
            dtde.dropComplete(false);
        }
    } else {
        dtde.rejectDrop();
        dtde.dropComplete(false);
    }
}

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 ww .jav a 2s.  c o 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:net.sf.nmedit.jtheme.component.JTModuleContainer.java

public void copyMoveModules(DropTargetDropEvent dtde) {
    DataFlavor chosen = PDragDrop.ModuleSelectionFlavor;
    Transferable transfer = dtde.getTransferable();
    Object data = null;/*from   w  w w .ja  va2 s .c o m*/

    try {
        // Get the data
        data = transfer.getTransferData(chosen);

        dtde.acceptDrop(dtde.getDropAction()
                & (DnDConstants.ACTION_MOVE | DnDConstants.ACTION_COPY | DnDConstants.ACTION_LINK));
    } catch (Throwable t) {
        if (log.isWarnEnabled()) {
            log.warn("copyMoveModules(DropTargetDropEvent=" + dtde + ") failed", t);
        }
        dtde.dropComplete(false);
        return;
    }

    if (data != null && data instanceof PModuleTransferData) {
        // Cast the data and create a nice module.
        PModuleTransferData tdata = ((PModuleTransferData) data);
        boolean isSamePatch = false;
        if (tdata.getSourcePatch() == getModuleContainer().getPatch())
            isSamePatch = true;

        //Point p = dtde.getLocation();

        int action = dtde.getDropAction();

        if ((action & DnDConstants.ACTION_MOVE) != 0 && isSamePatch) {
            MoveOperation op = tdata.getSourceModuleContainer().createMoveOperation();
            op.setDestination(getModuleContainer());
            executeOperationOnSelection(tdata, dtde.getLocation(), op);
        } else {
            copyModules(tdata, dtde.getLocation(), ((dtde.getDropAction() & DnDConstants.ACTION_LINK) != 0));
        }

    }
    dtde.dropComplete(true);
}

From source file:net.sf.nmedit.jtheme.component.JTModuleContainer.java

public void dropNewModule(DropTargetDropEvent dtde) {
    PModuleContainer mc = getModuleContainer();
    PModuleDescriptor md = PDragDrop.getModuleDescriptor(dtde.getTransferable());
    if (md == null || mc == null) {
        dtde.rejectDrop();//from  w  ww  .  ja  v  a2s .  c  o  m
        return;
    }

    Point l = dtde.getLocation();

    PModule module;
    try {
        module = mc.createModule(md);
        module.setScreenLocation(l.x, l.y);
    } catch (InvalidDescriptorException e) {
        if (log.isErrorEnabled()) {
            log.error("could not create module: " + md, e);
        }
        dtde.rejectDrop();
        return;
    }
    boolean moduleAdded = mc.add(module);

    if (!moduleAdded) {

        dtde.rejectDrop();
        return;
    }

    // TODO short after dropping a new module and then moving it
    // causes a NullPointerException in the next line
    PModuleContainer parent = module.getParentComponent();
    if (parent != null) {
        JTCableManager cm = getCableManager();
        try {
            cm.setAutoRepaintDisabled();
            MoveOperation move = parent.createMoveOperation();
            move.setScreenOffset(0, 0);
            move.add(module);
            move.move();
        } finally {
            cm.clearAutoRepaintDisabled();
        }
    } else {
        // XXX concurrency problems probably ?!
        throw new RuntimeException("Drop problem on illegal modules: for example 2 midi globals");
    }

    dtde.acceptDrop(DnDConstants.ACTION_COPY);

    // compute dimensions of container
    revalidate();
    repaint();
    dtde.dropComplete(true);
}

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

protected void dropTransfer(SlotObject<NordModular> s, DropTargetDropEvent dtde) {
    if (!acceptsDropData(s, dtde.getCurrentDataFlavors())) {
        dtde.rejectDrop();//from  ww  w  .  j  av  a 2  s  . c  om
        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:de.tor.tribes.ui.algo.AttackTimePanel.java

@Override
public void drop(DropTargetDropEvent dtde) {
    if (dtde.getDropTargetContext().getComponent().equals(jTimeFrameList)) {
        dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
        try {/*from   w w  w.  j  av a2  s  . c om*/
            String data = (String) dtde.getTransferable().getTransferData(DataFlavor.stringFlavor);
            TimeSpan s = TimeSpan.fromPropertyString(data);
            if (s == null) {
                throw new UnsupportedFlavorException(DataFlavor.stringFlavor);
            }
            addTimeSpan(s);
        } catch (UnsupportedFlavorException | IOException usfe) {
            //invalid data
        }
    }
}