Example usage for java.awt.datatransfer DataFlavor getParameter

List of usage examples for java.awt.datatransfer DataFlavor getParameter

Introduction

In this page you can find the example usage for java.awt.datatransfer DataFlavor getParameter.

Prototype

public String getParameter(String paramName) 

Source Link

Document

Returns the human presentable name for this DataFlavor if paramName equals "humanPresentableName".

Usage

From source file:Main.java

public static void main(String[] args) {

    DataFlavor df1 = new DataFlavor("text/plain; charset=ASCII", "Plain ASCII text");
    System.out.println(df1.getParameter(df1.getHumanPresentableName()));

}

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.nmedit.nomad.core.Nomad.java

public void export() {
    Document doc = getDocumentManager().getSelection();
    if (!(doc instanceof Transferable))
        return;/*from w ww  . j  av 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);
    }
}

From source file:EditorDropTarget2.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 ww .  jav a 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];

            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: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 www  .ja va2  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;
    }/*from   w  w w . j  av  a2s.  com*/

    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:org.simmi.GeneSetHead.java

License:asdf

public TransferHandler dragRows(final JTable table, final List<String> specs) {
    TransferHandler th = null;//from  ww w.  j a  v  a2s.c  o m
    try {
        final DataFlavor ndf = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType);
        final DataFlavor df = DataFlavor.getTextPlainUnicodeFlavor();
        final String charset = df.getParameter("charset");
        final Transferable transferable = new Transferable() {
            @Override
            public Object getTransferData(DataFlavor arg0) throws UnsupportedFlavorException, IOException {
                if (arg0.equals(ndf)) {
                    int[] rr = currentRowSelection; //table.getSelectedRows();
                    List<String> selseq = new ArrayList<String>(rr.length);
                    for (int r : rr) {
                        int i = table.convertRowIndexToModel(r);
                        selseq.add(specs.get(i));
                    }
                    return selseq;
                } else {
                    String ret = "";//makeCopyString();
                    for (int r = 0; r < table.getRowCount(); r++) {
                        Object o = table.getValueAt(r, 0);
                        if (o != null) {
                            ret += o.toString();
                        } else {
                            ret += "";
                        }
                        for (int c = 1; c < table.getColumnCount(); c++) {
                            o = table.getValueAt(r, c);
                            if (o != null) {
                                ret += "\t" + o.toString();
                            } else {
                                ret += "\t";
                            }
                        }
                        ret += "\n";
                    }
                    //return arg0.getReaderForText( this );
                    return new ByteArrayInputStream(ret.getBytes(charset));
                }
                //return ret;
            }

            @Override
            public DataFlavor[] getTransferDataFlavors() {
                return new DataFlavor[] { df, ndf };
            }

            @Override
            public boolean isDataFlavorSupported(DataFlavor arg0) {
                if (arg0.equals(df) || arg0.equals(ndf)) {
                    return true;
                }
                return false;
            }
        };

        th = new TransferHandler() {
            private static final long serialVersionUID = 1L;

            public int getSourceActions(JComponent c) {
                return TransferHandler.COPY_OR_MOVE;
            }

            public boolean canImport(TransferHandler.TransferSupport support) {
                return true;
            }

            protected Transferable createTransferable(JComponent c) {
                currentRowSelection = table.getSelectedRows();

                return transferable;
            }

            public boolean importData(TransferHandler.TransferSupport support) {
                try {
                    System.err.println(table.getSelectedRows().length);

                    DataFlavor[] dfs = support.getDataFlavors();
                    if (support.isDataFlavorSupported(ndf)) {
                        Object obj = support.getTransferable().getTransferData(ndf);
                        ArrayList<String> seqs = (ArrayList<String>) obj;

                        /*ArrayList<String> newlist = new ArrayList<String>( serifier.lgse.size() );
                        for( int r = 0; r < table.getRowCount(); r++ ) {
                           int i = table.convertRowIndexToModel(r);
                           newlist.add( specs.get(i) );
                        }
                        serifier.lgseq.clear();
                        serifier.lgseq = newlist;*/

                        Point p = support.getDropLocation().getDropPoint();
                        int k = table.rowAtPoint(p);

                        specs.removeAll(seqs);
                        for (String s : seqs) {
                            specs.add(k++, s);
                        }

                        TableRowSorter<TableModel> trs = (TableRowSorter<TableModel>) table.getRowSorter();
                        trs.setSortKeys(null);

                        table.tableChanged(new TableModelEvent(table.getModel()));

                        return true;
                    } /* else if( support.isDataFlavorSupported( df ) ) {                     
                        Object obj = support.getTransferable().getTransferData( df );
                        InputStream is = (InputStream)obj;
                                
                        System.err.println( charset );
                        importReader( new BufferedReader(new InputStreamReader(is, charset)) );
                                
                        updateView();
                                
                        return true;
                      }  else if( support.isDataFlavorSupported( DataFlavor.stringFlavor ) ) {                     
                        Object obj = support.getTransferable().getTransferData( DataFlavor.stringFlavor );
                        String str = (String)obj;
                        importReader( new BufferedReader( new StringReader(str) ) );
                                
                        updateView();
                                
                        return true;
                      }*/
                } catch (UnsupportedFlavorException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return false;
            }
        };
    } catch (Exception e) {
        e.printStackTrace();
    }
    return th;
}