jdk/src/share/classes/sun/awt/datatransfer/ClipboardTransferable.java
author denis
Wed, 27 Mar 2013 16:19:51 +0400
changeset 16705 1caaa379eded
parent 5506 202f599c92aa
child 19364 f6324eb18ece
permissions -rw-r--r--
7075105: WIN: Provide a way to format HTML on drop Reviewed-by: uta, 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().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    setToSortedDataFlavorArray(flavorsToData.keySet(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                               flavorsForFormats);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            clipboard.closeClipboard();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private boolean fetchOneFlavor(SunClipboard clipboard, DataFlavor flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                   Long lFormat, HashMap cached_data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (!flavorsToData.containsKey(flavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            long format = lFormat.longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            Object data = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            if (!cached_data.containsKey(lFormat)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    data = clipboard.getClipboardData(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    data = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                // Cache this data, even if it's null, so we don't have to go
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                // to native code again for this format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                cached_data.put(lFormat, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                data = cached_data.get(lFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            // Casting IOException to byte array causes ClassCastException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            // We should handle IOException separately - do not wrap them into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            // DataFactory and report failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (data instanceof IOException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                flavorsToData.put(flavor, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            } else if (data != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                flavorsToData.put(flavor, new DataFactory(format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                                          (byte[])data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public DataFlavor[] getTransferDataFlavors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return (DataFlavor[])flavors.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public boolean isDataFlavorSupported(DataFlavor flavor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return flavorsToData.containsKey(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public Object getTransferData(DataFlavor flavor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        throws UnsupportedFlavorException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (!isDataFlavorSupported(flavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            throw new UnsupportedFlavorException(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        Object ret = flavorsToData.get(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (ret instanceof IOException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            // rethrow IOExceptions generated while fetching data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            throw (IOException)ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        } else if (ret instanceof DataFactory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            // Now we can render the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            DataFactory factory = (DataFactory)ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            ret = factory.getTransferData(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
}