Exemple #1
0
  /**
   * 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();
  }