jdk/src/share/classes/java/awt/datatransfer/Clipboard.java
author erikj
Thu, 07 Jun 2012 18:05:09 -0700
changeset 12813 c10ab96dcf41
parent 5506 202f599c92aa
child 15994 5c8a3d840366
permissions -rw-r--r--
7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI Reviewed-by: ohair, ohrstrom, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1996, 2006, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    protected ClipboardOwner owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    protected Transferable contents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * An aggregate of flavor listeners registered on this local clipboard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private EventListenerAggregate flavorListeners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * A set of <code>DataFlavor</code>s that is available on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * this local clipboard. It is used for tracking changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * of <code>DataFlavor</code>s available on this clipboard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private Set currentDataFlavors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Creates a clipboard object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @see java.awt.Toolkit#getSystemClipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public Clipboard(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Returns the name of this clipboard object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @see java.awt.Toolkit#getSystemClipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Sets the current contents of the clipboard to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * transferable object and registers the specified clipboard owner
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * as the owner of the new contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * If there is an existing owner different from the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * <code>owner</code>, that owner is notified that it no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * holds ownership of the clipboard contents via an invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * of <code>ClipboardOwner.lostOwnership()</code> on that owner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * An implementation of <code>setContents()</code> is free not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * to invoke <code>lostOwnership()</code> directly from this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * For example, <code>lostOwnership()</code> may be invoked later on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * a different thread. The same applies to <code>FlavorListener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * registered on this clipboard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * The method throws <code>IllegalStateException</code> if the clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * is currently unavailable. For example, on some platforms, the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * clipboard is unavailable while it is accessed by another application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param contents the transferable object representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *                 clipboard content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param owner the object which owns the clipboard content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @throws IllegalStateException if the clipboard is currently unavailable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @see java.awt.Toolkit#getSystemClipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public synchronized void setContents(Transferable contents, ClipboardOwner owner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        final ClipboardOwner oldOwner = this.owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        final Transferable oldContents = this.contents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        this.owner = owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        this.contents = contents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (oldOwner != null && oldOwner != owner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            EventQueue.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    oldOwner.lostOwnership(Clipboard.this, oldContents);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        fireFlavorsChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Returns a transferable object representing the current contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * of the clipboard.  If the clipboard currently has no contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * it returns <code>null</code>. The parameter Object requestor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * not currently used.  The method throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * <code>IllegalStateException</code> if the clipboard is currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * unavailable.  For example, on some platforms, the system clipboard is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * unavailable while it is accessed by another application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param requestor the object requesting the clip data  (not used)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Returns an array of <code>DataFlavor</code>s in which the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * contents of this clipboard can be provided. If there are no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * <code>DataFlavor</code>s available, this method returns a zero-length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @return an array of <code>DataFlavor</code>s in which the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *         contents of this clipboard can be provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @throws IllegalStateException if this clipboard is currently unavailable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public DataFlavor[] getAvailableDataFlavors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        Transferable cntnts = getContents(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (cntnts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            return new DataFlavor[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return cntnts.getTransferDataFlavors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Returns whether or not the current contents of this clipboard can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * provided in the specified <code>DataFlavor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param flavor the requested <code>DataFlavor</code> for the contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @return <code>true</code> if the current contents of this clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *         can be provided in the specified <code>DataFlavor</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *         <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @throws NullPointerException if <code>flavor</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @throws IllegalStateException if this clipboard is currently unavailable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public boolean isDataFlavorAvailable(DataFlavor flavor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (flavor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            throw new NullPointerException("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
        Transferable cntnts = getContents(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (cntnts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return cntnts.isDataFlavorSupported(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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Returns an object representing the current contents of this clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * in the specified <code>DataFlavor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * The class of the object returned is defined by the representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * class of <code>flavor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @param flavor the requested <code>DataFlavor</code> for the contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @return 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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @throws NullPointerException if <code>flavor</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @throws IllegalStateException if this clipboard is currently unavailable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @throws UnsupportedFlavorException if the requested <code>DataFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *         is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @throws IOException if the data in the requested <code>DataFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *         can not be retrieved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @see DataFlavor#getRepresentationClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public Object getData(DataFlavor flavor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        throws UnsupportedFlavorException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (flavor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            throw new NullPointerException("flavor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        Transferable cntnts = getContents(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (cntnts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            throw new UnsupportedFlavorException(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return cntnts.getTransferData(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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Registers the specified <code>FlavorListener</code> to receive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * <code>FlavorEvent</code>s from this clipboard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * If <code>listener</code> is <code>null</code>, no exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * is thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @param listener the listener to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @see #removeFlavorListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @see #getFlavorListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @see FlavorListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @see FlavorEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    public synchronized void addFlavorListener(FlavorListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (flavorListeners == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            currentDataFlavors = getAvailableDataFlavorSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            flavorListeners = new EventListenerAggregate(FlavorListener.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        flavorListeners.add(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Removes the specified <code>FlavorListener</code> so that it no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * receives <code>FlavorEvent</code>s from this <code>Clipboard</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * This method performs no function, nor does it throw an exception, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * the listener specified by the argument was not previously added to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * <code>Clipboard</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * If <code>listener</code> is <code>null</code>, no exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * is thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @param listener the listener to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @see #addFlavorListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @see #getFlavorListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @see FlavorListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @see FlavorEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public synchronized void removeFlavorListener(FlavorListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if (listener == null || flavorListeners == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        flavorListeners.remove(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Returns an array of all the <code>FlavorListener</code>s currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * registered on this <code>Clipboard</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @return all of this clipboard's <code>FlavorListener</code>s or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *         array if no listeners are currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @see #addFlavorListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @see #removeFlavorListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @see FlavorListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @see FlavorEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public synchronized FlavorListener[] getFlavorListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return flavorListeners == null ? new FlavorListener[0] :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                (FlavorListener[])flavorListeners.getListenersCopy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Checks change of the <code>DataFlavor</code>s and, if necessary,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * notifies all listeners that have registered interest for notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * on <code>FlavorEvent</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    private void fireFlavorsChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        if (flavorListeners == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        Set prevDataFlavors = currentDataFlavors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        currentDataFlavors = getAvailableDataFlavorSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (prevDataFlavors.equals(currentDataFlavors)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        FlavorListener[] flavorListenerArray =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                (FlavorListener[])flavorListeners.getListenersInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        for (int i = 0; i < flavorListenerArray.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            final FlavorListener listener = flavorListenerArray[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            EventQueue.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    listener.flavorsChanged(new FlavorEvent(Clipboard.this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            });
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Returns a set of <code>DataFlavor</code>s currently available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * on this clipboard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @return a set of <code>DataFlavor</code>s currently available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *         on this clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    private Set getAvailableDataFlavorSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        Set set = new HashSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        Transferable contents = getContents(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if (contents != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            DataFlavor[] flavors = contents.getTransferDataFlavors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            if (flavors != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                set.addAll(Arrays.asList(flavors));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
}