jdk/src/share/classes/java/awt/datatransfer/StringSelection.java
author erikj
Thu, 07 Jun 2012 18:05:09 -0700
changeset 12813 c10ab96dcf41
parent 5506 202f599c92aa
child 16734 da1901d79073
permissions -rw-r--r--
7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI Reviewed-by: ohair, ohrstrom, ihse
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) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt.datatransfer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
12813
c10ab96dcf41 7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI
erikj
parents: 5506
diff changeset
    30
import javax.tools.annotation.GenerateNativeHeader;
c10ab96dcf41 7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI
erikj
parents: 5506
diff changeset
    31
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A <code>Transferable</code> which implements the capability required
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * to transfer a <code>String</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This <code>Transferable</code> properly supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <code>DataFlavor.stringFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * and all equivalent flavors. Support for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <code>DataFlavor.plainTextFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * and all equivalent flavors is <b>deprecated</b>. No other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <code>DataFlavor</code>s are supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @see java.awt.datatransfer.DataFlavor#stringFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @see java.awt.datatransfer.DataFlavor#plainTextFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
12813
c10ab96dcf41 7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI
erikj
parents: 5506
diff changeset
    46
/* No native methods here, but the constants are needed in the supporting JNI code */
c10ab96dcf41 7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI
erikj
parents: 5506
diff changeset
    47
@GenerateNativeHeader
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class StringSelection implements Transferable, ClipboardOwner {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final int STRING = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final int PLAIN_TEXT = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final DataFlavor[] flavors = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        DataFlavor.stringFlavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        DataFlavor.plainTextFlavor // deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private String data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Creates a <code>Transferable</code> capable of transferring
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * the specified <code>String</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public StringSelection(String data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        this.data = data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Returns an array of flavors in which this <code>Transferable</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * can provide the data. <code>DataFlavor.stringFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * is properly supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Support for <code>DataFlavor.plainTextFlavor</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * <b>deprecated</b>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @return an array of length two, whose elements are <code>DataFlavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *         stringFlavor</code> and <code>DataFlavor.plainTextFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public DataFlavor[] getTransferDataFlavors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // returning flavors itself would allow client code to modify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // our internal behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return (DataFlavor[])flavors.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Returns whether the requested flavor is supported by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * <code>Transferable</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param flavor the requested flavor for the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @return true if <code>flavor</code> is equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *   <code>DataFlavor.stringFlavor</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *   <code>DataFlavor.plainTextFlavor</code>; false if <code>flavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *   is not one of the above flavors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @throws NullPointerException if flavor is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public boolean isDataFlavorSupported(DataFlavor flavor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // JCK Test StringSelection0003: if 'flavor' is null, throw NPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        for (int i = 0; i < flavors.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if (flavor.equals(flavors[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Returns the <code>Transferable</code>'s data in the requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <code>DataFlavor</code> if possible. If the desired flavor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <code>DataFlavor.stringFlavor</code>, or an equivalent flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * the <code>String</code> representing the selection is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * returned. If the desired flavor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <code>DataFlavor.plainTextFlavor</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * or an equivalent flavor, a <code>Reader</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * <b>Note:</b> The behavior of this method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * <code>DataFlavor.plainTextFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * and equivalent <code>DataFlavor</code>s is inconsistent with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * definition of <code>DataFlavor.plainTextFlavor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param flavor the requested flavor for the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @return the data in the requested flavor, as outlined above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @throws UnsupportedFlavorException if the requested data flavor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *         not equivalent to either <code>DataFlavor.stringFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *         or <code>DataFlavor.plainTextFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @throws IOException if an IOException occurs while retrieving the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *         By default, StringSelection never throws this exception, but a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *         subclass may.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @throws NullPointerException if flavor is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @see java.io.Reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public Object getTransferData(DataFlavor flavor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        throws UnsupportedFlavorException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        // JCK Test StringSelection0007: if 'flavor' is null, throw NPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (flavor.equals(flavors[STRING])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            return (Object)data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        } else if (flavor.equals(flavors[PLAIN_TEXT])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            return new StringReader(data == null ? "" : data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            throw new UnsupportedFlavorException(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public void lostOwnership(Clipboard clipboard, Transferable contents) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
}