jdk/src/solaris/classes/sun/awt/X11/XDataTransferer.java
author yan
Mon, 06 Oct 2008 16:45:00 +0400
changeset 1966 12a51fb0db0d
parent 2 90ce3da70b43
child 4363 212067ed03e4
permissions -rw-r--r--
5100701: Toolkit.getLockingKeyState() does not work on XToolkit, but works on Motif Summary: Does not work on Motif but works on XToolkit now; implemented using XQueryPointer. Reviewed-by: 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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.image.WritableRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.imageio.ImageIO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.imageio.ImageTypeSpecifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.imageio.ImageWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.imageio.spi.ImageWriterSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.awt.datatransfer.DataTransferer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import sun.awt.datatransfer.ToolkitThreadBlockedHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Platform-specific support for the data transfer subsystem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public class XDataTransferer extends DataTransferer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static final XAtom FILE_NAME_ATOM = XAtom.get("FILE_NAME");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static final XAtom DT_NET_FILE_ATOM = XAtom.get("_DT_NETFILE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static final XAtom PNG_ATOM = XAtom.get("PNG");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static final XAtom JFIF_ATOM = XAtom.get("JFIF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static final XAtom TARGETS_ATOM = XAtom.get("TARGETS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static final XAtom INCR_ATOM = XAtom.get("INCR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static final XAtom MULTIPLE_ATOM = XAtom.get("MULTIPLE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Singleton constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private XDataTransferer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static XDataTransferer transferer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static XDataTransferer getInstanceImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        synchronized (XDataTransferer.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            if (transferer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                transferer = new XDataTransferer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return transferer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public String getDefaultUnicodeEncoding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return "iso-10646-ucs-2";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public boolean isLocaleDependentTextFormat(long format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return false;
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 boolean isTextFormat(long format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return super.isTextFormat(format)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            || isMimeFormat(format, "text");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    protected String getCharsetForTextFormat(Long lFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        long format = lFormat.longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (isMimeFormat(format, "text")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            String nat = getNativeForFormat(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            DataFlavor df = new DataFlavor(nat, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            // Ignore the charset parameter of the MIME type if the subtype
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            // doesn't support charset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (!DataTransferer.doesSubtypeSupportCharset(df)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            String charset = df.getParameter("charset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (charset != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                return charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return super.getCharsetForTextFormat(lFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public boolean isFileFormat(long format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return format == FILE_NAME_ATOM.getAtom() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            format == DT_NET_FILE_ATOM.getAtom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public boolean isImageFormat(long format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return format == PNG_ATOM.getAtom() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            format == JFIF_ATOM.getAtom() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            isMimeFormat(format, "image");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    protected Long getFormatForNativeAsLong(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // Just get the atom. If it has already been retrived
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // once, we'll get a copy so this should be very fast.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        long atom = XAtom.get(str).getAtom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return Long.valueOf(atom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    protected String getNativeForFormat(long format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return getTargetNameForAtom(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public ToolkitThreadBlockedHandler getToolkitThreadBlockedHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return XToolkitThreadBlockedHandler.getToolkitThreadBlockedHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Gets an format name for a given format (atom)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private String getTargetNameForAtom(long atom) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return XAtom.get(atom).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    protected byte[] imageToPlatformBytes(Image image, long format)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
      throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        String mimeType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (format == PNG_ATOM.getAtom()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            mimeType = "image/png";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        } else if (format == JFIF_ATOM.getAtom()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            mimeType = "image/jpeg";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            // Check if an image MIME format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                String nat = getNativeForFormat(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                DataFlavor df = new DataFlavor(nat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                String primaryType = df.getPrimaryType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                if ("image".equals(primaryType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    mimeType = df.getPrimaryType() + "/" + df.getSubType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                // Not an image MIME format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (mimeType != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return imageToStandardBytes(image, mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            String nativeFormat = getNativeForFormat(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            throw new IOException("Translation to " + nativeFormat +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                                  " is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Translates either a byte array or an input stream which contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * platform-specific image data in the given format into an Image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    protected Image platformImageBytesOrStreamToImage(InputStream inputStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                                                      byte[] bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                                                      long format)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
      throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        String mimeType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (format == PNG_ATOM.getAtom()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            mimeType = "image/png";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } else if (format == JFIF_ATOM.getAtom()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            mimeType = "image/jpeg";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            // Check if an image MIME format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                String nat = getNativeForFormat(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                DataFlavor df = new DataFlavor(nat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                String primaryType = df.getPrimaryType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                if ("image".equals(primaryType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    mimeType = df.getPrimaryType() + "/" + df.getSubType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                // Not an image MIME format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (mimeType != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            return standardImageBytesOrStreamToImage(inputStream, bytes, mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            String nativeFormat = getNativeForFormat(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            throw new IOException("Translation from " + nativeFormat +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                  " is not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    protected String[] dragQueryFile(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            return XlibWrapper.XTextPropertyToStringList(bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                                         XAtom.get("STRING").getAtom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Returns true if and only if the name of the specified format Atom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * constitutes a valid MIME type with the specified primary type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    private boolean isMimeFormat(long format, String primaryType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        String nat = getNativeForFormat(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (nat == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            DataFlavor df = new DataFlavor(nat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            if (primaryType.equals(df.getPrimaryType())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            // Not a MIME format.
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 false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * The XDnD protocol prescribes that the Atoms used as targets for data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * transfer should have string names that represent the corresponding MIME
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * To meet this requirement we check if the passed native format constitutes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * a valid MIME and return a list of flavors to which the data in this MIME
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * type can be translated by the Data Transfer subsystem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public List getPlatformMappingsForNative(String nat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        List flavors = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (nat == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return flavors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        DataFlavor df = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            df = new DataFlavor(nat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            // The string doesn't constitute a valid MIME type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            return flavors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        Object value = df;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        final String primaryType = df.getPrimaryType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        final String baseType = primaryType + "/" + df.getSubType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        // For text formats we map natives to MIME strings instead of data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        // flavors to enable dynamic text native-to-flavor mapping generation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        // See SystemFlavorMap.getFlavorsForNative() for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if ("text".equals(primaryType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            value = primaryType + "/" + df.getSubType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        } else if ("image".equals(primaryType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            Iterator readers = ImageIO.getImageReadersByMIMEType(baseType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            if (readers.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                flavors.add(DataFlavor.imageFlavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        flavors.add(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return flavors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    private static ImageTypeSpecifier defaultSpecifier = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    private ImageTypeSpecifier getDefaultImageTypeSpecifier() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (defaultSpecifier == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            ColorModel model = ColorModel.getRGBdefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            WritableRaster raster =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                model.createCompatibleWritableRaster(10, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            BufferedImage bufferedImage =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                new BufferedImage(model, raster, model.isAlphaPremultiplied(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                  null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            defaultSpecifier = new ImageTypeSpecifier(bufferedImage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return defaultSpecifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * The XDnD protocol prescribes that the Atoms used as targets for data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * transfer should have string names that represent the corresponding MIME
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * To meet this requirement we return a list of formats that represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * MIME types to which the data in this flavor can be translated by the Data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Transfer subsystem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public List getPlatformMappingsForFlavor(DataFlavor df) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        List natives = new ArrayList(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (df == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            return natives;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        String charset = df.getParameter("charset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        String baseType = df.getPrimaryType() + "/" + df.getSubType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        String mimeType = baseType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (charset != null && DataTransferer.isFlavorCharsetTextType(df)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            mimeType += ";charset=" + charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        // Add a mapping to the MIME native whenever the representation class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        // doesn't require translation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (df.getRepresentationClass() != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            (df.isRepresentationClassInputStream() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
             df.isRepresentationClassByteBuffer() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
             byteArrayClass.equals(df.getRepresentationClass()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            natives.add(mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        if (DataFlavor.imageFlavor.equals(df)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            String[] mimeTypes = ImageIO.getWriterMIMETypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            if (mimeTypes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                for (int i = 0; i < mimeTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    Iterator writers =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        ImageIO.getImageWritersByMIMEType(mimeTypes[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    while (writers.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                        ImageWriter imageWriter = (ImageWriter)writers.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                        ImageWriterSpi writerSpi =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                            imageWriter.getOriginatingProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        if (writerSpi != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                            writerSpi.canEncodeImage(getDefaultImageTypeSpecifier())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                            natives.add(mimeTypes[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                        }
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
        } else if (DataTransferer.isFlavorCharsetTextType(df)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            final Iterator iter = DataTransferer.standardEncodings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            // stringFlavor is semantically equivalent to the standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            // "text/plain" MIME type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            if (DataFlavor.stringFlavor.equals(df)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                baseType = "text/plain";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                String encoding = (String)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                if (!encoding.equals(charset)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    natives.add(baseType + ";charset=" + encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            // Add a MIME format without specified charset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            if (!natives.contains(baseType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                natives.add(baseType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        return natives;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
}