下面列出了java.awt.dnd.DropTargetDropEvent#acceptDrop ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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) {
//
}
}
}
}
@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);
}
}
@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) {
}
}
@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();
}
}
/**
* 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;
}
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);
}
}
@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();
}
@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();
}
@Override
public void drop(DropTargetDropEvent dtde) {
dtde.acceptDrop(dtde.getDropAction());
UIUtilities.getAncestorOfType(this, SheetDockable.class).addRows(mDragRows);
mDragRows = null;
dtde.dropComplete(true);
}
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);
}
}
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);
}
}
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);
}
}
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);
}
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);
}
}
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);
}
}
@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);
}
}
@Override
public void drop(DropTargetDropEvent dtde) {
dtde.acceptDrop(dtde.getDropAction());
showModalDialog(targetFrame);
dtde.dropComplete(true);
}
/**
* 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;
}
}
}
} );
}
@Override
public void drop(DropTargetDropEvent dtde) {
dtde.acceptDrop(dtde.getDropAction());
showModalDialog(targetFrame);
dtde.dropComplete(true);
}
@Override
public void drop(DropTargetDropEvent dtde) {
dtde.acceptDrop(dtde.getDropAction());
showModalDialog(targetFrame);
dtde.dropComplete(true);
}