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