下面列出了java.awt.datatransfer.Transferable#isDataFlavorSupported ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
protected String getBestCharsetForTextFormat(Long lFormat,
Transferable localeTransferable) throws IOException
{
String charset = null;
if (localeTransferable != null &&
isLocaleDependentTextFormat(lFormat) &&
localeTransferable.isDataFlavorSupported(javaTextEncodingFlavor)) {
try {
byte[] charsetNameBytes = (byte[])localeTransferable
.getTransferData(javaTextEncodingFlavor);
charset = new String(charsetNameBytes, StandardCharsets.UTF_8);
} catch (UnsupportedFlavorException cannotHappen) {
}
} else {
charset = getCharsetForTextFormat(lFormat);
}
if (charset == null) {
// Only happens when we have a custom text type.
charset = Charset.defaultCharset().name();
}
return charset;
}
private boolean handleDropImpl(Transferable t) {
try {
Object o;
if( t.isDataFlavorSupported( actionDataFlavor ) ) {
o = t.getTransferData( actionDataFlavor );
if( o instanceof Node ) {
DataObject dobj = ((Node)o).getLookup().lookup( DataObject.class );
return addButton( dobj, dropTargetButtonIndex, insertBefore );
}
} else {
o = t.getTransferData( buttonDataFlavor );
if( o instanceof DataObject ) {
return moveButton( (DataObject)o, dropTargetButtonIndex, insertBefore );
}
}
} catch( UnsupportedFlavorException e ) {
log.log( Level.INFO, null, e );
} catch( IOException ioE ) {
log.log( Level.INFO, null, ioE );
}
return false;
}
/**
* Remove a toolbar button represented by the given Transferable.
*/
private void removeButton( Transferable t ) {
try {
Object o = null;
if( t.isDataFlavorSupported(buttonDataFlavor) ) {
o = t.getTransferData(buttonDataFlavor);
}
if( null != o && o instanceof DataObject ) {
((DataObject) o).delete();
sourceToolbar.repaint();
}
} catch( UnsupportedFlavorException e ) {
log.log( Level.INFO, null, e );
} catch( IOException ioE ) {
log.log( Level.INFO, null, ioE );
}
}
public void pasteImage(ActionEvent actionEvent) {
try {
Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
if (transferable != null) {
if (transferable.isDataFlavorSupported(DataFlavor.imageFlavor)) {
java.awt.Image image = (java.awt.Image) transferable.getTransferData(DataFlavor.imageFlavor);
Image fimage = awtImageToFX(image);
ConverterApplication.getContext().addPosterIfMissingWithDelay(new ArtWorkImage(fimage));
} else if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
java.util.List<String> artFiles = (java.util.List<String>) transferable.getTransferData(DataFlavor.javaFileListFlavor);
artFiles.stream().filter(s -> ArrayUtils.contains(ArtWork.IMAGE_EXTENSIONS, FilenameUtils.getExtension(s))).forEach(f -> {
ConverterApplication.getContext().addPosterIfMissingWithDelay(new ArtWorkBean(f));
});
}
}
} catch (Exception e) {
logger.error("Failed to load from clipboard", e);
e.printStackTrace();
}
}
private static java.awt.Image getImageFromClipboard() {
Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
if (transferable != null && transferable.isDataFlavorSupported(DataFlavor.imageFlavor)) {
try {
return (java.awt.Image) transferable.getTransferData(DataFlavor.imageFlavor);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* @return if the flavor is supported
*/
public boolean isDataFlavorSupported(DataFlavor df) {
Transferable localTransferable = local;
if (localTransferable != null) {
return localTransferable.isDataFlavorSupported(df);
} else {
return DataTransferer.getInstance().getFlavorsForFormats
(currentT, DataTransferer.adaptFlavorMap
(currentDT.getFlavorMap())).
containsKey(df);
}
}
/**
* 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;
}
@Override
public byte[] translateTransferable(Transferable contents,
DataFlavor flavor,
long format) throws IOException
{
byte[] bytes = null;
if (format == CF_HTML) {
if (contents.isDataFlavorSupported(DataFlavor.selectionHtmlFlavor)) {
// if a user provides data represented by
// DataFlavor.selectionHtmlFlavor format, we use this
// type to store the data in the native clipboard
bytes = super.translateTransferable(contents,
DataFlavor.selectionHtmlFlavor,
format);
} else if (contents.isDataFlavorSupported(DataFlavor.allHtmlFlavor)) {
// if we cannot get data represented by the
// DataFlavor.selectionHtmlFlavor format
// but the DataFlavor.allHtmlFlavor format is avialable
// we belive that the user knows how to represent
// the data and how to mark up selection in a
// system specific manner. Therefor, we use this data
bytes = super.translateTransferable(contents,
DataFlavor.allHtmlFlavor,
format);
} else {
// handle other html flavor types, including custom and
// fragment ones
bytes = HTMLCodec.convertToHTMLFormat(super.translateTransferable(contents, flavor, format));
}
} else {
// we handle non-html types basing on their
// flavors
bytes = super.translateTransferable(contents, flavor, format);
}
return bytes;
}
@Override
public byte[] translateTransferable(Transferable contents,
DataFlavor flavor,
long format) throws IOException
{
byte[] bytes = null;
if (format == CF_HTML) {
if (contents.isDataFlavorSupported(DataFlavor.selectionHtmlFlavor)) {
// if a user provides data represented by
// DataFlavor.selectionHtmlFlavor format, we use this
// type to store the data in the native clipboard
bytes = super.translateTransferable(contents,
DataFlavor.selectionHtmlFlavor,
format);
} else if (contents.isDataFlavorSupported(DataFlavor.allHtmlFlavor)) {
// if we cannot get data represented by the
// DataFlavor.selectionHtmlFlavor format
// but the DataFlavor.allHtmlFlavor format is avialable
// we belive that the user knows how to represent
// the data and how to mark up selection in a
// system specific manner. Therefor, we use this data
bytes = super.translateTransferable(contents,
DataFlavor.allHtmlFlavor,
format);
} else {
// handle other html flavor types, including custom and
// fragment ones
bytes = HTMLCodec.convertToHTMLFormat(super.translateTransferable(contents, flavor, format));
}
} else {
// we handle non-html types basing on their
// flavors
bytes = super.translateTransferable(contents, flavor, format);
}
return bytes;
}
@Override
public byte[] translateTransferable(Transferable contents,
DataFlavor flavor,
long format) throws IOException
{
byte[] bytes = null;
if (format == CF_HTML) {
if (contents.isDataFlavorSupported(DataFlavor.selectionHtmlFlavor)) {
// if a user provides data represented by
// DataFlavor.selectionHtmlFlavor format, we use this
// type to store the data in the native clipboard
bytes = super.translateTransferable(contents,
DataFlavor.selectionHtmlFlavor,
format);
} else if (contents.isDataFlavorSupported(DataFlavor.allHtmlFlavor)) {
// if we cannot get data represented by the
// DataFlavor.selectionHtmlFlavor format
// but the DataFlavor.allHtmlFlavor format is avialable
// we belive that the user knows how to represent
// the data and how to mark up selection in a
// system specific manner. Therefor, we use this data
bytes = super.translateTransferable(contents,
DataFlavor.allHtmlFlavor,
format);
} else {
// handle other html flavor types, including custom and
// fragment ones
bytes = HTMLCodec.convertToHTMLFormat(super.translateTransferable(contents, flavor, format));
}
} else {
// we handle non-html types basing on their
// flavors
bytes = super.translateTransferable(contents, flavor, format);
}
return bytes;
}
/**
* @return if the flavor is supported
*/
public boolean isDataFlavorSupported(DataFlavor df) {
Transferable localTransferable = local;
if (localTransferable != null) {
return localTransferable.isDataFlavorSupported(df);
} else {
return DataTransferer.getInstance().getFlavorsForFormats
(currentT, DataTransferer.adaptFlavorMap
(currentDT.getFlavorMap())).
containsKey(df);
}
}
/**
* @see java.awt.Clipboard#isDataFlavorAvailable
* @since 1.5
*/
public boolean isDataFlavorAvailable(DataFlavor flavor) {
if (flavor == null) {
throw new NullPointerException("flavor");
}
Transferable cntnts = getContextContents();
if (cntnts != null) {
return cntnts.isDataFlavorSupported(flavor);
}
long[] formats = getClipboardFormatsOpenClose();
return formatArrayAsDataFlavorSet(formats).contains(flavor);
}
/**
* Return the file list on clipboard.
*/
public static java.util.List<File> getClipFile() {
Transferable transfer = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
if (!transfer.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
return null;
}
java.util.List<File> fileList;
try {
fileList = (java.util.List<File>) transfer.getTransferData(DataFlavor.javaFileListFlavor);
return fileList;
} catch (Exception e) {
logger.error(e.getMessage(), e);
return null;
}
}
/**
* @return if the flavor is supported
*/
public boolean isDataFlavorSupported(DataFlavor df) {
Transferable localTransferable = local;
if (localTransferable != null) {
return localTransferable.isDataFlavorSupported(df);
} else {
return DataTransferer.getInstance().getFlavorsForFormats
(currentT, DataTransferer.adaptFlavorMap
(currentDT.getFlavorMap())).
containsKey(df);
}
}
private static String getClipboardContent() {
String result = "";
final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
final Transferable contents = clipboard.getContents(null);
if (contents != null && contents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
try {
result = (String) contents.getTransferData(DataFlavor.stringFlavor);
}
catch (final UnsupportedFlavorException | IOException ex) {
}
}
return result;
}
/**
* @see java.awt.Clipboard#isDataFlavorAvailable
* @since 1.5
*/
public boolean isDataFlavorAvailable(DataFlavor flavor) {
if (flavor == null) {
throw new NullPointerException("flavor");
}
Transferable cntnts = getContextContents();
if (cntnts != null) {
return cntnts.isDataFlavorSupported(flavor);
}
long[] formats = getClipboardFormatsOpenClose();
return formatArrayAsDataFlavorSet(formats).contains(flavor);
}
@Override
public BiConsumer<Graph, DropInfo> drop(final DropTargetDropEvent dtde) {
final Transferable transferable = dtde.getTransferable();
if (transferable.isDataFlavorSupported(VX_DATA_FLAVOR) || transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
try {
final String data;
if (transferable.isDataFlavorSupported(VX_DATA_FLAVOR)) {
final InputStream in = new ByteArrayInputStream(((ByteBuffer) transferable.getTransferData(VX_DATA_FLAVOR)).array());
final ObjectInputStream oin = new ObjectInputStream(in);
data = (String) oin.readObject();
} else {
final String t = (String) transferable.getTransferData(DataFlavor.stringFlavor);
// Do we have the correct indicator?
if (t != null && t.startsWith(INDICATOR)) {
// Skip the leading "indicator=".
data = t.substring(INDICATOR.length());
} else {
data = null;
}
}
if (data != null) {
return (graph, dropInfo) -> {
PluginExecution.withPlugin(new SimpleEditPlugin("Drag and Drop: Type to Vertex") {
@Override
public void edit(final GraphWriteMethods wg, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
final int ix = data.indexOf(':');
if (ix != -1) {
final String attrLabel = data.substring(0, ix);
final String value = data.substring(ix + 1);
final int attrId = wg.getAttribute(GraphElementType.VERTEX, attrLabel);
if (attrId != Graph.NOT_FOUND) {
final int vxId;
if (dropInfo.isIsVertex()) {
vxId = dropInfo.id;
// If the vertex is selected, modify all of the selected vertices.
// Otherwise, just modify the dropped-on vertex.
final int selectedId = VisualConcept.VertexAttribute.SELECTED.ensure(wg);
if (wg.getBooleanValue(selectedId, vxId)) {
final GraphIndexResult gir = GraphIndexUtilities.filterElements(wg, selectedId, true);
while (true) {
final int selVxId = gir.getNextElement();
if (selVxId == Graph.NOT_FOUND) {
break;
}
wg.setStringValue(attrId, selVxId, data);
if (graph.getSchema() != null) {
graph.getSchema().completeVertex(wg, selVxId);
}
}
} else {
wg.setStringValue(attrId, vxId, data);
if (graph.getSchema() != null) {
graph.getSchema().completeVertex(wg, vxId);
}
}
} else {
vxId = wg.addVertex();
final int xId = VisualConcept.VertexAttribute.X.ensure(wg);
final int yId = VisualConcept.VertexAttribute.Y.ensure(wg);
final int zId = VisualConcept.VertexAttribute.Z.ensure(wg);
wg.setStringValue(attrId, vxId, value);
wg.setFloatValue(xId, vxId, dropInfo.location.getX());
wg.setFloatValue(yId, vxId, dropInfo.location.getY());
wg.setFloatValue(zId, vxId, dropInfo.location.getZ());
if (graph.getSchema() != null) {
graph.getSchema().newVertex(wg, vxId);
graph.getSchema().completeVertex(wg, vxId);
}
}
ConstellationLoggerHelper.importPropertyBuilder(
this,
Arrays.asList(data),
null,
ConstellationLoggerHelper.SUCCESS
);
}
}
}
}).executeLater(graph);
};
}
} catch (final UnsupportedFlavorException | IOException | ClassNotFoundException ex) {
Exceptions.printStackTrace(ex);
}
}
return null;
}
/**
* Perform the drop operation and add the dragged item into the given category.
*
* @param targetCategory Lookup of the category that accepts the drop.
* @param item Transferable holding the item being dragged.
* @param dndAction Drag'n'drop action type.
* @param dropIndex Zero-based position where the dragged item should be dropped.
*
* @return True if the drop has been successful, false otherwise.
*/
public boolean doDrop( Lookup targetCategory, Transferable item, int dndAction, int dropIndex ) {
Node categoryNode = (Node)targetCategory.lookup( Node.class );
try {
//first check if we're reordering items within the same category
if( item.isDataFlavorSupported( PaletteController.ITEM_DATA_FLAVOR ) ) {
Lookup itemLookup = (Lookup)item.getTransferData( PaletteController.ITEM_DATA_FLAVOR );
if( null != itemLookup ) {
Node itemNode = (Node)itemLookup.lookup( Node.class );
if( null != itemNode ) {
Index order = (Index)categoryNode.getCookie( Index.class );
if( null != order && order.indexOf( itemNode ) >= 0 ) {
//the drop item comes from the targetCategory so let's
//just change the order of items
return moveItem( targetCategory, itemLookup, dropIndex );
}
}
}
}
PasteType paste = categoryNode.getDropType( item, dndAction, dropIndex );
if( null != paste ) {
Node[] itemsBefore = categoryNode.getChildren().getNodes( DefaultModel.canBlock() );
paste.paste();
Node[] itemsAfter = categoryNode.getChildren().getNodes( DefaultModel.canBlock() );
if( itemsAfter.length == itemsBefore.length+1 ) {
int currentIndex = -1;
Node newItem = null;
for( int i=itemsAfter.length-1; i>=0; i-- ) {
newItem = itemsAfter[i];
currentIndex = i;
for( int j=0; j<itemsBefore.length; j++ ) {
if( newItem.equals( itemsBefore[j] ) ) {
newItem = null;
break;
}
}
if( null != newItem ) {
break;
}
}
if( null != newItem && dropIndex >= 0 ) {
if( currentIndex < dropIndex )
dropIndex++;
moveItem( targetCategory, newItem.getLookup(), dropIndex );
}
}
return true;
}
if( isTextDnDEnabled && null != DataFlavor.selectBestTextFlavor(item.getTransferDataFlavors()) ) {
importTextIntoPalette( targetCategory, item, dropIndex );
return false; //return false to retain the original dragged text in its source
}
} catch( IOException ioE ) {
Logger.getLogger( DragAndDropHandler.class.getName() ).log( Level.INFO, null, ioE );
} catch( UnsupportedFlavorException e ) {
Logger.getLogger( DragAndDropHandler.class.getName() ).log( Level.INFO, null, e );
}
return false;
}
public boolean canPaste() {
Clipboard clip = table.getToolkit().getSystemClipboard();
Transferable xfer = clip.getContents(this);
return xfer.isDataFlavorSupported(binaryFlavor);
}