author | chegar |
Sun, 17 Aug 2014 15:54:13 +0100 | |
changeset 25859 | 3317bb8137f4 |
parent 24172 | jdk/src/share/classes/java/awt/datatransfer/Clipboard.java@814fb69fa9a5 |
child 26037 | 508779ce6619 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
22281
830790e8640d
8031467: Fix doclint missing issues in java.awt.{dnd[.peer], datatransfer}
darcy
parents:
15994
diff
changeset
|
2 |
* Copyright (c) 1996, 2014, 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 |
||
28 |
import java.awt.EventQueue; |
|
29 |
||
24153
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
22281
diff
changeset
|
30 |
import java.util.Objects; |
2 | 31 |
import java.util.Set; |
32 |
import java.util.HashSet; |
|
33 |
import java.util.Arrays; |
|
34 |
||
35 |
import java.io.IOException; |
|
36 |
||
37 |
/** |
|
38 |
* A class that implements a mechanism to transfer data using |
|
39 |
* cut/copy/paste operations. |
|
40 |
* <p> |
|
41 |
* {@link FlavorListener}s may be registered on an instance of the |
|
42 |
* Clipboard class to be notified about changes to the set of |
|
43 |
* {@link DataFlavor}s available on this clipboard (see |
|
44 |
* {@link #addFlavorListener}). |
|
45 |
* |
|
46 |
* @see java.awt.Toolkit#getSystemClipboard |
|
47 |
* @see java.awt.Toolkit#getSystemSelection |
|
48 |
* |
|
49 |
* @author Amy Fowler |
|
50 |
* @author Alexander Gerasimov |
|
51 |
*/ |
|
52 |
public class Clipboard { |
|
53 |
||
54 |
String name; |
|
55 |
||
22281
830790e8640d
8031467: Fix doclint missing issues in java.awt.{dnd[.peer], datatransfer}
darcy
parents:
15994
diff
changeset
|
56 |
/** |
830790e8640d
8031467: Fix doclint missing issues in java.awt.{dnd[.peer], datatransfer}
darcy
parents:
15994
diff
changeset
|
57 |
* The owner of the clipboard. |
830790e8640d
8031467: Fix doclint missing issues in java.awt.{dnd[.peer], datatransfer}
darcy
parents:
15994
diff
changeset
|
58 |
*/ |
2 | 59 |
protected ClipboardOwner owner; |
22281
830790e8640d
8031467: Fix doclint missing issues in java.awt.{dnd[.peer], datatransfer}
darcy
parents:
15994
diff
changeset
|
60 |
/** |
830790e8640d
8031467: Fix doclint missing issues in java.awt.{dnd[.peer], datatransfer}
darcy
parents:
15994
diff
changeset
|
61 |
* Contents of the clipboard. |
830790e8640d
8031467: Fix doclint missing issues in java.awt.{dnd[.peer], datatransfer}
darcy
parents:
15994
diff
changeset
|
62 |
*/ |
2 | 63 |
protected Transferable contents; |
64 |
||
65 |
/** |
|
66 |
* An aggregate of flavor listeners registered on this local clipboard. |
|
67 |
* |
|
68 |
* @since 1.5 |
|
69 |
*/ |
|
24172
814fb69fa9a5
8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents:
24153
diff
changeset
|
70 |
private Set<FlavorListener> flavorListeners; |
2 | 71 |
|
72 |
/** |
|
73 |
* A set of <code>DataFlavor</code>s that is available on |
|
74 |
* this local clipboard. It is used for tracking changes |
|
75 |
* of <code>DataFlavor</code>s available on this clipboard. |
|
76 |
* |
|
77 |
* @since 1.5 |
|
78 |
*/ |
|
15994
5c8a3d840366
8007295: Reduce number of warnings in awt classes
mcherkas
parents:
12813
diff
changeset
|
79 |
private Set<DataFlavor> currentDataFlavors; |
2 | 80 |
|
81 |
/** |
|
82 |
* Creates a clipboard object. |
|
22281
830790e8640d
8031467: Fix doclint missing issues in java.awt.{dnd[.peer], datatransfer}
darcy
parents:
15994
diff
changeset
|
83 |
* @param name for the clipboard |
2 | 84 |
* @see java.awt.Toolkit#getSystemClipboard |
85 |
*/ |
|
86 |
public Clipboard(String name) { |
|
87 |
this.name = name; |
|
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* Returns the name of this clipboard object. |
|
22281
830790e8640d
8031467: Fix doclint missing issues in java.awt.{dnd[.peer], datatransfer}
darcy
parents:
15994
diff
changeset
|
92 |
* @return the name of this clipboard object |
2 | 93 |
* |
94 |
* @see java.awt.Toolkit#getSystemClipboard |
|
95 |
*/ |
|
96 |
public String getName() { |
|
97 |
return name; |
|
98 |
} |
|
99 |
||
100 |
/** |
|
101 |
* Sets the current contents of the clipboard to the specified |
|
102 |
* transferable object and registers the specified clipboard owner |
|
103 |
* as the owner of the new contents. |
|
104 |
* <p> |
|
105 |
* If there is an existing owner different from the argument |
|
106 |
* <code>owner</code>, that owner is notified that it no longer |
|
107 |
* holds ownership of the clipboard contents via an invocation |
|
108 |
* of <code>ClipboardOwner.lostOwnership()</code> on that owner. |
|
109 |
* An implementation of <code>setContents()</code> is free not |
|
110 |
* to invoke <code>lostOwnership()</code> directly from this method. |
|
111 |
* For example, <code>lostOwnership()</code> may be invoked later on |
|
112 |
* a different thread. The same applies to <code>FlavorListener</code>s |
|
113 |
* registered on this clipboard. |
|
114 |
* <p> |
|
115 |
* The method throws <code>IllegalStateException</code> if the clipboard |
|
116 |
* is currently unavailable. For example, on some platforms, the system |
|
117 |
* clipboard is unavailable while it is accessed by another application. |
|
118 |
* |
|
119 |
* @param contents the transferable object representing the |
|
120 |
* clipboard content |
|
121 |
* @param owner the object which owns the clipboard content |
|
122 |
* @throws IllegalStateException if the clipboard is currently unavailable |
|
123 |
* @see java.awt.Toolkit#getSystemClipboard |
|
124 |
*/ |
|
125 |
public synchronized void setContents(Transferable contents, ClipboardOwner owner) { |
|
126 |
final ClipboardOwner oldOwner = this.owner; |
|
127 |
final Transferable oldContents = this.contents; |
|
128 |
||
129 |
this.owner = owner; |
|
130 |
this.contents = contents; |
|
131 |
||
132 |
if (oldOwner != null && oldOwner != owner) { |
|
24153
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
22281
diff
changeset
|
133 |
EventQueue.invokeLater(() -> oldOwner.lostOwnership(Clipboard.this, oldContents)); |
2 | 134 |
} |
135 |
fireFlavorsChanged(); |
|
136 |
} |
|
137 |
||
138 |
/** |
|
139 |
* Returns a transferable object representing the current contents |
|
140 |
* of the clipboard. If the clipboard currently has no contents, |
|
141 |
* it returns <code>null</code>. The parameter Object requestor is |
|
142 |
* not currently used. The method throws |
|
143 |
* <code>IllegalStateException</code> if the clipboard is currently |
|
144 |
* unavailable. For example, on some platforms, the system clipboard is |
|
145 |
* unavailable while it is accessed by another application. |
|
146 |
* |
|
147 |
* @param requestor the object requesting the clip data (not used) |
|
148 |
* @return the current transferable object on the clipboard |
|
149 |
* @throws IllegalStateException if the clipboard is currently unavailable |
|
150 |
* @see java.awt.Toolkit#getSystemClipboard |
|
151 |
*/ |
|
152 |
public synchronized Transferable getContents(Object requestor) { |
|
153 |
return contents; |
|
154 |
} |
|
155 |
||
156 |
||
157 |
/** |
|
158 |
* Returns an array of <code>DataFlavor</code>s in which the current |
|
159 |
* contents of this clipboard can be provided. If there are no |
|
160 |
* <code>DataFlavor</code>s available, this method returns a zero-length |
|
161 |
* array. |
|
162 |
* |
|
163 |
* @return an array of <code>DataFlavor</code>s in which the current |
|
164 |
* contents of this clipboard can be provided |
|
165 |
* |
|
166 |
* @throws IllegalStateException if this clipboard is currently unavailable |
|
167 |
* |
|
168 |
* @since 1.5 |
|
169 |
*/ |
|
170 |
public DataFlavor[] getAvailableDataFlavors() { |
|
171 |
Transferable cntnts = getContents(null); |
|
172 |
if (cntnts == null) { |
|
173 |
return new DataFlavor[0]; |
|
174 |
} |
|
175 |
return cntnts.getTransferDataFlavors(); |
|
176 |
} |
|
177 |
||
178 |
/** |
|
179 |
* Returns whether or not the current contents of this clipboard can be |
|
180 |
* provided in the specified <code>DataFlavor</code>. |
|
181 |
* |
|
182 |
* @param flavor the requested <code>DataFlavor</code> for the contents |
|
183 |
* |
|
184 |
* @return <code>true</code> if the current contents of this clipboard |
|
185 |
* can be provided in the specified <code>DataFlavor</code>; |
|
186 |
* <code>false</code> otherwise |
|
187 |
* |
|
188 |
* @throws NullPointerException if <code>flavor</code> is <code>null</code> |
|
189 |
* @throws IllegalStateException if this clipboard is currently unavailable |
|
190 |
* |
|
191 |
* @since 1.5 |
|
192 |
*/ |
|
193 |
public boolean isDataFlavorAvailable(DataFlavor flavor) { |
|
194 |
if (flavor == null) { |
|
195 |
throw new NullPointerException("flavor"); |
|
196 |
} |
|
197 |
||
198 |
Transferable cntnts = getContents(null); |
|
199 |
if (cntnts == null) { |
|
200 |
return false; |
|
201 |
} |
|
202 |
return cntnts.isDataFlavorSupported(flavor); |
|
203 |
} |
|
204 |
||
205 |
/** |
|
206 |
* Returns an object representing the current contents of this clipboard |
|
207 |
* in the specified <code>DataFlavor</code>. |
|
208 |
* The class of the object returned is defined by the representation |
|
209 |
* class of <code>flavor</code>. |
|
210 |
* |
|
211 |
* @param flavor the requested <code>DataFlavor</code> for the contents |
|
212 |
* |
|
213 |
* @return an object representing the current contents of this clipboard |
|
214 |
* in the specified <code>DataFlavor</code> |
|
215 |
* |
|
216 |
* @throws NullPointerException if <code>flavor</code> is <code>null</code> |
|
217 |
* @throws IllegalStateException if this clipboard is currently unavailable |
|
218 |
* @throws UnsupportedFlavorException if the requested <code>DataFlavor</code> |
|
219 |
* is not available |
|
220 |
* @throws IOException if the data in the requested <code>DataFlavor</code> |
|
221 |
* can not be retrieved |
|
222 |
* |
|
223 |
* @see DataFlavor#getRepresentationClass |
|
224 |
* |
|
225 |
* @since 1.5 |
|
226 |
*/ |
|
227 |
public Object getData(DataFlavor flavor) |
|
228 |
throws UnsupportedFlavorException, IOException { |
|
229 |
if (flavor == null) { |
|
230 |
throw new NullPointerException("flavor"); |
|
231 |
} |
|
232 |
||
233 |
Transferable cntnts = getContents(null); |
|
234 |
if (cntnts == null) { |
|
235 |
throw new UnsupportedFlavorException(flavor); |
|
236 |
} |
|
237 |
return cntnts.getTransferData(flavor); |
|
238 |
} |
|
239 |
||
240 |
||
241 |
/** |
|
242 |
* Registers the specified <code>FlavorListener</code> to receive |
|
243 |
* <code>FlavorEvent</code>s from this clipboard. |
|
244 |
* If <code>listener</code> is <code>null</code>, no exception |
|
245 |
* is thrown and no action is performed. |
|
246 |
* |
|
247 |
* @param listener the listener to be added |
|
248 |
* |
|
249 |
* @see #removeFlavorListener |
|
250 |
* @see #getFlavorListeners |
|
251 |
* @see FlavorListener |
|
252 |
* @see FlavorEvent |
|
253 |
* @since 1.5 |
|
254 |
*/ |
|
255 |
public synchronized void addFlavorListener(FlavorListener listener) { |
|
256 |
if (listener == null) { |
|
257 |
return; |
|
258 |
} |
|
24172
814fb69fa9a5
8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents:
24153
diff
changeset
|
259 |
|
814fb69fa9a5
8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents:
24153
diff
changeset
|
260 |
if (flavorListeners == null) { |
814fb69fa9a5
8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents:
24153
diff
changeset
|
261 |
flavorListeners = new HashSet<>(); |
814fb69fa9a5
8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents:
24153
diff
changeset
|
262 |
currentDataFlavors = getAvailableDataFlavorSet(); |
814fb69fa9a5
8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents:
24153
diff
changeset
|
263 |
} |
814fb69fa9a5
8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents:
24153
diff
changeset
|
264 |
|
2 | 265 |
flavorListeners.add(listener); |
266 |
} |
|
267 |
||
268 |
/** |
|
269 |
* Removes the specified <code>FlavorListener</code> so that it no longer |
|
270 |
* receives <code>FlavorEvent</code>s from this <code>Clipboard</code>. |
|
271 |
* This method performs no function, nor does it throw an exception, if |
|
272 |
* the listener specified by the argument was not previously added to this |
|
273 |
* <code>Clipboard</code>. |
|
274 |
* If <code>listener</code> is <code>null</code>, no exception |
|
275 |
* is thrown and no action is performed. |
|
276 |
* |
|
277 |
* @param listener the listener to be removed |
|
278 |
* |
|
279 |
* @see #addFlavorListener |
|
280 |
* @see #getFlavorListeners |
|
281 |
* @see FlavorListener |
|
282 |
* @see FlavorEvent |
|
283 |
* @since 1.5 |
|
284 |
*/ |
|
285 |
public synchronized void removeFlavorListener(FlavorListener listener) { |
|
24172
814fb69fa9a5
8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents:
24153
diff
changeset
|
286 |
if (listener == null || flavorListeners == null) { |
2 | 287 |
return; |
288 |
} |
|
289 |
flavorListeners.remove(listener); |
|
290 |
} |
|
291 |
||
292 |
/** |
|
293 |
* Returns an array of all the <code>FlavorListener</code>s currently |
|
294 |
* registered on this <code>Clipboard</code>. |
|
295 |
* |
|
296 |
* @return all of this clipboard's <code>FlavorListener</code>s or an empty |
|
297 |
* array if no listeners are currently registered |
|
298 |
* @see #addFlavorListener |
|
299 |
* @see #removeFlavorListener |
|
300 |
* @see FlavorListener |
|
301 |
* @see FlavorEvent |
|
302 |
* @since 1.5 |
|
303 |
*/ |
|
304 |
public synchronized FlavorListener[] getFlavorListeners() { |
|
24172
814fb69fa9a5
8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents:
24153
diff
changeset
|
305 |
return flavorListeners == null ? new FlavorListener[0] : |
814fb69fa9a5
8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents:
24153
diff
changeset
|
306 |
flavorListeners.toArray(new FlavorListener[flavorListeners.size()]); |
2 | 307 |
} |
308 |
||
309 |
/** |
|
310 |
* Checks change of the <code>DataFlavor</code>s and, if necessary, |
|
311 |
* notifies all listeners that have registered interest for notification |
|
312 |
* on <code>FlavorEvent</code>s. |
|
313 |
* |
|
314 |
* @since 1.5 |
|
315 |
*/ |
|
316 |
private void fireFlavorsChanged() { |
|
24172
814fb69fa9a5
8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents:
24153
diff
changeset
|
317 |
if (flavorListeners == null) { |
814fb69fa9a5
8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents:
24153
diff
changeset
|
318 |
return; |
814fb69fa9a5
8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents:
24153
diff
changeset
|
319 |
} |
814fb69fa9a5
8039752: Regression: Clipboard couldn't be used on linux
pchelko
parents:
24153
diff
changeset
|
320 |
|
15994
5c8a3d840366
8007295: Reduce number of warnings in awt classes
mcherkas
parents:
12813
diff
changeset
|
321 |
Set<DataFlavor> prevDataFlavors = currentDataFlavors; |
2 | 322 |
currentDataFlavors = getAvailableDataFlavorSet(); |
24153
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
22281
diff
changeset
|
323 |
if (Objects.equals(prevDataFlavors, currentDataFlavors)) { |
2 | 324 |
return; |
325 |
} |
|
24153
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
22281
diff
changeset
|
326 |
flavorListeners.forEach(listener -> |
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
22281
diff
changeset
|
327 |
EventQueue.invokeLater(() -> |
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
22281
diff
changeset
|
328 |
listener.flavorsChanged(new FlavorEvent(Clipboard.this)))); |
2 | 329 |
} |
330 |
||
331 |
/** |
|
332 |
* Returns a set of <code>DataFlavor</code>s currently available |
|
333 |
* on this clipboard. |
|
334 |
* |
|
335 |
* @return a set of <code>DataFlavor</code>s currently available |
|
336 |
* on this clipboard |
|
337 |
* |
|
338 |
* @since 1.5 |
|
339 |
*/ |
|
15994
5c8a3d840366
8007295: Reduce number of warnings in awt classes
mcherkas
parents:
12813
diff
changeset
|
340 |
private Set<DataFlavor> getAvailableDataFlavorSet() { |
5c8a3d840366
8007295: Reduce number of warnings in awt classes
mcherkas
parents:
12813
diff
changeset
|
341 |
Set<DataFlavor> set = new HashSet<>(); |
2 | 342 |
Transferable contents = getContents(null); |
343 |
if (contents != null) { |
|
344 |
DataFlavor[] flavors = contents.getTransferDataFlavors(); |
|
345 |
if (flavors != null) { |
|
346 |
set.addAll(Arrays.asList(flavors)); |
|
347 |
} |
|
348 |
} |
|
349 |
return set; |
|
350 |
} |
|
351 |
} |