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