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

The following examples show how to use java.awt.datatransfer.DataFlavor#getReaderForText() . 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
private String extractText( Transferable t, DataFlavor flavor ) 
        throws IOException, UnsupportedFlavorException {
    
    Reader reader = flavor.getReaderForText(t);
    if( null == reader )
        return null;
    StringBuffer res = new StringBuffer();
    char[] buffer = new char[4*1024];
    int len;
    while( (len=reader.read( buffer )) > 0 ) {
        res.append(buffer, 0, len);
    }
    
    return res.toString();
}
 
Example 2
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 3
Source File: ReaderForUnicodeText.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  DataFlavor df = DataFlavor.plainTextFlavor;
  TextTransferable t = new TextTransferable();
  Reader reader;
  try {
      reader = df.getReaderForText(t);
  } catch (UnsupportedEncodingException e) {
       throw new RuntimeException("FAILED: Exception thrown in getReaderForText()");
  }
}
 
Example 4
Source File: GetReaderForTextIAEForStringSelectionTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  DataFlavor pt ;

  try {
      pt = DataFlavor.plainTextFlavor;
      StringSelection ss = new StringSelection("ReaderExample");
      Reader re = pt.getReaderForText(ss);
      if(re == null) {
          throw new RuntimeException("Test FAILED! reader==null");
      }
  } catch (Exception e) {
      throw new RuntimeException("Test FAILED because of the exception: " + e);
  }
}
 
Example 5
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;
    }
}