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