Java Code Examples for java.awt.datatransfer.UnsupportedFlavorException

The following examples show how to use java.awt.datatransfer.UnsupportedFlavorException. These examples are extracted from open source projects. 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
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();
  }
}
 
Example 2
Source Project: constellation   Source File: PasteFromClipboardPlugin.java    License: Apache License 2.0 6 votes vote down vote up
@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);
    }

}
 
Example 3
Source Project: markdown-image-kit   Source File: ImageUtils.java    License: MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example 4
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;
}
 
Example 5
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;
}
 
Example 6
Source Project: openjdk-jdk8u   Source File: DataTransferer.java    License: GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 7
@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);
}
 
Example 8
Source Project: ghidra   Source File: AbstractDockingTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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;
	}
}
 
Example 9
Source Project: TencentKona-8   Source File: DataTransferer.java    License: GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 10
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;
}
 
Example 11
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;
}
 
Example 12
Source Project: jdk8u60   Source File: HtmlTransferable.java    License: GNU General Public License v2.0 6 votes vote down vote up
@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);
}
 
Example 13
@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);
}
 
Example 14
Source Project: constellation   Source File: RecordStoreTransferable.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Object getTransferData(final DataFlavor flavor) throws UnsupportedFlavorException, IOException {
    if (!RECORDSTORE_FLAVOR.equals(flavor)) {
        throw new UnsupportedFlavorException(flavor);
    }

    return copy;
}
 
Example 15
Source Project: constellation   Source File: CopyDataToClipboard.java    License: Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 16
Source Project: openstock   Source File: ChartTransferable.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 17
Source Project: Java-Coding-Problems   Source File: Main.java    License: MIT License 5 votes vote down vote up
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;
}
 
Example 18
Source Project: Java-Coding-Problems   Source File: Main.java    License: MIT License 5 votes vote down vote up
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;
}
 
Example 19
Source Project: darklaf   Source File: TabbedPaneTransferHandler.java    License: MIT License 5 votes vote down vote up
@Override
public Object getTransferData(final DataFlavor flavor) throws UnsupportedFlavorException {
    if (!isDataFlavorSupported(flavor)) {
        throw new UnsupportedFlavorException(flavor);
    }
    return transferData;
}
 
Example 20
Source Project: darklaf   Source File: TabFrameTransferHandler.java    License: MIT License 5 votes vote down vote up
@Override
public Object getTransferData(final DataFlavor flavor) throws UnsupportedFlavorException {
    if (!isDataFlavorSupported(flavor)) {
        throw new UnsupportedFlavorException(flavor);
    }
    return transferData;
}
 
Example 21
Source Project: blog-codes   Source File: mxGraphTransferable.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @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);
	}
}
 
Example 22
Source Project: markdown-image-kit   Source File: ImageUtils.java    License: MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example 23
Source Project: markdown-image-kit   Source File: PasteImageActionTest.java    License: MIT License 5 votes vote down vote up
@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);
    }
}
 
Example 24
Source Project: markdown-image-kit   Source File: ImageUtilsTest.java    License: MIT License 5 votes vote down vote up
/**
 * 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();
        }
    }
}
 
Example 25
Source Project: markdown-image-kit   Source File: ImageUtilsTest.java    License: MIT License 5 votes vote down vote up
@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")));
}
 
Example 26
@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();
    }

}
 
Example 27
Source Project: MobyDroid   Source File: JPanel_FileManager.java    License: Apache License 2.0 5 votes vote down vote up
/**
 *
 */
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);*/
        }
    });
}
 
Example 28
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
    if (flavor.equals(flavors[IMAGE])) {
        return data;
    } else {
        throw new UnsupportedFlavorException(flavor);
    }
}
 
Example 29
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);
}
 
Example 30
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;
}