jdk/src/share/classes/java/awt/datatransfer/StringSelection.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
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 1996-2006 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 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * A <code>Transferable</code> which implements the capability required
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * to transfer a <code>String</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This <code>Transferable</code> properly supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <code>DataFlavor.stringFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * and all equivalent flavors. Support for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <code>DataFlavor.plainTextFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * and all equivalent flavors is <b>deprecated</b>. No other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <code>DataFlavor</code>s are supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @see java.awt.datatransfer.DataFlavor#stringFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @see java.awt.datatransfer.DataFlavor#plainTextFlavor
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final DataFlavor[] flavors = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        DataFlavor.stringFlavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        DataFlavor.plainTextFlavor // deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private String data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Creates a <code>Transferable</code> capable of transferring
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * the specified <code>String</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public StringSelection(String data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        this.data = data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Returns an array of flavors in which this <code>Transferable</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * can provide the data. <code>DataFlavor.stringFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * is properly supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Support for <code>DataFlavor.plainTextFlavor</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * <b>deprecated</b>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @return an array of length two, whose elements are <code>DataFlavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *         stringFlavor</code> and <code>DataFlavor.plainTextFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public DataFlavor[] getTransferDataFlavors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        // returning flavors itself would allow client code to modify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // our internal behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return (DataFlavor[])flavors.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Returns whether the requested flavor is supported by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * <code>Transferable</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param flavor the requested flavor for the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @return true if <code>flavor</code> is equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *   <code>DataFlavor.stringFlavor</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *   <code>DataFlavor.plainTextFlavor</code>; false if <code>flavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *   is not one of the above flavors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @throws NullPointerException if flavor is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public boolean isDataFlavorSupported(DataFlavor flavor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        // JCK Test StringSelection0003: if 'flavor' is null, throw NPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        for (int i = 0; i < flavors.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            if (flavor.equals(flavors[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Returns the <code>Transferable</code>'s data in the requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * <code>DataFlavor</code> if possible. If the desired flavor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <code>DataFlavor.stringFlavor</code>, or an equivalent flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * the <code>String</code> representing the selection is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * returned. If the desired flavor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <code>DataFlavor.plainTextFlavor</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * or an equivalent flavor, a <code>Reader</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <b>Note:</b> The behavior of this method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * <code>DataFlavor.plainTextFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * and equivalent <code>DataFlavor</code>s is inconsistent with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * definition of <code>DataFlavor.plainTextFlavor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param flavor the requested flavor for the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @return the data in the requested flavor, as outlined above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @throws UnsupportedFlavorException if the requested data flavor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *         not equivalent to either <code>DataFlavor.stringFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *         or <code>DataFlavor.plainTextFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @throws IOException if an IOException occurs while retrieving the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *         By default, StringSelection never throws this exception, but a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *         subclass may.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @throws NullPointerException if flavor is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @see java.io.Reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public Object getTransferData(DataFlavor flavor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        throws UnsupportedFlavorException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        // JCK Test StringSelection0007: if 'flavor' is null, throw NPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (flavor.equals(flavors[STRING])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            return (Object)data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        } else if (flavor.equals(flavors[PLAIN_TEXT])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            return new StringReader(data == null ? "" : data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            throw new UnsupportedFlavorException(flavor);
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
    public void lostOwnership(Clipboard clipboard, Transferable contents) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
}