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