List of usage examples for java.awt.datatransfer DataFlavor getHumanPresentableName
public String getHumanPresentableName()
From source file:EditorDropTarget3.java
protected boolean dropContent(Transferable transferable, DropTargetDropEvent dtde) { if (!pane.isEditable()) { // Can't drop content on a read-only text control return false; }/*from w w w. j a va 2s .co m*/ try { // Check for a match with the current content type DataFlavor[] flavors = dtde.getCurrentDataFlavors(); DataFlavor selectedFlavor = null; // Look for either plain text or a String. for (int i = 0; i < flavors.length; i++) { DataFlavor flavor = flavors[i]; DnDUtils.debugPrintln("Drop MIME type " + flavor.getMimeType() + " is available"); if (flavor.equals(DataFlavor.plainTextFlavor) || flavor.equals(DataFlavor.stringFlavor)) { selectedFlavor = flavor; break; } } if (selectedFlavor == null) { // No compatible flavor - should never happen return false; } DnDUtils.debugPrintln("Selected flavor is " + selectedFlavor.getHumanPresentableName()); // Get the transferable and then obtain the data Object data = transferable.getTransferData(selectedFlavor); DnDUtils.debugPrintln("Transfer data type is " + data.getClass().getName()); String insertData = null; if (data instanceof InputStream) { // Plain text flavor String charSet = selectedFlavor.getParameter("charset"); InputStream is = (InputStream) data; byte[] bytes = new byte[is.available()]; is.read(bytes); try { insertData = new String(bytes, charSet); } catch (UnsupportedEncodingException e) { // Use the platform default encoding insertData = new String(bytes); } } else if (data instanceof String) { // String flavor insertData = (String) data; } if (insertData != null) { int selectionStart = pane.getCaretPosition(); pane.replaceSelection(insertData); pane.select(selectionStart, selectionStart + insertData.length()); return true; } return false; } catch (Exception e) { return false; } }
From source file:net.sf.nmedit.nomad.core.Nomad.java
public void export() { Document doc = getDocumentManager().getSelection(); if (!(doc instanceof Transferable)) return;//from w w w. ja v a 2 s .c om Transferable transferable = (Transferable) doc; String title = doc.getTitle(); if (title == null) title = "Export"; else title = "Export '" + title + "'"; JComboBox src = new JComboBox(transferable.getTransferDataFlavors()); src.setRenderer(new DefaultListCellRenderer() { /** * */ private static final long serialVersionUID = -4553255745845039428L; public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { String text; if (value instanceof DataFlavor) { DataFlavor flavor = (DataFlavor) value; String mimeType = flavor.getMimeType(); String humanRep = flavor.getHumanPresentableName(); String charset = flavor.getParameter("charset"); if (mimeType == null) text = "?"; else { text = mimeType; int ix = text.indexOf(';'); if (ix >= 0) text = text.substring(0, ix).trim(); } if (charset != null) text += "; charset=" + charset; if (humanRep != null) text += " (" + humanRep + ")"; } else { text = String.valueOf(value); } return super.getListCellRendererComponent(list, text, index, isSelected, cellHasFocus); } }); JComboBox dst = new JComboBox(new Object[] { "File", "Clipboard" }); Object[] msg = { "Source:", doc.getTitle(), "Export as:", src, "Export to:", dst }; Object[] options = { "Ok", "Cancel" }; JOptionPane op = new JOptionPane(msg, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, options); JDialog dialog = op.createDialog(getWindow(), title); dialog.setModal(true); dialog.setVisible(true); boolean ok = "Ok".equals(op.getValue()); DataFlavor flavor = (DataFlavor) src.getSelectedItem(); dialog.dispose(); if (!ok) return; if (flavor == null) return; if ("Clipboard".equals(dst.getSelectedItem())) { Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard(); cb.setContents(new SelectedTransfer(flavor, transferable), null); } else { export(transferable, flavor); } }
From source file:EditorDropTarget4.java
protected boolean dropContent(Transferable transferable, DropTargetDropEvent dtde) { if (!pane.isEditable()) { // Can't drop content on a read-only text control return false; }//from ww w . j av a 2s.c o m try { // Check for a match with the current content type DataFlavor[] flavors = dtde.getCurrentDataFlavors(); DataFlavor selectedFlavor = null; // Look for either plain text or a String. for (int i = 0; i < flavors.length; i++) { DataFlavor flavor = flavors[i]; DnDUtils.debugPrintln("Drop MIME type " + flavor.getMimeType() + " is available"); if (flavor.equals(DataFlavor.plainTextFlavor) || flavor.equals(DataFlavor.stringFlavor)) { selectedFlavor = flavor; break; } } if (selectedFlavor == null) { // No compatible flavor - should never happen return false; } DnDUtils.debugPrintln("Selected flavor is " + selectedFlavor.getHumanPresentableName()); // Get the transferable and then obtain the data Object data = transferable.getTransferData(selectedFlavor); DnDUtils.debugPrintln("Transfer data type is " + data.getClass().getName()); String insertData = null; if (data instanceof InputStream) { // Plain text flavor String charSet = selectedFlavor.getParameter("charset"); InputStream is = (InputStream) data; byte[] bytes = new byte[is.available()]; is.read(bytes); try { insertData = new String(bytes, charSet); } catch (UnsupportedEncodingException e) { // Use the platform default encoding insertData = new String(bytes); } } else if (data instanceof String) { // String flavor insertData = (String) data; } if (insertData != null) { int selectionStart = pane.getCaretPosition(); pane.replaceSelection(insertData); pane.select(selectionStart, selectionStart + insertData.length()); return true; } return false; } catch (Exception e) { return false; } }
From source file:zsk.JFCMainClient.java
/** * processing event of dropping a HTTP URL, YT-Video Image or plain text (URL) onto the frame * //from w w w.j a v a2 s .c om * seems not to work with M$-IE (8,9) - what a pity! */ public void drop(DropTargetDropEvent dtde) { Transferable tr = dtde.getTransferable(); DataFlavor[] flavors = tr.getTransferDataFlavors(); DataFlavor fl = null; String str = ""; debugoutput("DataFlavors found: ".concat(Integer.toString(flavors.length))); for (int i = 0; i < flavors.length; i++) { fl = flavors[i]; if (fl.isFlavorTextType() /* || fl.isMimeTypeEqual("text/html") || fl.isMimeTypeEqual("application/x-java-url") || fl.isMimeTypeEqual("text/uri-list")*/) { try { dtde.acceptDrop(dtde.getDropAction()); } catch (Throwable t) { } try { if (tr.getTransferData(fl) instanceof InputStreamReader) { debugoutput("Text-InputStream"); BufferedReader textreader = new BufferedReader((Reader) tr.getTransferData(fl)); String sline = ""; try { while (sline != null) { sline = textreader.readLine(); if (sline != null) str += sline; } } catch (Exception e) { } finally { textreader.close(); } str = str.replaceAll("<[^>]*>", ""); // remove HTML tags, especially a hrefs - ignore HTML characters like ß (which are no tags) } else if (tr.getTransferData(fl) instanceof InputStream) { debugoutput("Byte-InputStream"); InputStream input = new BufferedInputStream((InputStream) tr.getTransferData(fl)); int idata = input.read(); String sresult = ""; while (idata != -1) { if (idata != 0) sresult += new Character((char) idata).toString(); idata = input.read(); } // while debugoutput("sresult: ".concat(sresult)); } else { str = tr.getTransferData(fl).toString(); } } catch (IOException ioe) { } catch (UnsupportedFlavorException ufe) { } debugoutput("drop event text: ".concat(str).concat(" (").concat(fl.getMimeType()).concat(") ")); // insert text into textfield - almost the same as user drops text/url into this field // except special characaters -> from http://de.wikipedia.org/wiki/GNU-Projekt (GNU is not Unix)(„GNU is not Unix“) // two drops from same source .. one time in textfield and elsewhere - maybe we change that later?! if (str.matches(szYTREGEX.concat("(.*)"))) { synchronized (JFCMainClient.frame.textinputfield) { JFCMainClient.frame.textinputfield .setText(str.concat(JFCMainClient.frame.textinputfield.getText())); } debugoutput("breaking for-loop with str: ".concat(str)); break; } } else { String sv = "drop event unknown type: ".concat(fl.getHumanPresentableName()); //output(sv); debugoutput(sv); } } // for dtde.dropComplete(true); }