Example usage for java.awt.dnd DropTargetDropEvent getDropTargetContext

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

Introduction

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

Prototype


public DropTargetContext getDropTargetContext() 

Source Link

Document

This method returns the DropTargetContext associated with this DropTargetEvent .

Usage

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();//  w  ww.j  a  va  2  s.  co  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:DNDList.java

/**
 * a drop has occurred//from w  w w. j ava2 s.c o  m
 * 
 */

public void drop(DropTargetDropEvent event) {

    try {
        Transferable transferable = event.getTransferable();

        // we accept only Strings
        if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {

            event.acceptDrop(DnDConstants.ACTION_MOVE);
            String s = (String) transferable.getTransferData(DataFlavor.stringFlavor);
            addElement(s);
            event.getDropTargetContext().dropComplete(true);
        } else {
            event.rejectDrop();
        }
    } catch (Exception exception) {
        System.err.println("Exception" + exception.getMessage());
        event.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 {//w w w. j a v a2  s .  co  m
            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
        }
    }
}

From source file:com.emental.mindraider.ui.frames.MindRaiderMainWindow.java

public void drop(DropTargetDropEvent evt) {
    logger.debug("=-> drop");

    try {//from  ww  w  .  j a  va2 s  . co m
        Transferable t = evt.getTransferable();

        if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            logger.debug(" Accepting 'string' data flavor...");
            evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
            String s = (String) t.getTransferData(DataFlavor.stringFlavor);
            evt.getDropTargetContext().dropComplete(true);

            logger.debug("DnD: '" + s + "'");
            if (s != null) {
                int indexOf = s.indexOf("\n");
                if (indexOf != -1) {
                    dragAndDropReference = new DragAndDropReference(s.substring(indexOf + 1),
                            s.substring(0, indexOf), DragAndDropReference.BROWSER_LINK);
                } else {
                    dragAndDropReference = new DragAndDropReference(s, DragAndDropReference.BROWSER_LINK);
                }
            }
        } else {
            if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                logger.debug(" Accepting 'file list' data flavor...");
                evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                List<Object> list = (List<Object>) t.getTransferData(DataFlavor.javaFileListFlavor);
                if (list != null) {
                    Iterator<Object> iterator = list.iterator();
                    while (iterator.hasNext()) {
                        Object next = iterator.next();
                        if (next instanceof File) {
                            logger.debug(" DnD file: " + next);
                            dragAndDropReference = new DragAndDropReference(((File) next).getAbsolutePath(),
                                    DragAndDropReference.EXPLORER_LINK);
                        }
                    }
                }
            } else {
                logger.debug("DnD rejected! ");
                dragAndDropReference = null;

                // DataFlavor[] dfs=t.getTransferDataFlavors();
                // for (int i = 0; i < dfs.length; i++) {
                // logger.debug(" "+i+" ... "+dfs[i].getMimeType());
                // logger.debug(" "+i+" ...
                // "+dfs[i].getDefaultRepresentationClassAsString());
                // logger.debug(" "+i+" ...
                // "+dfs[i].getHumanPresentableName());
                // }
            }
        }
    } catch (Exception e) {
        logger.debug("Drag&Drop error:", e);
        dragAndDropReference = null;
    }

    OutlineJPanel.getInstance().enableDisableAttachToolbarButton();

    if (dragAndDropReference != null) {
        JOptionPane.showMessageDialog(this,
                "Dropped local/web resource reference stored! Use \n'clip' icon from Notebook outline toolbar to attach it.",
                "Drag&Drop Info", JOptionPane.INFORMATION_MESSAGE);

        dragAndDropReference.debug();
    }
    logger.debug("<-= drop");
}

From source file:com.declarativa.interprolog.gui.ListenerWindow.java

void handlePrologInputDnD(DropTargetDropEvent dtde) {
    //System.out.println("drop:"+dtde);
    try {// w  ww .  j  ava 2s . c om
        Transferable transferable = dtde.getTransferable();
        /*
         DataFlavor[] flavors = transferable.getTransferDataFlavors();
         for (int f=0;f<flavors.length;f++)
         System.out.println("Flavor:"+flavors[f]);*/
        int action = dtde.getDropAction();
        if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            if (engine.isIdle()) {
                dtde.acceptDrop(action);
                final java.util.List files = (java.util.List) transferable
                        .getTransferData(DataFlavor.javaFileListFlavor);
                dtde.getDropTargetContext().dropComplete(true);
                boolean allPs = true;
                for (int f = 0; f < files.size(); f++) {
                    String filename = ((File) files.get(f)).getName();
                    int dot = filename.lastIndexOf('.');
                    if (!filename.endsWith(".P")) {
                        allPs = false;
                        break;
                    }
                }
                if (!allPs) {
                    errorMessage("All dragged files must be Prolog source files (with a .P extension)");
                } else {
                    prologOutput.append("\nReconsulting " + ((files.size() > 1 ? files.size() + " files...\n"
                            : files.size() + " file...\n")));
                    Runnable r = new Runnable() {
                        public void run() {
                            boolean crashed = false;
                            Toolkit.getDefaultToolkit().sync();
                            for (int f = 0; f < files.size() && !crashed; f++) {
                                File file = (File) files.get(f);
                                if (!processDraggedFile(file)) {
                                    crashed = true;
                                }
                            }
                            if (crashed) {
                                prologOutput.append("...terminated with errors.\n");
                            } else {
                                prologOutput.append("...done.\n");
                            }
                        }
                    };
                    SwingUtilities.invokeLater(r);
                }
            } else {
                dtde.rejectDrop();
                errorMessage("You can not consult files while Prolog is working");
            }
        } else {
            dtde.rejectDrop();
        }
    } catch (Exception e) {
        throw new IPException("Problem dropping:" + e);
    }
}

From source file:com.qspin.qtaste.ui.TestCaseTree.java

public void drop(DropTargetDropEvent dtde) {
    //try/*  ww  w .j  a  v a 2  s  . c o m*/
    {
        try {
            TCTreeNode tcTreeNode = (TCTreeNode) dtde.getTransferable().getTransferData(localObjectFlavor);
            Point dropPoint = dtde.getLocation();
            // int index = locationToIndex (dropPoint);
            TreePath path = getPathForLocation(dropPoint.x, dropPoint.y);
            Object targetNode = path.getLastPathComponent();
            if (targetNode instanceof TCTreeNode) {
                // rename the dragged dir into the new target one
                TCTreeNode tcTargetNode = (TCTreeNode) targetNode;
                FileNode fn = (FileNode) tcTargetNode.getUserObject();
                if (fn.isTestcaseDir()) {
                    dtde.rejectDrop();
                    return;
                }

                FileNode draggedFileNode = (FileNode) tcTreeNode.getUserObject();
                draggedFileNode.getFile()
                        .renameTo(new File(fn.getFile() + "/" + draggedFileNode.getFile().getName()));
                // update target tree

                testCasePane.parent.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

                TCTreeNode parentTreeNode = (TCTreeNode) tcTargetNode.getParent();
                if (parentTreeNode != null) {
                    parentTreeNode.removeAllChildren();
                    FileNode parentFileNode = (FileNode) parentTreeNode.getUserObject();
                    addTreeToDir(parentFileNode.getFile(), parentTreeNode);
                    ((DefaultTreeModel) getModel()).reload(parentTreeNode);
                } else {
                    tcTargetNode.removeAllChildren();
                    FileNode targetFileNode = (FileNode) tcTargetNode.getUserObject();
                    addTreeToDir(targetFileNode.getFile(), tcTargetNode);
                    ((DefaultTreeModel) getModel()).reload(tcTargetNode);
                }
                // update source tree
                parentTreeNode = (TCTreeNode) tcTreeNode.getParent();
                if (parentTreeNode != null) {
                    parentTreeNode.removeAllChildren();
                    FileNode parentFileNode = (FileNode) parentTreeNode.getUserObject();
                    addTreeToDir(parentFileNode.getFile(), parentTreeNode);
                    ((DefaultTreeModel) getModel()).reload(parentTreeNode);
                } else {
                    tcTreeNode.removeAllChildren();
                    FileNode targetFileNode = (FileNode) tcTreeNode.getUserObject();
                    addTreeToDir(targetFileNode.getFile(), tcTreeNode);
                    ((DefaultTreeModel) getModel()).reload(tcTreeNode);
                }
                testCasePane.parent.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                dtde.getDropTargetContext().dropComplete(true);
            } else {
                dtde.rejectDrop();
            }
        } catch (UnsupportedFlavorException ex) {
            logger.error(ex.getMessage());
        } catch (IOException ex) {
            logger.error(ex.getMessage());
        }

    }
}

From source file:de.tor.tribes.ui.windows.TribeTribeAttackFrame.java

@Override
public void drop(DropTargetDropEvent dtde) {
    Transferable t = dtde.getTransferable();
    List<Village> villages = new LinkedList<Village>();
    if (dtde.getDropTargetContext().getComponent() == jSourcesTable
            || dtde.getDropTargetContext().getComponent() == jVictimTable) {
        if (dtde.isDataFlavorSupported(VillageTransferable.villageDataFlavor)) {
            //village dnd
            dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
            try {
                villages = (List<Village>) t.getTransferData(VillageTransferable.villageDataFlavor);
            } catch (Exception ex) {
            }// w ww . j  a v a  2s .  c  o m
        } else if (dtde.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            //string dnd
            try {
                villages = PluginManager.getSingleton()
                        .executeVillageParser((String) t.getTransferData(DataFlavor.stringFlavor));
            } catch (Exception e) {
            }
        } else {
            dtde.rejectDrop();
            return;
        }
    } else {
        dtde.rejectDrop();
        return;
    }

    MapPanel.getSingleton().setCurrentCursor(MapPanel.getSingleton().getCurrentCursor());

    if (!villages.isEmpty()) {
        if (jideTabbedPane1.getSelectedIndex() == 0) {
            fireAddSourcesEvent(villages);
        } else if (jideTabbedPane1.getSelectedIndex() == 1) {
            fireAddTargetsEvent(villages);
        }
    }
}

From source file:pkgrenamer.Main.java

static void onDropFiles(DropTargetDropEvent dtde, FileDropListener onDrop) {
    try {/*  ww  w . ja v a 2 s  .  c om*/
        Transferable transferable = dtde.getTransferable();

        if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            dtde.acceptDrop(DnDConstants.ACTION_MOVE);
            java.util.List<?> files = (java.util.List<?>) transferable
                    .getTransferData(DataFlavor.javaFileListFlavor);
            File[] fa = new File[files.size()];
            for (int i = 0; i < fa.length; i++) {
                fa[i] = (File) files.get(i);
            }
            onDrop.onDropFile(fa);
            dtde.getDropTargetContext().dropComplete(true);

        } else {
            if (sNixFileDataFlavor == null) {
                sNixFileDataFlavor = new DataFlavor("text/uri-list;class=java.lang.String");
            }
            dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
            String data = (String) transferable.getTransferData(sNixFileDataFlavor);
            if (data != null) {
                ArrayList<File> fs = new ArrayList<>();
                for (StringTokenizer st = new StringTokenizer(data, "\r\n"); st.hasMoreTokens();) {
                    String token = st.nextToken().trim();
                    if (token.startsWith("#") || token.isEmpty()) {
                        continue;
                    }
                    try {
                        fs.add(new File(new URI(token)));
                    } catch (Exception e) {
                    }
                }
                onDrop.onDropFile(fs.toArray(new File[0]));
                dtde.getDropTargetContext().dropComplete(true);
            } else {
                dtde.rejectDrop();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:xtrememp.PlaylistManager.java

@Override
@SuppressWarnings("unchecked")
public void drop(DropTargetDropEvent ev) {
    DropTargetContext targetContext = ev.getDropTargetContext();
    Transferable t = ev.getTransferable();
    try {//w  w  w  . j av  a2 s.  co m
        // Windows
        if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            ev.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
            addFiles((List<File>) t.getTransferData(DataFlavor.javaFileListFlavor), false);
            targetContext.dropComplete(true);
            // Linux
        } else if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            ev.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
            String urls = (String) t.getTransferData(DataFlavor.stringFlavor);
            List<File> fileList = new ArrayList<>();
            StringTokenizer st = new StringTokenizer(urls);
            while (st.hasMoreTokens()) {
                URI uri = new URI(st.nextToken());
                fileList.add(new File(uri));
            }
            addFiles(fileList, false);
            targetContext.dropComplete(true);
        }
    } catch (UnsupportedFlavorException | InvalidDnDOperationException | IOException | URISyntaxException ex) {
        logger.error(ex.getMessage(), ex);
    }
}