public static void main(String[] args) { Comparator<DataFlavor> comparator = DataFlavorUtil.getDataFlavorComparator(); DataFlavor flavor1 = DataFlavor.imageFlavor; DataFlavor flavor2 = DataFlavor.selectionHtmlFlavor; if (comparator.compare(flavor1, flavor2) == 0) { throw new RuntimeException( flavor1.getMimeType() + " and " + flavor2.getMimeType() + " should not be equal"); } }
/** * Sets the current contents of the clipboard to the specified transferable object and registers * the specified clipboard owner as the owner of the new contents. * * <p>If there is an existing owner different from the argument <code>owner</code>, that owner is * notified that it no longer holds ownership of the clipboard contents via an invocation of * <code>ClipboardOwner.lostOwnership()</code> on that owner. An implementation of <code> * setContents()</code> is free not to invoke <code>lostOwnership()</code> directly from this * method. For example, <code>lostOwnership()</code> may be invoked later on a different thread. * The same applies to <code>FlavorListener</code>s registered on this clipboard. * * <p>The method throws <code>IllegalStateException</code> if the clipboard is currently * unavailable. For example, on some platforms, the system clipboard is unavailable while it is * accessed by another application. * * @param contents the transferable object representing the clipboard content * @param owner the object which owns the clipboard content * @throws IllegalStateException if the clipboard is currently unavailable * @see java.awt.Toolkit#getSystemClipboard */ public synchronized void setContents(Transferable contents, ClipboardOwner owner) { final ClipboardOwner oldOwner = this.owner; final Transferable oldContents = this.contents; this.owner = owner; this.contents = contents; if (oldOwner != null && oldOwner != owner) { DataFlavorUtil.getDesktopService() .invokeOnEventThread(() -> oldOwner.lostOwnership(Clipboard.this, oldContents)); } fireFlavorsChanged(); }
/** * Checks change of the <code>DataFlavor</code>s and, if necessary, notifies all listeners that * have registered interest for notification on <code>FlavorEvent</code>s. * * @since 1.5 */ private void fireFlavorsChanged() { if (flavorListeners == null) { return; } Set<DataFlavor> prevDataFlavors = currentDataFlavors; currentDataFlavors = getAvailableDataFlavorSet(); if (Objects.equals(prevDataFlavors, currentDataFlavors)) { return; } flavorListeners.forEach( listener -> DataFlavorUtil.getDesktopService() .invokeOnEventThread( () -> listener.flavorsChanged(new FlavorEvent(Clipboard.this)))); }