下面列出了java.awt.dnd.DropTargetEvent#java.awt.datatransfer.UnsupportedFlavorException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void drop(DropTargetDropEvent e) {
try {
if (e.isDataFlavorSupported(DataFlavor.stringFlavor)) {
Transferable tr = e.getTransferable();
e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
String s = (String) tr.getTransferData(DataFlavor.stringFlavor);
// dropList.add(s);
System.out.println(" NP myDropTargetListener got " + s);
e.dropComplete(true);
} else {
e.rejectDrop();
}
} catch (IOException | UnsupportedFlavorException io) {
io.printStackTrace();
e.rejectDrop();
}
}
@Override
protected void edit(final GraphWriteMethods wg, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
try {
final Clipboard cb = ConstellationClipboardOwner.getConstellationClipboard();
if (cb.isDataFlavorAvailable(RecordStoreTransferable.RECORDSTORE_FLAVOR)) {
final RecordStoreTransferable rt = (RecordStoreTransferable) cb.getContents(null);
final RecordStore cbRecordStore = (RecordStore) rt.getTransferData(RecordStoreTransferable.RECORDSTORE_FLAVOR);
if (cbRecordStore != null) {
// There is a graph on the local clipboard.
PluginExecution.withPlugin(InteractiveGraphPluginRegistry.PASTE_GRAPH)
.withParameter(PasteGraphPlugin.RECORDSTORE_PARAMETER_ID, cbRecordStore)
.executeNow(wg);
}
}
} catch (UnsupportedFlavorException | IOException ex) {
Exceptions.printStackTrace(ex);
}
}
/**
* Gets data from clipboard.
*
* @return the data from clipboard map 中只有一对 kev-value
*/
@Nullable
public static Map<DataFlavor, Object> getDataFromClipboard() {
Map<DataFlavor, Object> data = new HashMap<>(1);
Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
if (transferable != null) {
// 如果剪切板的内容是文件
try {
DataFlavor dataFlavor;
if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
// List<File>
dataFlavor = DataFlavor.javaFileListFlavor;
} else if (transferable.isDataFlavorSupported(DataFlavor.imageFlavor)) {
// Image
dataFlavor = DataFlavor.imageFlavor;
} else {
return null;
}
Object object = transferable.getTransferData(dataFlavor);
data.put(dataFlavor, object);
} catch (IOException | UnsupportedFlavorException ignored) {
// 如果 clipboard 没有文件, 不处理
}
}
return data;
}
private String getBestCharsetForTextFormat(Long lFormat,
Transferable localeTransferable) throws IOException
{
String charset = null;
if (localeTransferable != null &&
isLocaleDependentTextFormat(lFormat) &&
localeTransferable.isDataFlavorSupported(javaTextEncodingFlavor))
{
try {
charset = new String(
(byte[])localeTransferable.getTransferData(javaTextEncodingFlavor),
"UTF-8"
);
} catch (UnsupportedFlavorException cannotHappen) {
}
} else {
charset = getCharsetForTextFormat(lFormat);
}
if (charset == null) {
// Only happens when we have a custom text type.
charset = getDefaultTextCharset();
}
return charset;
}
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException
{
if (!isDataFlavorSupported(flavor)) {
throw new UnsupportedFlavorException(flavor);
}
Object ret = flavorsToData.get(flavor);
if (ret instanceof IOException) {
// rethrow IOExceptions generated while fetching data
throw (IOException)ret;
} else if (ret instanceof DataFactory) {
// Now we can render the data
DataFactory factory = (DataFactory)ret;
ret = factory.getTransferData(flavor);
}
return ret;
}
private String getBestCharsetForTextFormat(Long lFormat,
Transferable localeTransferable) throws IOException
{
String charset = null;
if (localeTransferable != null &&
isLocaleDependentTextFormat(lFormat) &&
localeTransferable.isDataFlavorSupported(javaTextEncodingFlavor))
{
try {
charset = new String(
(byte[])localeTransferable.getTransferData(javaTextEncodingFlavor),
"UTF-8"
);
} catch (UnsupportedFlavorException cannotHappen) {
}
} else {
charset = getCharsetForTextFormat(lFormat);
}
if (charset == null) {
// Only happens when we have a custom text type.
charset = getDefaultTextCharset();
}
return charset;
}
@Override
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException {
if (isDataFlavorSupported(flavor)) {
if (flavor.equals(DataFlavor.allHtmlFlavor)) {
return ALL_HTML_AS_STRING;
} else if (flavor.equals(DataFlavor.fragmentHtmlFlavor)) {
return FRAGMENT_HTML_AS_STRING;
} else if (flavor.equals(DataFlavor.selectionHtmlFlavor)) {
return SELECTION_HTML_AS_STRING;
}
}
throw new UnsupportedFlavorException(flavor);
}
/**
* Gets any current text on the clipboard
*
* @return the text on the clipboard; null if no text is on the clipboard
* @throws Exception if there are any issues copying from the clipboard
*/
public String getClipboardText() throws Exception {
Clipboard c = GClipboard.getSystemClipboard();
Transferable t = c.getContents(null);
try {
String text = (String) t.getTransferData(DataFlavor.stringFlavor);
return text;
}
catch (UnsupportedFlavorException e) {
Msg.error(this, "Unsupported data flavor - 'string'. Supported flavors: ");
DataFlavor[] flavors = t.getTransferDataFlavors();
for (DataFlavor dataFlavor : flavors) {
Msg.error(this, "\t" + dataFlavor.getHumanPresentableName());
}
throw e;
}
}
private String getBestCharsetForTextFormat(Long lFormat,
Transferable localeTransferable) throws IOException
{
String charset = null;
if (localeTransferable != null &&
isLocaleDependentTextFormat(lFormat) &&
localeTransferable.isDataFlavorSupported(javaTextEncodingFlavor))
{
try {
charset = new String(
(byte[])localeTransferable.getTransferData(javaTextEncodingFlavor),
"UTF-8"
);
} catch (UnsupportedFlavorException cannotHappen) {
}
} else {
charset = getCharsetForTextFormat(lFormat);
}
if (charset == null) {
// Only happens when we have a custom text type.
charset = getDefaultTextCharset();
}
return charset;
}
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException
{
if (!isDataFlavorSupported(flavor)) {
throw new UnsupportedFlavorException(flavor);
}
Object ret = flavorsToData.get(flavor);
if (ret instanceof IOException) {
// rethrow IOExceptions generated while fetching data
throw (IOException)ret;
} else if (ret instanceof DataFactory) {
// Now we can render the data
DataFactory factory = (DataFactory)ret;
ret = factory.getTransferData(flavor);
}
return ret;
}
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException
{
if (!isDataFlavorSupported(flavor)) {
throw new UnsupportedFlavorException(flavor);
}
Object ret = flavorsToData.get(flavor);
if (ret instanceof IOException) {
// rethrow IOExceptions generated while fetching data
throw (IOException)ret;
} else if (ret instanceof DataFactory) {
// Now we can render the data
DataFactory factory = (DataFactory)ret;
ret = factory.getTransferData(flavor);
}
return ret;
}
@Override
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException {
if (isDataFlavorSupported(flavor)) {
if (flavor.equals(DataFlavor.allHtmlFlavor)) {
return ALL_HTML_AS_STRING;
} else if (flavor.equals(DataFlavor.fragmentHtmlFlavor)) {
return FRAGMENT_HTML_AS_STRING;
} else if (flavor.equals(DataFlavor.selectionHtmlFlavor)) {
return SELECTION_HTML_AS_STRING;
}
}
throw new UnsupportedFlavorException(flavor);
}
@Override
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException {
if (isDataFlavorSupported(flavor)) {
if (flavor.equals(DataFlavor.allHtmlFlavor)) {
return ALL_HTML_AS_STRING;
} else if (flavor.equals(DataFlavor.fragmentHtmlFlavor)) {
return FRAGMENT_HTML_AS_STRING;
} else if (flavor.equals(DataFlavor.selectionHtmlFlavor)) {
return SELECTION_HTML_AS_STRING;
}
}
throw new UnsupportedFlavorException(flavor);
}
@Override
public Object getTransferData(final DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (!RECORDSTORE_FLAVOR.equals(flavor)) {
throw new UnsupportedFlavorException(flavor);
}
return copy;
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (String.class.equals(flavor.getRepresentationClass())) {
return html;
} else if (Reader.class.equals(flavor.getRepresentationClass())) {
return new StringReader(html);
} else if (InputStream.class.equals(flavor.getRepresentationClass())) {
return new ByteArrayInputStream(html.getBytes(StandardCharsets.UTF_8));
}
throw new UnsupportedFlavorException(flavor);
}
/**
* Returns the content for the requested flavor, if it is supported.
*
* @param flavor the requested flavor.
*
* @return The content.
*
* @throws java.awt.datatransfer.UnsupportedFlavorException if the flavor
* is not supported.
* @throws java.io.IOException if there is an IO problem.
*/
@Override
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException {
if (this.imageFlavor.equals(flavor)) {
return createBufferedImage(this.chart, this.width, this.height,
this.minDrawWidth, this.minDrawHeight, this.maxDrawWidth,
this.maxDrawHeight);
} else {
throw new UnsupportedFlavorException(flavor);
}
}
public Object fetchTransferableDataWithoutVar(String data)
throws UnsupportedFlavorException, IOException {
StringSelection ss = new StringSelection(data);
DataFlavor[] df = ss.getTransferDataFlavors();
Object obj = ss.getTransferData(df[0]);
return obj;
}
public Object fetchTransferableDataWithVar(String data)
throws UnsupportedFlavorException, IOException {
var stringSelection = new StringSelection(data);
var dataFlavorsArray = stringSelection.getTransferDataFlavors();
var obj = stringSelection.getTransferData(dataFlavorsArray[0]);
return obj;
}
@Override
public Object getTransferData(final DataFlavor flavor) throws UnsupportedFlavorException {
if (!isDataFlavorSupported(flavor)) {
throw new UnsupportedFlavorException(flavor);
}
return transferData;
}
@Override
public Object getTransferData(final DataFlavor flavor) throws UnsupportedFlavorException {
if (!isDataFlavorSupported(flavor)) {
throw new UnsupportedFlavorException(flavor);
}
return transferData;
}
/**
*
* @param flavor
* @return the richer data flavor of this and the specified
* @throws UnsupportedFlavorException
*/
public Object getRicherData(DataFlavor flavor)
throws UnsupportedFlavorException
{
if (flavor.equals(dataFlavor))
{
return this;
}
else
{
throw new UnsupportedFlavorException(flavor);
}
}
/**
* Gets image from clipboard.
*
* @return the image from clipboard
*/
@Nullable
public static Image getImageFromClipboard() {
Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
if (transferable != null && transferable.isDataFlavorSupported(DataFlavor.imageFlavor)) {
try {
return (Image) transferable.getTransferData(DataFlavor.imageFlavor);
} catch (IOException | UnsupportedFlavorException ignored) {
// 如果 clipboard 没有图片, 不处理
}
}
return null;
}
@Test
public void test() throws IOException, UnsupportedFlavorException {
Clipboard clipboard = java.awt.Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable cc = clipboard.getContents(null);
if (cc != null && cc.isDataFlavorSupported(DataFlavor.imageFlavor)) {
Image image = (Image) cc.getTransferData(DataFlavor.imageFlavor);
// 保存图片
BufferedImage bufferedImage = ImageUtils.toBufferedImage(image);
File imageFile = new File("/Users/dong4j/Develop/", "test.png");
ImageIO.write(bufferedImage, "png", imageFile);
}
}
/**
* Test 2.
*/
@Test
public void test2() {
final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
if (clipboard.isDataFlavorAvailable(DataFlavor.imageFlavor)) {
try {
System.out.println(clipboard.getData(DataFlavor.imageFlavor));
BufferedImage im = getImage((Image) clipboard.getData(DataFlavor.imageFlavor));
System.out.println(im);
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
}
@Test
public void test4() {
final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
if (clipboard.isDataFlavorAvailable(DataFlavor.imageFlavor)) {
try {
System.out.println(clipboard.getData(DataFlavor.imageFlavor));
setImageClipboard((Image) clipboard.getData(DataFlavor.imageFlavor));
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
setImageClipboard(ImageUtils.loadImageFromFile(new File("/Users/dong4j/Downloads/我可要开始皮了.png")));
}
@Override
public void drop(DropTargetDropEvent dtde) {
if (!dtde.isDataFlavorSupported(DataFlavor.allHtmlFlavor)) {
Sysout.println("DataFlavor.allHtmlFlavor is not present in the system clipboard");
dtde.rejectDrop();
return;
} else if (!dtde.isDataFlavorSupported(DataFlavor.fragmentHtmlFlavor)) {
Sysout.println("DataFlavor.fragmentHtmlFlavor is not present in the system clipboard");
dtde.rejectDrop();
return;
} else if (!dtde.isDataFlavorSupported(DataFlavor.selectionHtmlFlavor)) {
Sysout.println("DataFlavor.selectionHtmlFlavor is not present in the system clipboard");
dtde.rejectDrop();
return;
}
dtde.acceptDrop(DnDConstants.ACTION_COPY);
Transferable t = dtde.getTransferable();
try {
Sysout.println("ALL:");
Sysout.println(t.getTransferData(DataFlavor.allHtmlFlavor).toString());
Sysout.println("FRAGMENT:");
Sysout.println(t.getTransferData(DataFlavor.fragmentHtmlFlavor).toString());
Sysout.println("SELECTION:");
Sysout.println(t.getTransferData(DataFlavor.selectionHtmlFlavor).toString());
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
/**
*
*/
private void dropHandle() {
// Handle drag & drop files into jPanel
jPanel_Device.setDropTarget(new DropTarget() {
@Override
public synchronized void drop(DropTargetDropEvent dtde) {
// disable UI
try {
// set accepted drop action
dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
// Just going to grab the expected DataFlavor to make sure
// we know what is being dropped
// Grab expected flavor
DataFlavor dragAndDropPanelFlavor = DataFlavor.javaFileListFlavor;
// What does the Transferable support
if (dtde.getTransferable().isDataFlavorSupported(dragAndDropPanelFlavor)) {
Object transferableArrayListObj = dtde.getTransferable().getTransferData(dragAndDropPanelFlavor);
if (transferableArrayListObj != null) {
if (transferableArrayListObj instanceof ArrayList) {
// copyHandle
List<MyFile> src = new ArrayList<>();
((ArrayList) transferableArrayListObj).forEach(file -> {
String srcFile = ((File) file).getAbsolutePath();
src.add(new MyFile(srcFile, 0, 0, 0));
});
// copy from local
fileBrowserLocal.onCopy(src);
// paste to device
fileBrowserDevice.onPaste();
}
}
}
} catch (UnsupportedFlavorException | IOException ex) {
Log.log(Level.SEVERE, "DropItHandle", ex);
}
// handle drop inside current panel
//super.drop(dtde);*/
}
});
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
if (flavor.equals(flavors[IMAGE])) {
return data;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (supportedFlavor.equals(flavor)) {
return list.stream()
.map(URI::toASCIIString)
.collect(StringBuilder::new,
(builder, uri)-> { builder.append(uri).append("\r\n"); },
StringBuilder::append).toString();
}
throw new UnsupportedFlavorException(flavor);
}
private java.util.List<File> extractListOfFiles(DropTargetDropEvent dtde) {
java.util.List<File> fileList = null;
try {
fileList = (java.util.List<File>)dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
return fileList;
}