2
|
1 |
/*
|
|
2 |
* Copyright 2003-2007 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 |
|
|
26 |
package sun.awt.X11;
|
|
27 |
|
|
28 |
import java.awt.datatransfer.Transferable;
|
|
29 |
|
|
30 |
import java.util.SortedMap;
|
|
31 |
import java.util.Set;
|
|
32 |
import java.util.Iterator;
|
|
33 |
import java.util.HashSet;
|
|
34 |
|
|
35 |
import java.io.IOException;
|
|
36 |
|
|
37 |
import java.security.AccessController;
|
|
38 |
|
|
39 |
import sun.awt.datatransfer.DataTransferer;
|
|
40 |
import sun.awt.datatransfer.SunClipboard;
|
|
41 |
import sun.awt.datatransfer.ClipboardTransferable;
|
|
42 |
|
|
43 |
import sun.security.action.GetIntegerAction;
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
/**
|
|
48 |
* A class which interfaces with the X11 selection service in order to support
|
|
49 |
* data transfer via Clipboard operations.
|
|
50 |
*/
|
|
51 |
public class XClipboard extends SunClipboard implements Runnable {
|
|
52 |
private final XSelection selection;
|
|
53 |
|
|
54 |
private static final Object classLock = new Object();
|
|
55 |
|
|
56 |
private static final int defaultPollInterval = 200;
|
|
57 |
|
|
58 |
private static int pollInterval;
|
|
59 |
|
|
60 |
private static Set listenedClipboards;
|
|
61 |
|
|
62 |
|
|
63 |
/**
|
|
64 |
* Creates a system clipboard object.
|
|
65 |
*/
|
|
66 |
public XClipboard(String name, String selectionName) {
|
|
67 |
super(name);
|
|
68 |
selection = new XSelection(XAtom.get(selectionName), this);
|
|
69 |
}
|
|
70 |
|
|
71 |
/**
|
|
72 |
* The action to be run when we lose ownership
|
|
73 |
* NOTE: This method may be called by privileged threads.
|
|
74 |
* DO NOT INVOKE CLIENT CODE ON THIS THREAD!
|
|
75 |
*/
|
|
76 |
public void run() {
|
|
77 |
lostOwnershipImpl();
|
|
78 |
}
|
|
79 |
|
|
80 |
protected synchronized void setContentsNative(Transferable contents) {
|
|
81 |
SortedMap formatMap = DataTransferer.getInstance().getFormatsForTransferable
|
|
82 |
(contents, DataTransferer.adaptFlavorMap(flavorMap));
|
|
83 |
long[] formats =
|
|
84 |
DataTransferer.getInstance().keysToLongArray(formatMap);
|
|
85 |
|
|
86 |
if (!selection.setOwner(contents, formatMap, formats,
|
|
87 |
XToolkit.getCurrentServerTime())) {
|
|
88 |
this.owner = null;
|
|
89 |
this.contents = null;
|
|
90 |
}
|
|
91 |
}
|
|
92 |
|
|
93 |
public long getID() {
|
|
94 |
return selection.getSelectionAtom().getAtom();
|
|
95 |
}
|
|
96 |
|
|
97 |
public synchronized Transferable getContents(Object requestor) {
|
|
98 |
if (contents != null) {
|
|
99 |
return contents;
|
|
100 |
}
|
|
101 |
return new ClipboardTransferable(this);
|
|
102 |
}
|
|
103 |
|
|
104 |
/* Caller is synchronized on this. */
|
|
105 |
protected void clearNativeContext() {
|
|
106 |
selection.reset();
|
|
107 |
}
|
|
108 |
|
|
109 |
|
|
110 |
protected long[] getClipboardFormats() {
|
|
111 |
return selection.getTargets(XToolkit.getCurrentServerTime());
|
|
112 |
}
|
|
113 |
|
|
114 |
protected byte[] getClipboardData(long format) throws IOException {
|
|
115 |
return selection.getData(format, XToolkit.getCurrentServerTime());
|
|
116 |
}
|
|
117 |
|
|
118 |
// Called on the toolkit thread under awtLock.
|
|
119 |
public void checkChange(long[] formats) {
|
|
120 |
if (!selection.isOwner()) {
|
|
121 |
super.checkChange(formats);
|
|
122 |
}
|
|
123 |
}
|
|
124 |
|
|
125 |
void checkChangeHere(Transferable contents) {
|
|
126 |
if (areFlavorListenersRegistered()) {
|
|
127 |
super.checkChange(DataTransferer.getInstance().
|
|
128 |
getFormatsForTransferableAsArray(contents, flavorMap));
|
|
129 |
}
|
|
130 |
}
|
|
131 |
|
|
132 |
protected void registerClipboardViewerChecked() {
|
|
133 |
if (pollInterval <= 0) {
|
|
134 |
pollInterval = ((Integer)AccessController.doPrivileged(
|
|
135 |
new GetIntegerAction("awt.datatransfer.clipboard.poll.interval",
|
|
136 |
defaultPollInterval))).intValue();
|
|
137 |
if (pollInterval <= 0) {
|
|
138 |
pollInterval = defaultPollInterval;
|
|
139 |
}
|
|
140 |
}
|
|
141 |
selection.initializeSelectionForTrackingChanges();
|
|
142 |
boolean mustSchedule = false;
|
|
143 |
synchronized (XClipboard.classLock) {
|
|
144 |
if (listenedClipboards == null) {
|
|
145 |
listenedClipboards = new HashSet(2);
|
|
146 |
}
|
|
147 |
mustSchedule = listenedClipboards.isEmpty();
|
|
148 |
listenedClipboards.add(this);
|
|
149 |
}
|
|
150 |
if (mustSchedule) {
|
|
151 |
XToolkit.schedule(new CheckChangeTimerTask(), pollInterval);
|
|
152 |
}
|
|
153 |
}
|
|
154 |
|
|
155 |
private static class CheckChangeTimerTask implements Runnable {
|
|
156 |
public void run() {
|
|
157 |
for (Iterator iter = listenedClipboards.iterator(); iter.hasNext();) {
|
|
158 |
XClipboard clpbrd = (XClipboard)iter.next();
|
|
159 |
clpbrd.selection.getTargetsDelayed();
|
|
160 |
}
|
|
161 |
synchronized (XClipboard.classLock) {
|
|
162 |
if (listenedClipboards != null && !listenedClipboards.isEmpty()) {
|
|
163 |
XToolkit.schedule(this, pollInterval);
|
|
164 |
}
|
|
165 |
}
|
|
166 |
}
|
|
167 |
}
|
|
168 |
|
|
169 |
protected void unregisterClipboardViewerChecked() {
|
|
170 |
selection.deinitializeSelectionForTrackingChanges();
|
|
171 |
synchronized (XClipboard.classLock) {
|
|
172 |
listenedClipboards.remove(this);
|
|
173 |
}
|
|
174 |
}
|
|
175 |
|
|
176 |
}
|