List of usage examples for java.awt.datatransfer DataFlavor getMimeType
public String getMimeType()
From source file:Main.java
public static void main(String[] args) { DataFlavor df = DataFlavor.stringFlavor; System.out.println("Mime type: " + df.getMimeType()); }
From source file:MainClass.java
public static void main(String[] args) { DataFlavor df = DataFlavor.stringFlavor; System.out.println("Mime type: " + df.getMimeType()); System.out.println("Primary type: " + df.getPrimaryType()); System.out.println("Subtype: " + df.getSubType()); System.out.println("Name: " + df.getHumanPresentableName()); String s = df.getRepresentationClass().toString(); System.out.println("Representation class: " + s + "\n"); df = DataFlavor.javaFileListFlavor; System.out.println("Mime type: " + df.getMimeType()); System.out.println("Primary type: " + df.getPrimaryType()); System.out.println("Subtype: " + df.getSubType()); System.out.println("Name: " + df.getHumanPresentableName()); s = df.getRepresentationClass().toString(); System.out.println("Representation class: " + s); }
From source file:MainClass.java
public static void main(String[] args) { DataFlavor df1 = new DataFlavor("text/plain; charset=ASCII", "Plain ASCII text"); DataFlavor df2 = new DataFlavor(java.awt.Button.class, "AWT Button"); System.out.println("Mime type: " + df2.getMimeType()); System.out.println("Primary type: " + df2.getPrimaryType()); System.out.println("Subtype: " + df2.getSubType()); System.out.println("Name: " + df2.getHumanPresentableName()); String s = df2.getRepresentationClass().toString(); System.out.println("Representation class: " + s + "\n"); System.out.println("df1 equals df2: " + df1.isMimeTypeEqual(df2)); }
From source file:MainClass.java
public static void main(String[] args) { DataFlavor df = new DataFlavor("text/plain; charset=ASCII", "Plain ASCII text"); System.out.println("Mime type: " + df.getMimeType()); System.out.println("Primary type: " + df.getPrimaryType()); System.out.println("Subtype: " + df.getSubType()); System.out.println("Parameter: " + df.getParameter("charset")); System.out.println("Name: " + df.getHumanPresentableName()); String s = df.getRepresentationClass().toString(); System.out.println("Representation class: " + s + "\n"); }
From source file:net.sf.jabref.external.TransferableFileLinkSelection.java
@Override public boolean isDataFlavorSupported(DataFlavor dataFlavor) { LOGGER.debug("Query: " + dataFlavor.getHumanPresentableName() + " , " + dataFlavor.getDefaultRepresentationClass() + " , " + dataFlavor.getMimeType()); return dataFlavor.equals(DataFlavor.javaFileListFlavor) || dataFlavor.equals(DataFlavor.stringFlavor); }
From source file:EditorDropTarget2.java
protected void checkTransferType(DropTargetDragEvent dtde) { // Accept a list of files, or data content that // amounts to plain text or a Unicode text string acceptableType = false;/*from ww w . ja va 2 s. c om*/ draggingFile = false; if (DnDUtils.isDebugEnabled()) { DataFlavor[] flavors = dtde.getCurrentDataFlavors(); for (int i = 0; i < flavors.length; i++) { DataFlavor flavor = flavors[i]; DnDUtils.debugPrintln("Drop MIME type " + flavor.getMimeType() + " is available"); } } if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { acceptableType = true; draggingFile = true; } else if (dtde.isDataFlavorSupported(DataFlavor.plainTextFlavor) || dtde.isDataFlavorSupported(DataFlavor.stringFlavor)) { acceptableType = true; } DnDUtils.debugPrintln("File type acceptable - " + acceptableType); }
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];// w w w . ja v a 2 s. co 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:EditorDropTarget3.java
protected boolean dropContent(Transferable transferable, DropTargetDropEvent dtde) { if (!pane.isEditable()) { // Can't drop content on a read-only text control return false; }// w w w . j a v a 2 s. 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:EditorDropTarget4.java
protected boolean dropContent(Transferable transferable, DropTargetDropEvent dtde) { if (!pane.isEditable()) { // Can't drop content on a read-only text control return false; }//w w w . j a v a 2 s. 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;/* ww w . j a v a 2s . c o m*/ 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); } }