001: /*
002: * @(#)StringSelection.java 1.14 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package java.awt.datatransfer;
029:
030: import java.io.*;
031:
032: /**
033: * A Transferable which implements the capability required to transfer a
034: * String.
035: *
036: * This Transferable properly supports <code>DataFlavor.stringFlavor</code>
037: * and all equivalent flavors. Support for <code>DataFlavor.plainTextFlavor
038: * </code> and all equivalent flavors is <b>deprecated</b>. No other
039: * DataFlavors are supported.
040: *
041: * @see java.awt.datatransfer.DataFlavor.stringFlavor
042: * @see java.awt.datatransfer.DataFlavor.plainTextFlavor
043: */
044: public class StringSelection implements Transferable, ClipboardOwner {
045: private static final int STRING = 0;
046: private static final int PLAIN_TEXT = 1;
047: private static final DataFlavor[] flavors = {
048: DataFlavor.stringFlavor, DataFlavor.plainTextFlavor // deprecated
049: };
050: private String data;
051:
052: /**
053: * Creates a Transferable capable of transferring the specified String.
054: */
055: public StringSelection(String data) {
056: this .data = data;
057: }
058:
059: /**
060: * Returns an array of flavors in which this Transferable can provide
061: * the data. <code>DataFlavor.stringFlavor</code> is properly supported.
062: * Support for <code>DataFlavor.plainTextFlavor</code> is
063: * <b>deprecated</b>.
064: *
065: * @return an array of length two, whose elements are <code>DataFlavor.
066: * stringFlavor</code> and <code>DataFlavor.plainTextFlavor</code>.
067: */
068: public DataFlavor[] getTransferDataFlavors() {
069: // returning flavors itself would allow client code to modify
070: // our internal behavior
071: return (DataFlavor[]) flavors.clone();
072: }
073:
074: /**
075: * Returns whether the requested flavor is supported by this Transferable.
076: *
077: * @param flavor the requested flavor for the data
078: * @return true if <code>flavor</code> is equal to
079: * <code>DataFlavor.stringFlavor</code> or
080: * <code>DataFlavor.plainTextFlavor</code>; false if <code>flavor</code>
081: * is not one of the above flavors
082: * @throws NullPointerException if flavor is <code>null</code>
083: */
084: public boolean isDataFlavorSupported(DataFlavor flavor) {
085: // JCK Test StringSelection0003: if 'flavor' is null, throw NPE
086: for (int i = 0; i < flavors.length; i++) {
087: if (flavor.equals(flavors[i])) {
088: return true;
089: }
090: }
091: return false;
092: }
093:
094: /**
095: * Returns the Transferable's data in the requested DataFlavor if
096: * possible. If the desired flavor is <code>DataFlavor.stringFlavor</code>,
097: * or an equivalent flavor, the String representing the selection is
098: * returned. If the desired flavor is </code>DataFlavor.plainTextFlavor
099: * </code>, or an equivalent flavor, a Reader is returned. <b>Note:<b>
100: * The behavior of this method for </code>DataFlavor.plainTextFlavor</code>
101: * and equivalent DataFlavors is inconsistent with the definition of
102: * <code>DataFlavor.plainTextFlavor</code>.
103: *
104: * @param flavor the requested flavor for the data
105: * @return the data in the requested flavor, as outlined above.
106: * @throws UnsupportedFlavorException if the requested data flavor is
107: * not equivalent to either <code>DataFlavor.stringFlavor</code>
108: * or <code>DataFlavor.plainTextFlavor</code>.
109: * @throws IOException if an IOException occurs while retrieving the data.
110: * By default, StringSelection never throws this exception, but a
111: * subclass may.
112: * @throws NullPointerException if flavor is <code>null</code>
113: * @see java.io.Reader
114: */
115: public Object getTransferData(DataFlavor flavor)
116: throws UnsupportedFlavorException, IOException {
117: // JCK Test StringSelection0007: if 'flavor' is null, throw NPE
118: if (flavor.equals(flavors[STRING])) {
119: return (Object) data;
120: } else if (flavor.equals(flavors[PLAIN_TEXT])) {
121: return new StringReader(data);
122: } else {
123: throw new UnsupportedFlavorException(flavor);
124: }
125: }
126:
127: public void lostOwnership(Clipboard clipboard, Transferable contents) {
128: }
129: }
|