jdk/src/share/classes/sun/awt/datatransfer/SunClipboard.java
author pchelko
Wed, 02 Apr 2014 17:37:34 +0400
changeset 24153 2c19973ab9f2
parent 23279 16c1ddb7b66a
child 24183 db8b6b7d25c0
permissions -rw-r--r--
6463901: Either generify or deprecate sun.awt.EventListenerAggregate Reviewed-by: anthony, serb
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) 1999, 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 sun.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.awt.datatransfer.Clipboard;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.datatransfer.FlavorTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.datatransfer.SystemFlavorMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.datatransfer.Transferable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.datatransfer.ClipboardOwner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.datatransfer.DataFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.datatransfer.FlavorListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.datatransfer.FlavorEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.datatransfer.UnsupportedFlavorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
24153
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
    43
import java.util.Objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import sun.awt.PeerEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import sun.awt.SunToolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * Serves as a common, helper superclass for the Win32 and X11 system
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Clipboards.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author Danila Sinopalnikov
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author Alexander Gerasimov
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public abstract class SunClipboard extends Clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    implements PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public static final FlavorTable flavorMap =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        (FlavorTable)SystemFlavorMap.getDefaultFlavorMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private AppContext contentsContext = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private final Object CLIPBOARD_FLAVOR_LISTENER_KEY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * A number of <code>FlavorListener</code>s currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * on this clipboard across all <code>AppContext</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private volatile int numberOfFlavorListeners = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * A set of <code>DataFlavor</code>s that is available on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * this clipboard. It is used for tracking changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * of <code>DataFlavor</code>s available on this clipboard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private volatile Set currentDataFlavors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public SunClipboard(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        CLIPBOARD_FLAVOR_LISTENER_KEY = new StringBuffer(name + "_CLIPBOARD_FLAVOR_LISTENER_KEY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public synchronized void setContents(Transferable contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                         ClipboardOwner owner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // 4378007 : Toolkit.getSystemClipboard().setContents(null, null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        // should throw NPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (contents == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new NullPointerException("contents");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        initContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        final ClipboardOwner oldOwner = this.owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        final Transferable oldContents = this.contents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            this.owner = owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            this.contents = new TransferableProxy(contents, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            setContentsNative(contents);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            if (oldOwner != null && oldOwner != owner) {
24153
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   112
                EventQueue.invokeLater(() -> oldOwner.lostOwnership(SunClipboard.this, oldContents));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private synchronized void initContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        final AppContext context = AppContext.getAppContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (contentsContext != context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            // Need to synchronize on the AppContext to guarantee that it cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            // be disposed after the check, but before the listener is added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            synchronized (context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                if (context.isDisposed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    throw new IllegalStateException("Can't set contents from disposed AppContext");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                context.addPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    (AppContext.DISPOSED_PROPERTY_NAME, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            if (contentsContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                contentsContext.removePropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    (AppContext.DISPOSED_PROPERTY_NAME, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            contentsContext = context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public synchronized Transferable getContents(Object requestor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (contents != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return contents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return new ClipboardTransferable(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @return the contents of this clipboard if it has been set from the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *         AppContext as it is currently retrieved or null otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private synchronized Transferable getContextContents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        AppContext context = AppContext.getAppContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return (context == contentsContext) ? contents : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
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
     * @see java.awt.Clipboard#getAvailableDataFlavors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public DataFlavor[] getAvailableDataFlavors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        Transferable cntnts = getContextContents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (cntnts != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            return cntnts.getTransferDataFlavors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        long[] formats = getClipboardFormatsOpenClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return DataTransferer.getInstance().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            getFlavorsForFormatsAsArray(formats, flavorMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @see java.awt.Clipboard#isDataFlavorAvailable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public boolean isDataFlavorAvailable(DataFlavor flavor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (flavor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            throw new NullPointerException("flavor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        Transferable cntnts = getContextContents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (cntnts != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            return cntnts.isDataFlavorSupported(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        long[] formats = getClipboardFormatsOpenClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return formatArrayAsDataFlavorSet(formats).contains(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @see java.awt.Clipboard#getData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public Object getData(DataFlavor flavor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        throws UnsupportedFlavorException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (flavor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            throw new NullPointerException("flavor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        Transferable cntnts = getContextContents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (cntnts != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            return cntnts.getTransferData(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        long format = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        byte[] data = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        Transferable localeTransferable = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            openClipboard(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            long[] formats = getClipboardFormats();
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 5506
diff changeset
   215
            Long lFormat = DataTransferer.getInstance().
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    getFlavorsForFormats(formats, flavorMap).get(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            if (lFormat == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                throw new UnsupportedFlavorException(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            format = lFormat.longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            data = getClipboardData(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            if (DataTransferer.getInstance().isLocaleDependentTextFormat(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                localeTransferable = createLocaleTransferable(formats);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            closeClipboard();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        return DataTransferer.getInstance().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                translateBytes(data, flavor, format, localeTransferable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * The clipboard must be opened.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    protected Transferable createLocaleTransferable(long[] formats) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @throws IllegalStateException if the clipboard has not been opened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public void openClipboard(SunClipboard newOwner) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public void closeClipboard() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public abstract long getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public void propertyChange(PropertyChangeEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (AppContext.DISPOSED_PROPERTY_NAME.equals(evt.getPropertyName()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            Boolean.TRUE.equals(evt.getNewValue())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            final AppContext disposedContext = (AppContext)evt.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            lostOwnershipLater(disposedContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    protected void lostOwnershipImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        lostOwnershipLater(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Clears the clipboard state (contents, owner and contents context) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * notifies the current owner that ownership is lost. Does nothing if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * argument is not <code>null</code> and is not equal to the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * contents context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @param disposedContext the AppContext that is disposed or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *        <code>null</code> if the ownership is lost because another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *        application acquired ownership.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    protected void lostOwnershipLater(final AppContext disposedContext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        final AppContext context = this.contentsContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (context == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        final Runnable runnable = new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    final SunClipboard sunClipboard = SunClipboard.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    ClipboardOwner owner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    Transferable contents = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    synchronized (sunClipboard) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        final AppContext context = sunClipboard.contentsContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                        if (context == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                        if (disposedContext == null || context == disposedContext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                            owner = sunClipboard.owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                            contents = sunClipboard.contents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                            sunClipboard.contentsContext = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                            sunClipboard.owner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                            sunClipboard.contents = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                            sunClipboard.clearNativeContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                            context.removePropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                (AppContext.DISPOSED_PROPERTY_NAME, sunClipboard);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    if (owner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                        owner.lostOwnership(sunClipboard, contents);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        SunToolkit.postEvent(context, new PeerEvent(this, runnable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                                    PeerEvent.PRIORITY_EVENT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    protected abstract void clearNativeContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    protected abstract void setContentsNative(Transferable contents);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    protected long[] getClipboardFormatsOpenClose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            openClipboard(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            return getClipboardFormats();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            closeClipboard();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Returns zero-length array (not null) if the number of available formats is zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @throws IllegalStateException if formats could not be retrieved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    protected abstract long[] getClipboardFormats();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    protected abstract byte[] getClipboardData(long format) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    private static Set formatArrayAsDataFlavorSet(long[] formats) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return (formats == null) ? null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                DataTransferer.getInstance().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                getFlavorsForFormatsAsSet(formats, flavorMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    public synchronized void addFlavorListener(FlavorListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        AppContext appContext = AppContext.getAppContext();
24153
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   356
        Set<FlavorListener> flavorListeners = getFlavorListeners(appContext);
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   357
        if (flavorListeners == null) {
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   358
            flavorListeners = new HashSet<>();
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   359
            appContext.put(CLIPBOARD_FLAVOR_LISTENER_KEY, flavorListeners);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
24153
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   361
        flavorListeners.add(listener);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (numberOfFlavorListeners++ == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            long[] currentFormats = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                openClipboard(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                currentFormats = getClipboardFormats();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            } catch (IllegalStateException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                closeClipboard();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            currentDataFlavors = formatArrayAsDataFlavorSet(currentFormats);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            registerClipboardViewerChecked();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    public synchronized void removeFlavorListener(FlavorListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
24153
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   382
        Set<FlavorListener> flavorListeners = getFlavorListeners(AppContext.getAppContext());
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   383
        if (flavorListeners == null){
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            //else we throw NullPointerException, but it is forbidden
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
24153
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   387
        if (flavorListeners.remove(listener) && --numberOfFlavorListeners == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            unregisterClipboardViewerChecked();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            currentDataFlavors = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
24153
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   393
    @SuppressWarnings("unchecked")
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   394
    private Set<FlavorListener> getFlavorListeners(AppContext appContext) {
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   395
        return (Set<FlavorListener>)appContext.get(CLIPBOARD_FLAVOR_LISTENER_KEY);
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   396
    }
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   397
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    public synchronized FlavorListener[] getFlavorListeners() {
24153
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   399
        Set<FlavorListener> flavorListeners = getFlavorListeners(AppContext.getAppContext());
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   400
        return flavorListeners == null ? new FlavorListener[0]
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   401
                : flavorListeners.toArray(new FlavorListener[flavorListeners.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    public boolean areFlavorListenersRegistered() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        return (numberOfFlavorListeners > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    protected abstract void registerClipboardViewerChecked();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    protected abstract void unregisterClipboardViewerChecked();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * Checks change of the <code>DataFlavor</code>s and, if necessary,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * posts notifications on <code>FlavorEvent</code>s to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * AppContexts' EDTs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * The parameter <code>formats</code> is null iff we have just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * failed to get formats available on the clipboard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @param formats data formats that have just been retrieved from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *        this clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    public void checkChange(long[] formats) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        Set prevDataFlavors = currentDataFlavors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        currentDataFlavors = formatArrayAsDataFlavorSet(formats);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
24153
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   426
        if (Objects.equals(prevDataFlavors, currentDataFlavors)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            // we've been able to successfully get available on the clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            // DataFlavors this and previous time and they are coincident;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            // don't notify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
24153
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   433
        for (AppContext appContext : AppContext.getAppContexts()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            if (appContext == null || appContext.isDisposed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
24153
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   437
            Set<FlavorListener> flavorListeners = getFlavorListeners(appContext);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            if (flavorListeners != null) {
24153
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   439
                for (FlavorListener listener : flavorListeners) {
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   440
                    if (listener != null) {
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   441
                        PeerEvent peerEvent = new PeerEvent(this,
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   442
                                () -> listener.flavorsChanged(new FlavorEvent(SunClipboard.this)),
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   443
                                PeerEvent.PRIORITY_EVENT);
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   444
                        SunToolkit.postEvent(appContext, peerEvent);
2c19973ab9f2 6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents: 23279
diff changeset
   445
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
}