List of usage examples for java.awt.datatransfer DataFlavor getReaderForText
public Reader getReaderForText(Transferable transferable) throws UnsupportedFlavorException, IOException
From source file:UTest.java
public boolean importData(JComponent src, Transferable transferable) { // Ok, here's the tricky part... println("Receiving data from " + src); println("Transferable object is: " + transferable); println("Valid data flavors: "); DataFlavor[] flavors = transferable.getTransferDataFlavors(); DataFlavor listFlavor = null; DataFlavor objectFlavor = null; DataFlavor readerFlavor = null; int lastFlavor = flavors.length - 1; // Check the flavors and see if we find one we like. // If we do, save it. for (int f = 0; f <= lastFlavor; f++) { println(" " + flavors[f]); if (flavors[f].isFlavorJavaFileListType()) { listFlavor = flavors[f];//from ww w. java 2 s . c o m } if (flavors[f].isFlavorSerializedObjectType()) { objectFlavor = flavors[f]; } if (flavors[f].isRepresentationClassReader()) { readerFlavor = flavors[f]; } } // Ok, now try to display the content of the drop. try { DataFlavor bestTextFlavor = DataFlavor.selectBestTextFlavor(flavors); BufferedReader br = null; String line = null; if (bestTextFlavor != null) { println("Best text flavor: " + bestTextFlavor.getMimeType()); println("Content:"); Reader r = bestTextFlavor.getReaderForText(transferable); br = new BufferedReader(r); line = br.readLine(); while (line != null) { println(line); line = br.readLine(); } br.close(); } else if (listFlavor != null) { java.util.List list = (java.util.List) transferable.getTransferData(listFlavor); println(list); } else if (objectFlavor != null) { println("Data is a java object:\n" + transferable.getTransferData(objectFlavor)); } else if (readerFlavor != null) { println("Data is an InputStream:"); br = new BufferedReader((Reader) transferable.getTransferData(readerFlavor)); line = br.readLine(); while (line != null) { println(line); } br.close(); } else { // Don't know this flavor type yet... println("No text representation to show."); } println("\n\n"); } catch (Exception e) { println("Caught exception decoding transfer:"); println(e); return false; } return true; }
From source file:net.sf.nmedit.nomad.core.Nomad.java
private void export(Transferable transferable, DataFlavor flavor) { File file = getExportFile();/*from ww w . jav a 2 s . c o m*/ if (file == null) return; Reader reader; try { reader = flavor.getReaderForText(transferable); } catch (Exception e) { reader = null; } if (reader != null) { try { FileOutputStream out = new FileOutputStream(file); try { int data; while ((data = reader.read()) != -1) { out.write(data); } out.flush(); } finally { out.close(); } } catch (Exception e) { throw new RuntimeException(e); } return; } if (DataFlavor.imageFlavor.match(flavor)) { Image image; try { image = (Image) transferable.getTransferData(flavor); } catch (Exception e) { throw new RuntimeException(e); } try { ImageIO.write((RenderedImage) image, "png", file); } catch (IOException e) { throw new RuntimeException(e); } } // else report unsupported flavor }
From source file:org.fife.ui.rtextarea.RTATextTransferHandler.java
/** * This method causes a transfer to a component from a clipboard or a * DND drop operation. The Transferable represents the data to be * imported into the component. //from w w w . jav a 2s . co m * * @param comp The component to receive the transfer. This * argument is provided to enable sharing of TransferHandlers by * multiple components. * @param t The data to import * @return <code>true</code> iff the data was inserted into the component. */ @Override public boolean importData(JComponent comp, Transferable t) { JTextComponent c = (JTextComponent) comp; withinSameComponent = c == exportComp; // if we are importing to the same component that we exported from // then don't actually do anything if the drop location is inside // the drag location and set shouldRemove to false so that exportDone // knows not to remove any data if (withinSameComponent && c.getCaretPosition() >= p0 && c.getCaretPosition() <= p1) { shouldRemove = false; return true; } boolean imported = false; DataFlavor importFlavor = getImportFlavor(t.getTransferDataFlavors(), c); if (importFlavor != null) { try { InputContext ic = c.getInputContext(); if (ic != null) ic.endComposition(); Reader r = importFlavor.getReaderForText(t); handleReaderImport(r, c); imported = true; } catch (UnsupportedFlavorException ufe) { ufe.printStackTrace(); } catch (BadLocationException ble) { ble.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } } return imported; }
From source file:org.languagetool.gui.Main.java
private String getClipboardText() { // get text from clipboard or selection: Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemSelection(); if (clipboard == null) { // on Windows clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); }/*from www . j av a 2 s.c om*/ String s; Transferable data = clipboard.getContents(this); try { if (data != null && data.isDataFlavorSupported(DataFlavor.getTextPlainUnicodeFlavor())) { DataFlavor df = DataFlavor.getTextPlainUnicodeFlavor(); try (Reader sr = df.getReaderForText(data)) { s = StringTools.readerToString(sr); } } else { s = ""; } } catch (Exception ex) { s = data.toString(); } return s; }