public void testClipboard() throws Exception { MockServices.setServices(Cnv.class); Clipboard c = Lookup.getDefault().lookup(Clipboard.class); ExClipboard ec = Lookup.getDefault().lookup(ExClipboard.class); assertEquals("Clipboard == ExClipboard", c, ec); assertNotNull(Lookup.getDefault().lookup(ExClipboard.Convertor.class)); assertEquals(Cnv.class, Lookup.getDefault().lookup(ExClipboard.Convertor.class).getClass()); c.setContents(new ExTransferable.Single(DataFlavor.stringFlavor) { protected Object getData() throws IOException, UnsupportedFlavorException { return "17"; } }, null); Transferable t = c.getContents(null); assertTrue("still supports stringFlavor", t.isDataFlavorSupported(DataFlavor.stringFlavor)); assertEquals("correct string in clipboard", "17", t.getTransferData(DataFlavor.stringFlavor)); assertTrue("support Integer too", t.isDataFlavorSupported(MYFLAV)); assertEquals("correct Integer", new Integer(17), t.getTransferData(MYFLAV)); }
protected String getBestCharsetForTextFormat(Long lFormat, Transferable localeTransferable) throws IOException { String charset = null; if (localeTransferable != null && isLocaleDependentTextFormat(lFormat) && localeTransferable.isDataFlavorSupported(javaTextEncodingFlavor)) { try { byte[] charsetNameBytes = (byte[])localeTransferable .getTransferData(javaTextEncodingFlavor); charset = new String(charsetNameBytes, StandardCharsets.UTF_8); } catch (UnsupportedFlavorException cannotHappen) { } } else { charset = getCharsetForTextFormat(lFormat); } if (charset == null) { // Only happens when we have a custom text type. charset = Charset.defaultCharset().name(); } return charset; }
/** Creates new support. * @param t transferable to to copy values from * @param o clipobard owner (or null) */ private ExTransferable(final Transferable t) { map = new LinkedHashMap<DataFlavor,Single>(); final DataFlavor[] df = t.getTransferDataFlavors(); if (df != null) { for (int i = 0; i < df.length; i++) { try { final int fi = i; map.put( df[i], new Single(df[i]) { public Object getData() throws IOException, UnsupportedFlavorException { return t.getTransferData(df[fi]); } } ); } catch (Exception ex) { // ignore if the data cannot be retrived } } } }
private Boolean reorderProjects(TransferHandler.TransferSupport support) { JList list = (JList) support.getComponent(); try { int[] selectedIndices = (int[]) support.getTransferable().getTransferData(INDICES); DefaultListModel model = (DefaultListModel) list.getModel(); JList.DropLocation dl = (JList.DropLocation) support.getDropLocation(); if (dl.getIndex() != -1) { for (int selectedIndex : selectedIndices) { Object value = model.get(selectedIndex); model.removeElement(value); model.add(dl.getIndex(), value); } return true; } else { LOG.warning("Invalid Drop Location"); } } catch (UnsupportedFlavorException | IOException ex) { LOG.log(Level.SEVERE, ex.getMessage(), ex); } return false; }
/** * Remove a toolbar button represented by the given Transferable. */ private void removeButton( Transferable t ) { try { Object o = null; if( t.isDataFlavorSupported(buttonDataFlavor) ) { o = t.getTransferData(buttonDataFlavor); } if( null != o && o instanceof DataObject ) { ((DataObject) o).delete(); sourceToolbar.repaint(); } } catch( UnsupportedFlavorException e ) { log.log( Level.INFO, null, e ); } catch( IOException ioE ) { log.log( Level.INFO, null, ioE ); } }
private boolean handleDropImpl(Transferable t) { try { Object o; if( t.isDataFlavorSupported( actionDataFlavor ) ) { o = t.getTransferData( actionDataFlavor ); if( o instanceof Node ) { DataObject dobj = ((Node)o).getLookup().lookup( DataObject.class ); return addButton( dobj, dropTargetButtonIndex, insertBefore ); } } else { o = t.getTransferData( buttonDataFlavor ); if( o instanceof DataObject ) { return moveButton( (DataObject)o, dropTargetButtonIndex, insertBefore ); } } } catch( UnsupportedFlavorException e ) { log.log( Level.INFO, null, e ); } catch( IOException ioE ) { log.log( Level.INFO, null, ioE ); } return false; }
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if (!isDataFlavorSupported(flavor)) { throw new UnsupportedFlavorException(flavor); } Object ret = flavorsToData.get(flavor); if (ret instanceof IOException) { // rethrow IOExceptions generated while fetching data throw (IOException)ret; } else if (ret instanceof DataFactory) { // Now we can render the data DataFactory factory = (DataFactory)ret; ret = factory.getTransferData(flavor); } return ret; }
@Override public void clipboardChanged(ClipboardEvent ev) { ExClipboard clipboard = ev.getClipboard(); Transferable transferable = null; String clipboardContent = null; try { transferable = clipboard.getContents(null); if (transferable != null && transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) { clipboardContent = (String) transferable.getTransferData(DataFlavor.stringFlavor); } } catch (OutOfMemoryError oom) { NotificationDisplayer.getDefault().notify( NbBundle.getBundle(ClipboardHistory.class).getString("clipboard-history-oom"),NotificationDisplayer.Priority.NORMAL.getIcon(), NbBundle.getBundle(ClipboardHistory.class).getString("clipboard-history-oom-details"), null); return; } catch (IOException ioe) { //ignored for bug #218255 } catch (UnsupportedFlavorException ufe) { } if (clipboardContent != null) { addHistory(transferable, clipboardContent); } }
@Override public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if (flavor.equals(LOCAL_TRANSFERRED_OPERATORS_FLAVOR)) { return this.clonedOperators; } if (flavor.equals(DataFlavor.stringFlavor)) { StringBuilder b = new StringBuilder(); for (Operator op : clonedOperators) { b.append(op.getXML(false)); } return b.toString(); } else { throw new UnsupportedFlavorException(flavor); } }
/** * Is called if a {@link ValueSourceTreeNode} is dropped on the {@link PlotConfigurationTree}. */ private void valueSourceDrop(final ValueSourceTreeNode valueSourceTreeNode, TreePath path, final int childIndex, boolean importDataTableColumn) throws UnsupportedFlavorException, IOException, ChartConfigurationException { // fetch dropped value source and parent final ValueSource valueSource = valueSourceTreeNode.getUserObject(); Object lastPathComponent = path.getLastPathComponent(); if (lastPathComponent instanceof RangeAxisConfigTreeNode) { valueSourceDropOnRangeAxisConfig(valueSourceTreeNode, childIndex, valueSource, lastPathComponent); } else { valueSourceDropOnPlotConfiguration(valueSourceTreeNode, childIndex, valueSource, lastPathComponent, importDataTableColumn); } }
@Override public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if (flavor == binaryFlavor) { return this; } else if (flavor == DataFlavor.stringFlavor) { StringBuilder buf = new StringBuilder(); for (int i = 0; i < headers.length; i++) { buf.append(headers[i]); buf.append(i == headers.length - 1 ? '\n' : '\t'); } for (int i = 0; i < contents.length; i++) { for (int j = 0; j < contents[i].length; j++) { buf.append(contents[i][j]); buf.append(j == contents[i].length - 1 ? '\n' : '\t'); } } return buf.toString(); } else { throw new UnsupportedFlavorException(flavor); } }
@Override public void drop(DropTargetDropEvent dtde) { dtde.acceptDrop(DnDConstants.ACTION_COPY); try { Transferable t = dtde.getTransferable(); DataFlavor[] dataFlavors = t.getTransferDataFlavors(); for (DataFlavor df : dataFlavors) { if (df.isFlavorJavaFileListType()) { File[] filesArray = (File[]) ((List<File>) t.getTransferData(df)).toArray(); pathNameTextField.setText(getFilesName(filesArray)); } } } catch (UnsupportedFlavorException e2) { } catch (IOException ex) { Logger.getLogger(SubtitleDownloaderUI.class.getName()).log(Level.SEVERE, null, ex); } }
/** * Returns true if the drag came from the same component as we are testing for drop. Used to * prevent drops on the src file list. */ public boolean isSrcComponentSameAsTargetComponent(TransferHandler.TransferSupport info) throws UnsupportedFlavorException, IOException { boolean result = false; // First get where we are putting this file String myPanelDir = profilePanel.getCurrentDirectory().getPath(); // Don't accept drags from current panel. if (info.isDataFlavorSupported(ArrayListTransferrable.flavor)) { // Get the Array of items being dropped. Transferable t = info.getTransferable(); ArrayList arrayList = (ArrayList) t.getTransferData(ArrayListTransferrable.flavor); ArcMoverFile fileBeingMoved = (ArcMoverFile) arrayList.get(2); String fileBeingMovedDir = fileBeingMoved.getParent().getPath(); if (myPanelDir.equals(fileBeingMovedDir)) { result = true; } } return result; }
private String getBestCharsetForTextFormat(Long lFormat, Transferable localeTransferable) throws IOException { String charset = null; if (localeTransferable != null && isLocaleDependentTextFormat(lFormat) && localeTransferable.isDataFlavorSupported(javaTextEncodingFlavor)) { try { charset = new String( (byte[])localeTransferable.getTransferData(javaTextEncodingFlavor), "UTF-8" ); } catch (UnsupportedFlavorException cannotHappen) { } } else { charset = getCharsetForTextFormat(lFormat); } if (charset == null) { // Only happens when we have a custom text type. charset = getDefaultTextCharset(); } return charset; }
@Override public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if (!isDataFlavorSupported(flavor)) { throw new UnsupportedFlavorException(flavor); } Object ret = flavorsToData.get(flavor); if (ret instanceof IOException) { // rethrow IOExceptions generated while fetching data throw new IOException("Exception fetching data: ", (IOException)ret); } else if (ret instanceof DataFactory) { // Now we can render the data DataFactory factory = (DataFactory)ret; ret = factory.getTransferData(flavor); } return ret; }
void paste() throws IOException { Clipboard clip = java.awt.Toolkit.getDefaultToolkit().getSystemClipboard(); Transferable contents = clip.getContents(this); DataFlavor[] flavors = contents.getTransferDataFlavors(); for( int k=0 ; k<flavors.length ; k++) { try { if( flavors[k].getHumanPresentableName().indexOf("html")>=0 )continue; BufferedReader in = new BufferedReader( flavors[k].getReaderForText(contents)); System.out.println( flavors[k].getHumanPresentableName()); read( in ); break; } catch( UnsupportedFlavorException e) { } } }
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException { if (flavor.equals(DataFlavor.imageFlavor)) { return theImage; } else { throw new UnsupportedFlavorException(flavor); } }
@SuppressWarnings("unchecked") @Override public void drop(DropTargetDropEvent dtde) { try { Transferable t = dtde.getTransferable(); List<File> files = null; if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { dtde.acceptDrop(DnDConstants.ACTION_REFERENCE); files = (List<File>) t .getTransferData(DataFlavor.javaFileListFlavor); } else if (uriFlavor != null && t.isDataFlavorSupported(uriFlavor)) { dtde.acceptDrop(DnDConstants.ACTION_REFERENCE); files = textURIListToFileList((String) t .getTransferData(uriFlavor)); } else dtde.rejectDrop(); if (files != null && files.size() == 1) { openFile(files.get(0)); dtde.dropComplete(true); } else dtde.dropComplete(false); } catch (UnsupportedFlavorException ex) { dtde.rejectDrop(); } catch (IOException e) { dtde.rejectDrop(); } }
/** * Returns the value of the clipboard * * @return Object */ private Object getValue() { try { return clipboard.getContents(this).getTransferData(DataFlavors.object()); } catch (UnsupportedFlavorException | IOException e) { } return null; }
@Override public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException { if (flavor.equals(DataFlavors.object())) { return value; } else if (flavor.equals(DataFlavors.string())) { return value.toString(); } else { throw new UnsupportedFlavorException(flavor); } }
/** * Description of the Method * * @param c Description of the Parameter * @param t Description of the Parameter * @return Description of the Return Value */ public boolean importData(JComponent c, Transferable t) { if (canImport(c, t.getTransferDataFlavors())) { try { String str = (String) t.getTransferData(DataFlavor.stringFlavor); importString(c, str); return true; } catch (UnsupportedFlavorException | IOException ignored) { } } return false; }
@Override public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if (isDataFlavorSupported(flavor)) { return this; } else { throw new UnsupportedFlavorException(flavor); } }
@Override public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if (RECTANGULAR_SELECTION_FLAVOR.equals(flavor)) { return rectangularSelectionData; } return delegate.getTransferData(flavor); }
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { Single o = map.get(flavor); if (o == null) { throw new UnsupportedFlavorException(flavor); } return o.getTransferData(flavor); }
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if (!this.flavor.equals(flavor)) { throw new UnsupportedFlavorException(flavor); } return getData(); }
/** Get transfer data. * @param flavor the flavor ({@link #multiFlavor}) * @return {@link MultiTransferObject} that represents data in this object * @exception UnsupportedFlavorException when the flavor is not supported * @exception IOException when it is not possible to read data */ public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if (!isDataFlavorSupported(flavor)) { throw new UnsupportedFlavorException(flavor); } return transferObject; }
/** * * @param flavor * @return the richer data flavor of this and the specified * @throws UnsupportedFlavorException */ public Object getRicherData(DataFlavor flavor) throws UnsupportedFlavorException { if (flavor.equals(dataFlavor)) { return this; } else { throw new UnsupportedFlavorException(flavor); } }
private boolean importTextIntoPalette( Lookup targetCategory, Transferable item, int dropIndex ) throws IOException, UnsupportedFlavorException { DataFlavor flavor = DataFlavor.selectBestTextFlavor( item.getTransferDataFlavors() ); if( null == flavor ) return false; String textToImport = extractText( item, flavor ); SwingUtilities.invokeLater( new TextImporter( textToImport, targetCategory, dropIndex ) ); return true; }
private Transferable createTransferable( final Item item ) { return new ExTransferable.Single( PaletteController.ITEM_DATA_FLAVOR ) { protected Object getData() throws IOException, UnsupportedFlavorException { return item.getLookup(); } }; }
@Override public List<? extends File> getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if (!isDataFlavorSupported(flavor)) { throw new UnsupportedFlavorException(flavor); } return data; }
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if (!isDataFlavorSupported(flavor)) { throw new UnsupportedFlavorException(flavor); } else { return transferData; } }
private java.util.List<File> extractListOfFiles(DropTargetDropEvent dtde) { java.util.List<File> fileList = null; try { fileList = (java.util.List<File>)dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor); } catch (UnsupportedFlavorException | IOException e) { e.printStackTrace(); } return fileList; }
@Override public void drop(DropTargetDropEvent dtde) { if (!dtde.isDataFlavorSupported(DataFlavor.allHtmlFlavor)) { Sysout.println("DataFlavor.allHtmlFlavor is not present in the system clipboard"); dtde.rejectDrop(); return; } else if (!dtde.isDataFlavorSupported(DataFlavor.fragmentHtmlFlavor)) { Sysout.println("DataFlavor.fragmentHtmlFlavor is not present in the system clipboard"); dtde.rejectDrop(); return; } else if (!dtde.isDataFlavorSupported(DataFlavor.selectionHtmlFlavor)) { Sysout.println("DataFlavor.selectionHtmlFlavor is not present in the system clipboard"); dtde.rejectDrop(); return; } dtde.acceptDrop(DnDConstants.ACTION_COPY); Transferable t = dtde.getTransferable(); try { Sysout.println("ALL:"); Sysout.println(t.getTransferData(DataFlavor.allHtmlFlavor).toString()); Sysout.println("FRAGMENT:"); Sysout.println(t.getTransferData(DataFlavor.fragmentHtmlFlavor).toString()); Sysout.println("SELECTION:"); Sysout.println(t.getTransferData(DataFlavor.selectionHtmlFlavor).toString()); } catch (UnsupportedFlavorException | IOException e) { e.printStackTrace(); } }
@Override public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if (WS_FLAVOR.equals(flavor)) { return ref; } throw new UnsupportedFlavorException(flavor); }
/** * Returns the <code>Transferable</code>'s data in the requested * <code>DataFlavor</code> if possible. * @param flavor the requested flavor for the data * @return the data in the requested flavor, as outlined above * @throws UnsupportedFlavorException if the requested data flavor is * not supported. * @throws IOException if an IOException occurs while retrieving the data. * @throws NullPointerException if flavor is <code>null</code> */ public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if (flavor.equals(flavors[STRING])) { return (Object)str; } else if (flavor.equals(flavors[PLAIN_TEXT])) { return new StringReader(str == null ? "" : str); } else if (flavor.equals(flavors[WATCH])) { return watch; } else { throw new UnsupportedFlavorException(flavor); } }
private void paste() { int startRow = (insertRecordTableUI.getSelectedRows())[0]; int startCol = (insertRecordTableUI.getSelectedColumns())[0]; try { String trstring = (String) (clipBoard.getContents(this).getTransferData(DataFlavor.stringFlavor)); StringTokenizer st1 = new StringTokenizer(trstring, "\n"); for (int i = 0; st1.hasMoreTokens(); i++) { int rowIdx = startRow + i; String rowstring = st1.nextToken(); StringTokenizer st2 = new StringTokenizer(rowstring, "\t"); for (int j = 0; st2.hasMoreTokens(); j++) { int colIdx = startCol + j; String value = st2.nextToken(); if (colIdx < insertRecordTableUI.getColumnCount()) { // If more data is pasted than currently rows exists // empty rows are added to take the additional data if (rowIdx >= insertRecordTableUI.getRowCount()) { insertRecordTableUI.appendEmptyRow(); } insertRecordTableUI.setValueAt(value, rowIdx, colIdx); } } } } catch (UnsupportedFlavorException | IOException | RuntimeException ex) { LOG.log(Level.INFO, "Failed to paste the contents ", ex); } }
@Override public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if (flavor.getHumanPresentableName().equals(FLAVOR_NAME)) { return taskNodes; } return null; }
@Override public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if (flavor.equals(DataFlavor.imageFlavor) && image != null) { return image; } else { throw new UnsupportedFlavorException(flavor); } }