下面列出了java.awt.dnd.DropTargetDropEvent#getCurrentDataFlavors ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Nullable
public static String extractDropLink(@Nonnull final DropTargetDropEvent dtde) throws Exception {
String foundHtmlLink = null;
String foundMozLink = null;
for (final DataFlavor df : dtde.getCurrentDataFlavors()) {
if (df.getRepresentationClass() == String.class) {
if (foundHtmlLink == null && df.isMimeTypeEqual(MIME_TEXT_HTML)) {
final String link = extractHtmlLink(true, (String) dtde.getTransferable().getTransferData(df));
if (link != null) {
foundHtmlLink = link;
}
}
} else if (df.getRepresentationClass() == InputStream.class && df.isMimeTypeEqual(MIME_MOZ_URL)) {
if (foundMozLink == null) {
final InputStream in = ((InputStream) dtde.getTransferable().getTransferData(df));
final StringWriter string = new StringWriter();
IOUtils.copy(in, string, Charset.defaultCharset());
IOUtils.closeQuietly(in);
foundMozLink = removeZeroChars(string.toString().split("\\n")[0]).trim();
}
}
}
return foundMozLink == null ? foundHtmlLink : foundMozLink;
}
@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
@SuppressWarnings("unchecked")
public void drop(DropTargetDropEvent dtde) {
if (!stylesheet.isEnabled())
return;
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()) {
stylesheet.setText(file.getCanonicalPath());
lastPath = file.getParentFile();
break;
}
}
dtde.getDropTargetContext().dropComplete(true);
} catch (UnsupportedFlavorException | IOException e) {
//
}
}
}
}
@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 | IOException e) {
//
}
}
}
}
@SuppressWarnings("unchecked")
@Override
public void drop(DropTargetDropEvent dtde) {
for (DataFlavor dataFlover : dtde.getCurrentDataFlavors()) {
if (dataFlover.isFlavorJavaFileListType()) {
try {
dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
List<File> files = new ArrayList<>();
for (File file : (List<File>)dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor))
if (file.exists())
files.add(file.getAbsoluteFile());
else
log.warn("Failed to drop from clipboard: '" + file.getAbsolutePath() + "' is not a file.");
if (!files.isEmpty()) {
if (dtde.getDropAction() != DnDConstants.ACTION_COPY)
fileListModel.clear();
addFiles(files);
}
dtde.getDropTargetContext().dropComplete(true);
} catch (UnsupportedFlavorException | IOException e) {
//
}
}
}
}