Search in sources :

Example 51 with DataFlavor

use of java.awt.datatransfer.DataFlavor in project vcell by virtualcell.

the class VirtualFrapBatchRunFrame method drop.

public void drop(DropTargetDropEvent dtde) {
    try {
        // get the dropped object and try to figure out what it is
        Transferable tr = dtde.getTransferable();
        DataFlavor[] flavors = tr.getTransferDataFlavors();
        boolean bFileSelected = false;
        if (flavors.length > 0) {
            for (DataFlavor flavor : flavors) {
                // Check for file lists specifically
                if (flavor.isFlavorJavaFileListType()) {
                    // Accept copy and move drops...
                    dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                    @SuppressWarnings("unchecked") List<File> tData = (List<File>) tr.getTransferData(flavor);
                    for (Object dataElement : tData) {
                        if (dataElement instanceof File) {
                            File inputFile = (File) dataElement;
                            // check file extension, we'll open only .vfrap
                            if (VirtualFrapLoader.filter_vfbatch.accept(inputFile)) {
                                bFileSelected = true;
                                AsynchClientTask[] openTasks = open(inputFile);
                                ClientTaskDispatcher.dispatch(VirtualFrapBatchRunFrame.this, new Hashtable<String, Object>(), openTasks, true);
                            } else {
                                String msg = "Unkown file type of " + inputFile.getName() + ". Virtual FRAP Batchrun document names must have an extension of \'." + VirtualFrapLoader.VFRAP_BATCH_EXTENSION + "\'.";
                                DialogUtils.showErrorDialog(this, msg);
                                return;
                            }
                        }
                        if (bFileSelected) {
                            break;
                        }
                    }
                }
                // If we made it this far, everything worked.
                dtde.dropComplete(true);
                return;
            // other cases can be....
            // else if (flavor.isFlavorSerializedObjectType()) {
            // java object
            // }
            // else if (flavor.isRepresentationClassInputStream()) {
            // input stream
            // }
            }
        }
        // the user must not have dropped a file list
        System.out.println("Drop failed: " + dtde);
        dtde.rejectDrop();
    } catch (Exception e) {
        e.printStackTrace(System.out);
        dtde.rejectDrop();
    }
}
Also used : AsynchClientTask(cbit.vcell.client.task.AsynchClientTask) Transferable(java.awt.datatransfer.Transferable) UserCancelException(org.vcell.util.UserCancelException) DataFlavor(java.awt.datatransfer.DataFlavor) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File)

Example 52 with DataFlavor

use of java.awt.datatransfer.DataFlavor in project vcell by virtualcell.

the class DropTest2 method drop.

public void drop(DropTargetDropEvent dtde) {
    try {
        // Ok, get the dropped object and try to figure out what it is
        Transferable tr = dtde.getTransferable();
        DataFlavor[] flavors = tr.getTransferDataFlavors();
        for (int i = 0; i < flavors.length; i++) {
            System.out.println("Possible flavor: " + flavors[i].getMimeType());
            // Check for file lists specifically
            if (flavors[i].isFlavorJavaFileListType()) {
                // Great!  Accept copy drops...
                dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                ta.setText("Successful file list drop.\n\n");
                // And add the list of file names to our text area
                @SuppressWarnings("unchecked") List<File> list = (List<File>) tr.getTransferData(flavors[i]);
                for (int j = 0; j < list.size(); j++) {
                    ta.append(list.get(j) + "\n");
                }
                // If we made it this far, everything worked.
                dtde.dropComplete(true);
                return;
            } else // Ok, is it another Java object?
            if (flavors[i].isFlavorSerializedObjectType()) {
                dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                ta.setText("Successful text drop.\n\n");
                Object o = tr.getTransferData(flavors[i]);
                ta.append("Object: " + o);
                dtde.dropComplete(true);
                return;
            } else // How about an input stream?
            if (flavors[i].isRepresentationClassInputStream()) {
                dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                ta.setText("Successful text drop.\n\n");
                ta.read(new InputStreamReader((InputStream) tr.getTransferData(flavors[i])), "from system clipboard");
                dtde.dropComplete(true);
                return;
            }
        }
        // Hmm, the user must not have dropped a file list
        System.out.println("Drop failed: " + dtde);
        dtde.rejectDrop();
    } catch (Exception e) {
        e.printStackTrace();
        dtde.rejectDrop();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Transferable(java.awt.datatransfer.Transferable) List(java.util.List) File(java.io.File) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 53 with DataFlavor

use of java.awt.datatransfer.DataFlavor in project vcell by virtualcell.

the class VirtualFrapMainFrame method drop.

public void drop(DropTargetDropEvent dtde) {
    try {
        // get the dropped object and try to figure out what it is
        Transferable tr = dtde.getTransferable();
        DataFlavor[] flavors = tr.getTransferDataFlavors();
        boolean bFileSelected = false;
        if (flavors.length > 0) {
            for (DataFlavor flavor : flavors) {
                // Check for file lists specifically
                if (flavor.isFlavorJavaFileListType()) {
                    // Accept copy and move drops...
                    dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                    @SuppressWarnings("unchecked") List<File> tData = (List<File>) tr.getTransferData(flavor);
                    for (Object dataElement : tData) {
                        if (dataElement instanceof File) {
                            File inputFile = (File) dataElement;
                            // check file extension, we'll open only .vfrap
                            if (VirtualFrapLoader.filter_vfrap.accept(inputFile)) {
                                bFileSelected = true;
                                AsynchClientTask[] openTasks = frapStudyPanel.open(inputFile);
                                ClientTaskDispatcher.dispatch(VirtualFrapMainFrame.this, new Hashtable<String, Object>(), openTasks, true);
                            } else {
                                String msg = "Unkown file type of " + inputFile.getName() + ". Virtual FRAP document names must have an extension of \'." + VirtualFrapLoader.VFRAP_EXTENSION + "\'.";
                                DialogUtils.showErrorDialog(this, msg);
                                return;
                            }
                        }
                        if (bFileSelected) {
                            break;
                        }
                    }
                }
                // If we made it this far, everything worked.
                dtde.dropComplete(true);
                return;
            // other cases can be....
            // else if (flavor.isFlavorSerializedObjectType()) {
            // java object
            // }
            // else if (flavor.isRepresentationClassInputStream()) {
            // input stream
            // }
            }
        }
        // the user must not have dropped a file list
        System.out.println("Drop failed: " + dtde);
        dtde.rejectDrop();
    } catch (Exception e) {
        e.printStackTrace(System.out);
        dtde.rejectDrop();
    }
}
Also used : AsynchClientTask(cbit.vcell.client.task.AsynchClientTask) Transferable(java.awt.datatransfer.Transferable) IOException(java.io.IOException) DataFlavor(java.awt.datatransfer.DataFlavor) List(java.util.List) File(java.io.File)

Aggregations

DataFlavor (java.awt.datatransfer.DataFlavor)53 Transferable (java.awt.datatransfer.Transferable)13 IOException (java.io.IOException)13 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)11 List (java.util.List)11 JPanel (javax.swing.JPanel)11 JLabel (javax.swing.JLabel)9 ArrayList (java.util.ArrayList)8 Point (java.awt.Point)7 Clipboard (java.awt.datatransfer.Clipboard)6 File (java.io.File)6 NamedIcon (jmri.jmrit.catalog.NamedIcon)6 URL (java.net.URL)4 DragJLabel (jmri.jmrit.catalog.DragJLabel)4 InputStream (java.io.InputStream)3 AsynchClientTask (cbit.vcell.client.task.AsynchClientTask)2 BorderLayout (java.awt.BorderLayout)2 GraphicsConfiguration (java.awt.GraphicsConfiguration)2 GridBagConstraints (java.awt.GridBagConstraints)2 GridBagLayout (java.awt.GridBagLayout)2