Java Code Examples for java.awt.dnd.DropTargetDropEvent#dropComplete()

The following examples show how to use java.awt.dnd.DropTargetDropEvent#dropComplete() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainDropTarget.java    From jadx with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void drop(DropTargetDropEvent dtde) {
	if (!dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
		dtde.rejectDrop();
		return;
	}
	dtde.acceptDrop(dtde.getDropAction());
	try {
		Transferable transferable = dtde.getTransferable();
		List<File> transferData = (List<File>) transferable.getTransferData(DataFlavor.javaFileListFlavor);
		if (!transferData.isEmpty()) {
			dtde.dropComplete(true);
			// load first file
			mainWindow.open(transferData.get(0).toPath());
		}
	} catch (Exception e) {
		LOG.error("File drop operation failed", e);
	}
}
 
Example 2
Source File: QuietEditorPane.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void drop(DropTargetDropEvent e) {
    Collection<? extends ExternalDropHandler> handlers = Lookup.getDefault().lookupAll(ExternalDropHandler.class);
    for (ExternalDropHandler handler : handlers) {
        if (handler.canDrop(e)) {
            e.acceptDrop(DnDConstants.ACTION_COPY);
            boolean dropped = handler.handleDrop(e);
            if(!dropped) {
                continue; //try next ExternalDropHandler
            }
            e.dropComplete(true);
            isDragging = false;
            return;
        }
    }
    orig.drop(e);
    isDragging = false;
}
 
Example 3
Source File: TreePanel.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void drop(DropTargetDropEvent event) {
    if (mDragState != null) {
        event.acceptDrop(event.getDropAction());
        mDropReceived = true;
        event.dropComplete(mDragState.drop(event));
    } else if (mAlternateDragDestination != null) {
        UIUtilities.convertPoint(event.getLocation(), this, mAlternateDragDestination);
        mAlternateDragDestination.drop(event);
    } else {
        mDropReceived = false;
        event.rejectDrop();
    }
    clearDragState();
}
 
Example 4
Source File: Trace.java    From Method_Trace_Tool with Apache License 2.0 5 votes vote down vote up
public static void drag()//定义的拖拽方法
{
    //panel表示要接受拖拽的控件
    new DropTarget(JlPath, DnDConstants.ACTION_COPY_OR_MOVE, new DropTargetAdapter() {
        public void drop(DropTargetDropEvent dtde)//重写适配器的drop方法
        {
            try {
                if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor))//如果拖入的文件格式受支持
                {
                    dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);//接收拖拽来的数据
                    List<File> list = (List<File>) (dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor));
                    String temp = "";
                    for (File file : list) {
                        temp = file.getAbsolutePath();
                        JlPath.setText(temp);
                        break;
                    }
                    //JOptionPane.showMessageDialog(null, temp);
                    dtde.dropComplete(true);//指示拖拽操作已完成
                } else {
                    dtde.rejectDrop();//否则拒绝拖拽来的数据
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
 
Example 5
Source File: ImageTransferTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
void checkImage(DropTargetDropEvent dtde) {
    final Transferable t = dtde.getTransferable();
    if (t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
        dtde.acceptDrop(DnDConstants.ACTION_COPY);
        Image im;
        try {
            im = (Image) t.getTransferData(DataFlavor.imageFlavor);
            System.err.println("getTransferData was successful");
        } catch (Exception e) {
            System.err.println("Can't getTransferData: " + e);
            dtde.dropComplete(false);
            notifyTransferSuccess(false);
            return;
        }

        if (im == null) {
            System.err.println("getTransferData returned null");
            dtde.dropComplete(false);
            notifyTransferSuccess(false);
        } else if (areImagesIdentical(image, im)) {
            dtde.dropComplete(true);
            notifyTransferSuccess(true);
        } else {
            System.err.println("transferred image is different from initial image");
            dtde.dropComplete(false);
            notifyTransferSuccess(false);
        }

    } else {
        System.err.println("imageFlavor is not supported by Transferable");
        dtde.rejectDrop();
        notifyTransferSuccess(false);
    }
}
 
Example 6
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void drop(DropTargetDropEvent e) {
  DefaultListModel<E> model = (DefaultListModel<E>) getModel();
  // Transferable t = e.getTransferable();
  // DataFlavor[] f = t.getTransferDataFlavors();
  // try {
  //   Component comp = (Component) t.getTransferData(f[0]);
  // } catch (UnsupportedFlavorException | IOException ex) {
  //   e.dropComplete(false);
  // }
  if (isDropAcceptable(e) && targetIndex >= 0) {
    E str = model.get(draggedIndex);
    if (targetIndex == draggedIndex) {
      setSelectedIndex(targetIndex);
    } else if (targetIndex < draggedIndex) {
      model.remove(draggedIndex);
      model.add(targetIndex, str);
      setSelectedIndex(targetIndex);
    } else {
      model.add(targetIndex, str);
      model.remove(draggedIndex);
      setSelectedIndex(targetIndex - 1);
    }
    e.dropComplete(true);
  } else {
    e.dropComplete(false);
  }
  e.dropComplete(false);
  targetIndex = -1;
  repaint();
}
 
Example 7
Source File: AbstractTreeTransferHandler.java    From TrakEM2 with GNU General Public License v3.0 5 votes vote down vote up
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, draggedNode, action, pt)) {
                        TreePath pathTarget = tree.getPathForLocation(pt.x, pt.y);
                        DefaultMutableTreeNode node = (DefaultMutableTreeNode) transferable.getTransferData(TransferableNode.NODE_FLAVOR);
                        DefaultMutableTreeNode newParentNode = (DefaultMutableTreeNode)pathTarget.getLastPathComponent();
                        if (executeDrop(tree, node, newParentNode, 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);
        }
}
 
Example 8
Source File: MetaDataPanel.java    From Pixelitor with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void drop(DropTargetDropEvent e) {
    Transferable transferable = e.getTransferable();
    DataFlavor[] flavors = transferable.getTransferDataFlavors();
    for (DataFlavor flavor : flavors) {
        if (flavor.isFlavorJavaFileListType()) {
            // this is where we get after dropping a file or directory
            e.acceptDrop(DnDConstants.ACTION_COPY);

            try {
                @SuppressWarnings("unchecked")
                List<File> list = (List<File>) transferable.getTransferData(flavor);
                File file = list.get(0);
                if (file.isFile()) {
                    changeFile(file);
                }
            } catch (UnsupportedFlavorException | IOException ex) {
                ex.printStackTrace();
                e.rejectDrop();
            }
            e.dropComplete(true);
            return;
        }
    }

    // DataFlavor not recognized
    e.rejectDrop();
}
 
Example 9
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override public void drop(DropTargetDropEvent e) {
  try {
    DropTargetContext c = e.getDropTargetContext();
    Component o = c.getComponent();
    Transferable t = e.getTransferable();
    DataFlavor[] f = t.getTransferDataFlavors();

    if (o instanceof JTabbedPane) {
      JTabbedPane jtp = (JTabbedPane) o;
      JScrollPane sp = (JScrollPane) jtp.getComponentAt(targetTabIndex);
      JViewport vp = sp.getViewport();
      JList<String> targetList = (JList<String>) SwingUtilities.getUnwrappedView(vp);
      JList<String> sourceList = (JList<String>) t.getTransferData(f[0]);

      DefaultListModel<String> tm = (DefaultListModel<String>) targetList.getModel();
      DefaultListModel<String> sm = (DefaultListModel<String>) sourceList.getModel();

      int[] indices = sourceList.getSelectedIndices();
      for (int j = indices.length - 1; j >= 0; j--) {
        tm.addElement(sm.remove(indices[j]));
      }
      e.dropComplete(true);
    } else {
      e.dropComplete(false);
    }
  } catch (UnsupportedFlavorException | IOException ex) {
    e.dropComplete(false);
  }
}
 
Example 10
Source File: ImageTransferTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
void checkImage(DropTargetDropEvent dtde) {
    final Transferable t = dtde.getTransferable();
    if (t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
        dtde.acceptDrop(DnDConstants.ACTION_COPY);
        Image im;
        try {
            im = (Image) t.getTransferData(DataFlavor.imageFlavor);
            System.err.println("getTransferData was successful");
        } catch (Exception e) {
            System.err.println("Can't getTransferData: " + e);
            dtde.dropComplete(false);
            notifyTransferSuccess(false);
            return;
        }

        if (im == null) {
            System.err.println("getTransferData returned null");
            dtde.dropComplete(false);
            notifyTransferSuccess(false);
        } else if (areImagesIdentical(image, im)) {
            dtde.dropComplete(true);
            notifyTransferSuccess(true);
        } else {
            System.err.println("transferred image is different from initial image");
            dtde.dropComplete(false);
            notifyTransferSuccess(false);
        }

    } else {
        System.err.println("imageFlavor is not supported by Transferable");
        dtde.rejectDrop();
        notifyTransferSuccess(false);
    }
}
 
Example 11
Source File: ImageTransferTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
void checkImage(DropTargetDropEvent dtde) {
    final Transferable t = dtde.getTransferable();
    if (t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
        dtde.acceptDrop(DnDConstants.ACTION_COPY);
        Image im;
        try {
            im = (Image) t.getTransferData(DataFlavor.imageFlavor);
            System.err.println("getTransferData was successful");
        } catch (Exception e) {
            System.err.println("Can't getTransferData: " + e);
            dtde.dropComplete(false);
            notifyTransferSuccess(false);
            return;
        }

        if (im == null) {
            System.err.println("getTransferData returned null");
            dtde.dropComplete(false);
            notifyTransferSuccess(false);
        } else if (areImagesIdentical(image, im)) {
            dtde.dropComplete(true);
            notifyTransferSuccess(true);
        } else {
            System.err.println("transferred image is different from initial image");
            dtde.dropComplete(false);
            notifyTransferSuccess(false);
        }

    } else {
        System.err.println("imageFlavor is not supported by Transferable");
        dtde.rejectDrop();
        notifyTransferSuccess(false);
    }
}
 
Example 12
Source File: JTreeUtil.java    From Logisim with GNU General Public License v3.0 5 votes vote down vote up
@Override
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(NODE_FLAVOR)
				&& controller.canPerformAction(tree, draggedNode, action, pt)) {
			TreePath pathTarget = tree.getPathForLocation(pt.x, pt.y);
			Object node = transferable.getTransferData(NODE_FLAVOR);
			Object newParentNode = pathTarget.getLastPathComponent();
			if (controller.executeDrop(tree, node, newParentNode, action)) {
				dtde.acceptDrop(action);
				dtde.dropComplete(true);
				return;
			}
		}
		dtde.rejectDrop();
		dtde.dropComplete(false);
	} catch (Exception e) {
		dtde.rejectDrop();
		dtde.dropComplete(false);
	}
}
 
Example 13
Source File: MissingEventsOnModalDialogTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void drop(DropTargetDropEvent dtde) {
    dtde.acceptDrop(dtde.getDropAction());
    showModalDialog(targetFrame);
    dtde.dropComplete(true);
}
 
Example 14
Source File: QueryBuilderPanel.java    From nextreports-designer with Apache License 2.0 4 votes vote down vote up
public void drop(DropTargetDropEvent dtde) {
    if ((dtde.getDropAction() & DnDConstants.ACTION_MOVE) == DnDConstants.ACTION_MOVE) {
        dtde.acceptDrop(DnDConstants.ACTION_MOVE);

        Transferable t = dtde.getTransferable();
        Point point = dtde.getLocation();

        try {
            DBProcedure proc = (DBProcedure) t.getTransferData(DBProcTransferable.DATA_FLAVOR);
            List<DBProcedureColumn> columns = Globals.getDBViewer().getProcedureColumns(proc.getSchema(), proc.getCatalog(), proc.getName());
            if (!Globals.getDBViewer().isValidProcedure(columns)) {
                Show.info(I18NSupport.getString("procedure.invalid"));
                return;
            } else {
                StringBuilder sb = new StringBuilder("call ");
                boolean order = Globals.getDialect().schemaBeforeCatalog();
                if (!order) {
                    if (proc.getCatalog() != null) {
                        sb.append(proc.getCatalog()).append(".");
                    }
                }
                if (!"%".equals(proc.getSchema())) {
                    sb.append(proc.getSchema()).append(".");
                }
                if (order) {
                    if (proc.getCatalog() != null) {
                        sb.append(proc.getCatalog()).append(".");
                    }
                }
                sb.append(proc.getName());
                sb.append("(");
                int index = 1;
                for (int i = 0, size = columns.size(); i < size; i++) {
                    DBProcedureColumn col = columns.get(i);
                    if (ProcUtil.IN.equals(col.getReturnType())) {
                        sb.append("${P").append(index).append("}");
                        index++;
                        if (i < size - 1) {
                            sb.append(", ");
                        }
                    } else if (ProcUtil.OUT.equals(col.getReturnType())) {
                        if (ProcUtil.REF_CURSOR.equals(col.getDataType())) {
                            sb.append("?");
                            if (i < size - 1) {
                                sb.append(" , ");
                            }
                        }
                    }
                }
                sb.append(")");
                sqlView.setQueryString(sb.toString());
            }

        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            e.printStackTrace();
        }

        dtde.dropComplete(true);
    }
}
 
Example 15
Source File: CardSetDndHandler.java    From opencards with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void drop(DropTargetDropEvent dtde) {
        try {
            Transferable tr = dtde.getTransferable();
            DataFlavor[] flavors = tr.getTransferDataFlavors();

            for (DataFlavor flavor : flavors) {
                // Check for file lists specifically
                if (flavor.isFlavorJavaFileListType()) {
                    // Great!  Accept copy drops...
                    dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);

                    // And add the list of file names to our text area

                    boolean invalidDrop = false;

                    List<File> droppedFiles = (List<File>) tr.getTransferData(flavor);
                    for (File file : droppedFiles) {
                        if (CardFileBackend.hasSupportedExtension(file)) {
                            Category curCat = CategoryUtils.getSelectedCategory();
                            curCat.registerCardSet(CardFileCache.getCardFile(file));
                        } else {
                            invalidDrop = true;
                        }
                    }

                    if (invalidDrop) {
                        Runnable task2 = () -> {
                            JOptionPane.showMessageDialog(OpenCards.getInstance(),
                                    "Just PowerPoint (ppt) and MarkDown (md) are supported as flashcard-sets by OpenCards",
                                    "Invalid file format of dropped file",
                                    JOptionPane.WARNING_MESSAGE)
                            ;
                        };

// start the thread
                        new Thread(task2).start();
                    }

                    // If we made it this far, everything worked.
                    dtde.dropComplete(true);
                    return;
                }
            }
            // Hmm, the user must not have dropped a file list
            System.out.println("Drop failed: " + dtde);
            dtde.rejectDrop();
        } catch (Exception e) {
            e.printStackTrace();
            dtde.rejectDrop();
        }
    }
 
Example 16
Source File: MissingEventsOnModalDialogTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void drop(DropTargetDropEvent dtde) {
    dtde.acceptDrop(dtde.getDropAction());
    showModalDialog(targetFrame);
    dtde.dropComplete(true);
}
 
Example 17
Source File: Button2DragTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public void run() {
    frame = new Frame();

    final DragSourceListener dragSourceListener = new DragSourceAdapter() {
        public void dragDropEnd(DragSourceDropEvent e) {
            dropSuccess = e.getDropSuccess();
            System.err.println("Drop was successful: " + dropSuccess);
        }
    };
    DragGestureListener dragGestureListener = new DragGestureListener() {
        public void dragGestureRecognized(DragGestureEvent dge) {
            dge.startDrag(null, new StringSelection("OK"), dragSourceListener);
        }
    };
    new DragSource().createDefaultDragGestureRecognizer(frame, DnDConstants.ACTION_MOVE,
                                                        dragGestureListener);

    DropTargetAdapter dropTargetListener = new DropTargetAdapter() {
        public void drop(DropTargetDropEvent dtde) {
            dtde.acceptDrop(DnDConstants.ACTION_MOVE);
            dtde.dropComplete(true);
            System.err.println("Drop");
        }
    };
    new DropTarget(frame, dropTargetListener);

    //What would normally go into main() will probably go here.
    //Use System.out.println for diagnostic messages that you want
    //to read after the test is done.
    frame.setUndecorated(true);
    frame.setBounds(100, 100, 200, 200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    Robot robot = Util.createRobot();

    Util.waitForIdle(robot);

    Point startPoint = frame.getLocationOnScreen();
    Point endPoint = new Point(startPoint);
    startPoint.translate(50, 50);
    endPoint.translate(150, 150);

    Util.drag(robot, startPoint, endPoint, InputEvent.BUTTON2_MASK);

    Util.waitForIdle(robot);
    robot.delay(500);

    if (dropSuccess) {
        System.err.println("test passed");
    } else {
        throw new RuntimeException("test failed: drop was not successful");
    }
}
 
Example 18
Source File: MissingEventsOnModalDialogTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void drop(DropTargetDropEvent dtde) {
    dtde.acceptDrop(dtde.getDropAction());
    showModalDialog(targetFrame);
    dtde.dropComplete(true);
}
 
Example 19
Source File: MainFrame.java    From sc2gears with Apache License 2.0 4 votes vote down vote up
/**
 * Enables dragging and dropping files onto Sc2gears.<br>
 * If the dropped file list contains only a single replay (amongst other files), that will be opened in the Replay analyzer.<br>
 * If the dropped file list contains multiple replays and/or folders, those will be opened in a replay search.<br>
 * If the dropped file list contains replay sources and/or replay lists, those will be opened respectively in replay searches.<br>
 */
private void setupDropTarget() {
	new DropTarget( this, new DropTargetAdapter() {
		@Override
		public void drop( final DropTargetDropEvent event ) {
			final Transferable transferable = event.getTransferable();
			for ( final DataFlavor flavor : transferable.getTransferDataFlavors() ) {
				if ( flavor.isFlavorJavaFileListType() ) {
					// It's a file list, accept it!
					event.acceptDrop( DnDConstants.ACTION_COPY_OR_MOVE );
					try {
						@SuppressWarnings("unchecked")
						final List< File > fileList       = (List< File >) transferable.getTransferData( flavor );
						final List< File > replayFileList = new ArrayList< File >();
						
						for ( final File file : fileList ) {
							if ( file.isDirectory() )
								replayFileList.add( file );
							else {
								if ( GuiUtils.SC2_REPLAY_LIST_FILE_FILTER.accept( file ) )
									createNewInternalFrame( newRepSearchNode, null, file );
								else if ( GuiUtils.SC2_REPLAY_SOURCE_FILE_FILTER.accept( file ) )
									createNewInternalFrame( newRepSearchNode, file, null );
								else
									replayFileList.add( file );
							}
						}
						
						if ( replayFileList.size() == 1 && replayFileList.get( 0 ).isFile() )
							openReplayFile( replayFileList.get( 0 ) );
						else if ( replayFileList.size() > 0 ) // It might contain 1 folder only...
							createNewInternalFrame( newRepSearchNode, null, null, replayFileList.toArray( new File[ replayFileList.size() ] ) );
						
						event.dropComplete( true );
						
					} catch ( final Exception e ) {
						e.printStackTrace();
						event.rejectDrop();
					}
					break;
				}
			}
		}
	} );
}
 
Example 20
Source File: MissingEventsOnModalDialogTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void drop(DropTargetDropEvent dtde) {
    dtde.acceptDrop(dtde.getDropAction());
    showModalDialog(targetFrame);
    dtde.dropComplete(true);
}