List of usage examples for java.awt.datatransfer Transferable getTransferDataFlavors
public DataFlavor[] getTransferDataFlavors();
From source file:ja.lingo.application.gui.drophandler.DropHandler.java
public boolean importData(JComponent c, Transferable t) { if (engine.getFinder().isEmpty()) { return false; }/* www .ja v a2 s .c o m*/ if (canImport(c, t.getTransferDataFlavors())) { try { String value = (String) t.getTransferData(DataFlavor.stringFlavor); model.navigateAndTranslate(value.trim()); model.main_showAtTop(); return true; } catch (Throwable tt) { LOG.warn("Could not drop object", tt); Messages.info(c, resources.text("couldNotDropObject")); } } return false; }
From source file:edu.ku.brc.specify.tasks.subpane.wb.ImageTransferable.java
@Override public boolean canImport(TransferSupport support) { Transferable trans = support.getTransferable(); DataFlavor[] flavors = trans.getTransferDataFlavors(); for (DataFlavor df : flavors) { if (df.getHumanPresentableName().equals(MIME_TYPE) && df.getRepresentationClass() == String.class) { return true; /*try/*from w w w. ja va 2 s . c o m*/ { String uris = (String)trans.getTransferData(df); String[] filePaths = StringUtils.split(uris, "\r\n"); for (String path : filePaths) { System.out.println("canImport2: "+filter.isImageFile(path)); return filter.isImageFile(path); } } catch (UnsupportedFlavorException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }*/ } } return false; }
From source file:edu.ku.brc.specify.tasks.subpane.wb.ImageTransferable.java
@Override public boolean importData(TransferSupport support) { Vector<File> fileList = new Vector<File>(); try {/* w ww.j a v a2 s. c om*/ Transferable trans = support.getTransferable(); DataFlavor[] flavors = trans.getTransferDataFlavors(); for (DataFlavor df : flavors) { if (df.getHumanPresentableName().equals("text/uri-list") && df.getRepresentationClass() == String.class) { String uris = (String) trans.getTransferData(df); String[] filePaths = StringUtils.split(uris, "\r\n"); for (String path : filePaths) { URI uri = URI.create(path); File f = new File(uri); if (filter.isImageFile(path)) { fileList.add(f); } } break; } } } catch (UnsupportedFlavorException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (fileList.size() > 0) { processImages(fileList); return true; } return false; }
From source file:net.sf.nmedit.patchmodifier.mutator.VariationTransferHandler.java
public boolean importData(JComponent c, Transferable t) { //System.out.println("import data"); Variation target;//from w w w . j a v a2s .c o m Vector<Integer> data = null; if (!canImport(c, t.getTransferDataFlavors())) { return false; } try { target = (Variation) c; if (t.isDataFlavorSupported(variationFlavor)) { data = ((VariationTransferData) t.getTransferData(variationFlavor)).getVariationData(); } else { return false; } } catch (UnsupportedFlavorException ufe) { if (log.isErrorEnabled()) { log.error("importData: unsupported data flavor", ufe); } return false; } catch (IOException ioe) { if (log.isErrorEnabled()) { log.error("importData: I/O exception", ioe); } return false; } target.getState().updateValues(new Vector<Integer>(data)); c.repaint(); return true; }
From source file:TreeDragTest.java
public void drop(DropTargetDropEvent dtde) { Point pt = dtde.getLocation(); DropTargetContext dtc = dtde.getDropTargetContext(); JTree tree = (JTree) dtc.getComponent(); TreePath parentpath = tree.getClosestPathForLocation(pt.x, pt.y); DefaultMutableTreeNode parent = (DefaultMutableTreeNode) parentpath.getLastPathComponent(); if (parent.isLeaf()) { dtde.rejectDrop();/* w w w . jav a2 s .c o m*/ return; } try { Transferable tr = dtde.getTransferable(); DataFlavor[] flavors = tr.getTransferDataFlavors(); for (int i = 0; i < flavors.length; i++) { if (tr.isDataFlavorSupported(flavors[i])) { dtde.acceptDrop(dtde.getDropAction()); TreePath p = (TreePath) tr.getTransferData(flavors[i]); DefaultMutableTreeNode node = (DefaultMutableTreeNode) p.getLastPathComponent(); DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); model.insertNodeInto(node, parent, 0); dtde.dropComplete(true); return; } } dtde.rejectDrop(); } catch (Exception e) { e.printStackTrace(); dtde.rejectDrop(); } }
From source file:DragColorTextFieldDemo.java
/** * Overridden to import a Color if it is available. * getChangesForegroundColor is used to determine whether the foreground or * the background color is changed.//w w w.j av a 2 s .c o m */ public boolean importData(JComponent c, Transferable t) { if (hasColorFlavor(t.getTransferDataFlavors())) { try { Color col = (Color) t.getTransferData(colorFlavor); if (getChangesForegroundColor()) { c.setForeground(col); } else { c.setBackground(col); } return true; } catch (UnsupportedFlavorException ufe) { System.out.println("importData: unsupported data flavor"); } catch (IOException ioe) { System.out.println("importData: I/O exception"); } } return false; }
From source file:ExtendedDnDDemo.java
public boolean importData(JComponent c, Transferable t) { if (canImport(c, t.getTransferDataFlavors())) { try {//from w w w . jav a 2 s . c o m String str = (String) t.getTransferData(DataFlavor.stringFlavor); importString(c, str); return true; } catch (UnsupportedFlavorException ufe) { } catch (IOException ioe) { } } return false; }
From source file:DragColorTextFieldDemo.java
public boolean importData(JComponent c, Transferable t) { JTextComponent tc = (JTextComponent) c; if (!canImport(c, t.getTransferDataFlavors())) { return false; }/*from w w w.j a va 2s . co m*/ if (tc.equals(source) && (tc.getCaretPosition() >= p0.getOffset()) && (tc.getCaretPosition() <= p1.getOffset())) { shouldRemove = false; return true; } if (hasStringFlavor(t.getTransferDataFlavors())) { try { String str = (String) t.getTransferData(stringFlavor); tc.replaceSelection(str); return true; } catch (UnsupportedFlavorException ufe) { System.out.println("importData: unsupported data flavor"); } catch (IOException ioe) { System.out.println("importData: I/O exception"); } } //The ColorTransferHandler superclass handles color. return super.importData(c, t); }
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 . j av a 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:DragListDemo.java
public boolean importData(JComponent c, Transferable t) { JList target = null;/*w ww. ja va2 s. c o m*/ ArrayList alist = null; if (!canImport(c, t.getTransferDataFlavors())) { return false; } try { target = (JList) c; if (hasLocalArrayListFlavor(t.getTransferDataFlavors())) { alist = (ArrayList) t.getTransferData(localArrayListFlavor); } else if (hasSerialArrayListFlavor(t.getTransferDataFlavors())) { alist = (ArrayList) t.getTransferData(serialArrayListFlavor); } else { return false; } } catch (UnsupportedFlavorException ufe) { System.out.println("importData: unsupported data flavor"); return false; } catch (IOException ioe) { System.out.println("importData: I/O exception"); return false; } //At this point we use the same code to retrieve the data //locally or serially. //We'll drop at the current selected index. int index = target.getSelectedIndex(); //Prevent the user from dropping data back on itself. //For example, if the user is moving items #4,#5,#6 and #7 and //attempts to insert the items after item #5, this would //be problematic when removing the original items. //This is interpreted as dropping the same data on itself //and has no effect. if (source.equals(target)) { if (indices != null && index >= indices[0] - 1 && index <= indices[indices.length - 1]) { indices = null; return true; } } DefaultListModel listModel = (DefaultListModel) target.getModel(); int max = listModel.getSize(); if (index < 0) { index = max; } else { index++; if (index > max) { index = max; } } addIndex = index; addCount = alist.size(); for (int i = 0; i < alist.size(); i++) { listModel.add(index++, alist.get(i)); } return true; }