java.awt.datatransfer.UnsupportedFlavorException Java Examples

The following examples show how to use java.awt.datatransfer.UnsupportedFlavorException. 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: HtmlTransferable.java    From dragonwell8_jdk with 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 #2
Source File: ImageUtils.java    From markdown-image-kit with 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 #3
Source File: DataTransferer.java    From dragonwell8_jdk with 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 #4
Source File: HtmlTransferable.java    From jdk8u60 with 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 #5
Source File: ClipboardTransferable.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
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 File: ClipboardTransferable.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
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 #7
Source File: AbstractDockingTest.java    From ghidra with 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 #8
Source File: ClipboardTransferable.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
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 #9
Source File: DataTransferer.java    From openjdk-jdk8u with 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
Source File: DataTransferer.java    From TencentKona-8 with 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 #11
Source File: PasteFromClipboardPlugin.java    From constellation with 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 #12
Source File: HtmlTransferable.java    From openjdk-jdk8u with 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
Source File: NavigatedPanel.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 #14
Source File: URIListTransferable.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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 #15
Source File: bug8024061.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object getTransferData(DataFlavor flavor)
        throws UnsupportedFlavorException, IOException {
    if (isDataFlavorSupported(flavor)) {
        return this;
    } else {
        throw new UnsupportedFlavorException(flavor);
    }
}
 
Example #16
Source File: ImageTransferTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
    if (flavor.equals(flavors[IMAGE])) {
        return data;
    } else {
        throw new UnsupportedFlavorException(flavor);
    }
}
 
Example #17
Source File: URIListTransferable.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
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 #18
Source File: TransferableTreeNode.java    From radiance with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Object getTransferData(DataFlavor flavor)
		throws UnsupportedFlavorException, IOException {
	if (flavor.equals(TransferableTreeNode.javaJVMLocalObjectFlavor)) {
		return this;
	} else if (flavor.equals(DataFlavor.stringFlavor)) {
		return this.getSourceNode().toString();
	} else
		throw new UnsupportedFlavorException(flavor);
}
 
Example #19
Source File: GuiAdd.java    From LiquidBounce with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void actionPerformed(GuiButton button) throws IOException {
    if (!button.enabled)
        return;

    switch(button.id) {
        case 0:
            mc.displayGuiScreen(prevGui);
            break;
        case 1:
            if (LiquidBounce.fileManager.accountsConfig.accountExists(username.getText())) {
                status = "§cThe account has already been added.";
                break;
            }

            addAccount(username.getText(), password.getText());
            break;
        case 2:
            try{
                final String clipboardData = (String) Toolkit.getDefaultToolkit().getSystemClipboard()
                        .getData(DataFlavor.stringFlavor);
                final String[] accountData = clipboardData.split(":", 2);

                if (!clipboardData.contains(":") || accountData.length != 2) {
                    status = "§cInvalid clipboard data. (Use: E-Mail:Password)";
                    return;
                }

                addAccount(accountData[0], accountData[1]);
            }catch(final UnsupportedFlavorException e) {
                status = "§cClipboard flavor unsupported!";
                ClientUtils.getLogger().error("Failed to read data from clipboard.", e);
            }
            break;
    }

    super.actionPerformed(button);
}
 
Example #20
Source File: JModPanelCheckBoxList.java    From ModTheSpire with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public boolean importData(TransferSupport info) {
    TransferHandler.DropLocation tdl = info.getDropLocation();
    if (!canImport(info) || !(tdl instanceof JList.DropLocation)) {
        return false;
    }

    JList.DropLocation dl = (JList.DropLocation) tdl;
    @SuppressWarnings("rawtypes")
    JList target = (JList) info.getComponent();
    @SuppressWarnings("rawtypes")
    DefaultListModel listModel = (DefaultListModel) target.getModel();
    int max = listModel.getSize();
    int index = dl.getIndex();
    index = index < 0 ? max : index; // If it is out of range, it is
                                        // appended to the end
    index = Math.min(index, max);

    addIndex = index;

    try {
        Object[] values = (Object[]) info.getTransferable().getTransferData(localObjectFlavor);
        for (int i = 0; i < values.length; i++) {
            int idx = index++;
            ((ModPanel)values[i]).checkBox.addItemListener((event) -> {
                ((JModPanelCheckBoxList)target).publishBoxChecked();
            });
            listModel.add(idx, values[i]);
            target.addSelectionInterval(idx, idx);
        }
        addCount = values.length;
        return true;
    } catch (UnsupportedFlavorException | IOException ex) {
        ex.printStackTrace();
    }

    return false;
}
 
Example #21
Source File: TransferableProxy.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public Object getTransferData(DataFlavor df)
    throws UnsupportedFlavorException, IOException
{
    Object data = transferable.getTransferData(df);

    // If the data is a Serializable object, then create a new instance
    // before returning it. This insulates applications sharing DnD and
    // Clipboard data from each other.
    if (data != null && isLocal && df.isFlavorSerializedObjectType()) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        ClassLoaderObjectOutputStream oos =
            new ClassLoaderObjectOutputStream(baos);
        oos.writeObject(data);

        ByteArrayInputStream bais =
            new ByteArrayInputStream(baos.toByteArray());

        try {
            ClassLoaderObjectInputStream ois =
                new ClassLoaderObjectInputStream(bais,
                                                 oos.getClassLoaderMap());
            data = ois.readObject();
        } catch (ClassNotFoundException cnfe) {
            throw (IOException)new IOException().initCause(cnfe);
        }
    }

    return data;
}
 
Example #22
Source File: URIListTransferable.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
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 #23
Source File: TargetFileListFrame.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #24
Source File: DataHandler.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return the Transfer Data of type DataFlavor from InputStream.
 * @param df        the DataFlavor
 * @param ds        the DataSource
 * @return          the constructed Object
 */
public Object getTransferData(DataFlavor df, DataSource ds) throws
                            UnsupportedFlavorException, IOException {

    if (dch != null)
        return dch.getTransferData(df, ds);
    else if (df.equals(getTransferDataFlavors()[0])) // only have one now
        return ds.getInputStream();
    else
        throw new UnsupportedFlavorException(df);
}
 
Example #25
Source File: GroupTransferable.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Return the transfer data with the given data flavor.
 */
public synchronized Object getTransferData(DataFlavor f) 
    throws UnsupportedFlavorException, IOException {
        
    if (f.equals(localGroupFlavor)) {
        return group;
    }
    if (f.equals(DataFlavor.stringFlavor)) {
        return name;
    }
    throw new UnsupportedFlavorException(f);
    
}
 
Example #26
Source File: TreeTransferable.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Return the transfer data with the given data flavor.
 */
public synchronized Object getTransferData(DataFlavor f) 
    throws UnsupportedFlavorException, IOException {
        
    if (f.equals(localTreeNodeFlavor)) {
        return nodeList;
    }
    throw new UnsupportedFlavorException(f);
    
}
 
Example #27
Source File: bug8024061.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object getTransferData(DataFlavor flavor)
        throws UnsupportedFlavorException, IOException {
    if (isDataFlavorSupported(flavor)) {
        return this;
    } else {
        throw new UnsupportedFlavorException(flavor);
    }
}
 
Example #28
Source File: bug8024061.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object getTransferData(DataFlavor flavor)
        throws UnsupportedFlavorException, IOException {
    if (isDataFlavorSupported(flavor)) {
        return this;
    } else {
        throw new UnsupportedFlavorException(flavor);
    }
}
 
Example #29
Source File: TargetFileListFrame.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #30
Source File: bug8024061.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object getTransferData(DataFlavor flavor)
        throws UnsupportedFlavorException, IOException {
    if (isDataFlavorSupported(flavor)) {
        return this;
    } else {
        throw new UnsupportedFlavorException(flavor);
    }
}