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

The following examples show how to use java.awt.dnd.DropTargetDropEvent#isDataFlavorSupported() . 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: 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 2
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 3
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override public void drop(DropTargetDropEvent e) {
  try {
    if (e.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
      e.acceptDrop(DnDConstants.ACTION_COPY);
      Transferable transferable = e.getTransferable();
      ((List<?>) transferable.getTransferData(DataFlavor.javaFileListFlavor))
          .stream().filter(File.class::isInstance).map(File.class::cast)
          .map(File::toPath)
          .forEach(MainPanel.this::addImage);
      e.dropComplete(true);
    } else {
      e.rejectDrop();
    }
  } catch (UnsupportedFlavorException | IOException ex) {
    e.rejectDrop();
  }
}
 
Example 4
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@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);
    } else {
      dtde.rejectDrop();
    }
  } catch (UnsupportedFlavorException | IOException ex) {
    dtde.rejectDrop();
  }
}
 
Example 5
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 6
Source File: DragManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void drop(DropTargetDropEvent dtde) {
    if (dtde.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        dtde.dropComplete(true);
        if (activeDragItem != null) {
            activeDragItem.dragAccepted();
        }
    } else {
        dtde.rejectDrop();
    }
    component.setCursor(oCursor);
    activeDragItem = null;
    component.repaint();
}
 
Example 7
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 8
Source File: Desktop.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void drop(DropTargetDropEvent e)
{

  // This code is f***ing unbelievable.
  // How is anyone supposed to create it from scratch?
  try {
    final Transferable tr = e.getTransferable();

    if (e.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
      e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
      final java.util.List<?> files =
        (java.util.List<?>) tr.getTransferData(DataFlavor.javaFileListFlavor);
      for (final Object name : files) {
        final File file = (File) name;
        addFile(file);
      }
      e.dropComplete(true);
    } else if (e.isDataFlavorSupported(DataFlavor.stringFlavor)) {
      e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
      final String filename =
        (String) tr.getTransferData(DataFlavor.stringFlavor);

      addFile(new File(filename));
      e.dropComplete(true);
    } else {
      // Reject drop
    }
  } catch (final IOException ioe) {
    showErr(null, ioe);
  } catch (final UnsupportedFlavorException ufe) {
    showErr("Unsupported data type", ufe);
  }
}
 
Example 9
Source File: PaletteDropTarget.java    From niftyeditor with Apache License 2.0 5 votes vote down vote up
@Override
public void drop(DropTargetDropEvent dtde) {
    if (dtde.getTransferable().isDataFlavorSupported(WidgetData.EFFECTFLAVOR)){
       GEffectDropEvent(dtde);
    }else if(dtde.isDataFlavorSupported(WidgetData.FLAVOR)){
        GElementDropEvent(dtde);
    }else{
       dtde.rejectDrop(); 
    }
}