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.Objects;
  31 import java.util.Set;
  32 import java.util.HashSet;
  33 import java.util.Arrays;
  34 
  35 import java.io.IOException;
  36 
  37 /**
  38  * A class that implements a mechanism to transfer data using
  39  * cut/copy/paste operations.
  40  * <p>
  41  * {@link FlavorListener}s may be registered on an instance of the
  42  * Clipboard class to be notified about changes to the set of
  43  * {@link DataFlavor}s available on this clipboard (see
  44  * {@link #addFlavorListener}).
  45  *
  46  * @see java.awt.Toolkit#getSystemClipboard
  47  * @see java.awt.Toolkit#getSystemSelection
  48  *
  49  * @author      Amy Fowler
  50  * @author      Alexander Gerasimov
  51  */
  52 public class Clipboard {
  53 
  54     String name;
  55 
  56     /**
  57      * The owner of the clipboard.
  58      */
  59     protected ClipboardOwner owner;
  60     /**
  61      * Contents of the clipboard.
  62      */
  63     protected Transferable contents;
  64 
  65     /**
  66      * An aggregate of flavor listeners registered on this local clipboard.
  67      *
  68      * @since 1.5
  69      */
  70     private final Set<FlavorListener> flavorListeners = new HashSet<>();
  71 
  72     /**
  73      * A set of <code>DataFlavor</code>s that is available on
  74      * this local clipboard. It is used for tracking changes
  75      * of <code>DataFlavor</code>s available on this clipboard.
  76      *
  77      * @since 1.5
  78      */
  79     private Set<DataFlavor> currentDataFlavors;
  80 
  81     /**
  82      * Creates a clipboard object.
  83      * @param name for the clipboard
  84      * @see java.awt.Toolkit#getSystemClipboard
  85      */
  86     public Clipboard(String name) {
  87         this.name = name;
  88     }
  89 
  90     /**
  91      * Returns the name of this clipboard object.
  92      * @return the name of this clipboard object
  93      *
  94      * @see java.awt.Toolkit#getSystemClipboard
  95      */
  96     public String getName() {
  97         return name;
  98     }
  99 
 100     /**
 101      * Sets the current contents of the clipboard to the specified
 102      * transferable object and registers the specified clipboard owner
 103      * as the owner of the new contents.
 104      * <p>
 105      * If there is an existing owner different from the argument
 106      * <code>owner</code>, that owner is notified that it no longer
 107      * holds ownership of the clipboard contents via an invocation
 108      * of <code>ClipboardOwner.lostOwnership()</code> on that owner.
 109      * An implementation of <code>setContents()</code> is free not
 110      * to invoke <code>lostOwnership()</code> directly from this method.
 111      * For example, <code>lostOwnership()</code> may be invoked later on
 112      * a different thread. The same applies to <code>FlavorListener</code>s
 113      * registered on this clipboard.
 114      * <p>
 115      * The method throws <code>IllegalStateException</code> if the clipboard
 116      * is currently unavailable. For example, on some platforms, the system
 117      * clipboard is unavailable while it is accessed by another application.
 118      *
 119      * @param contents the transferable object representing the
 120      *                 clipboard content
 121      * @param owner the object which owns the clipboard content
 122      * @throws IllegalStateException if the clipboard is currently unavailable
 123      * @see java.awt.Toolkit#getSystemClipboard
 124      */
 125     public synchronized void setContents(Transferable contents, ClipboardOwner owner) {
 126         final ClipboardOwner oldOwner = this.owner;
 127         final Transferable oldContents = this.contents;
 128 
 129         this.owner = owner;
 130         this.contents = contents;
 131 
 132         if (oldOwner != null && oldOwner != owner) {
 133             EventQueue.invokeLater(() -> oldOwner.lostOwnership(Clipboard.this, oldContents));
 134         }
 135         fireFlavorsChanged();
 136     }
 137 
 138     /**
 139      * Returns a transferable object representing the current contents
 140      * of the clipboard.  If the clipboard currently has no contents,
 141      * it returns <code>null</code>. The parameter Object requestor is
 142      * not currently used.  The method throws
 143      * <code>IllegalStateException</code> if the clipboard is currently
 144      * unavailable.  For example, on some platforms, the system clipboard is
 145      * unavailable while it is accessed by another application.
 146      *
 147      * @param requestor the object requesting the clip data  (not used)
 148      * @return the current transferable object on the clipboard
 149      * @throws IllegalStateException if the clipboard is currently unavailable
 150      * @see java.awt.Toolkit#getSystemClipboard
 151      */
 152     public synchronized Transferable getContents(Object requestor) {
 153         return contents;
 154     }
 155 
 156 
 157     /**
 158      * Returns an array of <code>DataFlavor</code>s in which the current
 159      * contents of this clipboard can be provided. If there are no
 160      * <code>DataFlavor</code>s available, this method returns a zero-length
 161      * array.
 162      *
 163      * @return an array of <code>DataFlavor</code>s in which the current
 164      *         contents of this clipboard can be provided
 165      *
 166      * @throws IllegalStateException if this clipboard is currently unavailable
 167      *
 168      * @since 1.5
 169      */
 170     public DataFlavor[] getAvailableDataFlavors() {
 171         Transferable cntnts = getContents(null);
 172         if (cntnts == null) {
 173             return new DataFlavor[0];
 174         }
 175         return cntnts.getTransferDataFlavors();
 176     }
 177 
 178     /**
 179      * Returns whether or not the current contents of this clipboard can be
 180      * provided in the specified <code>DataFlavor</code>.
 181      *
 182      * @param flavor the requested <code>DataFlavor</code> for the contents
 183      *
 184      * @return <code>true</code> if the current contents of this clipboard
 185      *         can be provided in the specified <code>DataFlavor</code>;
 186      *         <code>false</code> otherwise
 187      *
 188      * @throws NullPointerException if <code>flavor</code> is <code>null</code>
 189      * @throws IllegalStateException if this clipboard is currently unavailable
 190      *
 191      * @since 1.5
 192      */
 193     public boolean isDataFlavorAvailable(DataFlavor flavor) {
 194         if (flavor == null) {
 195             throw new NullPointerException("flavor");
 196         }
 197 
 198         Transferable cntnts = getContents(null);
 199         if (cntnts == null) {
 200             return false;
 201         }
 202         return cntnts.isDataFlavorSupported(flavor);
 203     }
 204 
 205     /**
 206      * Returns an object representing the current contents of this clipboard
 207      * in the specified <code>DataFlavor</code>.
 208      * The class of the object returned is defined by the representation
 209      * class of <code>flavor</code>.
 210      *
 211      * @param flavor the requested <code>DataFlavor</code> for the contents
 212      *
 213      * @return an object representing the current contents of this clipboard
 214      *         in the specified <code>DataFlavor</code>
 215      *
 216      * @throws NullPointerException if <code>flavor</code> is <code>null</code>
 217      * @throws IllegalStateException if this clipboard is currently unavailable
 218      * @throws UnsupportedFlavorException if the requested <code>DataFlavor</code>
 219      *         is not available
 220      * @throws IOException if the data in the requested <code>DataFlavor</code>
 221      *         can not be retrieved
 222      *
 223      * @see DataFlavor#getRepresentationClass
 224      *
 225      * @since 1.5
 226      */
 227     public Object getData(DataFlavor flavor)
 228         throws UnsupportedFlavorException, IOException {
 229         if (flavor == null) {
 230             throw new NullPointerException("flavor");
 231         }
 232 
 233         Transferable cntnts = getContents(null);
 234         if (cntnts == null) {
 235             throw new UnsupportedFlavorException(flavor);
 236         }
 237         return cntnts.getTransferData(flavor);
 238     }
 239 
 240 
 241     /**
 242      * Registers the specified <code>FlavorListener</code> to receive
 243      * <code>FlavorEvent</code>s from this clipboard.
 244      * If <code>listener</code> is <code>null</code>, no exception
 245      * is thrown and no action is performed.
 246      *
 247      * @param listener the listener to be added
 248      *
 249      * @see #removeFlavorListener
 250      * @see #getFlavorListeners
 251      * @see FlavorListener
 252      * @see FlavorEvent
 253      * @since 1.5
 254      */
 255     public synchronized void addFlavorListener(FlavorListener listener) {
 256         if (listener == null) {
 257             return;
 258         }
 259         if (flavorListeners.isEmpty()) {
 260             currentDataFlavors = getAvailableDataFlavorSet();
 261         }
 262         flavorListeners.add(listener);
 263     }
 264 
 265     /**
 266      * Removes the specified <code>FlavorListener</code> so that it no longer
 267      * receives <code>FlavorEvent</code>s from this <code>Clipboard</code>.
 268      * This method performs no function, nor does it throw an exception, if
 269      * the listener specified by the argument was not previously added to this
 270      * <code>Clipboard</code>.
 271      * If <code>listener</code> is <code>null</code>, no exception
 272      * is thrown and no action is performed.
 273      *
 274      * @param listener the listener to be removed
 275      *
 276      * @see #addFlavorListener
 277      * @see #getFlavorListeners
 278      * @see FlavorListener
 279      * @see FlavorEvent
 280      * @since 1.5
 281      */
 282     public synchronized void removeFlavorListener(FlavorListener listener) {
 283         if (listener == null) {
 284             return;
 285         }
 286         flavorListeners.remove(listener);
 287     }
 288 
 289     /**
 290      * Returns an array of all the <code>FlavorListener</code>s currently
 291      * registered on this <code>Clipboard</code>.
 292      *
 293      * @return all of this clipboard's <code>FlavorListener</code>s or an empty
 294      *         array if no listeners are currently registered
 295      * @see #addFlavorListener
 296      * @see #removeFlavorListener
 297      * @see FlavorListener
 298      * @see FlavorEvent
 299      * @since 1.5
 300      */
 301     public synchronized FlavorListener[] getFlavorListeners() {
 302         return flavorListeners.toArray(new FlavorListener[flavorListeners.size()]);
 303     }
 304 
 305     /**
 306      * Checks change of the <code>DataFlavor</code>s and, if necessary,
 307      * notifies all listeners that have registered interest for notification
 308      * on <code>FlavorEvent</code>s.
 309      *
 310      * @since 1.5
 311      */
 312     private void fireFlavorsChanged() {
 313         Set<DataFlavor> prevDataFlavors = currentDataFlavors;
 314         currentDataFlavors = getAvailableDataFlavorSet();
 315         if (Objects.equals(prevDataFlavors, 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 }