author | ngthomas |
Tue, 15 Apr 2014 22:57:02 -0700 | |
changeset 24183 | db8b6b7d25c0 |
parent 24153 | 2c19973ab9f2 |
parent 23905 | 51af43e32270 |
child 24538 | 25bf8153fbfe |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1999, 2006, 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 sun.awt.datatransfer; |
|
27 |
||
28 |
import java.awt.EventQueue; |
|
29 |
||
30 |
import java.awt.datatransfer.Clipboard; |
|
31 |
import java.awt.datatransfer.FlavorTable; |
|
32 |
import java.awt.datatransfer.SystemFlavorMap; |
|
33 |
import java.awt.datatransfer.Transferable; |
|
34 |
import java.awt.datatransfer.ClipboardOwner; |
|
35 |
import java.awt.datatransfer.DataFlavor; |
|
36 |
import java.awt.datatransfer.FlavorListener; |
|
37 |
import java.awt.datatransfer.FlavorEvent; |
|
38 |
import java.awt.datatransfer.UnsupportedFlavorException; |
|
39 |
||
40 |
import java.beans.PropertyChangeEvent; |
|
41 |
import java.beans.PropertyChangeListener; |
|
42 |
||
24153
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
43 |
import java.util.Objects; |
2 | 44 |
import java.util.Set; |
45 |
import java.util.HashSet; |
|
46 |
||
47 |
import java.io.IOException; |
|
48 |
||
49 |
import sun.awt.AppContext; |
|
50 |
import sun.awt.PeerEvent; |
|
51 |
import sun.awt.SunToolkit; |
|
52 |
||
53 |
||
54 |
/** |
|
55 |
* Serves as a common, helper superclass for the Win32 and X11 system |
|
56 |
* Clipboards. |
|
57 |
* |
|
58 |
* @author Danila Sinopalnikov |
|
59 |
* @author Alexander Gerasimov |
|
60 |
* |
|
61 |
* @since 1.3 |
|
62 |
*/ |
|
63 |
public abstract class SunClipboard extends Clipboard |
|
64 |
implements PropertyChangeListener { |
|
65 |
||
66 |
private AppContext contentsContext = null; |
|
67 |
||
68 |
private final Object CLIPBOARD_FLAVOR_LISTENER_KEY; |
|
69 |
||
70 |
/** |
|
71 |
* A number of <code>FlavorListener</code>s currently registered |
|
72 |
* on this clipboard across all <code>AppContext</code>s. |
|
73 |
*/ |
|
74 |
private volatile int numberOfFlavorListeners = 0; |
|
75 |
||
76 |
/** |
|
77 |
* A set of <code>DataFlavor</code>s that is available on |
|
78 |
* this clipboard. It is used for tracking changes |
|
79 |
* of <code>DataFlavor</code>s available on this clipboard. |
|
80 |
*/ |
|
81 |
private volatile Set currentDataFlavors; |
|
82 |
||
83 |
||
84 |
public SunClipboard(String name) { |
|
85 |
super(name); |
|
86 |
CLIPBOARD_FLAVOR_LISTENER_KEY = new StringBuffer(name + "_CLIPBOARD_FLAVOR_LISTENER_KEY"); |
|
87 |
} |
|
88 |
||
89 |
public synchronized void setContents(Transferable contents, |
|
90 |
ClipboardOwner owner) { |
|
91 |
// 4378007 : Toolkit.getSystemClipboard().setContents(null, null) |
|
92 |
// should throw NPE |
|
93 |
if (contents == null) { |
|
94 |
throw new NullPointerException("contents"); |
|
95 |
} |
|
96 |
||
97 |
initContext(); |
|
98 |
||
99 |
final ClipboardOwner oldOwner = this.owner; |
|
100 |
final Transferable oldContents = this.contents; |
|
101 |
||
102 |
try { |
|
103 |
this.owner = owner; |
|
104 |
this.contents = new TransferableProxy(contents, true); |
|
105 |
||
106 |
setContentsNative(contents); |
|
107 |
} finally { |
|
108 |
if (oldOwner != null && oldOwner != owner) { |
|
24153
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
109 |
EventQueue.invokeLater(() -> oldOwner.lostOwnership(SunClipboard.this, oldContents)); |
2 | 110 |
} |
111 |
} |
|
112 |
} |
|
113 |
||
114 |
private synchronized void initContext() { |
|
115 |
final AppContext context = AppContext.getAppContext(); |
|
116 |
||
117 |
if (contentsContext != context) { |
|
118 |
// Need to synchronize on the AppContext to guarantee that it cannot |
|
119 |
// be disposed after the check, but before the listener is added. |
|
120 |
synchronized (context) { |
|
121 |
if (context.isDisposed()) { |
|
122 |
throw new IllegalStateException("Can't set contents from disposed AppContext"); |
|
123 |
} |
|
124 |
context.addPropertyChangeListener |
|
125 |
(AppContext.DISPOSED_PROPERTY_NAME, this); |
|
126 |
} |
|
127 |
if (contentsContext != null) { |
|
128 |
contentsContext.removePropertyChangeListener |
|
129 |
(AppContext.DISPOSED_PROPERTY_NAME, this); |
|
130 |
} |
|
131 |
contentsContext = context; |
|
132 |
} |
|
133 |
} |
|
134 |
||
135 |
public synchronized Transferable getContents(Object requestor) { |
|
136 |
if (contents != null) { |
|
137 |
return contents; |
|
138 |
} |
|
139 |
return new ClipboardTransferable(this); |
|
140 |
} |
|
141 |
||
142 |
||
143 |
/** |
|
144 |
* @return the contents of this clipboard if it has been set from the same |
|
145 |
* AppContext as it is currently retrieved or null otherwise |
|
146 |
* @since 1.5 |
|
147 |
*/ |
|
148 |
private synchronized Transferable getContextContents() { |
|
149 |
AppContext context = AppContext.getAppContext(); |
|
150 |
return (context == contentsContext) ? contents : null; |
|
151 |
} |
|
152 |
||
153 |
||
154 |
/** |
|
155 |
* @see java.awt.Clipboard#getAvailableDataFlavors |
|
156 |
* @since 1.5 |
|
157 |
*/ |
|
158 |
public DataFlavor[] getAvailableDataFlavors() { |
|
159 |
Transferable cntnts = getContextContents(); |
|
160 |
if (cntnts != null) { |
|
161 |
return cntnts.getTransferDataFlavors(); |
|
162 |
} |
|
163 |
||
164 |
long[] formats = getClipboardFormatsOpenClose(); |
|
165 |
||
166 |
return DataTransferer.getInstance(). |
|
23905 | 167 |
getFlavorsForFormatsAsArray(formats, getDefaultFlavorTable()); |
2 | 168 |
} |
169 |
||
170 |
/** |
|
171 |
* @see java.awt.Clipboard#isDataFlavorAvailable |
|
172 |
* @since 1.5 |
|
173 |
*/ |
|
174 |
public boolean isDataFlavorAvailable(DataFlavor flavor) { |
|
175 |
if (flavor == null) { |
|
176 |
throw new NullPointerException("flavor"); |
|
177 |
} |
|
178 |
||
179 |
Transferable cntnts = getContextContents(); |
|
180 |
if (cntnts != null) { |
|
181 |
return cntnts.isDataFlavorSupported(flavor); |
|
182 |
} |
|
183 |
||
184 |
long[] formats = getClipboardFormatsOpenClose(); |
|
185 |
||
186 |
return formatArrayAsDataFlavorSet(formats).contains(flavor); |
|
187 |
} |
|
188 |
||
189 |
/** |
|
190 |
* @see java.awt.Clipboard#getData |
|
191 |
* @since 1.5 |
|
192 |
*/ |
|
193 |
public Object getData(DataFlavor flavor) |
|
194 |
throws UnsupportedFlavorException, IOException { |
|
195 |
if (flavor == null) { |
|
196 |
throw new NullPointerException("flavor"); |
|
197 |
} |
|
198 |
||
199 |
Transferable cntnts = getContextContents(); |
|
200 |
if (cntnts != null) { |
|
201 |
return cntnts.getTransferData(flavor); |
|
202 |
} |
|
203 |
||
204 |
long format = 0; |
|
205 |
byte[] data = null; |
|
206 |
Transferable localeTransferable = null; |
|
207 |
||
208 |
try { |
|
209 |
openClipboard(null); |
|
210 |
||
211 |
long[] formats = getClipboardFormats(); |
|
23279 | 212 |
Long lFormat = DataTransferer.getInstance(). |
23905 | 213 |
getFlavorsForFormats(formats, getDefaultFlavorTable()).get(flavor); |
2 | 214 |
|
215 |
if (lFormat == null) { |
|
216 |
throw new UnsupportedFlavorException(flavor); |
|
217 |
} |
|
218 |
||
219 |
format = lFormat.longValue(); |
|
220 |
data = getClipboardData(format); |
|
221 |
||
222 |
if (DataTransferer.getInstance().isLocaleDependentTextFormat(format)) { |
|
223 |
localeTransferable = createLocaleTransferable(formats); |
|
224 |
} |
|
225 |
||
226 |
} finally { |
|
227 |
closeClipboard(); |
|
228 |
} |
|
229 |
||
230 |
return DataTransferer.getInstance(). |
|
231 |
translateBytes(data, flavor, format, localeTransferable); |
|
232 |
} |
|
233 |
||
234 |
/** |
|
235 |
* The clipboard must be opened. |
|
236 |
* |
|
237 |
* @since 1.5 |
|
238 |
*/ |
|
239 |
protected Transferable createLocaleTransferable(long[] formats) throws IOException { |
|
240 |
return null; |
|
241 |
} |
|
242 |
||
243 |
/** |
|
244 |
* @throws IllegalStateException if the clipboard has not been opened |
|
245 |
*/ |
|
246 |
public void openClipboard(SunClipboard newOwner) {} |
|
247 |
public void closeClipboard() {} |
|
248 |
||
249 |
public abstract long getID(); |
|
250 |
||
251 |
public void propertyChange(PropertyChangeEvent evt) { |
|
252 |
if (AppContext.DISPOSED_PROPERTY_NAME.equals(evt.getPropertyName()) && |
|
253 |
Boolean.TRUE.equals(evt.getNewValue())) { |
|
254 |
final AppContext disposedContext = (AppContext)evt.getSource(); |
|
255 |
lostOwnershipLater(disposedContext); |
|
256 |
} |
|
257 |
} |
|
258 |
||
259 |
protected void lostOwnershipImpl() { |
|
260 |
lostOwnershipLater(null); |
|
261 |
} |
|
262 |
||
263 |
/** |
|
264 |
* Clears the clipboard state (contents, owner and contents context) and |
|
265 |
* notifies the current owner that ownership is lost. Does nothing if the |
|
266 |
* argument is not <code>null</code> and is not equal to the current |
|
267 |
* contents context. |
|
268 |
* |
|
269 |
* @param disposedContext the AppContext that is disposed or |
|
270 |
* <code>null</code> if the ownership is lost because another |
|
271 |
* application acquired ownership. |
|
272 |
*/ |
|
273 |
protected void lostOwnershipLater(final AppContext disposedContext) { |
|
274 |
final AppContext context = this.contentsContext; |
|
275 |
if (context == null) { |
|
276 |
return; |
|
277 |
} |
|
278 |
||
279 |
final Runnable runnable = new Runnable() { |
|
280 |
public void run() { |
|
281 |
final SunClipboard sunClipboard = SunClipboard.this; |
|
282 |
ClipboardOwner owner = null; |
|
283 |
Transferable contents = null; |
|
284 |
||
285 |
synchronized (sunClipboard) { |
|
286 |
final AppContext context = sunClipboard.contentsContext; |
|
287 |
||
288 |
if (context == null) { |
|
289 |
return; |
|
290 |
} |
|
291 |
||
292 |
if (disposedContext == null || context == disposedContext) { |
|
293 |
owner = sunClipboard.owner; |
|
294 |
contents = sunClipboard.contents; |
|
295 |
sunClipboard.contentsContext = null; |
|
296 |
sunClipboard.owner = null; |
|
297 |
sunClipboard.contents = null; |
|
298 |
sunClipboard.clearNativeContext(); |
|
299 |
context.removePropertyChangeListener |
|
300 |
(AppContext.DISPOSED_PROPERTY_NAME, sunClipboard); |
|
301 |
} else { |
|
302 |
return; |
|
303 |
} |
|
304 |
} |
|
305 |
if (owner != null) { |
|
306 |
owner.lostOwnership(sunClipboard, contents); |
|
307 |
} |
|
308 |
} |
|
309 |
}; |
|
310 |
||
311 |
SunToolkit.postEvent(context, new PeerEvent(this, runnable, |
|
312 |
PeerEvent.PRIORITY_EVENT)); |
|
313 |
} |
|
314 |
||
315 |
protected abstract void clearNativeContext(); |
|
316 |
||
317 |
protected abstract void setContentsNative(Transferable contents); |
|
318 |
||
319 |
/** |
|
320 |
* @since 1.5 |
|
321 |
*/ |
|
322 |
protected long[] getClipboardFormatsOpenClose() { |
|
323 |
try { |
|
324 |
openClipboard(null); |
|
325 |
return getClipboardFormats(); |
|
326 |
} finally { |
|
327 |
closeClipboard(); |
|
328 |
} |
|
329 |
} |
|
330 |
||
331 |
/** |
|
332 |
* Returns zero-length array (not null) if the number of available formats is zero. |
|
333 |
* |
|
334 |
* @throws IllegalStateException if formats could not be retrieved |
|
335 |
*/ |
|
336 |
protected abstract long[] getClipboardFormats(); |
|
337 |
||
338 |
protected abstract byte[] getClipboardData(long format) throws IOException; |
|
339 |
||
340 |
||
341 |
private static Set formatArrayAsDataFlavorSet(long[] formats) { |
|
342 |
return (formats == null) ? null : |
|
343 |
DataTransferer.getInstance(). |
|
23905 | 344 |
getFlavorsForFormatsAsSet(formats, getDefaultFlavorTable()); |
2 | 345 |
} |
346 |
||
347 |
||
348 |
public synchronized void addFlavorListener(FlavorListener listener) { |
|
349 |
if (listener == null) { |
|
350 |
return; |
|
351 |
} |
|
352 |
AppContext appContext = AppContext.getAppContext(); |
|
24153
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
353 |
Set<FlavorListener> flavorListeners = getFlavorListeners(appContext); |
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
354 |
if (flavorListeners == null) { |
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
355 |
flavorListeners = new HashSet<>(); |
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
356 |
appContext.put(CLIPBOARD_FLAVOR_LISTENER_KEY, flavorListeners); |
2 | 357 |
} |
24153
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
358 |
flavorListeners.add(listener); |
2 | 359 |
|
360 |
if (numberOfFlavorListeners++ == 0) { |
|
361 |
long[] currentFormats = null; |
|
362 |
try { |
|
363 |
openClipboard(null); |
|
364 |
currentFormats = getClipboardFormats(); |
|
365 |
} catch (IllegalStateException exc) { |
|
366 |
} finally { |
|
367 |
closeClipboard(); |
|
368 |
} |
|
369 |
currentDataFlavors = formatArrayAsDataFlavorSet(currentFormats); |
|
370 |
||
371 |
registerClipboardViewerChecked(); |
|
372 |
} |
|
373 |
} |
|
374 |
||
375 |
public synchronized void removeFlavorListener(FlavorListener listener) { |
|
376 |
if (listener == null) { |
|
377 |
return; |
|
378 |
} |
|
24153
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
379 |
Set<FlavorListener> flavorListeners = getFlavorListeners(AppContext.getAppContext()); |
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
380 |
if (flavorListeners == null){ |
2 | 381 |
//else we throw NullPointerException, but it is forbidden |
382 |
return; |
|
383 |
} |
|
24153
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
384 |
if (flavorListeners.remove(listener) && --numberOfFlavorListeners == 0) { |
2 | 385 |
unregisterClipboardViewerChecked(); |
386 |
currentDataFlavors = null; |
|
387 |
} |
|
388 |
} |
|
389 |
||
24153
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
390 |
@SuppressWarnings("unchecked") |
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
391 |
private Set<FlavorListener> getFlavorListeners(AppContext appContext) { |
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
392 |
return (Set<FlavorListener>)appContext.get(CLIPBOARD_FLAVOR_LISTENER_KEY); |
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
393 |
} |
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
394 |
|
2 | 395 |
public synchronized FlavorListener[] getFlavorListeners() { |
24153
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
396 |
Set<FlavorListener> flavorListeners = getFlavorListeners(AppContext.getAppContext()); |
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
397 |
return flavorListeners == null ? new FlavorListener[0] |
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
398 |
: flavorListeners.toArray(new FlavorListener[flavorListeners.size()]); |
2 | 399 |
} |
400 |
||
401 |
public boolean areFlavorListenersRegistered() { |
|
402 |
return (numberOfFlavorListeners > 0); |
|
403 |
} |
|
404 |
||
405 |
protected abstract void registerClipboardViewerChecked(); |
|
406 |
||
407 |
protected abstract void unregisterClipboardViewerChecked(); |
|
408 |
||
409 |
/** |
|
410 |
* Checks change of the <code>DataFlavor</code>s and, if necessary, |
|
411 |
* posts notifications on <code>FlavorEvent</code>s to the |
|
412 |
* AppContexts' EDTs. |
|
413 |
* The parameter <code>formats</code> is null iff we have just |
|
414 |
* failed to get formats available on the clipboard. |
|
415 |
* |
|
416 |
* @param formats data formats that have just been retrieved from |
|
417 |
* this clipboard |
|
418 |
*/ |
|
419 |
public void checkChange(long[] formats) { |
|
420 |
Set prevDataFlavors = currentDataFlavors; |
|
421 |
currentDataFlavors = formatArrayAsDataFlavorSet(formats); |
|
422 |
||
24153
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
423 |
if (Objects.equals(prevDataFlavors, currentDataFlavors)) { |
2 | 424 |
// we've been able to successfully get available on the clipboard |
425 |
// DataFlavors this and previous time and they are coincident; |
|
426 |
// don't notify |
|
427 |
return; |
|
428 |
} |
|
429 |
||
24153
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
430 |
for (AppContext appContext : AppContext.getAppContexts()) { |
2 | 431 |
if (appContext == null || appContext.isDisposed()) { |
432 |
continue; |
|
433 |
} |
|
24153
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
434 |
Set<FlavorListener> flavorListeners = getFlavorListeners(appContext); |
2 | 435 |
if (flavorListeners != null) { |
24153
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
436 |
for (FlavorListener listener : flavorListeners) { |
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
437 |
if (listener != null) { |
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
438 |
PeerEvent peerEvent = new PeerEvent(this, |
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
439 |
() -> listener.flavorsChanged(new FlavorEvent(SunClipboard.this)), |
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
440 |
PeerEvent.PRIORITY_EVENT); |
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
441 |
SunToolkit.postEvent(appContext, peerEvent); |
2c19973ab9f2
6463901: Either generify or deprecate sun.awt.EventListenerAggregate
pchelko
parents:
23279
diff
changeset
|
442 |
} |
2 | 443 |
} |
444 |
} |
|
445 |
} |
|
446 |
} |
|
447 |
||
23905 | 448 |
public static FlavorTable getDefaultFlavorTable() { |
449 |
return (FlavorTable) SystemFlavorMap.getDefaultFlavorMap(); |
|
450 |
} |
|
2 | 451 |
} |