List of usage examples for java.awt.datatransfer Transferable getTransferData
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException;
From source file:DnDDemo2.java
public boolean importData(JComponent component, Transferable transferable) { String colorMimeType = DataFlavor.javaJVMLocalObjectMimeType + ";class=java.awt.Color"; JTextComponent textComponent = (JTextComponent) component; try {//from ww w . j a v a 2s . c o m DataFlavor colorFlavor = new DataFlavor(colorMimeType); Color color = (Color) transferable.getTransferData(colorFlavor); String text = (String) transferable.getTransferData(DataFlavor.stringFlavor); textComponent.setForeground(color); textComponent.setText(text); } catch (Exception e) { e.printStackTrace(); } return true; }
From source file:Main.java
public void drop(DropTargetDropEvent dtde) { try {//www . ja va2 s . co m Transferable tr = dtde.getTransferable(); DataFlavor[] flavors = tr.getTransferDataFlavors(); for (int i = 0; i < flavors.length; i++) { if (flavors[i].isFlavorJavaFileListType()) { dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); List list = (List) tr.getTransferData(flavors[i]); for (int j = 0; j < list.size(); j++) { ta.append(list.get(j) + "\n"); } dtde.dropComplete(true); return; } else if (flavors[i].isFlavorSerializedObjectType()) { dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); Object o = tr.getTransferData(flavors[i]); ta.append("Object: " + o); dtde.dropComplete(true); return; } else if (flavors[i].isRepresentationClassInputStream()) { dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); ta.read(new InputStreamReader((InputStream) tr.getTransferData(flavors[i])), "from system clipboard"); dtde.dropComplete(true); return; } } dtde.rejectDrop(); } catch (Exception e) { e.printStackTrace(); dtde.rejectDrop(); } }
From source file:net.sf.jhylafax.addressbook.AbstractContactTransferHandler.java
@Override public boolean importData(JComponent component, Transferable transferable) { if (canImport(component, transferable.getTransferDataFlavors())) { try {//from w w w.ja v a 2s . c o m ContactIOFactory factory = Pim.getContactIOFactory(); ContactUnmarshaller unmarshaller = factory.createContactUnmarshaller(); InputStream in = (InputStream) transferable.getTransferData(ContactTransferable.VCARD_FLAVOR); unmarshaller.setEncoding("UTF-8"); Contact[] contacts = unmarshaller.unmarshallContacts(in); if (contacts != null && contacts.length > 0) { importData(contacts); } return true; } catch (Exception e) { logger.debug("Error during import", e); } } return false; }
From source file:ru.scratch.ScratchListenClipboardAction.java
@Override public void contentChanged(@Nullable Transferable oldTransferable, Transferable newTransferable) { if (!ScratchData.getInstance().isAppendContentFromClipboard()) return;//from w w w.ja v a2 s .c o m try { String oldClipboard = null; if (oldTransferable != null && oldTransferable.getTransferData(DataFlavor.stringFlavor) != null) { oldClipboard = oldTransferable.getTransferData(DataFlavor.stringFlavor).toString(); } String clipboard = null; if (newTransferable != null && newTransferable.getTransferData(DataFlavor.stringFlavor) != null) { clipboard = newTransferable.getTransferData(DataFlavor.stringFlavor).toString(); } if (clipboard != null && !StringUtils.equals(oldClipboard, clipboard)) { writeToScratch(clipboard); } } catch (UnsupportedFlavorException e) { LOG.info(e); } catch (IOException e) { LOG.info(e); } }
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 a v a2 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:Main.java
public boolean importData(JComponent comp, Transferable t) { DataFlavor[] flavors = t.getTransferDataFlavors(); System.out.println("Trying to import:" + t); for (int i = 0; i < flavors.length; i++) { DataFlavor flavor = flavors[i]; try {/* w w w .j a v a 2 s . c om*/ if (flavor.equals(DataFlavor.javaFileListFlavor)) { System.out.println("importData: FileListFlavor"); List l = (List) t.getTransferData(DataFlavor.javaFileListFlavor); Iterator iter = l.iterator(); while (iter.hasNext()) { File file = (File) iter.next(); System.out.println("GOT FILE: " + file.getCanonicalPath()); } return true; } else if (flavor.equals(DataFlavor.stringFlavor)) { System.out.println("importData: String Flavor"); String fileOrURL = (String) t.getTransferData(flavor); System.out.println("GOT STRING: " + fileOrURL); try { URL url = new URL(fileOrURL); System.out.println("Valid URL: " + url.toString()); return true; } catch (MalformedURLException ex) { System.err.println("Not a valid URL"); return false; } } else { System.out.println("importData rejected: " + flavor); } } catch (IOException ex) { System.err.println("IOError getting data: " + ex); } catch (UnsupportedFlavorException e) { System.err.println("Unsupported Flavor: " + e); } } return false; }
From source file:net.sf.jabref.gui.ClipBoardManager.java
public List<BibEntry> extractBibEntriesFromClipboard() { // Get clipboard contents, and see if TransferableBibtexEntry is among the content flavors offered Transferable content = CLIPBOARD.getContents(null); List<BibEntry> result = new ArrayList<>(); if (content.isDataFlavorSupported(TransferableBibtexEntry.entryFlavor)) { // We have determined that the clipboard data is a set of entries. try {/* w ww. j av a2 s .c om*/ result = (List<BibEntry>) content.getTransferData(TransferableBibtexEntry.entryFlavor); } catch (UnsupportedFlavorException | ClassCastException ex) { LOGGER.warn("Could not paste this type", ex); } catch (IOException ex) { LOGGER.warn("Could not paste", ex); } } else if (content.isDataFlavorSupported(DataFlavor.stringFlavor)) { try { String data = (String) content.getTransferData(DataFlavor.stringFlavor); // fetch from doi if (DOI.build(data).isPresent()) { LOGGER.info("Found DOI in clipboard"); Optional<BibEntry> entry = new DOItoBibTeXFetcher().getEntryFromDOI(new DOI(data).getDOI()); entry.ifPresent(result::add); } else { // parse bibtex string BibtexParser bp = new BibtexParser(new StringReader(data)); BibDatabase db = bp.parse().getDatabase(); LOGGER.info("Parsed " + db.getEntryCount() + " entries from clipboard text"); if (db.hasEntries()) { result = db.getEntries(); } } } catch (UnsupportedFlavorException ex) { LOGGER.warn("Could not parse this type", ex); } catch (IOException ex) { LOGGER.warn("Data is no longer available in the requested flavor", ex); } } return result; }
From source file:net.pandoragames.far.ui.swing.component.UndoHistoryPopupMenu.java
private void init(SwingConfig config, ComponentRepository componentRepository) { // COPY/*from w w w . j av a 2 s . co m*/ JMenuItem copy = new JMenuItem(config.getLocalizer().localize("label.copy")); copy.setAccelerator(KeyStroke.getKeyStroke("control C")); copy.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String selection = textComponent.getSelectedText(); if (selection != null) { StringSelection textTransfer = new StringSelection(selection); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(textTransfer, null); } } }); this.add(copy); // PASTE JMenuItem paste = new JMenuItem(config.getLocalizer().localize("label.paste")); paste.setAccelerator(KeyStroke.getKeyStroke("control V")); paste.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { Transferable transfer = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); try { if (transfer != null && transfer.isDataFlavorSupported(DataFlavor.stringFlavor)) { String text = (String) transfer.getTransferData(DataFlavor.stringFlavor); String selected = textComponent.getSelectedText(); if (selected == null) { int insertAt = textComponent.getCaretPosition(); textComponent.getDocument().insertString(insertAt, text, null); } else { int start = textComponent.getSelectionStart(); int end = textComponent.getSelectionEnd(); textComponent.getDocument().remove(start, end - start); textComponent.getDocument().insertString(start, text, null); } } } catch (UnsupportedFlavorException e) { LogFactory.getLog(this.getClass()).error("UnsupportedFlavorException reading from clipboard", e); } catch (IOException iox) { LogFactory.getLog(this.getClass()).error("IOException reading from clipboard", iox); } catch (BadLocationException blx) { LogFactory.getLog(this.getClass()).error("BadLocationException reading from clipboard", blx); } } }); this.add(paste); // UNDO Action undoAction = textComponent.getActionMap().get(UndoHistory.ACTION_KEY_UNDO); if (undoAction != null) { undoAction.putValue(Action.NAME, config.getLocalizer().localize("label.undo")); this.add(undoAction); } // REDO Action redoAction = textComponent.getActionMap().get(UndoHistory.ACTION_KEY_REDO); if (redoAction != null) { redoAction.putValue(Action.NAME, config.getLocalizer().localize("label.redo")); this.add(redoAction); } // PREVIOUS Action prevAction = textComponent.getActionMap().get(UndoHistory.ACTION_KEY_PREVIOUS); if (prevAction != null) { prevAction.putValue(Action.NAME, config.getLocalizer().localize("label.previous")); this.add(prevAction); } // NEXT Action nextAction = textComponent.getActionMap().get(UndoHistory.ACTION_KEY_NEXT); if (nextAction != null) { nextAction.putValue(Action.NAME, config.getLocalizer().localize("label.next")); this.add(nextAction); } }
From source file:de.tor.tribes.ui.windows.AbstractDSWorkbenchFrame.java
@Override public void drop(DropTargetDropEvent dtde) { if (dtde.isDataFlavorSupported(VillageTransferable.villageDataFlavor) || dtde.isDataFlavorSupported(DataFlavor.stringFlavor)) { dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); } else {/*from ww w . j a va 2 s . co m*/ dtde.rejectDrop(); return; } Transferable t = dtde.getTransferable(); List<Village> v; MapPanel.getSingleton().setCurrentCursor(MapPanel.getSingleton().getCurrentCursor()); try { v = (List<Village>) t.getTransferData(VillageTransferable.villageDataFlavor); fireVillagesDraggedEvent(v, dtde.getLocation()); } catch (Exception ignored) { } }
From source file:ExtendedDnDDemo.java
public boolean importData(JComponent c, Transferable t) { if (canImport(c, t.getTransferDataFlavors())) { try {//www. j ava 2 s . c om String str = (String) t.getTransferData(DataFlavor.stringFlavor); importString(c, str); return true; } catch (UnsupportedFlavorException ufe) { } catch (IOException ioe) { } } return false; }