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

The following examples show how to use java.awt.dnd.DropTargetDropEvent#acceptDrop() . 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: Outline.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void drop(DropTargetDropEvent dtde) {
    if (mDragWasAcceptable) {
        dtde.acceptDrop(dtde.getDropAction());
        if (mModel.getDragColumn() != null) {
            dropColumn(dtde);
        } else {
            Row[] rows = mModel.getDragRows();
            if (rows != null && rows.length > 0) {
                dropRow(dtde);
            }
        }
        dtde.dropComplete(true);
    } else if (mAlternateDragDestination != null) {
        UIUtilities.updateDropTargetDropPointTo(dtde, mAlternateDragDestination);
        mAlternateDragDestination.drop(dtde);
    }
}
 
Example 2
Source File: ExportPanel.java    From importer-exporter with Apache License 2.0 6 votes vote down vote up
@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()) {
						browseText.setText(file.getCanonicalPath());
						break;
					}
				}

				dtde.getDropTargetContext().dropComplete(true);	
			} catch (UnsupportedFlavorException | IOException e) {
				//
			}
		}
	}
}
 
Example 3
Source File: GenericDialogPlus.java    From ij-ridgedetection with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Gets the string.
 *
 * @param event
 *            the event
 * @return the string
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws UnsupportedFlavorException
 *             the unsupported flavor exception
 */
@SuppressWarnings("unchecked")
static String getString(DropTargetDropEvent event) throws IOException, UnsupportedFlavorException {
	String text = null;
	DataFlavor fileList = DataFlavor.javaFileListFlavor;

	if (event.isDataFlavorSupported(fileList)) {
		event.acceptDrop(DnDConstants.ACTION_COPY);
		List<File> list = (List<File>) event.getTransferable().getTransferData(fileList);
		text = list.get(0).getAbsolutePath();
	} else if (event.isDataFlavorSupported(DataFlavor.stringFlavor)) {
		event.acceptDrop(DnDConstants.ACTION_COPY);
		text = (String) event.getTransferable().getTransferData(DataFlavor.stringFlavor);
		if (text.startsWith("file://"))
			text = text.substring(7);
		text = stripSuffix(stripSuffix(text, "\n"), "\r").replaceAll("%20", " ");
	} else {
		event.rejectDrop();
		return null;
	}

	event.dropComplete(text != null);
	return text;
}
 
Example 4
Source File: JarDropTarget.java    From BON2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public synchronized void drop(DropTargetDropEvent dtde) {
    try {
        dtde.acceptDrop(DnDConstants.ACTION_REFERENCE);
        List<File> droppedFiles = (List<File>) dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
        if (droppedFiles.size() == 1){
            String target = droppedFiles.get(0).getAbsolutePath();
            if (target.endsWith(".jar")) {
                action.accept(target);
            } else {
                JOptionPane.showMessageDialog(parent, "Only JAR files are supported.", "Error dropping file", JOptionPane.ERROR_MESSAGE);
            }
        }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(parent, ex, "Error dropping file", JOptionPane.ERROR_MESSAGE);
        ex.printStackTrace();
    }
}
 
Example 5
Source File: AbstractDSWorkbenchFrame.java    From dsworkbench with Apache License 2.0 6 votes vote down vote up
@Override
public void drop(DropTargetDropEvent dtde) {
    if (dtde.isDataFlavorSupported(VillageTransferable.villageDataFlavor) || dtde.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
    } else {
        dtde.rejectDrop();
        return;
    }

    Transferable t = dtde.getTransferable();
    List<Village> v;
    MapPanel.getSingleton().setCurrentCursor(MapPanel.getSingleton().getCurrentCursor());
    try {
        v = (List<Village>) t.getTransferData(VillageTransferable.villageDataFlavor);
        fireVillagesDraggedEvent(v, dtde.getLocation());
    } catch (Exception ignored) {
    }
}
 
Example 6
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 7
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 8
Source File: ImageTransferTest.java    From openjdk-jdk9 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 9
Source File: FileDropTargetListener.java    From magarena with GNU General Public License v3.0 5 votes vote down vote up
private void doDropAction(final DropTargetDropEvent event) {

        event.acceptDrop(DnDConstants.ACTION_COPY);

        final Optional<File> file = getDroppedFile(event.getTransferable());
        if (file.isPresent()) {
            doFileAction(file.get());
        } else {
            MagicSound.ALERT.play();
        }

        // Inform that the drop is complete
        event.dropComplete(true);
    }
 
Example 10
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 11
Source File: ImageTransferTest.java    From openjdk-jdk8u-backup 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: 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 13
Source File: CharacterSheet.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void drop(DropTargetDropEvent dtde) {
    dtde.acceptDrop(dtde.getDropAction());
    UIUtilities.getAncestorOfType(this, SheetDockable.class).addRows(mDragRows);
    mDragRows = null;
    dtde.dropComplete(true);
}
 
Example 14
Source File: DropListener.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.equals(DataFlavor.imageFlavor)) {
            // it is unclear how this could be used
            e.rejectDrop();
            return;
        }
        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);
                destination.handleDrop(list);
            } catch (UnsupportedFlavorException | IOException ex) {
                Messages.showException(ex);
                e.rejectDrop();
            }
            e.dropComplete(true);
            return;
        }
    }

    // DataFlavor not recognized
    e.rejectDrop();
}
 
Example 15
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 16
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 17
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 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);
}