jdk/src/share/classes/sun/awt/datatransfer/ClipboardTransferable.java
author pchelko
Wed, 14 Aug 2013 17:20:09 +0400
changeset 19364 f6324eb18ece
parent 5506 202f599c92aa
child 22567 5816a47fa4dd
permissions -rw-r--r--
7173464: Clipboard.getAvailableDataFlavors: Comparison method violates contract Reviewed-by: anthony, art, 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) 2000, 2005, 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.datatransfer.DataFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.datatransfer.Transferable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.datatransfer.UnsupportedFlavorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Reads all of the data from the system Clipboard which the data transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * subsystem knows how to translate. This includes all text data, File Lists,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Serializable objects, Remote objects, and properly registered, arbitrary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * data as InputStreams. The data is stored in byte format until requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * by client code. At that point, the data is converted, if necessary, into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * the proper format to deliver to the application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * This hybrid pre-fetch/delayed-rendering approach allows us to circumvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * the API restriction that client code cannot lock the Clipboard to discover
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * its formats before requesting data in a particular format, while avoiding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * the overhead of fully rendering all data ahead of time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author Danila Sinopalnikov
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @since 1.4 (appeared in modified form as FullyRenderedTransferable in 1.3.1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
public class ClipboardTransferable implements Transferable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private final HashMap flavorsToData = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private DataFlavor[] flavors = new DataFlavor[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private final class DataFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        final long format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        final byte[] data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        DataFactory(long format, byte[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            this.format = format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            this.data   = data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        public Object getTransferData(DataFlavor flavor) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            return DataTransferer.getInstance().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                translateBytes(data, flavor, format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                               ClipboardTransferable.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public ClipboardTransferable(SunClipboard clipboard) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        clipboard.openClipboard(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            long[] formats = clipboard.getClipboardFormats();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            if (formats != null && formats.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                // Since the SystemFlavorMap will specify many DataFlavors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                // which map to the same format, we should cache data as we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                // read it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                HashMap cached_data = new HashMap(formats.length, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                Map flavorsForFormats = DataTransferer.getInstance().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    getFlavorsForFormats(formats, SunClipboard.flavorMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                for (Iterator iter = flavorsForFormats.keySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                     iter.hasNext(); )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    DataFlavor flavor = (DataFlavor)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    Long lFormat = (Long)flavorsForFormats.get(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    fetchOneFlavor(clipboard, flavor, lFormat, cached_data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                flavors = DataTransferer.getInstance().
19364
f6324eb18ece 7173464: Clipboard.getAvailableDataFlavors: Comparison method violates contract
pchelko
parents: 5506
diff changeset
   101
                    setToSortedDataFlavorArray(flavorsToData.keySet());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            clipboard.closeClipboard();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private boolean fetchOneFlavor(SunClipboard clipboard, DataFlavor flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                                   Long lFormat, HashMap cached_data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (!flavorsToData.containsKey(flavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            long format = lFormat.longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            Object data = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if (!cached_data.containsKey(lFormat)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    data = clipboard.getClipboardData(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    data = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                // Cache this data, even if it's null, so we don't have to go
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                // to native code again for this format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                cached_data.put(lFormat, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                data = cached_data.get(lFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            // Casting IOException to byte array causes ClassCastException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            // We should handle IOException separately - do not wrap them into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            // DataFactory and report failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if (data instanceof IOException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                flavorsToData.put(flavor, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            } else if (data != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                flavorsToData.put(flavor, new DataFactory(format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                                          (byte[])data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public DataFlavor[] getTransferDataFlavors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return (DataFlavor[])flavors.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public boolean isDataFlavorSupported(DataFlavor flavor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return flavorsToData.containsKey(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public Object getTransferData(DataFlavor flavor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        throws UnsupportedFlavorException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (!isDataFlavorSupported(flavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw new UnsupportedFlavorException(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        Object ret = flavorsToData.get(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (ret instanceof IOException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            // rethrow IOExceptions generated while fetching data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            throw (IOException)ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        } else if (ret instanceof DataFactory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            // Now we can render the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            DataFactory factory = (DataFactory)ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            ret = factory.getTransferData(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
}