author | hannesw |
Thu, 23 Nov 2017 16:20:10 +0100 | |
changeset 47929 | df9e7f2df01f |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
45655
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
2 |
* Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.awt.datatransfer; |
|
27 |
||
45655
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
28 |
import java.io.IOException; |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
29 |
import java.io.StringReader; |
12813
c10ab96dcf41
7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI
erikj
parents:
5506
diff
changeset
|
30 |
|
2 | 31 |
/** |
45655
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
32 |
* A {@code Transferable} which implements the capability required to transfer a |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
33 |
* {@code String}. |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
34 |
* <p> |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
35 |
* This {@code Transferable} properly supports {@code DataFlavor.stringFlavor} |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
36 |
* and all equivalent flavors. Support for {@code DataFlavor.plainTextFlavor} |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
37 |
* and all equivalent flavors is <b>deprecated</b>. No other {@code DataFlavor}s |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
38 |
* are supported. |
2 | 39 |
* |
45655
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
40 |
* @see DataFlavor#stringFlavor |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
41 |
* @see DataFlavor#plainTextFlavor |
45434
4582657c7260
8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents:
28987
diff
changeset
|
42 |
* @since 1.1 |
2 | 43 |
*/ |
44 |
public class StringSelection implements Transferable, ClipboardOwner { |
|
45 |
||
46 |
private static final int STRING = 0; |
|
47 |
private static final int PLAIN_TEXT = 1; |
|
48 |
||
28231
b608ffcaed74
8066621: Suppress deprecation warnings in java.desktop module
darcy
parents:
25859
diff
changeset
|
49 |
@SuppressWarnings("deprecation") |
2 | 50 |
private static final DataFlavor[] flavors = { |
51 |
DataFlavor.stringFlavor, |
|
52 |
DataFlavor.plainTextFlavor // deprecated |
|
53 |
}; |
|
54 |
||
55 |
private String data; |
|
56 |
||
57 |
/** |
|
45655
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
58 |
* Creates a {@code Transferable} capable of transferring the specified |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
59 |
* {@code String}. |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
60 |
* |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
61 |
* @param data the string to be transferred |
2 | 62 |
*/ |
63 |
public StringSelection(String data) { |
|
64 |
this.data = data; |
|
65 |
} |
|
66 |
||
67 |
/** |
|
45655
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
68 |
* Returns an array of flavors in which this {@code Transferable} can |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
69 |
* provide the data. {@code DataFlavor.stringFlavor} is properly supported. |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
70 |
* Support for {@code DataFlavor.plainTextFlavor} is <b>deprecated</b>. |
2 | 71 |
* |
45655
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
72 |
* @return an array of length two, whose elements are |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
73 |
* {@code DataFlavor.stringFlavor} and |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
74 |
* {@code DataFlavor.plainTextFlavor} |
2 | 75 |
*/ |
76 |
public DataFlavor[] getTransferDataFlavors() { |
|
77 |
// returning flavors itself would allow client code to modify |
|
78 |
// our internal behavior |
|
22584
eed64ee05369
8032733: Fix cast lint warnings in client libraries
darcy
parents:
22281
diff
changeset
|
79 |
return flavors.clone(); |
2 | 80 |
} |
81 |
||
82 |
/** |
|
83 |
* Returns whether the requested flavor is supported by this |
|
45655
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
84 |
* {@code Transferable}. |
2 | 85 |
* |
45655
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
86 |
* @param flavor the requested flavor for the data |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
87 |
* @return {@code true} if {@code flavor} is equal to |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
88 |
* {@code DataFlavor.stringFlavor} or |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
89 |
* {@code DataFlavor.plainTextFlavor}; {@code false} if |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
90 |
* {@code flavor} is not one of the above flavors |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
91 |
* @throws NullPointerException if {@code flavor} is {@code null} |
2 | 92 |
*/ |
93 |
public boolean isDataFlavorSupported(DataFlavor flavor) { |
|
94 |
// JCK Test StringSelection0003: if 'flavor' is null, throw NPE |
|
95 |
for (int i = 0; i < flavors.length; i++) { |
|
96 |
if (flavor.equals(flavors[i])) { |
|
97 |
return true; |
|
98 |
} |
|
99 |
} |
|
100 |
return false; |
|
101 |
} |
|
102 |
||
103 |
/** |
|
45655
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
104 |
* Returns the {@code Transferable}'s data in the requested |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
105 |
* {@code DataFlavor} if possible. If the desired flavor is |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
106 |
* {@code DataFlavor.stringFlavor}, or an equivalent flavor, the |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
107 |
* {@code String} representing the selection is returned. If the desired |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
108 |
* flavor is {@code DataFlavor.plainTextFlavor}, or an equivalent flavor, a |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
109 |
* {@code Reader} is returned. |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
110 |
* <br> |
2 | 111 |
* <b>Note:</b> The behavior of this method for |
45655
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
112 |
* {@code DataFlavor.plainTextFlavor} |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
113 |
* and equivalent {@code DataFlavor}s is inconsistent with the definition of |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
114 |
* {@code DataFlavor.plainTextFlavor}. |
2 | 115 |
* |
45655
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
116 |
* @param flavor the requested flavor for the data |
2 | 117 |
* @return the data in the requested flavor, as outlined above |
45655
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
118 |
* @throws UnsupportedFlavorException if the requested data flavor is not |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
119 |
* equivalent to either {@code DataFlavor.stringFlavor} or |
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
120 |
* {@code DataFlavor.plainTextFlavor} |
2 | 121 |
* @throws IOException if an IOException occurs while retrieving the data. |
122 |
* By default, StringSelection never throws this exception, but a |
|
123 |
* subclass may. |
|
45655
02c95aa8a53a
8180889: Cleanup of javadoc in java.datatransfer module
serb
parents:
45434
diff
changeset
|
124 |
* @throws NullPointerException if {@code flavor} is {@code null} |
2 | 125 |
* @see java.io.Reader |
126 |
*/ |
|
127 |
public Object getTransferData(DataFlavor flavor) |
|
128 |
throws UnsupportedFlavorException, IOException |
|
129 |
{ |
|
130 |
// JCK Test StringSelection0007: if 'flavor' is null, throw NPE |
|
131 |
if (flavor.equals(flavors[STRING])) { |
|
132 |
return (Object)data; |
|
133 |
} else if (flavor.equals(flavors[PLAIN_TEXT])) { |
|
134 |
return new StringReader(data == null ? "" : data); |
|
135 |
} else { |
|
136 |
throw new UnsupportedFlavorException(flavor); |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
public void lostOwnership(Clipboard clipboard, Transferable contents) { |
|
141 |
} |
|
142 |
} |