下面列出了java.awt.datatransfer.DataFlavor#getReaderForText ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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();
}
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;
}
}
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()");
}
}
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);
}
}
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;
}
}