src/java.datatransfer/share/classes/java/awt/datatransfer/StringSelection.java
author hannesw
Thu, 23 Nov 2017 16:20:10 +0100
changeset 47929 df9e7f2df01f
parent 47216 71c04702a3d5
permissions -rw-r--r--
8191819: String.prototype.match should follow ES6 specification Reviewed-by: sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
     2
 * Copyright (c) 1996, 2017, 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
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    28
import java.io.IOException;
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    29
import java.io.StringReader;
12813
c10ab96dcf41 7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI
erikj
parents: 5506
diff changeset
    30
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    32
 * A {@code Transferable} which implements the capability required to transfer a
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    33
 * {@code String}.
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    34
 * <p>
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    35
 * This {@code Transferable} properly supports {@code DataFlavor.stringFlavor}
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    36
 * and all equivalent flavors. Support for {@code DataFlavor.plainTextFlavor}
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    37
 * and all equivalent flavors is <b>deprecated</b>. No other {@code DataFlavor}s
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    38
 * are supported.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    40
 * @see DataFlavor#stringFlavor
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    41
 * @see DataFlavor#plainTextFlavor
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 28987
diff changeset
    42
 * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class StringSelection implements Transferable, ClipboardOwner {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final int STRING = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final int PLAIN_TEXT = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
28231
b608ffcaed74 8066621: Suppress deprecation warnings in java.desktop module
darcy
parents: 25859
diff changeset
    49
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final DataFlavor[] flavors = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        DataFlavor.stringFlavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        DataFlavor.plainTextFlavor // deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private String data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    58
     * Creates a {@code Transferable} capable of transferring the specified
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    59
     * {@code String}.
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    60
     *
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    61
     * @param  data the string to be transferred
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public StringSelection(String data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        this.data = data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    68
     * Returns an array of flavors in which this {@code Transferable} can
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    69
     * provide the data. {@code DataFlavor.stringFlavor} is properly supported.
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    70
     * Support for {@code DataFlavor.plainTextFlavor} is <b>deprecated</b>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    72
     * @return an array of length two, whose elements are
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    73
     *         {@code DataFlavor.stringFlavor} and
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    74
     *         {@code DataFlavor.plainTextFlavor}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public DataFlavor[] getTransferDataFlavors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        // returning flavors itself would allow client code to modify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        // our internal behavior
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 22281
diff changeset
    79
        return flavors.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Returns whether the requested flavor is supported by this
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    84
     * {@code Transferable}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    86
     * @param  flavor the requested flavor for the data
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    87
     * @return {@code true} if {@code flavor} is equal to
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    88
     *         {@code DataFlavor.stringFlavor} or
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    89
     *         {@code DataFlavor.plainTextFlavor}; {@code false} if
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    90
     *         {@code flavor} is not one of the above flavors
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
    91
     * @throws NullPointerException if {@code flavor} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public boolean isDataFlavorSupported(DataFlavor flavor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // JCK Test StringSelection0003: if 'flavor' is null, throw NPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        for (int i = 0; i < flavors.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if (flavor.equals(flavors[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
   104
     * Returns the {@code Transferable}'s data in the requested
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
   105
     * {@code DataFlavor} if possible. If the desired flavor is
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
   106
     * {@code DataFlavor.stringFlavor}, or an equivalent flavor, the
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
   107
     * {@code String} representing the selection is returned. If the desired
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
   108
     * flavor is {@code DataFlavor.plainTextFlavor}, or an equivalent flavor, a
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
   109
     * {@code Reader} is returned.
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
   110
     * <br>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <b>Note:</b> The behavior of this method for
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
   112
     * {@code DataFlavor.plainTextFlavor}
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
   113
     * and equivalent {@code DataFlavor}s is inconsistent with the definition of
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
   114
     * {@code DataFlavor.plainTextFlavor}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
   116
     * @param  flavor the requested flavor for the data
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @return the data in the requested flavor, as outlined above
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
   118
     * @throws UnsupportedFlavorException if the requested data flavor is not
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
   119
     *         equivalent to either {@code DataFlavor.stringFlavor} or
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
   120
     *         {@code DataFlavor.plainTextFlavor}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @throws IOException if an IOException occurs while retrieving the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *         By default, StringSelection never throws this exception, but a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *         subclass may.
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 45434
diff changeset
   124
     * @throws NullPointerException if {@code flavor} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @see java.io.Reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public Object getTransferData(DataFlavor flavor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        throws UnsupportedFlavorException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        // JCK Test StringSelection0007: if 'flavor' is null, throw NPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (flavor.equals(flavors[STRING])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            return (Object)data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } else if (flavor.equals(flavors[PLAIN_TEXT])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            return new StringReader(data == null ? "" : data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throw new UnsupportedFlavorException(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public void lostOwnership(Clipboard clipboard, Transferable contents) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
}