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