List of usage examples for java.awt.datatransfer Transferable getTransferData
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException;
From source file:Samples.Advanced.GraphEditorDemo.java
/** * Get the String residing on the clipboard. * * @return any text found on the Clipboard; if none found, return an * empty String.//www . ja va2 s . c om */ public String getClipboardContents() { String result = ""; Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); //odd: the Object param of getContents is not currently used Transferable contents = clipboard.getContents(null); boolean hasTransferableText = (contents != null) && contents.isDataFlavorSupported(DataFlavor.stringFlavor); if (hasTransferableText) { try { result = (String) contents.getTransferData(DataFlavor.stringFlavor); } catch (UnsupportedFlavorException ex) { //highly unlikely since we are using a standard DataFlavor System.out.println(ex); ex.printStackTrace(); } catch (IOException ex) { System.out.println(ex); ex.printStackTrace(); } } return result; }
From source file:org.apache.openmeetings.screenshare.job.RemoteJob.java
public String getClipboardText() { try {//from ww w .j a v a 2 s . co m // get the system clipboard Clipboard systemClipboard = getDefaultToolkit().getSystemClipboard(); // get the contents on the clipboard in a transferable object Transferable clipboardContents = systemClipboard.getContents(null); // check if clipboard is empty if (clipboardContents == null) { // Clipboard is empty!!! } else if (clipboardContents.isDataFlavorSupported(stringFlavor)) { // see if DataFlavor of DataFlavor.stringFlavor is supported // return text content String returnText = (String) clipboardContents.getTransferData(stringFlavor); return returnText; } } catch (Exception e) { log.error("Unexpected exception while getting clipboard text", e); } return ""; }
From source file:DragListDemo.java
public boolean importData(JComponent c, Transferable t) { JList target = null;/*from w w w . java 2 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; }
From source file:org.orbisgis.sif.components.fstree.TreeNodeFolder.java
/** * Create a file from a Reader instance in this folder * @param tf Transferable instance//from w w w. j av a 2 s .c om * @param flavor DataFlavor * @return If the file has been created */ private boolean importReader(Transferable tf, DataFlavor flavor) { try { // From this transferable, a file can be created Object transferData = tf.getTransferData(flavor); if (!(transferData instanceof Reader)) { return false; } BufferedReader br = new BufferedReader((Reader) transferData); File fileName; if (transferData instanceof TransferableFileContent) { // The filename is given by the drag source fileName = new File(getFilePath(), ((TransferableFileContent) transferData).getFileNameHint()); } else { // The filename must be given by the user String contentFileName = JOptionPane.showInputDialog(UIFactory.getMainFrame(), I18N.tr("Enter the folder name"), I18N.tr("Folder")); if (contentFileName != null) { fileName = new File(getFilePath(), contentFileName); } else { return false; } } // If the file exists found a new one fileName = getUniqueFileName(fileName); FileWriter writer = new FileWriter(fileName); String res = br.readLine(); while (res != null) { writer.write(res + "\n"); res = br.readLine(); } writer.close(); return true; } catch (UnsupportedFlavorException ex) { LOGGER.error(ex.getLocalizedMessage(), ex); return false; } catch (IOException ex) { LOGGER.error(ex.getLocalizedMessage(), ex); return false; } }
From source file:net.sf.jabref.gui.UrlDragDrop.java
@Override public void drop(DropTargetDropEvent dtde) { Transferable tsf = dtde.getTransferable(); dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); //try with an URL DataFlavor dtURL = null;/*from w ww . j av a2 s .c o m*/ try { dtURL = new DataFlavor("application/x-java-url; class=java.net.URL"); } catch (ClassNotFoundException e) { LOGGER.warn("Could not find DropTargetDropEvent class.", e); } try { URL url = (URL) tsf.getTransferData(dtURL); JOptionChoice res = (JOptionChoice) JOptionPane.showInputDialog(editor, "", Localization.lang("Select action"), JOptionPane.QUESTION_MESSAGE, null, new JOptionChoice[] { new JOptionChoice(Localization.lang("Insert URL"), 0), new JOptionChoice(Localization.lang("Download file"), 1) }, new JOptionChoice(Localization.lang("Insert URL"), 0)); if (res != null) { switch (res.getId()) { //insert URL case 0: feditor.setText(url.toString()); editor.updateField(feditor); break; //download file case 1: try { //auto filename: File file = new File(new File(Globals.prefs.get("pdfDirectory")), editor.getEntry().getCiteKey() + ".pdf"); frame.output(Localization.lang("Downloading...")); MonitoredURLDownload.buildMonitoredDownload(editor, url).downloadToFile(file); frame.output(Localization.lang("Download completed")); feditor.setText(file.toURI().toURL().toString()); editor.updateField(feditor); } catch (IOException ioex) { LOGGER.error("Error while downloading file.", ioex); JOptionPane.showMessageDialog(editor, Localization.lang("File download"), Localization.lang("Error while downloading file:" + ioex.getMessage()), JOptionPane.ERROR_MESSAGE); } break; default: LOGGER.warn("Unknown selection (should not happen)"); break; } } return; } catch (UnsupportedFlavorException nfe) { // not an URL then... LOGGER.warn("Could not parse URL.", nfe); } catch (IOException ioex) { LOGGER.warn("Could not perform drag and drop.", ioex); } try { //try with a File List @SuppressWarnings("unchecked") List<File> filelist = (List<File>) tsf.getTransferData(DataFlavor.javaFileListFlavor); if (filelist.size() > 1) { JOptionPane.showMessageDialog(editor, Localization.lang("Only one item is supported"), Localization.lang("Drag and Drop Error"), JOptionPane.ERROR_MESSAGE); return; } File fl = filelist.get(0); feditor.setText(fl.toURI().toURL().toString()); editor.updateField(feditor); } catch (UnsupportedFlavorException nfe) { JOptionPane.showMessageDialog(editor, Localization.lang("Operation not supported"), Localization.lang("Drag and Drop Error"), JOptionPane.ERROR_MESSAGE); LOGGER.warn("Could not perform drag and drop.", nfe); } catch (IOException ioex) { LOGGER.warn("Could not perform drag and drop.", ioex); } }
From source file:com.mirth.connect.client.ui.TemplatePanel.java
public void drop(DropTargetDropEvent dtde) { try {/*from w w w . j a v a2 s . c o m*/ Transferable tr = dtde.getTransferable(); if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); File file = ((List<File>) tr.getTransferData(DataFlavor.javaFileListFlavor)).get(0); String dataType = PlatformUI.MIRTH_FRAME.displayNameToDataType.get(getDataType()); DataTypeClientPlugin dataTypePlugin = LoadedExtensions.getInstance().getDataTypePlugins() .get(dataType); if (dataTypePlugin.isBinary()) { byte[] content = FileUtils.readFileToByteArray(file); // The plugin should decide how to convert the byte array to string pasteBox.setText(dataTypePlugin.getTemplateString(content)); } else { pasteBox.setText(FileUtils.readFileToString(file, UIConstants.CHARSET)); } parent.modified = true; } } catch (Exception e) { dtde.rejectDrop(); } }
From source file:com.mirth.connect.client.ui.TemplatePanel.java
public void dragEnter(DropTargetDragEvent dtde) { try {/*from www .jav a 2s . com*/ Transferable tr = dtde.getTransferable(); if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE); List<File> fileList = (List<File>) tr.getTransferData(DataFlavor.javaFileListFlavor); Iterator<File> iterator = fileList.iterator(); while (iterator.hasNext()) { iterator.next(); } } else { dtde.rejectDrag(); } } catch (Exception e) { dtde.rejectDrag(); } }
From source file:DropTest.java
public void drop(DropTargetDropEvent dtde) { try {//w ww. ja v a2 s . c o m // 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); ta.setText("Successful file list drop.\n\n"); // And add the list of file names to our text area java.util.List list = (java.util.List) 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; } } // 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(); } }
From source file:de.mycrobase.jcloudapp.Main.java
@SuppressWarnings("unchecked") public boolean doUploadClipboard() { Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard(); Transferable t = cb.getContents(null); if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { try {//from w w w . j a va 2 s . c o m List<File> data = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor); uploadFilesFromClipboard(data); } catch (UnsupportedFlavorException ex) { } catch (IOException ex) { } return true; } else if (t.isDataFlavorSupported(DataFlavor.imageFlavor)) { try { Image data = (Image) t.getTransferData(DataFlavor.imageFlavor); BufferedImage bi = (BufferedImage) data; uploadImageFromClipboard(bi); } catch (UnsupportedFlavorException ex) { } catch (IOException ex) { } return true; } else if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) { try { String data = (String) t.getTransferData(DataFlavor.stringFlavor); if (isValidURL(data)) { createBookmarkFromClipboard(data); } else { uploadStringFromClipboard(data); } } catch (UnsupportedFlavorException ex) { } catch (IOException ex) { } return true; } return false; }
From source file:com.projity.pm.graphic.spreadsheet.common.transfer.NodeListTransferHandler.java
public boolean importData(JComponent c, Transferable t) { SpreadSheet spreadSheet = getSpreadSheet(c); if (spreadSheet == null) return false; DataFlavor flavor = getFlavor(t.getTransferDataFlavors()); if (flavor != null) { try {/*from w w w . j a v a 2s.c om*/ NodeModel model = ((CommonSpreadSheetModel) spreadSheet.getModel()).getCache().getModel(); Object data = t.getTransferData(flavor); if (data == null) return false; List nodes = null; if (data instanceof ArrayList) { nodes = (List) data; for (Iterator i = nodes.iterator(); i.hasNext();) { Node node = (Node) i.next(); transformSubprojectBranches(node, model.getDataFactory(), new Predicate() { public boolean evaluate(Object arg0) { Node parent = (Node) arg0; //change implementation NormalTask task = new NormalTask(); Task source = ((Task) parent.getImpl()); source.cloneTo(task); //task.setDuration(source.getActualDuration()); parent.setImpl(task); return true; } }); } SpreadSheet.SpreadSheetAction a = getNodeListPasteAction().getSpreadSheetAction(); a.execute(nodes); } else if (data instanceof String) { // ArrayList fields=spreadSheet.getSelectedFields(); // if (fields==null){ // fields=spreadSheet.getSelectableFields(); //The whole line is selected // nodes=NodeListTransferable.stringToNodeList((String)data,spreadSheet,fields,model.getDataFactory()); // }else{ // NodeListTransferable.pasteString((String)data,spreadSheet); // } NodeListTransferable.pasteString((String) data, spreadSheet); } else return false; return true; } catch (UnsupportedFlavorException ufe) { } catch (IOException ioe) { } } return false; }