-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathSunClipboard.java
447 lines (373 loc) · 14.7 KB
/
SunClipboard.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.awt.datatransfer;
import java.awt.EventQueue;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.FlavorTable;
import java.awt.datatransfer.SystemFlavorMap;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.FlavorListener;
import java.awt.datatransfer.FlavorEvent;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Arrays;
import java.util.Set;
import java.util.HashSet;
import java.io.IOException;
import sun.awt.AppContext;
import sun.awt.PeerEvent;
import sun.awt.SunToolkit;
/**
* Serves as a common, helper superclass for the Win32 and X11 system
* Clipboards.
*
* @author Danila Sinopalnikov
* @author Alexander Gerasimov
*
* @since 1.3
*/
public abstract class SunClipboard extends Clipboard
implements PropertyChangeListener {
private AppContext contentsContext = null;
private final Object CLIPBOARD_FLAVOR_LISTENER_KEY;
/**
* A number of {@code FlavorListener}s currently registered
* on this clipboard across all {@code AppContext}s.
*/
private volatile int numberOfFlavorListeners;
/**
* A set of {@code DataFlavor}s that is available on this clipboard. It is
* used for tracking changes of {@code DataFlavor}s available on this
* clipboard. Can be {@code null}.
*/
private volatile long[] currentFormats;
public SunClipboard(String name) {
super(name);
CLIPBOARD_FLAVOR_LISTENER_KEY = new StringBuffer(name + "_CLIPBOARD_FLAVOR_LISTENER_KEY");
}
public synchronized void setContents(Transferable contents,
ClipboardOwner owner) {
// 4378007 : Toolkit.getSystemClipboard().setContents(null, null)
// should throw NPE
if (contents == null) {
throw new NullPointerException("contents");
}
initContext();
final ClipboardOwner oldOwner = this.owner;
final Transferable oldContents = this.contents;
try {
this.owner = owner;
this.contents = new TransferableProxy(contents, true);
setContentsNative(contents);
} finally {
if (oldOwner != null && oldOwner != owner) {
EventQueue.invokeLater(() -> oldOwner.lostOwnership(SunClipboard.this, oldContents));
}
}
}
private synchronized void initContext() {
final AppContext context = AppContext.getAppContext();
if (contentsContext != context) {
// Need to synchronize on the AppContext to guarantee that it cannot
// be disposed after the check, but before the listener is added.
synchronized (context) {
if (context.isDisposed()) {
throw new IllegalStateException("Can't set contents from disposed AppContext");
}
context.addPropertyChangeListener
(AppContext.DISPOSED_PROPERTY_NAME, this);
}
if (contentsContext != null) {
contentsContext.removePropertyChangeListener
(AppContext.DISPOSED_PROPERTY_NAME, this);
}
contentsContext = context;
}
}
public synchronized Transferable getContents(Object requestor) {
if (contents != null) {
return contents;
}
return new ClipboardTransferable(this);
}
/**
* @return the contents of this clipboard if it has been set from the same
* AppContext as it is currently retrieved or null otherwise
* @since 1.5
*/
protected synchronized Transferable getContextContents() {
AppContext context = AppContext.getAppContext();
return (context == contentsContext) ? contents : null;
}
/**
* @see java.awt.datatransfer.Clipboard#getAvailableDataFlavors
* @since 1.5
*/
public DataFlavor[] getAvailableDataFlavors() {
Transferable cntnts = getContextContents();
if (cntnts != null) {
return cntnts.getTransferDataFlavors();
}
long[] formats = getClipboardFormatsOpenClose();
return DataTransferer.getInstance().
getFlavorsForFormatsAsArray(formats, getDefaultFlavorTable());
}
/**
* @see java.awt.datatransfer.Clipboard#isDataFlavorAvailable
* @since 1.5
*/
public boolean isDataFlavorAvailable(DataFlavor flavor) {
if (flavor == null) {
throw new NullPointerException("flavor");
}
Transferable cntnts = getContextContents();
if (cntnts != null) {
return cntnts.isDataFlavorSupported(flavor);
}
long[] formats = getClipboardFormatsOpenClose();
return formatArrayAsDataFlavorSet(formats).contains(flavor);
}
/**
* @see java.awt.datatransfer.Clipboard#getData
* @since 1.5
*/
public Object getData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException {
if (flavor == null) {
throw new NullPointerException("flavor");
}
Transferable cntnts = getContextContents();
if (cntnts != null) {
return cntnts.getTransferData(flavor);
}
long format = 0;
byte[] data = null;
Transferable localeTransferable = null;
try {
openClipboard(null);
long[] formats = getClipboardFormats();
Long lFormat = DataTransferer.getInstance().
getFlavorsForFormats(formats, getDefaultFlavorTable()).get(flavor);
if (lFormat == null) {
throw new UnsupportedFlavorException(flavor);
}
format = lFormat.longValue();
data = getClipboardData(format);
if (DataTransferer.getInstance().isLocaleDependentTextFormat(format)) {
localeTransferable = createLocaleTransferable(formats);
}
} finally {
closeClipboard();
}
return DataTransferer.getInstance().
translateBytes(data, flavor, format, localeTransferable);
}
/**
* The clipboard must be opened.
*
* @since 1.5
*/
protected Transferable createLocaleTransferable(long[] formats) throws IOException {
return null;
}
/**
* @throws IllegalStateException if the clipboard has not been opened
*/
public void openClipboard(SunClipboard newOwner) {}
public void closeClipboard() {}
public abstract long getID();
public void propertyChange(PropertyChangeEvent evt) {
if (AppContext.DISPOSED_PROPERTY_NAME.equals(evt.getPropertyName()) &&
Boolean.TRUE.equals(evt.getNewValue())) {
final AppContext disposedContext = (AppContext)evt.getSource();
lostOwnershipLater(disposedContext);
}
}
protected void lostOwnershipImpl() {
lostOwnershipLater(null);
}
/**
* Clears the clipboard state (contents, owner and contents context) and
* notifies the current owner that ownership is lost. Does nothing if the
* argument is not {@code null} and is not equal to the current
* contents context.
*
* @param disposedContext the AppContext that is disposed or
* {@code null} if the ownership is lost because another
* application acquired ownership.
*/
protected void lostOwnershipLater(final AppContext disposedContext) {
final AppContext context = this.contentsContext;
if (context == null) {
return;
}
SunToolkit.postEvent(context, new PeerEvent(this, () -> lostOwnershipNow(disposedContext),
PeerEvent.PRIORITY_EVENT));
}
protected void lostOwnershipNow(final AppContext disposedContext) {
final SunClipboard sunClipboard = SunClipboard.this;
ClipboardOwner owner = null;
Transferable contents = null;
synchronized (sunClipboard) {
final AppContext context = sunClipboard.contentsContext;
if (context == null) {
return;
}
if (disposedContext == null || context == disposedContext) {
owner = sunClipboard.owner;
contents = sunClipboard.contents;
sunClipboard.contentsContext = null;
sunClipboard.owner = null;
sunClipboard.contents = null;
sunClipboard.clearNativeContext();
context.removePropertyChangeListener
(AppContext.DISPOSED_PROPERTY_NAME, sunClipboard);
} else {
return;
}
}
if (owner != null) {
owner.lostOwnership(sunClipboard, contents);
}
}
protected abstract void clearNativeContext();
protected abstract void setContentsNative(Transferable contents);
/**
* @since 1.5
*/
protected long[] getClipboardFormatsOpenClose() {
try {
openClipboard(null);
return getClipboardFormats();
} finally {
closeClipboard();
}
}
/**
* Returns zero-length array (not null) if the number of available formats is zero.
*
* @throws IllegalStateException if formats could not be retrieved
*/
protected abstract long[] getClipboardFormats();
protected abstract byte[] getClipboardData(long format) throws IOException;
private static Set<DataFlavor> formatArrayAsDataFlavorSet(long[] formats) {
return (formats == null) ? null :
DataTransferer.getInstance().
getFlavorsForFormatsAsSet(formats, getDefaultFlavorTable());
}
public synchronized void addFlavorListener(FlavorListener listener) {
if (listener == null) {
return;
}
AppContext appContext = AppContext.getAppContext();
Set<FlavorListener> flavorListeners = getFlavorListeners(appContext);
if (flavorListeners == null) {
flavorListeners = new HashSet<>();
appContext.put(CLIPBOARD_FLAVOR_LISTENER_KEY, flavorListeners);
}
flavorListeners.add(listener);
if (numberOfFlavorListeners++ == 0) {
long[] currentFormats = null;
try {
openClipboard(null);
currentFormats = getClipboardFormats();
} catch (final IllegalStateException ignored) {
} finally {
closeClipboard();
}
this.currentFormats = currentFormats;
registerClipboardViewerChecked();
}
}
public synchronized void removeFlavorListener(FlavorListener listener) {
if (listener == null) {
return;
}
Set<FlavorListener> flavorListeners = getFlavorListeners(AppContext.getAppContext());
if (flavorListeners == null){
//else we throw NullPointerException, but it is forbidden
return;
}
if (flavorListeners.remove(listener) && --numberOfFlavorListeners == 0) {
unregisterClipboardViewerChecked();
currentFormats = null;
}
}
@SuppressWarnings("unchecked")
private Set<FlavorListener> getFlavorListeners(AppContext appContext) {
return (Set<FlavorListener>)appContext.get(CLIPBOARD_FLAVOR_LISTENER_KEY);
}
public synchronized FlavorListener[] getFlavorListeners() {
Set<FlavorListener> flavorListeners = getFlavorListeners(AppContext.getAppContext());
return flavorListeners == null ? new FlavorListener[0]
: flavorListeners.toArray(new FlavorListener[flavorListeners.size()]);
}
public boolean areFlavorListenersRegistered() {
return (numberOfFlavorListeners > 0);
}
protected abstract void registerClipboardViewerChecked();
protected abstract void unregisterClipboardViewerChecked();
/**
* Checks change of the {@code DataFlavor}s and, if necessary,
* posts notifications on {@code FlavorEvent}s to the
* AppContexts' EDTs.
* The parameter {@code formats} is null iff we have just
* failed to get formats available on the clipboard.
*
* @param formats data formats that have just been retrieved from
* this clipboard
*/
protected final void checkChange(final long[] formats) {
if (Arrays.equals(formats, currentFormats)) {
// we've been able to successfully get available on the clipboard
// DataFlavors this and previous time and they are coincident;
// don't notify
return;
}
currentFormats = formats;
for (final AppContext appContext : AppContext.getAppContexts()) {
if (appContext == null || appContext.isDisposed()) {
continue;
}
Set<FlavorListener> flavorListeners = getFlavorListeners(appContext);
if (flavorListeners != null) {
for (FlavorListener listener : flavorListeners) {
if (listener != null) {
PeerEvent peerEvent = new PeerEvent(this,
() -> listener.flavorsChanged(new FlavorEvent(SunClipboard.this)),
PeerEvent.PRIORITY_EVENT);
SunToolkit.postEvent(appContext, peerEvent);
}
}
}
}
}
public static FlavorTable getDefaultFlavorTable() {
return (FlavorTable) SystemFlavorMap.getDefaultFlavorMap();
}
}