jdk/src/java.desktop/share/classes/java/awt/datatransfer/Clipboard.java
author ddehaven
Tue, 19 Aug 2014 10:32:16 -0700
changeset 26037 508779ce6619
parent 26010 jdk/src/share/classes/java/awt/datatransfer/Clipboard.java@9a3cf8ee0776
parent 25859 jdk/src/share/classes/java/awt/datatransfer/Clipboard.java@3317bb8137f4
permissions -rw-r--r--
Merge
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
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24172
diff changeset
    28
import sun.datatransfer.DataFlavorUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
24153
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 22281
diff changeset
    30
import java.util.Objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * A class that implements a mechanism to transfer data using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * cut/copy/paste operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * {@link FlavorListener}s may be registered on an instance of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Clipboard class to be notified about changes to the set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * {@link DataFlavor}s available on this clipboard (see
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * {@link #addFlavorListener}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see java.awt.Toolkit#getSystemClipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @see java.awt.Toolkit#getSystemSelection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author      Amy Fowler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author      Alexander Gerasimov
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public class Clipboard {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
22281
830790e8640d 8031467: Fix doclint missing issues in java.awt.{dnd[.peer], datatransfer}
darcy
parents: 15994
diff changeset
    56
    /**
830790e8640d 8031467: Fix doclint missing issues in java.awt.{dnd[.peer], datatransfer}
darcy
parents: 15994
diff changeset
    57
     * The owner of the clipboard.
830790e8640d 8031467: Fix doclint missing issues in java.awt.{dnd[.peer], datatransfer}
darcy
parents: 15994
diff changeset
    58
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    protected ClipboardOwner owner;
22281
830790e8640d 8031467: Fix doclint missing issues in java.awt.{dnd[.peer], datatransfer}
darcy
parents: 15994
diff changeset
    60
    /**
830790e8640d 8031467: Fix doclint missing issues in java.awt.{dnd[.peer], datatransfer}
darcy
parents: 15994
diff changeset
    61
     * Contents of the clipboard.
830790e8640d 8031467: Fix doclint missing issues in java.awt.{dnd[.peer], datatransfer}
darcy
parents: 15994
diff changeset
    62
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    protected Transferable contents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * An aggregate of flavor listeners registered on this local clipboard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
24172
814fb69fa9a5 8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents: 24153
diff changeset
    70
    private Set<FlavorListener> flavorListeners;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * A set of <code>DataFlavor</code>s that is available on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * this local clipboard. It is used for tracking changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * of <code>DataFlavor</code>s available on this clipboard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
15994
5c8a3d840366 8007295: Reduce number of warnings in awt classes
mcherkas
parents: 12813
diff changeset
    79
    private Set<DataFlavor> currentDataFlavors;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Creates a clipboard object.
22281
830790e8640d 8031467: Fix doclint missing issues in java.awt.{dnd[.peer], datatransfer}
darcy
parents: 15994
diff changeset
    83
     * @param name for the clipboard
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @see java.awt.Toolkit#getSystemClipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public Clipboard(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * 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
    92
     * @return the name of this clipboard object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @see java.awt.Toolkit#getSystemClipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Sets the current contents of the clipboard to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * transferable object and registers the specified clipboard owner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * as the owner of the new contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * If there is an existing owner different from the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * <code>owner</code>, that owner is notified that it no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * holds ownership of the clipboard contents via an invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * of <code>ClipboardOwner.lostOwnership()</code> on that owner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * An implementation of <code>setContents()</code> is free not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * to invoke <code>lostOwnership()</code> directly from this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * For example, <code>lostOwnership()</code> may be invoked later on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * a different thread. The same applies to <code>FlavorListener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * registered on this clipboard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * The method throws <code>IllegalStateException</code> if the clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * is currently unavailable. For example, on some platforms, the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * clipboard is unavailable while it is accessed by another application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param contents the transferable object representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *                 clipboard content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param owner the object which owns the clipboard content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @throws IllegalStateException if the clipboard is currently unavailable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @see java.awt.Toolkit#getSystemClipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public synchronized void setContents(Transferable contents, ClipboardOwner owner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        final ClipboardOwner oldOwner = this.owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        final Transferable oldContents = this.contents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        this.owner = owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        this.contents = contents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (oldOwner != null && oldOwner != owner) {
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24172
diff changeset
   133
            DataFlavorUtil.getDesktopService().invokeOnEventThread(() ->
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24172
diff changeset
   134
                    oldOwner.lostOwnership(Clipboard.this, oldContents));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        fireFlavorsChanged();
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
     * Returns a transferable object representing the current contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * of the clipboard.  If the clipboard currently has no contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * it returns <code>null</code>. The parameter Object requestor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * not currently used.  The method throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * <code>IllegalStateException</code> if the clipboard is currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * unavailable.  For example, on some platforms, the system clipboard is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * unavailable while it is accessed by another application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param requestor the object requesting the clip data  (not used)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @return the current transferable object on the clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @throws IllegalStateException if the clipboard is currently unavailable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @see java.awt.Toolkit#getSystemClipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public synchronized Transferable getContents(Object requestor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return contents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Returns an array of <code>DataFlavor</code>s in which the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * contents of this clipboard can be provided. If there are no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * <code>DataFlavor</code>s available, this method returns a zero-length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @return an array of <code>DataFlavor</code>s in which the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *         contents of this clipboard can be provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @throws IllegalStateException if this clipboard is currently unavailable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public DataFlavor[] getAvailableDataFlavors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        Transferable cntnts = getContents(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (cntnts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return new DataFlavor[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return cntnts.getTransferDataFlavors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Returns whether or not the current contents of this clipboard can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * provided in the specified <code>DataFlavor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param flavor the requested <code>DataFlavor</code> for the contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @return <code>true</code> if the current contents of this clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *         can be provided in the specified <code>DataFlavor</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *         <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @throws NullPointerException if <code>flavor</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @throws IllegalStateException if this clipboard is currently unavailable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public boolean isDataFlavorAvailable(DataFlavor flavor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (flavor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            throw new NullPointerException("flavor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        Transferable cntnts = getContents(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (cntnts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return cntnts.isDataFlavorSupported(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Returns an object representing the current contents of this clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * in the specified <code>DataFlavor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * The class of the object returned is defined by the representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * class of <code>flavor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @param flavor the requested <code>DataFlavor</code> for the contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @return an object representing the current contents of this clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *         in the specified <code>DataFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @throws NullPointerException if <code>flavor</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @throws IllegalStateException if this clipboard is currently unavailable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @throws UnsupportedFlavorException if the requested <code>DataFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *         is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @throws IOException if the data in the requested <code>DataFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *         can not be retrieved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @see DataFlavor#getRepresentationClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public Object getData(DataFlavor flavor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        throws UnsupportedFlavorException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (flavor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            throw new NullPointerException("flavor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        Transferable cntnts = getContents(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (cntnts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            throw new UnsupportedFlavorException(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return cntnts.getTransferData(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Registers the specified <code>FlavorListener</code> to receive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * <code>FlavorEvent</code>s from this clipboard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * If <code>listener</code> is <code>null</code>, no exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * is thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @param listener the listener to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @see #removeFlavorListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @see #getFlavorListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @see FlavorListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @see FlavorEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public synchronized void addFlavorListener(FlavorListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
24172
814fb69fa9a5 8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents: 24153
diff changeset
   260
814fb69fa9a5 8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents: 24153
diff changeset
   261
        if (flavorListeners == null) {
814fb69fa9a5 8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents: 24153
diff changeset
   262
            flavorListeners = new HashSet<>();
814fb69fa9a5 8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents: 24153
diff changeset
   263
            currentDataFlavors = getAvailableDataFlavorSet();
814fb69fa9a5 8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents: 24153
diff changeset
   264
        }
814fb69fa9a5 8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents: 24153
diff changeset
   265
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        flavorListeners.add(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Removes the specified <code>FlavorListener</code> so that it no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * receives <code>FlavorEvent</code>s from this <code>Clipboard</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * This method performs no function, nor does it throw an exception, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * the listener specified by the argument was not previously added to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * <code>Clipboard</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * If <code>listener</code> is <code>null</code>, no exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * is thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param listener the listener to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @see #addFlavorListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @see #getFlavorListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @see FlavorListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @see FlavorEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public synchronized void removeFlavorListener(FlavorListener listener) {
24172
814fb69fa9a5 8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents: 24153
diff changeset
   287
        if (listener == null || flavorListeners == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        flavorListeners.remove(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Returns an array of all the <code>FlavorListener</code>s currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * registered on this <code>Clipboard</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @return all of this clipboard's <code>FlavorListener</code>s or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *         array if no listeners are currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @see #addFlavorListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @see #removeFlavorListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @see FlavorListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @see FlavorEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public synchronized FlavorListener[] getFlavorListeners() {
24172
814fb69fa9a5 8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents: 24153
diff changeset
   306
        return flavorListeners == null ? new FlavorListener[0] :
814fb69fa9a5 8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents: 24153
diff changeset
   307
            flavorListeners.toArray(new FlavorListener[flavorListeners.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Checks change of the <code>DataFlavor</code>s and, if necessary,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * notifies all listeners that have registered interest for notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * on <code>FlavorEvent</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    private void fireFlavorsChanged() {
24172
814fb69fa9a5 8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents: 24153
diff changeset
   318
        if (flavorListeners == null) {
814fb69fa9a5 8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents: 24153
diff changeset
   319
            return;
814fb69fa9a5 8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents: 24153
diff changeset
   320
        }
814fb69fa9a5 8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents: 24153
diff changeset
   321
15994
5c8a3d840366 8007295: Reduce number of warnings in awt classes
mcherkas
parents: 12813
diff changeset
   322
        Set<DataFlavor> prevDataFlavors = currentDataFlavors;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        currentDataFlavors = getAvailableDataFlavorSet();
24153
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 22281
diff changeset
   324
        if (Objects.equals(prevDataFlavors, currentDataFlavors)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
24153
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 22281
diff changeset
   327
        flavorListeners.forEach(listener ->
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 24172
diff changeset
   328
                DataFlavorUtil.getDesktopService().invokeOnEventThread(() ->
24153
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 22281
diff changeset
   329
                        listener.flavorsChanged(new FlavorEvent(Clipboard.this))));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * Returns a set of <code>DataFlavor</code>s currently available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * on this clipboard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @return a set of <code>DataFlavor</code>s currently available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *         on this clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
15994
5c8a3d840366 8007295: Reduce number of warnings in awt classes
mcherkas
parents: 12813
diff changeset
   341
    private Set<DataFlavor> getAvailableDataFlavorSet() {
5c8a3d840366 8007295: Reduce number of warnings in awt classes
mcherkas
parents: 12813
diff changeset
   342
        Set<DataFlavor> set = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        Transferable contents = getContents(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (contents != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            DataFlavor[] flavors = contents.getTransferDataFlavors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            if (flavors != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                set.addAll(Arrays.asList(flavors));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        return set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
}