Path: blob/master/src/java.datatransfer/share/classes/java/awt/datatransfer/StringSelection.java
41159 views
/*1* Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.awt.datatransfer;2627import java.io.IOException;28import java.io.StringReader;2930/**31* A {@code Transferable} which implements the capability required to transfer a32* {@code String}.33* <p>34* This {@code Transferable} properly supports {@code DataFlavor.stringFlavor}35* and all equivalent flavors. Support for {@code DataFlavor.plainTextFlavor}36* and all equivalent flavors is <b>deprecated</b>. No other {@code DataFlavor}s37* are supported.38*39* @see DataFlavor#stringFlavor40* @see DataFlavor#plainTextFlavor41* @since 1.142*/43public class StringSelection implements Transferable, ClipboardOwner {4445private static final int STRING = 0;46private static final int PLAIN_TEXT = 1;4748@SuppressWarnings("deprecation")49private static final DataFlavor[] flavors = {50DataFlavor.stringFlavor,51DataFlavor.plainTextFlavor // deprecated52};5354private String data;5556/**57* Creates a {@code Transferable} capable of transferring the specified58* {@code String}.59*60* @param data the string to be transferred61*/62public StringSelection(String data) {63this.data = data;64}6566/**67* Returns an array of flavors in which this {@code Transferable} can68* 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 are72* {@code DataFlavor.stringFlavor} and73* {@code DataFlavor.plainTextFlavor}74*/75public DataFlavor[] getTransferDataFlavors() {76// returning flavors itself would allow client code to modify77// our internal behavior78return flavors.clone();79}8081/**82* Returns whether the requested flavor is supported by this83* {@code Transferable}.84*85* @param flavor the requested flavor for the data86* @return {@code true} if {@code flavor} is equal to87* {@code DataFlavor.stringFlavor} or88* {@code DataFlavor.plainTextFlavor}; {@code false} if89* {@code flavor} is not one of the above flavors90* @throws NullPointerException if {@code flavor} is {@code null}91*/92public boolean isDataFlavorSupported(DataFlavor flavor) {93// JCK Test StringSelection0003: if 'flavor' is null, throw NPE94for (int i = 0; i < flavors.length; i++) {95if (flavor.equals(flavors[i])) {96return true;97}98}99return false;100}101102/**103* Returns the {@code Transferable}'s data in the requested104* {@code DataFlavor} if possible. If the desired flavor is105* {@code DataFlavor.stringFlavor}, or an equivalent flavor, the106* {@code String} representing the selection is returned. If the desired107* flavor is {@code DataFlavor.plainTextFlavor}, or an equivalent flavor, a108* {@code Reader} is returned.109* <br>110* <b>Note:</b> The behavior of this method for111* {@code DataFlavor.plainTextFlavor}112* and equivalent {@code DataFlavor}s is inconsistent with the definition of113* {@code DataFlavor.plainTextFlavor}.114*115* @param flavor the requested flavor for the data116* @return the data in the requested flavor, as outlined above117* @throws UnsupportedFlavorException if the requested data flavor is not118* equivalent to either {@code DataFlavor.stringFlavor} or119* {@code DataFlavor.plainTextFlavor}120* @throws IOException if an IOException occurs while retrieving the data.121* By default, StringSelection never throws this exception, but a122* subclass may.123* @throws NullPointerException if {@code flavor} is {@code null}124* @see java.io.Reader125*/126public Object getTransferData(DataFlavor flavor)127throws UnsupportedFlavorException, IOException128{129// JCK Test StringSelection0007: if 'flavor' is null, throw NPE130if (flavor.equals(flavors[STRING])) {131return (Object)data;132} else if (flavor.equals(flavors[PLAIN_TEXT])) {133return new StringReader(data == null ? "" : data);134} else {135throw new UnsupportedFlavorException(flavor);136}137}138139public void lostOwnership(Clipboard clipboard, Transferable contents) {140}141}142143144