1 /*
   2  * Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt.datatransfer;
  27 
  28 import java.awt.EventQueue;
  29 
  30 import java.util.Set;
  31 import java.util.HashSet;
  32 import java.util.Arrays;
  33 
  34 import java.io.IOException;
  35 
  36 /**
  37  * A class that implements a mechanism to transfer data using
  38  * cut/copy/paste operations.
  39  * <p>
  40  * {@link FlavorListener}s may be registered on an instance of the
  41  * Clipboard class to be notified about changes to the set of
  42  * {@link DataFlavor}s available on this clipboard (see
  43  * {@link #addFlavorListener}).
  44  *
  45  * @see java.awt.Toolkit#getSystemClipboard
  46  * @see java.awt.Toolkit#getSystemSelection
  47  *
  48  * @author      Amy Fowler
  49  * @author      Alexander Gerasimov
  50  */
  51 public class Clipboard {
  52 
  53     String name;
  54 
  55     /**
  56      * The owner of the clipboard.
  57      */
  58     protected ClipboardOwner owner;
  59     /**
  60      * Contents of the clipboard.
  61      */
  62     protected Transferable contents;
  63 
  64     /**
  65      * An aggregate of flavor listeners registered on this local clipboard.
  66      *
  67      * @since 1.5
  68      */
  69     private final Set<FlavorListener> flavorListeners = new HashSet<>();
  70 
  71     /**
  72      * A set of <code>DataFlavor</code>s that is available on
  73      * this local clipboard. It is used for tracking changes
  74      * of <code>DataFlavor</code>s available on this clipboard.
  75      *
  76      * @since 1.5
  77      */
  78     private Set<DataFlavor> currentDataFlavors;
  79 
  80     /**
  81      * Creates a clipboard object.
  82      * @param name for the clipboard
  83      * @see java.awt.Toolkit#getSystemClipboard
  84      */
  85     public Clipboard(String name) {
  86         this.name = name;
  87     }
  88 
  89     /**
  90      * Returns the name of this clipboard object.
  91      * @return the name of this clipboard object
  92      *
  93      * @see java.awt.Toolkit#getSystemClipboard
  94      */
  95     public String getName() {
  96         return name;
  97     }
  98 
  99     /**
 100      * Sets the current contents of the clipboard to the specified
 101      * transferable object and registers the specified clipboard owner
 102      * as the owner of the new contents.
 103      * <p>
 104      * If there is an existing owner different from the argument
 105      * <code>owner</code>, that owner is notified that it no longer
 106      * holds ownership of the clipboard contents via an invocation
 107      * of <code>ClipboardOwner.lostOwnership()</code> on that owner.
 108      * An implementation of <code>setContents()</code> is free not
 109      * to invoke <code>lostOwnership()</code> directly from this method.
 110      * For example, <code>lostOwnership()</code> may be invoked later on
 111      * a different thread. The same applies to <code>FlavorListener</code>s
 112      * registered on this clipboard.
 113      * <p>
 114      * The method throws <code>IllegalStateException</code> if the clipboard
 115      * is currently unavailable. For example, on some platforms, the system
 116      * clipboard is unavailable while it is accessed by another application.
 117      *
 118      * @param contents the transferable object representing the
 119      *                 clipboard content
 120      * @param owner the object which owns the clipboard content
 121      * @throws IllegalStateException if the clipboard is currently unavailable
 122      * @see java.awt.Toolkit#getSystemClipboard
 123      */
 124     public synchronized void setContents(Transferable contents, ClipboardOwner owner) {
 125         final ClipboardOwner oldOwner = this.owner;
 126         final Transferable oldContents = this.contents;
 127 
 128         this.owner = owner;
 129         this.contents = contents;
 130 
 131         if (oldOwner != null && oldOwner != owner) {
 132             EventQueue.invokeLater(() -> oldOwner.lostOwnership(Clipboard.this, oldContents));
 133         }
 134         fireFlavorsChanged();
 135     }
 136 
 137     /**
 138      * Returns a transferable object representing the current contents
 139      * of the clipboard.  If the clipboard currently has no contents,
 140      * it returns <code>null</code>. The parameter Object requestor is
 141      * not currently used.  The method throws
 142      * <code>IllegalStateException</code> if the clipboard is currently
 143      * unavailable.  For example, on some platforms, the system clipboard is
 144      * unavailable while it is accessed by another application.
 145      *
 146      * @param requestor the object requesting the clip data  (not used)
 147      * @return the current transferable object on the clipboard
 148      * @throws IllegalStateException if the clipboard is currently unavailable
 149      * @see java.awt.Toolkit#getSystemClipboard
 150      */
 151     public synchronized Transferable getContents(Object requestor) {
 152         return contents;
 153     }
 154 
 155 
 156     /**
 157      * Returns an array of <code>DataFlavor</code>s in which the current
 158      * contents of this clipboard can be provided. If there are no
 159      * <code>DataFlavor</code>s available, this method returns a zero-length
 160      * array.
 161      *
 162      * @return an array of <code>DataFlavor</code>s in which the current
 163      *         contents of this clipboard can be provided
 164      *
 165      * @throws IllegalStateException if this clipboard is currently unavailable
 166      *
 167      * @since 1.5
 168      */
 169     public DataFlavor[] getAvailableDataFlavors() {
 170         Transferable cntnts = getContents(null);
 171         if (cntnts == null) {
 172             return new DataFlavor[0];
 173         }
 174         return cntnts.getTransferDataFlavors();
 175     }
 176 
 177     /**
 178      * Returns whether or not the current contents of this clipboard can be
 179      * provided in the specified <code>DataFlavor</code>.
 180      *
 181      * @param flavor the requested <code>DataFlavor</code> for the contents
 182      *
 183      * @return <code>true</code> if the current contents of this clipboard
 184      *         can be provided in the specified <code>DataFlavor</code>;
 185      *         <code>false</code> otherwise
 186      *
 187      * @throws NullPointerException if <code>flavor</code> is <code>null</code>
 188      * @throws IllegalStateException if this clipboard is currently unavailable
 189      *
 190      * @since 1.5
 191      */
 192     public boolean isDataFlavorAvailable(DataFlavor flavor) {
 193         if (flavor == null) {
 194             throw new NullPointerException("flavor");
 195         }
 196 
 197         Transferable cntnts = getContents(null);
 198         if (cntnts == null) {
 199             return false;
 200         }
 201         return cntnts.isDataFlavorSupported(flavor);
 202     }
 203 
 204     /**
 205      * Returns an object representing the current contents of this clipboard
 206      * in the specified <code>DataFlavor</code>.
 207      * The class of the object returned is defined by the representation
 208      * class of <code>flavor</code>.
 209      *
 210      * @param flavor the requested <code>DataFlavor</code> for the contents
 211      *
 212      * @return an object representing the current contents of this clipboard
 213      *         in the specified <code>DataFlavor</code>
 214      *
 215      * @throws NullPointerException if <code>flavor</code> is <code>null</code>
 216      * @throws IllegalStateException if this clipboard is currently unavailable
 217      * @throws UnsupportedFlavorException if the requested <code>DataFlavor</code>
 218      *         is not available
 219      * @throws IOException if the data in the requested <code>DataFlavor</code>
 220      *         can not be retrieved
 221      *
 222      * @see DataFlavor#getRepresentationClass
 223      *
 224      * @since 1.5
 225      */
 226     public Object getData(DataFlavor flavor)
 227         throws UnsupportedFlavorException, IOException {
 228         if (flavor == null) {
 229             throw new NullPointerException("flavor");
 230         }
 231 
 232         Transferable cntnts = getContents(null);
 233         if (cntnts == null) {
 234             throw new UnsupportedFlavorException(flavor);
 235         }
 236         return cntnts.getTransferData(flavor);
 237     }
 238 
 239 
 240     /**
 241      * Registers the specified <code>FlavorListener</code> to receive
 242      * <code>FlavorEvent</code>s from this clipboard.
 243      * If <code>listener</code> is <code>null</code>, no exception
 244      * is thrown and no action is performed.
 245      *
 246      * @param listener the listener to be added
 247      *
 248      * @see #removeFlavorListener
 249      * @see #getFlavorListeners
 250      * @see FlavorListener
 251      * @see FlavorEvent
 252      * @since 1.5
 253      */
 254     public synchronized void addFlavorListener(FlavorListener listener) {
 255         if (listener == null) {
 256             return;
 257         }
 258         if (flavorListeners.isEmpty()) {
 259             currentDataFlavors = getAvailableDataFlavorSet();
 260         }
 261         flavorListeners.add(listener);
 262     }
 263 
 264     /**
 265      * Removes the specified <code>FlavorListener</code> so that it no longer
 266      * receives <code>FlavorEvent</code>s from this <code>Clipboard</code>.
 267      * This method performs no function, nor does it throw an exception, if
 268      * the listener specified by the argument was not previously added to this
 269      * <code>Clipboard</code>.
 270      * If <code>listener</code> is <code>null</code>, no exception
 271      * is thrown and no action is performed.
 272      *
 273      * @param listener the listener to be removed
 274      *
 275      * @see #addFlavorListener
 276      * @see #getFlavorListeners
 277      * @see FlavorListener
 278      * @see FlavorEvent
 279      * @since 1.5
 280      */
 281     public synchronized void removeFlavorListener(FlavorListener listener) {
 282         if (listener == null) {
 283             return;
 284         }
 285         flavorListeners.remove(listener);
 286     }
 287 
 288     /**
 289      * Returns an array of all the <code>FlavorListener</code>s currently
 290      * registered on this <code>Clipboard</code>.
 291      *
 292      * @return all of this clipboard's <code>FlavorListener</code>s or an empty
 293      *         array if no listeners are currently registered
 294      * @see #addFlavorListener
 295      * @see #removeFlavorListener
 296      * @see FlavorListener
 297      * @see FlavorEvent
 298      * @since 1.5
 299      */
 300     public synchronized FlavorListener[] getFlavorListeners() {
 301         return flavorListeners.toArray(new FlavorListener[flavorListeners.size()]);
 302     }
 303 
 304     /**
 305      * Checks change of the <code>DataFlavor</code>s and, if necessary,
 306      * notifies all listeners that have registered interest for notification
 307      * on <code>FlavorEvent</code>s.
 308      *
 309      * @since 1.5
 310      */
 311     private void fireFlavorsChanged() {
 312         Set<DataFlavor> prevDataFlavors = currentDataFlavors;
 313         currentDataFlavors = getAvailableDataFlavorSet();
 314         if (prevDataFlavors != null && currentDataFlavors != null
 315                 && prevDataFlavors.equals(currentDataFlavors)) {
 316             return;
 317         }
 318         flavorListeners.forEach(listener ->
 319                 EventQueue.invokeLater(() ->
 320                         listener.flavorsChanged(new FlavorEvent(Clipboard.this))));
 321     }
 322 
 323     /**
 324      * Returns a set of <code>DataFlavor</code>s currently available
 325      * on this clipboard.
 326      *
 327      * @return a set of <code>DataFlavor</code>s currently available
 328      *         on this clipboard
 329      *
 330      * @since 1.5
 331      */
 332     private Set<DataFlavor> getAvailableDataFlavorSet() {
 333         Set<DataFlavor> set = new HashSet<>();
 334         Transferable contents = getContents(null);
 335         if (contents != null) {
 336             DataFlavor[] flavors = contents.getTransferDataFlavors();
 337             if (flavors != null) {
 338                 set.addAll(Arrays.asList(flavors));
 339             }
 340         }
 341         return set;
 342     }
 343 }