blob: 0e0353b952e2dd356b7e626fd53edbb0223ee387 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1996-2006 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package java.awt.datatransfer;
27
28import java.io.*;
29
30/**
31 * A <code>Transferable</code> which implements the capability required
32 * to transfer a <code>String</code>.
33 *
34 * This <code>Transferable</code> properly supports
35 * <code>DataFlavor.stringFlavor</code>
36 * and all equivalent flavors. Support for
37 * <code>DataFlavor.plainTextFlavor</code>
38 * and all equivalent flavors is <b>deprecated</b>. No other
39 * <code>DataFlavor</code>s are supported.
40 *
41 * @see java.awt.datatransfer.DataFlavor#stringFlavor
42 * @see java.awt.datatransfer.DataFlavor#plainTextFlavor
43 */
44public class StringSelection implements Transferable, ClipboardOwner {
45
46 private static final int STRING = 0;
47 private static final int PLAIN_TEXT = 1;
48
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</code> capable of transferring
58 * the specified <code>String</code>.
59 */
60 public StringSelection(String data) {
61 this.data = data;
62 }
63
64 /**
65 * Returns an array of flavors in which this <code>Transferable</code>
66 * can provide the data. <code>DataFlavor.stringFlavor</code>
67 * is properly supported.
68 * Support for <code>DataFlavor.plainTextFlavor</code> is
69 * <b>deprecated</b>.
70 *
71 * @return an array of length two, whose elements are <code>DataFlavor.
72 * stringFlavor</code> and <code>DataFlavor.plainTextFlavor</code>
73 */
74 public DataFlavor[] getTransferDataFlavors() {
75 // returning flavors itself would allow client code to modify
76 // our internal behavior
77 return (DataFlavor[])flavors.clone();
78 }
79
80 /**
81 * Returns whether the requested flavor is supported by this
82 * <code>Transferable</code>.
83 *
84 * @param flavor the requested flavor for the data
85 * @return true if <code>flavor</code> is equal to
86 * <code>DataFlavor.stringFlavor</code> or
87 * <code>DataFlavor.plainTextFlavor</code>; false if <code>flavor</code>
88 * is not one of the above flavors
89 * @throws NullPointerException if flavor is <code>null</code>
90 */
91 public boolean isDataFlavorSupported(DataFlavor flavor) {
92 // JCK Test StringSelection0003: if 'flavor' is null, throw NPE
93 for (int i = 0; i < flavors.length; i++) {
94 if (flavor.equals(flavors[i])) {
95 return true;
96 }
97 }
98 return false;
99 }
100
101 /**
102 * Returns the <code>Transferable</code>'s data in the requested
103 * <code>DataFlavor</code> if possible. If the desired flavor is
104 * <code>DataFlavor.stringFlavor</code>, or an equivalent flavor,
105 * the <code>String</code> representing the selection is
106 * returned. If the desired flavor is
107 * <code>DataFlavor.plainTextFlavor</code>,
108 * or an equivalent flavor, a <code>Reader</code> is returned.
109 * <b>Note:</b> The behavior of this method for
110 * <code>DataFlavor.plainTextFlavor</code>
111 * and equivalent <code>DataFlavor</code>s is inconsistent with the
112 * definition of <code>DataFlavor.plainTextFlavor</code>.
113 *
114 * @param flavor the requested flavor for the data
115 * @return the data in the requested flavor, as outlined above
116 * @throws UnsupportedFlavorException if the requested data flavor is
117 * not equivalent to either <code>DataFlavor.stringFlavor</code>
118 * or <code>DataFlavor.plainTextFlavor</code>
119 * @throws IOException if an IOException occurs while retrieving the data.
120 * By default, StringSelection never throws this exception, but a
121 * subclass may.
122 * @throws NullPointerException if flavor is <code>null</code>
123 * @see java.io.Reader
124 */
125 public Object getTransferData(DataFlavor flavor)
126 throws UnsupportedFlavorException, IOException
127 {
128 // JCK Test StringSelection0007: if 'flavor' is null, throw NPE
129 if (flavor.equals(flavors[STRING])) {
130 return (Object)data;
131 } else if (flavor.equals(flavors[PLAIN_TEXT])) {
132 return new StringReader(data == null ? "" : data);
133 } else {
134 throw new UnsupportedFlavorException(flavor);
135 }
136 }
137
138 public void lostOwnership(Clipboard clipboard, Transferable contents) {
139 }
140}