jdk/src/solaris/classes/sun/awt/X11/XDataTransferer.java
author dcherepanov
Mon, 07 Dec 2009 13:43:04 +0300
changeset 4371 dc9dcb8b0ae7
parent 4363 212067ed03e4
child 5506 202f599c92aa
permissions -rw-r--r--
6823138: Need to replace ComponentAccessor with AWTAccessor Reviewed-by: art, anthony
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2005 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.X11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.datatransfer.DataFlavor;
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
    31
import java.awt.datatransfer.Transferable;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
    32
import java.awt.datatransfer.UnsupportedFlavorException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.image.WritableRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
    38
import java.io.BufferedReader;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.InputStream;
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
    40
import java.io.InputStreamReader;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
    43
import java.net.URI;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
    44
import java.net.URISyntaxException;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
    45
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.imageio.ImageIO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.imageio.ImageTypeSpecifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.imageio.ImageWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.imageio.spi.ImageWriterSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import sun.awt.datatransfer.DataTransferer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import sun.awt.datatransfer.ToolkitThreadBlockedHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
    58
import java.io.ByteArrayOutputStream;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
    59
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Platform-specific support for the data transfer subsystem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public class XDataTransferer extends DataTransferer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static final XAtom FILE_NAME_ATOM = XAtom.get("FILE_NAME");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static final XAtom DT_NET_FILE_ATOM = XAtom.get("_DT_NETFILE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static final XAtom PNG_ATOM = XAtom.get("PNG");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static final XAtom JFIF_ATOM = XAtom.get("JFIF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static final XAtom TARGETS_ATOM = XAtom.get("TARGETS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static final XAtom INCR_ATOM = XAtom.get("INCR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    static final XAtom MULTIPLE_ATOM = XAtom.get("MULTIPLE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Singleton constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private XDataTransferer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static XDataTransferer transferer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static XDataTransferer getInstanceImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        synchronized (XDataTransferer.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            if (transferer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                transferer = new XDataTransferer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        return transferer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public String getDefaultUnicodeEncoding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return "iso-10646-ucs-2";
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 boolean isLocaleDependentTextFormat(long format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public boolean isTextFormat(long format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return super.isTextFormat(format)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            || isMimeFormat(format, "text");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    protected String getCharsetForTextFormat(Long lFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        long format = lFormat.longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (isMimeFormat(format, "text")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            String nat = getNativeForFormat(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            DataFlavor df = new DataFlavor(nat, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            // Ignore the charset parameter of the MIME type if the subtype
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            // doesn't support charset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (!DataTransferer.doesSubtypeSupportCharset(df)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            String charset = df.getParameter("charset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (charset != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                return charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return super.getCharsetForTextFormat(lFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   120
    protected boolean isURIListFormat(long format) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   121
        String nat = getNativeForFormat(format);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   122
        if (nat == null) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   123
            return false;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   124
        }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   125
        try {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   126
            DataFlavor df = new DataFlavor(nat);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   127
            if (df.getPrimaryType().equals("text") && df.getSubType().equals("uri-list")) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   128
                return true;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   129
            }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   130
        } catch (Exception e) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   131
            // Not a MIME format.
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   132
        }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   133
        return false;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   134
    }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   135
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public boolean isFileFormat(long format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return format == FILE_NAME_ATOM.getAtom() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            format == DT_NET_FILE_ATOM.getAtom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public boolean isImageFormat(long format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return format == PNG_ATOM.getAtom() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            format == JFIF_ATOM.getAtom() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            isMimeFormat(format, "image");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    protected Long getFormatForNativeAsLong(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // Just get the atom. If it has already been retrived
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // once, we'll get a copy so this should be very fast.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        long atom = XAtom.get(str).getAtom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return Long.valueOf(atom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    protected String getNativeForFormat(long format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return getTargetNameForAtom(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public ToolkitThreadBlockedHandler getToolkitThreadBlockedHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return XToolkitThreadBlockedHandler.getToolkitThreadBlockedHandler();
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
     * Gets an format name for a given format (atom)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private String getTargetNameForAtom(long atom) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return XAtom.get(atom).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    protected byte[] imageToPlatformBytes(Image image, long format)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
      throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        String mimeType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (format == PNG_ATOM.getAtom()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            mimeType = "image/png";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } else if (format == JFIF_ATOM.getAtom()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            mimeType = "image/jpeg";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            // Check if an image MIME format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                String nat = getNativeForFormat(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                DataFlavor df = new DataFlavor(nat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                String primaryType = df.getPrimaryType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                if ("image".equals(primaryType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    mimeType = df.getPrimaryType() + "/" + df.getSubType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                // Not an image MIME format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (mimeType != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            return imageToStandardBytes(image, mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            String nativeFormat = getNativeForFormat(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            throw new IOException("Translation to " + nativeFormat +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                                  " is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   198
    protected ByteArrayOutputStream convertFileListToBytes(ArrayList<String> fileList)
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   199
        throws IOException
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   200
    {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   201
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   202
        for (int i = 0; i < fileList.size(); i++)
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   203
        {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   204
               byte[] bytes = fileList.get(i).getBytes();
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   205
               if (i != 0) bos.write(0);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   206
               bos.write(bytes, 0, bytes.length);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   207
        }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   208
        return bos;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   209
    }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   210
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Translates either a byte array or an input stream which contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * platform-specific image data in the given format into an Image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    protected Image platformImageBytesOrStreamToImage(InputStream inputStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                                      byte[] bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                                      long format)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
      throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        String mimeType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (format == PNG_ATOM.getAtom()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            mimeType = "image/png";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        } else if (format == JFIF_ATOM.getAtom()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            mimeType = "image/jpeg";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            // Check if an image MIME format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                String nat = getNativeForFormat(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                DataFlavor df = new DataFlavor(nat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                String primaryType = df.getPrimaryType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                if ("image".equals(primaryType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    mimeType = df.getPrimaryType() + "/" + df.getSubType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                // Not an image MIME format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (mimeType != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            return standardImageBytesOrStreamToImage(inputStream, bytes, mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            String nativeFormat = getNativeForFormat(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            throw new IOException("Translation from " + nativeFormat +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                                  " is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    protected String[] dragQueryFile(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            return XlibWrapper.XTextPropertyToStringList(bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                                                         XAtom.get("STRING").getAtom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
4363
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   256
    protected URI[] dragQueryURIs(InputStream stream,
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   257
                                  byte[] bytes,
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   258
                                  long format,
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   259
                                  Transferable localeTransferable)
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   260
      throws IOException {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   261
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   262
        String charset = null;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   263
        if (localeTransferable != null &&
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   264
            isLocaleDependentTextFormat(format) &&
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   265
            localeTransferable.isDataFlavorSupported(javaTextEncodingFlavor)) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   266
            try {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   267
                charset = new String(
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   268
                    (byte[])localeTransferable.getTransferData(javaTextEncodingFlavor),
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   269
                    "UTF-8"
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   270
                );
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   271
            } catch (UnsupportedFlavorException cannotHappen) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   272
            }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   273
        } else {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   274
            charset = getCharsetForTextFormat(format);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   275
        }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   276
        if (charset == null) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   277
            // Only happens when we have a custom text type.
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   278
            charset = getDefaultTextCharset();
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   279
        }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   280
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   281
        BufferedReader reader = null;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   282
        try {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   283
            reader = new BufferedReader(new InputStreamReader(stream, charset));
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   284
            String line;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   285
            ArrayList<URI> uriList = new ArrayList<URI>();
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   286
            URI uri;
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   287
            while ((line = reader.readLine()) != null) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   288
                try {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   289
                    uri = new URI(line);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   290
                } catch (URISyntaxException uriSyntaxException) {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   291
                    throw new IOException(uriSyntaxException);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   292
                }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   293
                uriList.add(uri);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   294
            }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   295
            return uriList.toArray(new URI[uriList.size()]);
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   296
        } finally {
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   297
            if (reader != null)
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   298
                reader.close();
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   299
        }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   300
    }
212067ed03e4 4899516: Transferable has no DataFlavors when dragging from Gnome window to Swing
denis
parents: 2
diff changeset
   301
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Returns true if and only if the name of the specified format Atom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * constitutes a valid MIME type with the specified primary type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    private boolean isMimeFormat(long format, String primaryType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        String nat = getNativeForFormat(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (nat == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            DataFlavor df = new DataFlavor(nat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if (primaryType.equals(df.getPrimaryType())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            // Not a MIME format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * The XDnD protocol prescribes that the Atoms used as targets for data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * transfer should have string names that represent the corresponding MIME
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * To meet this requirement we check if the passed native format constitutes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * a valid MIME and return a list of flavors to which the data in this MIME
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * type can be translated by the Data Transfer subsystem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public List getPlatformMappingsForNative(String nat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        List flavors = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        if (nat == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            return flavors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        DataFlavor df = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            df = new DataFlavor(nat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            // The string doesn't constitute a valid MIME type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            return flavors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        Object value = df;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        final String primaryType = df.getPrimaryType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        final String baseType = primaryType + "/" + df.getSubType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        // For text formats we map natives to MIME strings instead of data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        // flavors to enable dynamic text native-to-flavor mapping generation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        // See SystemFlavorMap.getFlavorsForNative() for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if ("text".equals(primaryType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            value = primaryType + "/" + df.getSubType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        } else if ("image".equals(primaryType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            Iterator readers = ImageIO.getImageReadersByMIMEType(baseType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            if (readers.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                flavors.add(DataFlavor.imageFlavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        flavors.add(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        return flavors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    private static ImageTypeSpecifier defaultSpecifier = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    private ImageTypeSpecifier getDefaultImageTypeSpecifier() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (defaultSpecifier == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            ColorModel model = ColorModel.getRGBdefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            WritableRaster raster =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                model.createCompatibleWritableRaster(10, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            BufferedImage bufferedImage =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                new BufferedImage(model, raster, model.isAlphaPremultiplied(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                                  null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            defaultSpecifier = new ImageTypeSpecifier(bufferedImage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return defaultSpecifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * The XDnD protocol prescribes that the Atoms used as targets for data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * transfer should have string names that represent the corresponding MIME
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * To meet this requirement we return a list of formats that represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * MIME types to which the data in this flavor can be translated by the Data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * Transfer subsystem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    public List getPlatformMappingsForFlavor(DataFlavor df) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        List natives = new ArrayList(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if (df == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            return natives;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        String charset = df.getParameter("charset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        String baseType = df.getPrimaryType() + "/" + df.getSubType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        String mimeType = baseType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if (charset != null && DataTransferer.isFlavorCharsetTextType(df)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            mimeType += ";charset=" + charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        // Add a mapping to the MIME native whenever the representation class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        // doesn't require translation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        if (df.getRepresentationClass() != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            (df.isRepresentationClassInputStream() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
             df.isRepresentationClassByteBuffer() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
             byteArrayClass.equals(df.getRepresentationClass()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            natives.add(mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        if (DataFlavor.imageFlavor.equals(df)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            String[] mimeTypes = ImageIO.getWriterMIMETypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            if (mimeTypes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                for (int i = 0; i < mimeTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    Iterator writers =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                        ImageIO.getImageWritersByMIMEType(mimeTypes[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    while (writers.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                        ImageWriter imageWriter = (ImageWriter)writers.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                        ImageWriterSpi writerSpi =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                            imageWriter.getOriginatingProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                        if (writerSpi != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                            writerSpi.canEncodeImage(getDefaultImageTypeSpecifier())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                            natives.add(mimeTypes[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        } else if (DataTransferer.isFlavorCharsetTextType(df)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            final Iterator iter = DataTransferer.standardEncodings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            // stringFlavor is semantically equivalent to the standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            // "text/plain" MIME type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            if (DataFlavor.stringFlavor.equals(df)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                baseType = "text/plain";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                String encoding = (String)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                if (!encoding.equals(charset)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    natives.add(baseType + ";charset=" + encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            // Add a MIME format without specified charset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            if (!natives.contains(baseType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                natives.add(baseType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        return natives;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
}