java.awt.datatransfer.DataFlavor#selectBestTextFlavor ( )源码实例Demo

下面列出了java.awt.datatransfer.DataFlavor#selectBestTextFlavor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: netbeans   文件: DragAndDropHandler.java
/**
 * @param targetCategory Lookup of the category under the drop cursor.
 * @param flavors Supported DataFlavors.
 * @param dndAction Drop action type.
 *
 * @return True if the given category can accept the item being dragged.
 */
public boolean canDrop( Lookup targetCategory, DataFlavor[] flavors, int dndAction ) {
    for( int i=0; i<flavors.length; i++ ) {
        if( PaletteController.ITEM_DATA_FLAVOR.equals( flavors[i] ) ) {
            return true;
        }
    }
    return (isTextDnDEnabled && DataFlavor.selectBestTextFlavor(flavors) != null);
}
 
源代码2 项目: netbeans   文件: DragAndDropHandler.java
private boolean importTextIntoPalette( Lookup targetCategory, Transferable item, int dropIndex ) 
        throws IOException, UnsupportedFlavorException {
    
    DataFlavor flavor = DataFlavor.selectBestTextFlavor( item.getTransferDataFlavors() );
    if( null == flavor )
        return false;
    
    String textToImport = extractText( item, flavor );
    SwingUtilities.invokeLater( new TextImporter( textToImport, targetCategory, dropIndex ) );
    return true;
}
 
源代码3 项目: netbeans   文件: WatchesNodeModel.java
public PasteType[] getPasteTypes(final Object node, final Transferable t) throws UnknownTypeException {
    if (node != TreeModel.ROOT && getWatch(node) == null) {
        return null;
    }
    DataFlavor[] flavors = t.getTransferDataFlavors();
    final DataFlavor textFlavor = DataFlavor.selectBestTextFlavor(flavors);
    if (textFlavor != null) {
        return new PasteType[] { new PasteType() {

            public Transferable paste() {
                try {
                    java.io.Reader r = textFlavor.getReaderForText(t);
                    java.nio.CharBuffer cb = java.nio.CharBuffer.allocate(1000);
                    r.read(cb);
                    cb.flip();
                    Watch w = getWatch(node);
                    if (w != null) {
                        w.setExpression(cb.toString());
                        //fireModelChange(new ModelEvent.NodeChanged(WatchesNodeModel.this, node));
                    } else {
                        // Root => add a new watch
                        DebuggerManager.getDebuggerManager().createWatch(cb.toString());
                    }
                } catch (Exception ex) {}
                return null;
            }
        } };
    } else {
        return null;
    }
}
 
源代码4 项目: openjdk-jdk9   文件: SelectBestFlavorNPETest.java
public static void main(String[] args) {

        DataFlavor flavor1 = new DataFlavor("text/plain; charset=unicode; class=java.io.InputStream",
                "Flavor 1");
        DataFlavor flavor2 = new DataFlavor("text/plain; class=java.io.InputStream", "Flavor 2");
        DataFlavor[] flavors = new DataFlavor[]{flavor1, flavor2};
        try {
            DataFlavor best = DataFlavor.selectBestTextFlavor(flavors);
            System.out.println("best=" + best);
        } catch (NullPointerException e1) {
            throw new RuntimeException("Test FAILED because of NPE in selectBestTextFlavor");
        }
    }
 
public static void main(String[] args) {
    final String[] failureMessages = {
        "DataFlavor.selectBestTextFlavor(null) doesn't return null.",
        "DataFlavor.selectBestTextFlavor() doesn't return null for an empty array.",
        "DataFlavor.selectBestTextFlavor() shouldn't return flavor in an unsupported encoding."
    };
    Stack<String> failures = new Stack<>();

    DataFlavor flavor = DataFlavor.selectBestTextFlavor(null);
    if (flavor != null) {
        failures.push(failureMessages[0]);
    }
    flavor = DataFlavor.selectBestTextFlavor(new DataFlavor[0]);
    if (flavor != null) {
        failures.push(failureMessages[1]);
    }

    try {
        flavor = DataFlavor.selectBestTextFlavor(new DataFlavor[]
            { new DataFlavor("text/plain; charset=unsupported; class=java.io.InputStream") });
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        failures.push("Exception thrown: " + e.toString());
    }
    if (flavor != null) {
        failures.push(failureMessages[2]);
    }

    if (failures.size() > 0) {
        String failureReport = failures.stream().collect(Collectors.joining("\n"));
        throw new RuntimeException("TEST FAILED: \n" + failureReport);
    }
}
 
源代码6 项目: netbeans   文件: DragAndDropHandler.java
/**
 * 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;
}
 
源代码7 项目: netbeans   文件: WatchesNodeModel.java
public PasteType getDropType(final Object node, final Transferable t, int action,
                             final int index) throws UnknownTypeException {
    //System.err.println("\n\ngetDropType("+node+", "+t+", "+action+", "+index+")");
    DataFlavor[] flavors = t.getTransferDataFlavors();
    final DataFlavor textFlavor = DataFlavor.selectBestTextFlavor(flavors);
    //System.err.println("Text Flavor = "+textFlavor);
    if (textFlavor != null) {
        return new PasteType() {

            public Transferable paste() {
                String watchExpression;
                try {
                    java.io.Reader r = textFlavor.getReaderForText(t);
                    java.nio.CharBuffer cb = java.nio.CharBuffer.allocate(1000);
                    r.read(cb);
                    cb.flip();
                    watchExpression = cb.toString();
                } catch (Exception ex) {
                    return t;
                }
                Watch w = getWatch(node);
                if (w != null) {
                    w.setExpression(watchExpression);
                    //fireModelChange(new ModelEvent.NodeChanged(WatchesNodeModel.this, node));
                } else {
                    // Root => add a new watch
                    if (index < 0) {
                        DebuggerManager.getDebuggerManager().createWatch(watchExpression);
                    } else {
                        DebuggerManager.getDebuggerManager().createWatch(
                                Math.min(index, DebuggerManager.getDebuggerManager().getWatches().length),
                                watchExpression);
                    }
                }
                return t;
            }
        };
    } else {
        return null;
    }
}