2
|
1 |
/*
|
117
|
2 |
* Copyright 2003-2008 Sun Microsystems, Inc. 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
|
|
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 |
import java.util.SortedMap;
|
|
30 |
import java.io.IOException;
|
|
31 |
import java.security.AccessController;
|
117
|
32 |
import java.util.HashMap;
|
|
33 |
import java.util.Map;
|
|
34 |
import sun.awt.UNIXToolkit;
|
2
|
35 |
import sun.awt.datatransfer.DataTransferer;
|
|
36 |
import sun.awt.datatransfer.SunClipboard;
|
|
37 |
import sun.awt.datatransfer.ClipboardTransferable;
|
|
38 |
import sun.security.action.GetIntegerAction;
|
|
39 |
|
|
40 |
/**
|
|
41 |
* A class which interfaces with the X11 selection service in order to support
|
|
42 |
* data transfer via Clipboard operations.
|
|
43 |
*/
|
117
|
44 |
public final class XClipboard extends SunClipboard implements OwnershipListener
|
|
45 |
{
|
2
|
46 |
private final XSelection selection;
|
117
|
47 |
// Time of calling XConvertSelection().
|
|
48 |
private long convertSelectionTime;
|
|
49 |
// The flag used not to call XConvertSelection() if the previous SelectionNotify
|
|
50 |
// has not been processed by checkChange().
|
|
51 |
private volatile boolean isSelectionNotifyProcessed;
|
|
52 |
// The property in which the owner should place requested targets
|
|
53 |
// when tracking changes of available data flavors (practically targets).
|
|
54 |
private volatile XAtom targetsPropertyAtom;
|
2
|
55 |
|
|
56 |
private static final Object classLock = new Object();
|
|
57 |
|
|
58 |
private static final int defaultPollInterval = 200;
|
|
59 |
|
|
60 |
private static int pollInterval;
|
|
61 |
|
117
|
62 |
private static Map<Long, XClipboard> targetsAtom2Clipboard;
|
2
|
63 |
|
|
64 |
/**
|
|
65 |
* Creates a system clipboard object.
|
|
66 |
*/
|
|
67 |
public XClipboard(String name, String selectionName) {
|
|
68 |
super(name);
|
117
|
69 |
selection = new XSelection(XAtom.get(selectionName));
|
|
70 |
selection.registerOwershipListener(this);
|
2
|
71 |
}
|
|
72 |
|
117
|
73 |
/*
|
2
|
74 |
* NOTE: This method may be called by privileged threads.
|
|
75 |
* DO NOT INVOKE CLIENT CODE ON THIS THREAD!
|
|
76 |
*/
|
117
|
77 |
public void ownershipChanged(final boolean isOwner) {
|
|
78 |
if (isOwner) {
|
|
79 |
checkChangeHere(contents);
|
|
80 |
} else {
|
|
81 |
lostOwnershipImpl();
|
|
82 |
}
|
2
|
83 |
}
|
|
84 |
|
|
85 |
protected synchronized void setContentsNative(Transferable contents) {
|
|
86 |
SortedMap formatMap = DataTransferer.getInstance().getFormatsForTransferable
|
|
87 |
(contents, DataTransferer.adaptFlavorMap(flavorMap));
|
117
|
88 |
long[] formats = DataTransferer.keysToLongArray(formatMap);
|
2
|
89 |
|
|
90 |
if (!selection.setOwner(contents, formatMap, formats,
|
|
91 |
XToolkit.getCurrentServerTime())) {
|
|
92 |
this.owner = null;
|
|
93 |
this.contents = null;
|
|
94 |
}
|
|
95 |
}
|
|
96 |
|
|
97 |
public long getID() {
|
|
98 |
return selection.getSelectionAtom().getAtom();
|
|
99 |
}
|
|
100 |
|
117
|
101 |
@Override
|
2
|
102 |
public synchronized Transferable getContents(Object requestor) {
|
|
103 |
if (contents != null) {
|
|
104 |
return contents;
|
|
105 |
}
|
|
106 |
return new ClipboardTransferable(this);
|
|
107 |
}
|
|
108 |
|
|
109 |
/* Caller is synchronized on this. */
|
|
110 |
protected void clearNativeContext() {
|
|
111 |
selection.reset();
|
|
112 |
}
|
|
113 |
|
|
114 |
|
|
115 |
protected long[] getClipboardFormats() {
|
|
116 |
return selection.getTargets(XToolkit.getCurrentServerTime());
|
|
117 |
}
|
|
118 |
|
|
119 |
protected byte[] getClipboardData(long format) throws IOException {
|
|
120 |
return selection.getData(format, XToolkit.getCurrentServerTime());
|
|
121 |
}
|
|
122 |
|
117
|
123 |
private void checkChangeHere(Transferable contents) {
|
2
|
124 |
if (areFlavorListenersRegistered()) {
|
117
|
125 |
checkChange(DataTransferer.getInstance().
|
2
|
126 |
getFormatsForTransferableAsArray(contents, flavorMap));
|
|
127 |
}
|
|
128 |
}
|
|
129 |
|
117
|
130 |
private static int getPollInterval() {
|
|
131 |
synchronized (XClipboard.classLock) {
|
|
132 |
if (pollInterval <= 0) {
|
|
133 |
pollInterval = AccessController.doPrivileged(
|
|
134 |
new GetIntegerAction("awt.datatransfer.clipboard.poll.interval",
|
|
135 |
defaultPollInterval));
|
|
136 |
if (pollInterval <= 0) {
|
|
137 |
pollInterval = defaultPollInterval;
|
|
138 |
}
|
|
139 |
}
|
|
140 |
return pollInterval;
|
|
141 |
}
|
|
142 |
}
|
|
143 |
|
|
144 |
private XAtom getTargetsPropertyAtom() {
|
|
145 |
if (null == targetsPropertyAtom) {
|
|
146 |
targetsPropertyAtom =
|
|
147 |
XAtom.get("XAWT_TARGETS_OF_SELECTION:" + selection.getSelectionAtom().getName());
|
|
148 |
}
|
|
149 |
return targetsPropertyAtom;
|
|
150 |
}
|
|
151 |
|
2
|
152 |
protected void registerClipboardViewerChecked() {
|
117
|
153 |
// for XConvertSelection() to be called for the first time in getTargetsDelayed()
|
|
154 |
isSelectionNotifyProcessed = true;
|
|
155 |
|
|
156 |
boolean mustSchedule = false;
|
|
157 |
synchronized (XClipboard.classLock) {
|
|
158 |
if (targetsAtom2Clipboard == null) {
|
|
159 |
targetsAtom2Clipboard = new HashMap<Long, XClipboard>(2);
|
|
160 |
}
|
|
161 |
mustSchedule = targetsAtom2Clipboard.isEmpty();
|
|
162 |
targetsAtom2Clipboard.put(getTargetsPropertyAtom().getAtom(), this);
|
|
163 |
if (mustSchedule) {
|
|
164 |
XToolkit.addEventDispatcher(XWindow.getXAWTRootWindow().getWindow(),
|
|
165 |
new SelectionNotifyHandler());
|
2
|
166 |
}
|
|
167 |
}
|
|
168 |
if (mustSchedule) {
|
117
|
169 |
XToolkit.schedule(new CheckChangeTimerTask(), XClipboard.getPollInterval());
|
2
|
170 |
}
|
|
171 |
}
|
|
172 |
|
|
173 |
private static class CheckChangeTimerTask implements Runnable {
|
|
174 |
public void run() {
|
117
|
175 |
for (XClipboard clpbrd : targetsAtom2Clipboard.values()) {
|
|
176 |
clpbrd.getTargetsDelayed();
|
2
|
177 |
}
|
|
178 |
synchronized (XClipboard.classLock) {
|
117
|
179 |
if (targetsAtom2Clipboard != null && !targetsAtom2Clipboard.isEmpty()) {
|
|
180 |
XToolkit.schedule(this, XClipboard.getPollInterval());
|
|
181 |
}
|
|
182 |
}
|
|
183 |
}
|
|
184 |
}
|
|
185 |
|
|
186 |
private static class SelectionNotifyHandler implements XEventDispatcher {
|
|
187 |
public void dispatchEvent(XEvent ev) {
|
|
188 |
if (ev.get_type() == XlibWrapper.SelectionNotify) {
|
|
189 |
final XSelectionEvent xse = ev.get_xselection();
|
|
190 |
XClipboard clipboard = null;
|
|
191 |
synchronized (XClipboard.classLock) {
|
|
192 |
if (targetsAtom2Clipboard != null && !targetsAtom2Clipboard.isEmpty()) {
|
|
193 |
XToolkit.removeEventDispatcher(XWindow.getXAWTRootWindow().getWindow(), this);
|
|
194 |
return;
|
|
195 |
}
|
|
196 |
final long propertyAtom = xse.get_property();
|
|
197 |
clipboard = targetsAtom2Clipboard.get(propertyAtom);
|
|
198 |
}
|
|
199 |
if (null != clipboard) {
|
|
200 |
clipboard.checkChange(xse);
|
2
|
201 |
}
|
|
202 |
}
|
|
203 |
}
|
|
204 |
}
|
|
205 |
|
|
206 |
protected void unregisterClipboardViewerChecked() {
|
117
|
207 |
isSelectionNotifyProcessed = false;
|
2
|
208 |
synchronized (XClipboard.classLock) {
|
117
|
209 |
targetsAtom2Clipboard.remove(getTargetsPropertyAtom().getAtom());
|
|
210 |
}
|
|
211 |
}
|
|
212 |
|
|
213 |
// checkChange() will be called on SelectionNotify
|
|
214 |
private void getTargetsDelayed() {
|
|
215 |
XToolkit.awtLock();
|
|
216 |
try {
|
|
217 |
long curTime = System.currentTimeMillis();
|
|
218 |
if (isSelectionNotifyProcessed || curTime >= (convertSelectionTime + UNIXToolkit.getDatatransferTimeout()))
|
|
219 |
{
|
|
220 |
convertSelectionTime = curTime;
|
|
221 |
XlibWrapper.XConvertSelection(XToolkit.getDisplay(),
|
|
222 |
selection.getSelectionAtom().getAtom(),
|
|
223 |
XDataTransferer.TARGETS_ATOM.getAtom(),
|
|
224 |
getTargetsPropertyAtom().getAtom(),
|
|
225 |
XWindow.getXAWTRootWindow().getWindow(),
|
|
226 |
XlibWrapper.CurrentTime);
|
|
227 |
isSelectionNotifyProcessed = false;
|
|
228 |
}
|
|
229 |
} finally {
|
|
230 |
XToolkit.awtUnlock();
|
2
|
231 |
}
|
|
232 |
}
|
|
233 |
|
117
|
234 |
/*
|
|
235 |
* Tracks changes of available formats.
|
|
236 |
* NOTE: This method may be called by privileged threads.
|
|
237 |
* DO NOT INVOKE CLIENT CODE ON THIS THREAD!
|
|
238 |
*/
|
|
239 |
private void checkChange(XSelectionEvent xse) {
|
|
240 |
final long propertyAtom = xse.get_property();
|
|
241 |
if (propertyAtom != getTargetsPropertyAtom().getAtom()) {
|
|
242 |
// wrong atom
|
|
243 |
return;
|
|
244 |
}
|
|
245 |
|
|
246 |
final XAtom selectionAtom = XAtom.get(xse.get_selection());
|
|
247 |
final XSelection changedSelection = XSelection.getSelection(selectionAtom);
|
|
248 |
|
|
249 |
if (null == changedSelection || changedSelection != selection) {
|
|
250 |
// unknown selection - do nothing
|
|
251 |
return;
|
|
252 |
}
|
|
253 |
|
|
254 |
isSelectionNotifyProcessed = true;
|
|
255 |
|
|
256 |
if (selection.isOwner()) {
|
|
257 |
// selection is owner - do not need formats
|
|
258 |
return;
|
|
259 |
}
|
|
260 |
|
|
261 |
long[] formats = null;
|
|
262 |
|
|
263 |
if (propertyAtom == XlibWrapper.None) {
|
|
264 |
// We treat None property atom as "empty selection".
|
|
265 |
formats = new long[0];
|
|
266 |
} else {
|
|
267 |
WindowPropertyGetter targetsGetter =
|
|
268 |
new WindowPropertyGetter(XWindow.getXAWTRootWindow().getWindow(),
|
|
269 |
XAtom.get(propertyAtom), 0,
|
|
270 |
XSelection.MAX_LENGTH, true,
|
|
271 |
XlibWrapper.AnyPropertyType);
|
|
272 |
try {
|
|
273 |
targetsGetter.execute();
|
|
274 |
formats = XSelection.getFormats(targetsGetter);
|
|
275 |
} finally {
|
|
276 |
targetsGetter.dispose();
|
|
277 |
}
|
|
278 |
}
|
|
279 |
|
|
280 |
checkChange(formats);
|
|
281 |
}
|
2
|
282 |
}
|