Java Code Examples for java.awt.datatransfer.DataFlavor#selectBestTextFlavor()

The following examples show how to use java.awt.datatransfer.DataFlavor#selectBestTextFlavor() . 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: DragAndDropHandler.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * @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);
}
 
Example 2
Source File: DragAndDropHandler.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 3
Source File: WatchesNodeModel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example 4
Source File: SelectBestFlavorNPETest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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");
        }
    }
 
Example 5
Source File: SelectBestTextFlavorBadArrayTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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);
    }
}
 
Example 6
Source File: DragAndDropHandler.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 7
Source File: WatchesNodeModel.java    From netbeans with Apache License 2.0 4 votes vote down vote up
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;
    }
}