/** * The file or URL has been dropped. * * @see java.awt.dnd.DropTargetListener#drop(java.awt.dnd.DropTargetDropEvent) */ public void drop(DropTargetDropEvent event) { // important to first try urlFlavor if (event.isDataFlavorSupported(urlFlavor)) { try { event.acceptDrop(DnDConstants.ACTION_COPY); Transferable trans = event.getTransferable(); URL url = (URL) (trans.getTransferData(urlFlavor)); String urlStr = url.toString(); gridIO.openShape(url); reset(); event.dropComplete(true); } catch (Exception e) { event.dropComplete(false); } } else if (event.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { try { event.acceptDrop(DnDConstants.ACTION_COPY); Transferable trans = event.getTransferable(); java.util.List list = (java.util.List) (trans.getTransferData(DataFlavor.javaFileListFlavor)); File droppedFile = (File) list.get(0); // More than one file -> get only first file gridIO.openShape(droppedFile.getPath()); reset(); event.dropComplete(true); } catch (Exception e) { event.dropComplete(false); } } }
private void acceptDrop(DropTargetDropEvent dtde) { int dragaction = dtde.getDropAction(); if (dragaction != 0) { dtde.acceptDrop(dragaction); } else { dtde.acceptDrop(ALL_ACTIONS); } }
public void drop(DropTargetDropEvent dtde) { dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { try { content = (java.util.List) dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor); repaint(); } catch (UnsupportedFlavorException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } dtde.dropComplete(true); boolean listsAreEqual = true; for (int i = 0; i < content.size(); i++) { if (!FileListTransferable.files[i].getName().equals(content.get(i).getName())) { listsAreEqual = false; } } if (listsAreEqual) { System.err.println(InterprocessMessages.EXECUTION_IS_SUCCESSFULL); System.exit(0); } } dtde.rejectDrop(); System.err.println(InterprocessMessages.FILES_ON_TARGET_ARE_CORRUPTED); System.exit(1); }
public void drop(DropTargetDropEvent dtde) { if ((dtde.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) == 0) { dtde.rejectDrop(); return; } dtde.acceptDrop(DnDConstants.ACTION_COPY); Vector<RowContainer> oldvec = this.filevector; Transferable transferable = dtde.getTransferable(); try { java.util.List<File> filelist = (java.util.List<File>) transferable.getTransferData(DataFlavor.javaFileListFlavor); for (File f : filelist) { filevector.add(new RowContainer(f)); model.fireTableDataChanged(); System.out.println(f.toString()); } } catch (IOException ex) { ex.printStackTrace(); } catch (UnsupportedFlavorException ex) { ex.printStackTrace(); } dtde.dropComplete(true); File[] filar = new File[filevector.size()]; for (int i = 0; i < filevector.size(); i++) { filar[i] = filevector.get(i).getFile(); } super.firePropertyChange("filevector", null, filar); }
public final void drop(DropTargetDropEvent dtde) { try { if (drawImage) { clearImage(); } int action = dtde.getDropAction(); Transferable transferable = dtde.getTransferable(); Point pt = dtde.getLocation(); if (transferable.isDataFlavorSupported(TransferableNode.NODE_FLAVOR) && canPerformAction(tree, draggedPath, action, pt)) { TreePath pathTarget = tree.getPathForLocation(pt.x, pt.y); TreePath node = (TreePath) transferable.getTransferData(TransferableNode.NODE_FLAVOR); if (executeDrop(tree, node, pathTarget, action)) { dtde.acceptDrop(action); dtde.dropComplete(true); return; } } dtde.rejectDrop(); dtde.dropComplete(false); } catch (Exception e) { System.out.println(e); dtde.rejectDrop(); dtde.dropComplete(false); } }
private boolean processDrop(AWorkspaceTreeNode targetNode, DropTargetDropEvent event) { event.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); final Transferable transferable = event.getTransferable(); final int dropAction = event.getDropAction(); try { if (!targetNode.getAllowsChildren()) { targetNode = targetNode.getParent(); } if (!DnDController.isDropAllowed(targetNode)) { event.dropComplete(false); return false; } if (handleDrop(targetNode, transferable, dropAction)) { event.dropComplete(true); return true; } } catch (NoDropHandlerFoundExeption e) { LogUtils.info( "org.freeplane.plugin.workspace.dnd.WorkspaceTransferHandler.processDrop(targetNode, event): " + e.getMessage()); } event.dropComplete(false); return false; }
@SuppressWarnings("unchecked") @Override public void drop(DropTargetDropEvent dtde) { for (DataFlavor dataFlover : dtde.getCurrentDataFlavors()) { if (dataFlover.isFlavorJavaFileListType()) { try { dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); for (File file : (List<File>) dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor)) { if (file.isFile() && file.canRead()) { fileText.setText(file.getCanonicalPath()); break; } } dtde.getDropTargetContext().dropComplete(true); } catch (UnsupportedFlavorException e1) { // } catch (IOException e2) { // } } } }
@SuppressWarnings("unchecked") public void drop(DropTargetDropEvent event) { try { if (event.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { Transferable tr = event.getTransferable(); int action = event.getDropAction(); event.acceptDrop(action); java.util.List<File> files = (java.util.List<File>) tr.getTransferData(DataFlavor.javaFileListFlavor); for (File f : files) { JEXStatics.logManager.log("Copying file " + f, 1, this); String newDest = parent.folder.getPath() + File.separator + f.getName(); FileUtility.copy(f, new File(newDest)); } parent.rebuild(); event.dropComplete(true); JEXStatics.logManager.log("Drop completed...", 1, this); return; } event.rejectDrop(); } catch (Exception e) { e.printStackTrace(); event.rejectDrop(); } }
/** * The drag operation has terminated with a drop on this <code>DropTarget</code>. This method is * responsible for undertaking the transfer of the data associated with the gesture. The <code> * DropTargetDropEvent</code> provides a means to obtain a <code>Transferable</code> object that * represents the data object(s) to be transfered. * * <p>From this method, the <code>DropTargetListener</code> shall accept or reject the drop via * the acceptDrop(int dropAction) or rejectDrop() methods of the <code>DropTargetDropEvent * </code> parameter. * * <p>Subsequent to acceptDrop(), but not before, <code>DropTargetDropEvent</code>'s * getTransferable() method may be invoked, and data transfer may be performed via the returned * <code>Transferable</code>'s getTransferData() method. * * <p>At the completion of a drop, an implementation of this method is required to signal the * success/failure of the drop by passing an appropriate <code>boolean</code> to the <code> * DropTargetDropEvent</code>'s dropComplete(boolean success) method. * * <p>Note: The actual processing of the data transfer is not required to finish before this * method returns. It may be deferred until later. * * <p> * * @param dtde the <code>DropTargetDropEvent</code> */ @Override @SuppressWarnings("unchecked") public void drop(DropTargetDropEvent event) { if (event.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { event.acceptDrop(DnDConstants.ACTION_COPY); try { List<File> files = (List<File>) event.getTransferable().getTransferData(DataFlavor.javaFileListFlavor); splitMPOFiles(files); } catch (IOException e) { JOptionPane.showConfirmDialog( MPOImageSplitter.this, "Could not access the dropped data.", "MPOImageSplitter: Drop Failed", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE); } catch (UnsupportedFlavorException e) { JOptionPane.showConfirmDialog( MPOImageSplitter.this, "Unsupported data flavor.", "MPOImageSplitter: Drop Failed", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE); } } else { event.rejectDrop(); } }
@Override public void drop(DropTargetDropEvent dtde) { Transferable t = dtde.getTransferable(); if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { try { dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); java.util.List files = (java.util.List) t.getTransferData(DataFlavor.javaFileListFlavor); File f = (File) files.get(0); System.out.println("Reading from " + f.getAbsolutePath()); colorSchemeList.setColorSchemeList(f); colorSchemeComp.clearContent(); dtde.dropComplete(true); JitterbugEditor.this.setTitle(APP_TITLE + " - " + f.getAbsolutePath()); return; } catch (Exception exc) { exc.printStackTrace(); } } // if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) { // try { // dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); // String content = (String) t // .getTransferData(DataFlavor.stringFlavor); // colorSchemeComp.setContent(new ByteArrayInputStream(content // .getBytes())); // dtde.dropComplete(true); // return; // } catch (Exception exc) { // exc.printStackTrace(); // } // } }
@SuppressWarnings("rawtypes") public void drop(DropTargetDropEvent dtde) { try { Transferable tr = dtde.getTransferable(); if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); List files = (List) tr.getTransferData(DataFlavor.javaFileListFlavor); this.txtRawData.setText(((File) files.get(0)).getAbsolutePath()); dtde.getDropTargetContext().dropComplete(true); } else { System.err.println("DataFlavor.javaFileListFlavor is not supported, rejected"); dtde.rejectDrop(); } } catch (IOException ex) { System.err.println("IOException"); ex.printStackTrace(); dtde.rejectDrop(); } catch (UnsupportedFlavorException ex) { System.err.println("UnsupportedFlavorException"); ex.printStackTrace(); dtde.rejectDrop(); } }
public void drop(DropTargetDropEvent dtde) { // System.out.println("drop"); // System.out.println(dtde.getDropAction()); // System.out.println(); // 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 { boolean result = false; if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { result = dropFile(transferable); } else { result = false; } dtde.dropComplete(result); } catch (Exception e) { System.out.println("Exception while handling drop " + e); dtde.rejectDrop(); } } else { System.out.println("Drop target rejected drop"); dtde.dropComplete(false); } }
private void processDrop(DropData dropData, DropTargetDropEvent dropTargetDropEvent) { putEntryAtPoint(dropData.whatToDrop, dropData.whereInTable); _widget.addEntry(dropData.whatToDrop); _underlyingTable.setRowSelectionInterval(dropData.whereInTable.x, dropData.whereInTable.x); _underlyingTable.setColumnSelectionInterval(dropData.whereInTable.y, dropData.whereInTable.y); dropTargetDropEvent.acceptDrop(dropTargetDropEvent.getDropAction()); dropTargetDropEvent.dropComplete(true); }
public void drop(DropTargetDropEvent dtde) { dtde.acceptDrop(DnDConstants.ACTION_COPY); DataFlavor[] flavors = null; try { Transferable t = dtde.getTransferable(); iterator = null; flavors = t.getTransferDataFlavors(); if (IJ.debugMode) IJ.log("DragAndDrop.drop: " + flavors.length + " flavors"); for (int i = 0; i < flavors.length; i++) { if (IJ.debugMode) IJ.log(" flavor[" + i + "]: " + flavors[i].getMimeType()); if (flavors[i].isFlavorJavaFileListType()) { Object data = t.getTransferData(DataFlavor.javaFileListFlavor); iterator = ((List) data).iterator(); break; } else if (flavors[i].isFlavorTextType()) { Object ob = t.getTransferData(flavors[i]); if (!(ob instanceof String)) continue; String s = ob.toString().trim(); if (IJ.isLinux() && s.length() > 1 && (int) s.charAt(1) == 0) s = fixLinuxString(s); ArrayList list = new ArrayList(); if (s.indexOf("href=\"") != -1 || s.indexOf("src=\"") != -1) { s = parseHTML(s); if (IJ.debugMode) IJ.log(" url: " + s); list.add(s); this.iterator = list.iterator(); break; } BufferedReader br = new BufferedReader(new StringReader(s)); String tmp; while (null != (tmp = br.readLine())) { tmp = java.net.URLDecoder.decode(tmp.replaceAll("\\+", "%2b"), "UTF-8"); if (tmp.startsWith("file://")) tmp = tmp.substring(7); if (IJ.debugMode) IJ.log(" content: " + tmp); if (tmp.startsWith("http://")) list.add(s); else list.add(new File(tmp)); } this.iterator = list.iterator(); break; } } if (iterator != null) { Thread thread = new Thread(this, "DrawAndDrop"); thread.setPriority(Math.max(thread.getPriority() - 1, Thread.MIN_PRIORITY)); thread.start(); } } catch (Exception e) { dtde.dropComplete(false); return; } dtde.dropComplete(true); if (flavors == null || flavors.length == 0) { if (IJ.isMacOSX()) IJ.error( "First drag and drop ignored. Please try again. You can avoid this\n" + "problem by dragging to the toolbar instead of the status bar."); else IJ.error("Drag and drop failed"); } }
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(); try { // Transferable tr = dtde.getTransferable(); // !!! this should work, but doesn't. after one // click on the draggable node, this call here will not return the TransferableTreeNode that // is created in dragGestureRecognized() Transferable tr = ObjectInspector .transferNode; // therefore, we use this hack, which just stores the current // transferable in a static variable 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(); Object sourceObject = node.getUserObject(); FieldWrapperObjectTree targetField = ((FieldWrapperObjectTree) parent.getUserObject()); if (sourceObject != null) { if (sourceObject instanceof FieldWrapperObjectTree) { FieldWrapperObjectTree sourceField = (FieldWrapperObjectTree) sourceObject; if (sourceField.getTheField().getType().equals(targetField.getTheField().getType())) { targetField .getTheField() .set( targetField.getOnObject(), sourceField.getTheField().get(sourceField.getOnObject())); } } else if (sourceObject.getClass().equals(targetField.getTheField().getType())) { targetField.getTheField().set(targetField.getOnObject(), sourceObject); } tree.invalidate(); tree.repaint(); } dtde.dropComplete(true); return; } } dtde.rejectDrop(); } catch (Exception e) { e.printStackTrace(); // dtde.rejectDrop(); } }
public void drop(DropTargetDropEvent dtde) { if (draggingFiles != null) { dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); fireActionPerformed(dtde); draggingFiles = null; } else { dtde.rejectDrop(); } }
public boolean processDrop(DropTargetDropEvent event) { event.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); Transferable transferable = event.getTransferable(); if (processDrop(transferable, event.getDropAction())) { event.dropComplete(true); return true; } event.dropComplete(false); return false; }
/** * {@inheritDoc} * * @see DropTargetListener#drop(DropTargetDropEvent) */ public final void drop(DropTargetDropEvent event) { event.acceptDrop(event.getDropAction()); try { Transferable transferable = event.getTransferable(); event.dropComplete(getTransferHandler().importData(this, transferable)); } catch (RuntimeException exc) { event.dropComplete(false); } this.dropPoint = null; repaint(); }
@Override 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(); 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()); EMedium eMedium = (EMedium) tr.getTransferData(flavors[i]); TreeNodeUserData userData = (TreeNodeUserData) parent.getUserObject(); String shelfName = userData.getCaption(); if (isLibrarySelected(dtde) && shelfName.equals(AppProperties.INSTANCE.RENTALS_SHELF_NAME)) { if (!uiDelegate.addRentalEMedium(eMedium)) JOptionPane.showMessageDialog( frame, "Cannot rent e-medium!", "Add document error", JOptionPane.ERROR_MESSAGE); } else if (!isLibrarySelected(dtde) && !shelfName.equals(AppProperties.INSTANCE.RENTALS_SHELF_NAME)) { if (!uiDelegate.addEMediumShelf(shelfName, eMedium)) JOptionPane.showMessageDialog( frame, "Duplicated e-medium!", "Add document error", JOptionPane.ERROR_MESSAGE); } else JOptionPane.showMessageDialog( frame, "Need to rent the e-medium first!", "Add document error", JOptionPane.ERROR_MESSAGE); dtde.dropComplete(true); return; } } // dtde.rejectDrop(); } catch (OperationNotSupportedException e) { JOptionPane.showMessageDialog( frame, "Cannot add a document to this shelf", "Add document error", JOptionPane.ERROR_MESSAGE); } catch (Exception e) { e.printStackTrace(); } }
@Override public void drop(DropTargetDropEvent dtde) { dtde.acceptDrop(info.getDnDAction()); Transferable transferable = dtde.getTransferable(); boolean success = false; try { Object result = transferable.getTransferData(info.getDataFlavor()); success = info.handleDroppedObject(result); } catch (UnsupportedFlavorException e) { Log.log(e); } catch (IOException e) { Log.log(e); } dtde.dropComplete(success); }
public void drop(DropTargetDropEvent event) { if (!isDropAcceptable(event)) { event.rejectDrop(); return; } event.acceptDrop(DnDConstants.ACTION_COPY); Transferable transferable = event.getTransferable(); MessageCollection messages = new MessageCollection(); try { List<File> files = (List<File>) transferable.getTransferData(DataFlavor.javaFileListFlavor); for (File file : files) { try { ResourceLocator locator = new ResourceLocator(file.getAbsolutePath()); IGV.getInstance().load(locator, panel); } catch (DataLoadException de) { messages.append(de.getMessage()); } } String obj = transferable.getTransferData(DataFlavor.stringFlavor).toString(); if (HttpUtils.isRemoteURL(obj)) { IGV.getInstance().load(new ResourceLocator(obj), panel); } if (messages != null && !messages.isEmpty()) { log.error(messages.getFormattedMessage()); MessageUtils.showMessage(messages.getFormattedMessage()); } } catch (Exception e) { String obj = null; try { obj = transferable.getTransferData(DataFlavor.stringFlavor).toString(); if (HttpUtils.isRemoteURL(obj)) { IGV.getInstance().load(new ResourceLocator(obj), panel); } } catch (Exception e1) { log.error(e1); if (messages != null && !messages.isEmpty()) { MessageUtils.showMessage(messages.getFormattedMessage()); } } } IGV.getMainFrame().repaint(); event.dropComplete(true); }
public void drop(DropTargetDropEvent event) { if (!event.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { event.rejectDrop(); return; } event.acceptDrop(event.getDropAction()); try { List<File> data = (List<File>) event.getTransferable().getTransferData(DataFlavor.javaFileListFlavor); Vector<String> pages = this.model.addWikiPages((File[]) data.toArray()); JList pagelist = new JList(pages); this.updateUI(this.ui, pagelist); event.dropComplete(true); } catch (Exception e) { event.dropComplete(false); log.error("Failed to accept drag n drop", e); } highlightUI(false); }
public void drop(DropTargetDropEvent evt) { try { Transferable t = evt.getTransferable(); if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) { evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); String s = (String) t.getTransferData(DataFlavor.stringFlavor); evt.getDropTargetContext().dropComplete(true); process(s); } else { evt.rejectDrop(); } } catch (IOException e) { evt.rejectDrop(); } catch (UnsupportedFlavorException e) { evt.rejectDrop(); } }
@Override public void drop(DropTargetDropEvent dtde) { Point dropPoint = dtde.getLocation(); TreePath path = getPathForLocation(dropPoint.x, dropPoint.y); System.out.println("drop path is " + path); boolean dropped = false; try { dtde.acceptDrop(DnDConstants.ACTION_MOVE); // Object dropObject = dtde.getTransferable().getTransferData(dataFlavor); // if (dropObject instanceof DefaultMutableTreeNode) { if (draggedNode instanceof DefaultMutableTreeNode) { DefaultMutableTreeNode dropNode = (DefaultMutableTreeNode) draggedNode; if (dropNode.getAllowsChildren()) { DefaultMutableTreeNode destinyNode = (DefaultMutableTreeNode) path.getLastPathComponent(); if (!dropNode.isNodeDescendant(destinyNode) && !destinyNode.isNodeChild(dropNode)) { if (moveDirectory(dropNode, destinyNode)) { ((DefaultTreeModel) getModel()).removeNodeFromParent(dropNode); ((DefaultTreeModel) getModel()) .insertNodeInto(dropNode, destinyNode, destinyNode.getChildCount()); dropped = true; } } } else { DefaultMutableTreeNode destinyNode = (DefaultMutableTreeNode) path.getLastPathComponent(); DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) this.getLastSelectedPathComponent(); if (!selectedNode.getUserObject().equals(destinyNode.getUserObject())) { if (moveDocument(dropNode, destinyNode)) { setSelectionPath(path); dropped = true; dragDropEnd(); } } } } } catch (Exception e) { e.printStackTrace(); } dtde.dropComplete(dropped); }
@Override public void drop(DropTargetDropEvent dtde) { if (dtde.isDataFlavorSupported(CardFlavor.cardFlavor) && this.canDrop) { Transferable trans = dtde.getTransferable(); try { Object data = trans.getTransferData(CardFlavor.cardFlavor); CardWindow card = (CardWindow) data; DropTargetContext context = dtde.getDropTargetContext(); CardCollection comp = (CardCollection) context.getComponent(); Container parent = comp.getParent(); GameController.getController(false) .makeMove( CardCollection.getMoveFromIndex( comp.getCardMouseLocation(), new OrderedCard(card.card, card.cardLocation))); if (parent != null) parent.remove(card); comp.setCard(card, comp.getCardMouseLocation()); dtde.acceptDrop(DnDConstants.ACTION_MOVE); comp.validate(); comp.repaint(); dtde.dropComplete(true); } catch (Exception e) { dtde.rejectDrop(); dtde.dropComplete(false); JOptionPane.showMessageDialog( MainWindow.getMainWindow(), "Invalid move.", "Invalid Move Error", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } } else { dtde.rejectDrop(); dtde.dropComplete(false); JOptionPane.showMessageDialog( MainWindow.getMainWindow(), "Invalid move.", "Invalid Move Error", JOptionPane.ERROR_MESSAGE); } }
@Override public void drop(DropTargetDropEvent dtde) { try { if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { dtde.acceptDrop(DnDConstants.ACTION_COPY); Transferable t = dtde.getTransferable(); List list = (List) t.getTransferData(DataFlavor.javaFileListFlavor); for (Object o : list) { if (o instanceof File) { File f = (File) o; System.out.println(f.getAbsolutePath()); } } dtde.dropComplete(true); return; } } catch (UnsupportedFlavorException | IOException ex) { ex.printStackTrace(); } dtde.rejectDrop(); }
public void drop( java.awt.dnd.DropTargetDropEvent evt) { // System.out.println( "DropTargetListener.drop" ); java.awt.datatransfer.Transferable transferable = evt.getTransferable(); // If it's our native TransferableObject, use that if (transferable.isDataFlavorSupported(TransferableObject.DATA_FLAVOR)) { evt.acceptDrop(java.awt.dnd.DnDConstants.ACTION_MOVE); Object obj = null; try { obj = transferable.getTransferData(TransferableObject.DATA_FLAVOR); } // end try catch (java.awt.datatransfer.UnsupportedFlavorException e) { e.printStackTrace(); } // end catch catch (java.io.IOException e) { e.printStackTrace(); } // end catch if (obj != null) { // See where in the list we dropped the element. int dropIndex = locationToIndex(evt.getLocation()); javax.swing.DefaultListModel model = (javax.swing.DefaultListModel) getModel(); if (dropIndex < 0) model.addElement(obj); // Else is it moving down the list? else if (sourceIndex >= 0 && dropIndex > sourceIndex) model.add(dropIndex - 1, obj); else model.add(dropIndex, obj); } // end if: we got the object // Else there was a problem getting the object else { evt.rejectDrop(); } // end else: can't get the object } // end if: it's a native TransferableObject // Else we can't handle this else evt.rejectDrop(); } // end drop
/** * 当拖动系统中的文件放置drop到该List中时,调用此方法。 */ public synchronized void drop(DropTargetDropEvent dropTargetDropEvent) { System.out .println("method: drop(DropTargetDropEvent dropTargetDropEvent)"); try { // 获取传入的Transfer对象 Transferable tr = dropTargetDropEvent.getTransferable(); // 如果Transfer对象支持文件放置到java平台,则进行下一步处理 if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { // 使用“拷贝、移动”方式接收放置操作。 dropTargetDropEvent .acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); // 从Transfer中取数据,得到的是一个文件列表。即可以一次拖放多个文件 java.util.List fileList = (java.util.List) tr .getTransferData(DataFlavor.javaFileListFlavor); Iterator iterator = fileList.iterator(); while (iterator.hasNext()) { // 将拖放进来的文件的信息添加到List的数据模型中 File file = (File) iterator.next(); Hashtable hashtable = new Hashtable(); hashtable.put("name", file.getName()); hashtable.put("url", file.toURL().toString()); hashtable.put("path", file.getAbsolutePath()); ((DefaultListModel) getModel()).addElement(hashtable); } // 放置操作成功结束 dropTargetDropEvent.getDropTargetContext().dropComplete(true); } else { // Transferable对象不支持文件放置到java中,拒绝。 System.err.println("Rejected"); dropTargetDropEvent.rejectDrop(); } } catch (IOException io) { io.printStackTrace(); dropTargetDropEvent.rejectDrop(); } catch (UnsupportedFlavorException ufe) { ufe.printStackTrace(); dropTargetDropEvent.rejectDrop(); } }
/* * (non-Javadoc) * * @see java.awt.dnd.DropTargetListener#drop(java.awt.dnd.DropTargetDropEvent) */ public void drop(DropTargetDropEvent e) { if (dragSource != null) { e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); Point p = e.getLocation(); int targetRow = rowAtPoint(p); Object edge = graph.insertEdge( null, null, null, dragSource.cell, JTableRenderer.this.cell, "sourceRow=" + sourceRow + ";targetRow=" + targetRow); graph.setSelectionCell(edge); // System.out.println("clearing drag source"); dragSource = null; e.dropComplete(true); } else { e.rejectDrop(); } }
public void drop(final DropTargetDropEvent e) { _timerHover.stop(); // Prevent hover timer from doing an unwanted expandPath or collapsePath if (!isDropAcceptable(e)) { e.rejectDrop(); return; } e.acceptDrop(e.getDropAction()); final Transferable transferable = e.getTransferable(); final DataFlavor[] flavors = transferable.getTransferDataFlavors(); for (int i = 0; i < flavors.length; i++) { final DataFlavor flavor = flavors[i]; if (flavor.isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType)) { try { final Point pt = e.getLocation(); final TreePath pathTarget = getClosestPathForLocation(pt.x, pt.y); final TreePath pathSource = (TreePath) transferable.getTransferData(flavor); if ((pathTarget == null) || (pathSource == null)) { e.dropComplete(false); return; } final MutableTreeNode sourceNode = (MutableTreeNode) pathSource.getLastPathComponent(); final MutableTreeNode oldParent = (MutableTreeNode) sourceNode.getParent(); final MutableTreeNode targetNode = (MutableTreeNode) pathTarget.getLastPathComponent(); final MutableTreeNode newParent = (MutableTreeNode) targetNode.getParent(); if (!sourceNode.isLeaf() && (targetNode.getParent() == sourceNode)) { // trying to drag a folder into its own childs e.dropComplete(false); return; } final DefaultTreeModel model = (DefaultTreeModel) getModel(); final TreePath pathNewChild = null; if (targetNode.isLeaf() || JDragTree.this.isCollapsed(pathTarget)) { // collapsed tree node or leaf // dropped on a leaf, insert into leaf's parent AFTER leaf int idx = newParent.getIndex(targetNode); if (idx < 0) { JDragTree.logger.warning("child not found in parent!!!"); e.dropComplete(false); return; } else { idx++; // insert AFTER targetNode // remove node from oldParent ... final Object[] removedChilds = {sourceNode}; final int[] childIndices = {oldParent.getIndex(sourceNode)}; sourceNode.removeFromParent(); model.nodesWereRemoved(oldParent, childIndices, removedChilds); // ... and insert into newParent if (idx >= newParent.getChildCount()) { // newParent.add( sourceNode ); newParent.insert(sourceNode, newParent.getChildCount()); final int insertedIndex[] = {newParent.getChildCount() - 1}; model.nodesWereInserted(newParent, insertedIndex); } else { newParent.insert(sourceNode, idx); final int insertedIndex[] = {idx}; model.nodesWereInserted(newParent, insertedIndex); } } } else { // expanded node, insert UNDER the node (before first child) // remove node from oldParent ... final Object[] removedChilds = {sourceNode}; final int[] childIndices = {oldParent.getIndex(sourceNode)}; sourceNode.removeFromParent(); model.nodesWereRemoved(oldParent, childIndices, removedChilds); // ... and add to newParent targetNode.insert(sourceNode, 0); final int insertedIndex[] = {0}; model.nodesWereInserted(targetNode, insertedIndex); } if (pathNewChild != null) setSelectionPath(pathNewChild); // Mark this as the selected path in the tree break; // No need to check remaining flavors } catch (final UnsupportedFlavorException ufe) { JDragTree.logger.log( Level.SEVERE, "Exception thrown in drop(DropTargetDropEvent e)", ufe); e.dropComplete(false); return; } catch (final IOException ioe) { JDragTree.logger.log( Level.SEVERE, "Exception thrown in drop(DropTargetDropEvent e)", ioe); e.dropComplete(false); return; } } } e.dropComplete(true); }