2
|
1 |
/*
|
|
2 |
* Copyright 2003-2006 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.util.ArrayList;
|
|
29 |
import java.util.Collections;
|
|
30 |
import java.util.Enumeration;
|
|
31 |
import java.util.HashMap;
|
|
32 |
import java.util.HashSet;
|
|
33 |
import java.util.Hashtable;
|
|
34 |
import java.util.Iterator;
|
|
35 |
import java.util.List;
|
|
36 |
import java.util.logging.*;
|
|
37 |
|
|
38 |
import java.awt.Point;
|
|
39 |
|
|
40 |
import sun.awt.dnd.SunDropTargetContextPeer;
|
|
41 |
import sun.awt.dnd.SunDropTargetEvent;
|
|
42 |
|
|
43 |
/**
|
|
44 |
* The class responsible for registration/deregistration of drop sites.
|
|
45 |
*
|
|
46 |
* @since 1.5
|
|
47 |
*/
|
|
48 |
final class XDropTargetRegistry {
|
|
49 |
private static final Logger logger =
|
|
50 |
Logger.getLogger("sun.awt.X11.xembed.xdnd.XDropTargetRegistry");
|
|
51 |
|
|
52 |
private static final long DELAYED_REGISTRATION_PERIOD = 200;
|
|
53 |
|
|
54 |
private static final XDropTargetRegistry theInstance =
|
|
55 |
new XDropTargetRegistry();
|
|
56 |
|
|
57 |
private final HashMap<Long, Runnable> delayedRegistrationMap =
|
|
58 |
new HashMap<Long, Runnable>();
|
|
59 |
|
|
60 |
private XDropTargetRegistry() {}
|
|
61 |
|
|
62 |
static XDropTargetRegistry getRegistry() {
|
|
63 |
return theInstance;
|
|
64 |
}
|
|
65 |
|
|
66 |
/**
|
|
67 |
* Returns the XID of the topmost window with WM_STATE set in the ancestor
|
|
68 |
* heirarchy of the specified window or 0 if none found.
|
|
69 |
*/
|
|
70 |
private long getToplevelWindow(long window) {
|
|
71 |
XBaseWindow candWindow = XToolkit.windowToXWindow(window);
|
|
72 |
if (candWindow != null) {
|
|
73 |
XWindowPeer toplevel = candWindow.getToplevelXWindow();
|
|
74 |
if (toplevel != null && !(toplevel instanceof XEmbeddedFramePeer)) {
|
|
75 |
return toplevel.getWindow();
|
|
76 |
}
|
|
77 |
}
|
|
78 |
|
|
79 |
/* Traverse the ancestor tree from window up to the root and find
|
|
80 |
the top-level client window nearest to the root. */
|
|
81 |
do {
|
|
82 |
if (XlibUtil.isTrueToplevelWindow(window)) {
|
|
83 |
return window;
|
|
84 |
}
|
|
85 |
|
|
86 |
window = XlibUtil.getParentWindow(window);
|
|
87 |
|
|
88 |
} while (window != 0);
|
|
89 |
|
|
90 |
return window;
|
|
91 |
}
|
|
92 |
|
|
93 |
static final long getDnDProxyWindow() {
|
|
94 |
return XWindow.getXAWTRootWindow().getWindow();
|
|
95 |
}
|
|
96 |
|
|
97 |
private static final class EmbeddedDropSiteEntry {
|
|
98 |
private final long root;
|
|
99 |
private final long event_mask;
|
|
100 |
private List<XDropTargetProtocol> supportedProtocols;
|
|
101 |
private final HashSet<Long> nonXEmbedClientSites = new HashSet<Long>();
|
|
102 |
private final List<Long> sites = new ArrayList<Long>();
|
|
103 |
|
|
104 |
public EmbeddedDropSiteEntry(long root, long event_mask,
|
|
105 |
List<XDropTargetProtocol> supportedProtocols) {
|
|
106 |
if (supportedProtocols == null) {
|
|
107 |
throw new NullPointerException("Null supportedProtocols");
|
|
108 |
}
|
|
109 |
this.root = root;
|
|
110 |
this.event_mask = event_mask;
|
|
111 |
this.supportedProtocols = supportedProtocols;
|
|
112 |
}
|
|
113 |
|
|
114 |
public long getRoot() {
|
|
115 |
return root;
|
|
116 |
}
|
|
117 |
public long getEventMask() {
|
|
118 |
return event_mask;
|
|
119 |
}
|
|
120 |
public boolean hasNonXEmbedClientSites() {
|
|
121 |
return !nonXEmbedClientSites.isEmpty();
|
|
122 |
}
|
|
123 |
public synchronized void addSite(long window, boolean isXEmbedClient) {
|
|
124 |
Long lWindow = Long.valueOf(window);
|
|
125 |
if (!sites.contains(lWindow)) {
|
|
126 |
sites.add(lWindow);
|
|
127 |
}
|
|
128 |
if (!isXEmbedClient) {
|
|
129 |
nonXEmbedClientSites.add(lWindow);
|
|
130 |
}
|
|
131 |
}
|
|
132 |
public synchronized void removeSite(long window) {
|
|
133 |
Long lWindow = Long.valueOf(window);
|
|
134 |
sites.remove(lWindow);
|
|
135 |
nonXEmbedClientSites.remove(lWindow);
|
|
136 |
}
|
|
137 |
public void setSupportedProtocols(List<XDropTargetProtocol> list) {
|
|
138 |
supportedProtocols = list;
|
|
139 |
}
|
|
140 |
public List<XDropTargetProtocol> getSupportedProtocols() {
|
|
141 |
return supportedProtocols;
|
|
142 |
}
|
|
143 |
public boolean hasSites() {
|
|
144 |
return !sites.isEmpty();
|
|
145 |
}
|
|
146 |
public long[] getSites() {
|
|
147 |
long[] ret = new long[sites.size()];
|
|
148 |
Iterator iter = sites.iterator();
|
|
149 |
int index = 0;
|
|
150 |
while (iter.hasNext()) {
|
|
151 |
Long l = (Long)iter.next();
|
|
152 |
ret[index++] = l.longValue();
|
|
153 |
}
|
|
154 |
return ret;
|
|
155 |
}
|
|
156 |
public long getSite(int x, int y) {
|
|
157 |
assert XToolkit.isAWTLockHeldByCurrentThread();
|
|
158 |
|
|
159 |
Iterator<Long> iter = sites.iterator();
|
|
160 |
while (iter.hasNext()) {
|
|
161 |
Long l = iter.next();
|
|
162 |
long window = l.longValue();
|
|
163 |
|
|
164 |
Point p = XBaseWindow.toOtherWindow(getRoot(), window, x, y);
|
|
165 |
|
|
166 |
if (p == null) {
|
|
167 |
continue;
|
|
168 |
}
|
|
169 |
|
|
170 |
int dest_x = p.x;
|
|
171 |
int dest_y = p.y;
|
|
172 |
if (dest_x >= 0 && dest_y >= 0) {
|
|
173 |
XWindowAttributes wattr = new XWindowAttributes();
|
|
174 |
try {
|
|
175 |
XToolkit.WITH_XERROR_HANDLER(XToolkit.IgnoreBadWindowHandler);
|
|
176 |
int status = XlibWrapper.XGetWindowAttributes(XToolkit.getDisplay(),
|
|
177 |
window, wattr.pData);
|
|
178 |
XToolkit.RESTORE_XERROR_HANDLER();
|
|
179 |
|
|
180 |
if (status == 0 ||
|
|
181 |
(XToolkit.saved_error != null &&
|
|
182 |
XToolkit.saved_error.get_error_code() != XlibWrapper.Success)) {
|
|
183 |
continue;
|
|
184 |
}
|
|
185 |
|
|
186 |
if (wattr.get_map_state() != XlibWrapper.IsUnmapped
|
|
187 |
&& dest_x < wattr.get_width()
|
|
188 |
&& dest_y < wattr.get_height()) {
|
|
189 |
return window;
|
|
190 |
}
|
|
191 |
} finally {
|
|
192 |
wattr.dispose();
|
|
193 |
}
|
|
194 |
}
|
|
195 |
}
|
|
196 |
return 0;
|
|
197 |
}
|
|
198 |
}
|
|
199 |
|
|
200 |
private final HashMap<Long, EmbeddedDropSiteEntry> embeddedDropSiteRegistry =
|
|
201 |
new HashMap<Long, EmbeddedDropSiteEntry>();
|
|
202 |
|
|
203 |
private EmbeddedDropSiteEntry registerEmbedderDropSite(long embedder) {
|
|
204 |
assert XToolkit.isAWTLockHeldByCurrentThread();
|
|
205 |
|
|
206 |
Iterator dropTargetProtocols =
|
|
207 |
XDragAndDropProtocols.getDropTargetProtocols();
|
|
208 |
// The list of protocols supported by the embedder.
|
|
209 |
List<XDropTargetProtocol> embedderProtocols = new ArrayList();
|
|
210 |
|
|
211 |
while (dropTargetProtocols.hasNext()) {
|
|
212 |
XDropTargetProtocol dropTargetProtocol =
|
|
213 |
(XDropTargetProtocol)dropTargetProtocols.next();
|
|
214 |
if (dropTargetProtocol.isProtocolSupported(embedder)) {
|
|
215 |
embedderProtocols.add(dropTargetProtocol);
|
|
216 |
}
|
|
217 |
}
|
|
218 |
|
|
219 |
embedderProtocols = Collections.unmodifiableList(embedderProtocols);
|
|
220 |
|
|
221 |
/* Grab server, since we are working with the window that belongs to
|
|
222 |
another client. */
|
|
223 |
XlibWrapper.XGrabServer(XToolkit.getDisplay());
|
|
224 |
try {
|
|
225 |
long root = 0;
|
|
226 |
long event_mask = 0;
|
|
227 |
XWindowAttributes wattr = new XWindowAttributes();
|
|
228 |
try {
|
|
229 |
XToolkit.WITH_XERROR_HANDLER(XToolkit.IgnoreBadWindowHandler);
|
|
230 |
int status = XlibWrapper.XGetWindowAttributes(XToolkit.getDisplay(),
|
|
231 |
embedder, wattr.pData);
|
|
232 |
XToolkit.RESTORE_XERROR_HANDLER();
|
|
233 |
|
|
234 |
if (status == 0 ||
|
|
235 |
(XToolkit.saved_error != null &&
|
|
236 |
XToolkit.saved_error.get_error_code() != XlibWrapper.Success)) {
|
|
237 |
throw new XException("XGetWindowAttributes failed");
|
|
238 |
}
|
|
239 |
|
|
240 |
event_mask = wattr.get_your_event_mask();
|
|
241 |
root = wattr.get_root();
|
|
242 |
} finally {
|
|
243 |
wattr.dispose();
|
|
244 |
}
|
|
245 |
|
|
246 |
if ((event_mask & XlibWrapper.PropertyChangeMask) == 0) {
|
|
247 |
XToolkit.WITH_XERROR_HANDLER(XToolkit.IgnoreBadWindowHandler);
|
|
248 |
XlibWrapper.XSelectInput(XToolkit.getDisplay(), embedder,
|
|
249 |
event_mask | XlibWrapper.PropertyChangeMask);
|
|
250 |
XToolkit.RESTORE_XERROR_HANDLER();
|
|
251 |
|
|
252 |
if (XToolkit.saved_error != null &&
|
|
253 |
XToolkit.saved_error.get_error_code() != XlibWrapper.Success) {
|
|
254 |
throw new XException("XSelectInput failed");
|
|
255 |
}
|
|
256 |
}
|
|
257 |
|
|
258 |
return new EmbeddedDropSiteEntry(root, event_mask, embedderProtocols);
|
|
259 |
} finally {
|
|
260 |
XlibWrapper.XUngrabServer(XToolkit.getDisplay());
|
|
261 |
}
|
|
262 |
}
|
|
263 |
|
|
264 |
private static final boolean XEMBED_PROTOCOLS = true;
|
|
265 |
private static final boolean NON_XEMBED_PROTOCOLS = false;
|
|
266 |
|
|
267 |
private void registerProtocols(long embedder, boolean protocols,
|
|
268 |
List<XDropTargetProtocol> supportedProtocols) {
|
|
269 |
Iterator dropTargetProtocols = null;
|
|
270 |
|
|
271 |
/*
|
|
272 |
* By default, we register a drop site that supports all dnd
|
|
273 |
* protocols. This approach is not appropriate in plugin
|
|
274 |
* scenario if the browser supports Motif DnD and doesn't support
|
|
275 |
* XDnD. If we forcibly set XdndAware on the browser toplevel, any drag
|
|
276 |
* source that supports both protocols and prefers XDnD will be unable
|
|
277 |
* to drop anything on the browser.
|
|
278 |
* The solution for this problem is not to register XDnD drop site
|
|
279 |
* if the browser supports only Motif DnD.
|
|
280 |
* In general, if the browser already supports some protocols, we
|
|
281 |
* register the embedded drop site only for those protocols. Otherwise
|
|
282 |
* we register the embedded drop site for all protocols.
|
|
283 |
*/
|
|
284 |
if (!supportedProtocols.isEmpty()) {
|
|
285 |
dropTargetProtocols = supportedProtocols.iterator();
|
|
286 |
} else {
|
|
287 |
dropTargetProtocols =
|
|
288 |
XDragAndDropProtocols.getDropTargetProtocols();
|
|
289 |
}
|
|
290 |
|
|
291 |
/* Grab server, since we are working with the window that belongs to
|
|
292 |
another client. */
|
|
293 |
XlibWrapper.XGrabServer(XToolkit.getDisplay());
|
|
294 |
try {
|
|
295 |
while (dropTargetProtocols.hasNext()) {
|
|
296 |
XDropTargetProtocol dropTargetProtocol =
|
|
297 |
(XDropTargetProtocol)dropTargetProtocols.next();
|
|
298 |
if ((protocols == XEMBED_PROTOCOLS) ==
|
|
299 |
dropTargetProtocol.isXEmbedSupported()) {
|
|
300 |
dropTargetProtocol.registerEmbedderDropSite(embedder);
|
|
301 |
}
|
|
302 |
}
|
|
303 |
} finally {
|
|
304 |
XlibWrapper.XUngrabServer(XToolkit.getDisplay());
|
|
305 |
}
|
|
306 |
}
|
|
307 |
|
|
308 |
public void updateEmbedderDropSite(long embedder) {
|
|
309 |
XBaseWindow xbaseWindow = XToolkit.windowToXWindow(embedder);
|
|
310 |
// No need to update our own drop sites.
|
|
311 |
if (xbaseWindow != null) {
|
|
312 |
return;
|
|
313 |
}
|
|
314 |
|
|
315 |
assert XToolkit.isAWTLockHeldByCurrentThread();
|
|
316 |
|
|
317 |
Iterator dropTargetProtocols =
|
|
318 |
XDragAndDropProtocols.getDropTargetProtocols();
|
|
319 |
// The list of protocols supported by the embedder.
|
|
320 |
List<XDropTargetProtocol> embedderProtocols = new ArrayList();
|
|
321 |
|
|
322 |
while (dropTargetProtocols.hasNext()) {
|
|
323 |
XDropTargetProtocol dropTargetProtocol =
|
|
324 |
(XDropTargetProtocol)dropTargetProtocols.next();
|
|
325 |
if (dropTargetProtocol.isProtocolSupported(embedder)) {
|
|
326 |
embedderProtocols.add(dropTargetProtocol);
|
|
327 |
}
|
|
328 |
}
|
|
329 |
|
|
330 |
embedderProtocols = Collections.unmodifiableList(embedderProtocols);
|
|
331 |
|
|
332 |
Long lToplevel = new Long(embedder);
|
|
333 |
boolean isXEmbedServer = false;
|
|
334 |
synchronized (this) {
|
|
335 |
EmbeddedDropSiteEntry entry =
|
|
336 |
(EmbeddedDropSiteEntry)embeddedDropSiteRegistry.get(lToplevel);
|
|
337 |
if (entry == null) {
|
|
338 |
return;
|
|
339 |
}
|
|
340 |
entry.setSupportedProtocols(embedderProtocols);
|
|
341 |
isXEmbedServer = !entry.hasNonXEmbedClientSites();
|
|
342 |
}
|
|
343 |
|
|
344 |
/*
|
|
345 |
* By default, we register a drop site that supports all dnd
|
|
346 |
* protocols. This approach is not appropriate in plugin
|
|
347 |
* scenario if the browser supports Motif DnD and doesn't support
|
|
348 |
* XDnD. If we forcibly set XdndAware on the browser toplevel, any drag
|
|
349 |
* source that supports both protocols and prefers XDnD will be unable
|
|
350 |
* to drop anything on the browser.
|
|
351 |
* The solution for this problem is not to register XDnD drop site
|
|
352 |
* if the browser supports only Motif DnD.
|
|
353 |
* In general, if the browser already supports some protocols, we
|
|
354 |
* register the embedded drop site only for those protocols. Otherwise
|
|
355 |
* we register the embedded drop site for all protocols.
|
|
356 |
*/
|
|
357 |
if (!embedderProtocols.isEmpty()) {
|
|
358 |
dropTargetProtocols = embedderProtocols.iterator();
|
|
359 |
} else {
|
|
360 |
dropTargetProtocols =
|
|
361 |
XDragAndDropProtocols.getDropTargetProtocols();
|
|
362 |
}
|
|
363 |
|
|
364 |
/* Grab server, since we are working with the window that belongs to
|
|
365 |
another client. */
|
|
366 |
XlibWrapper.XGrabServer(XToolkit.getDisplay());
|
|
367 |
try {
|
|
368 |
while (dropTargetProtocols.hasNext()) {
|
|
369 |
XDropTargetProtocol dropTargetProtocol =
|
|
370 |
(XDropTargetProtocol)dropTargetProtocols.next();
|
|
371 |
if (!isXEmbedServer || !dropTargetProtocol.isXEmbedSupported()) {
|
|
372 |
dropTargetProtocol.registerEmbedderDropSite(embedder);
|
|
373 |
}
|
|
374 |
}
|
|
375 |
} finally {
|
|
376 |
XlibWrapper.XUngrabServer(XToolkit.getDisplay());
|
|
377 |
}
|
|
378 |
}
|
|
379 |
|
|
380 |
private void unregisterEmbedderDropSite(long embedder,
|
|
381 |
EmbeddedDropSiteEntry entry) {
|
|
382 |
assert XToolkit.isAWTLockHeldByCurrentThread();
|
|
383 |
|
|
384 |
Iterator dropTargetProtocols =
|
|
385 |
XDragAndDropProtocols.getDropTargetProtocols();
|
|
386 |
|
|
387 |
/* Grab server, since we are working with the window that belongs to
|
|
388 |
another client. */
|
|
389 |
XlibWrapper.XGrabServer(XToolkit.getDisplay());
|
|
390 |
try {
|
|
391 |
while (dropTargetProtocols.hasNext()) {
|
|
392 |
XDropTargetProtocol dropTargetProtocol =
|
|
393 |
(XDropTargetProtocol)dropTargetProtocols.next();
|
|
394 |
dropTargetProtocol.unregisterEmbedderDropSite(embedder);
|
|
395 |
}
|
|
396 |
|
|
397 |
long event_mask = entry.getEventMask();
|
|
398 |
|
|
399 |
/* Restore the original event mask for the embedder. */
|
|
400 |
if ((event_mask & XlibWrapper.PropertyChangeMask) == 0) {
|
|
401 |
XToolkit.WITH_XERROR_HANDLER(XToolkit.IgnoreBadWindowHandler);
|
|
402 |
XlibWrapper.XSelectInput(XToolkit.getDisplay(), embedder,
|
|
403 |
event_mask);
|
|
404 |
XToolkit.RESTORE_XERROR_HANDLER();
|
|
405 |
|
|
406 |
if (XToolkit.saved_error != null &&
|
|
407 |
XToolkit.saved_error.get_error_code() != XlibWrapper.Success) {
|
|
408 |
throw new XException("XSelectInput failed");
|
|
409 |
}
|
|
410 |
}
|
|
411 |
} finally {
|
|
412 |
XlibWrapper.XUngrabServer(XToolkit.getDisplay());
|
|
413 |
}
|
|
414 |
}
|
|
415 |
|
|
416 |
private void registerEmbeddedDropSite(long toplevel, long window) {
|
|
417 |
XBaseWindow xBaseWindow = XToolkit.windowToXWindow(window);
|
|
418 |
boolean isXEmbedClient =
|
|
419 |
(xBaseWindow instanceof XEmbeddedFramePeer) &&
|
|
420 |
((XEmbeddedFramePeer)xBaseWindow).isXEmbedActive();
|
|
421 |
|
|
422 |
XEmbedCanvasPeer peer = null;
|
|
423 |
{
|
|
424 |
XBaseWindow xbaseWindow = XToolkit.windowToXWindow(toplevel);
|
|
425 |
if (xbaseWindow != null) {
|
|
426 |
if (xbaseWindow instanceof XEmbedCanvasPeer) {
|
|
427 |
peer = (XEmbedCanvasPeer)xbaseWindow;
|
|
428 |
} else {
|
|
429 |
throw new UnsupportedOperationException();
|
|
430 |
}
|
|
431 |
}
|
|
432 |
}
|
|
433 |
|
|
434 |
Long lToplevel = Long.valueOf(toplevel);
|
|
435 |
EmbeddedDropSiteEntry entry = null;
|
|
436 |
synchronized (this) {
|
|
437 |
entry =
|
|
438 |
(EmbeddedDropSiteEntry)embeddedDropSiteRegistry.get(lToplevel);
|
|
439 |
if (entry == null) {
|
|
440 |
if (peer != null) {
|
|
441 |
// Toplevel is an XEmbed server within this VM.
|
|
442 |
// Register an XEmbed drop site.
|
|
443 |
peer.setXEmbedDropTarget();
|
|
444 |
// Create a dummy entry to register the embedded site.
|
|
445 |
entry = new EmbeddedDropSiteEntry(0, 0,
|
|
446 |
Collections.<XDropTargetProtocol>emptyList());
|
|
447 |
} else {
|
|
448 |
// Foreign toplevel.
|
|
449 |
// Select for PropertyNotify events on the toplevel, so that
|
|
450 |
// we can track changes of the properties relevant to DnD
|
|
451 |
// protocols.
|
|
452 |
entry = registerEmbedderDropSite(toplevel);
|
|
453 |
// Register the toplevel with all DnD protocols that are not
|
|
454 |
// supported by XEmbed - actually setup a proxy, so that
|
|
455 |
// all DnD notifications sent to the toplevel are first
|
|
456 |
// routed to us.
|
|
457 |
registerProtocols(toplevel, NON_XEMBED_PROTOCOLS,
|
|
458 |
entry.getSupportedProtocols());
|
|
459 |
}
|
|
460 |
embeddedDropSiteRegistry.put(lToplevel, entry);
|
|
461 |
}
|
|
462 |
}
|
|
463 |
|
|
464 |
assert entry != null;
|
|
465 |
|
|
466 |
synchronized (entry) {
|
|
467 |
// For a foreign toplevel.
|
|
468 |
if (peer == null) {
|
|
469 |
if (!isXEmbedClient) {
|
|
470 |
// Since this is not an XEmbed client we can no longer rely
|
|
471 |
// on XEmbed to route DnD notifications even for DnD
|
|
472 |
// protocols that are supported by XEmbed.
|
|
473 |
// We rollback to the XEmbed-unfriendly solution - setup
|
|
474 |
// a proxy, so that all DnD notifications sent to the
|
|
475 |
// toplevel are first routed to us.
|
|
476 |
registerProtocols(toplevel, XEMBED_PROTOCOLS,
|
|
477 |
entry.getSupportedProtocols());
|
|
478 |
} else {
|
|
479 |
Iterator dropTargetProtocols =
|
|
480 |
XDragAndDropProtocols.getDropTargetProtocols();
|
|
481 |
|
|
482 |
// Register the embedded window as a plain drop site with
|
|
483 |
// all DnD protocols that are supported by XEmbed.
|
|
484 |
while (dropTargetProtocols.hasNext()) {
|
|
485 |
XDropTargetProtocol dropTargetProtocol =
|
|
486 |
(XDropTargetProtocol)dropTargetProtocols.next();
|
|
487 |
if (dropTargetProtocol.isXEmbedSupported()) {
|
|
488 |
dropTargetProtocol.registerEmbedderDropSite(window);
|
|
489 |
}
|
|
490 |
}
|
|
491 |
}
|
|
492 |
}
|
|
493 |
|
|
494 |
entry.addSite(window, isXEmbedClient);
|
|
495 |
}
|
|
496 |
}
|
|
497 |
|
|
498 |
private void unregisterEmbeddedDropSite(long toplevel, long window) {
|
|
499 |
Long lToplevel = Long.valueOf(toplevel);
|
|
500 |
EmbeddedDropSiteEntry entry = null;
|
|
501 |
synchronized (this) {
|
|
502 |
entry =
|
|
503 |
(EmbeddedDropSiteEntry)embeddedDropSiteRegistry.get(lToplevel);
|
|
504 |
if (entry == null) {
|
|
505 |
return;
|
|
506 |
}
|
|
507 |
entry.removeSite(window);
|
|
508 |
if (!entry.hasSites()) {
|
|
509 |
embeddedDropSiteRegistry.remove(lToplevel);
|
|
510 |
|
|
511 |
XBaseWindow xbaseWindow = XToolkit.windowToXWindow(toplevel);
|
|
512 |
if (xbaseWindow != null) {
|
|
513 |
if (xbaseWindow instanceof XEmbedCanvasPeer) {
|
|
514 |
XEmbedCanvasPeer peer = (XEmbedCanvasPeer)xbaseWindow;
|
|
515 |
// Unregister an XEmbed drop site.
|
|
516 |
peer.removeXEmbedDropTarget();
|
|
517 |
} else {
|
|
518 |
throw new UnsupportedOperationException();
|
|
519 |
}
|
|
520 |
} else {
|
|
521 |
unregisterEmbedderDropSite(toplevel, entry);
|
|
522 |
}
|
|
523 |
}
|
|
524 |
}
|
|
525 |
}
|
|
526 |
|
|
527 |
/*
|
|
528 |
* Returns a drop site that is embedded in the specified embedder window and
|
|
529 |
* contains the point with the specified root coordinates.
|
|
530 |
*/
|
|
531 |
public long getEmbeddedDropSite(long embedder, int x, int y) {
|
|
532 |
Long lToplevel = Long.valueOf(embedder);
|
|
533 |
EmbeddedDropSiteEntry entry =
|
|
534 |
(EmbeddedDropSiteEntry)embeddedDropSiteRegistry.get(lToplevel);
|
|
535 |
if (entry == null) {
|
|
536 |
return 0;
|
|
537 |
}
|
|
538 |
return entry.getSite(x, y);
|
|
539 |
}
|
|
540 |
|
|
541 |
public void registerDropSite(long window) {
|
|
542 |
if (window == 0) {
|
|
543 |
throw new IllegalArgumentException();
|
|
544 |
}
|
|
545 |
|
|
546 |
XDropTargetEventProcessor.activate();
|
|
547 |
|
|
548 |
XToolkit.awtLock();
|
|
549 |
try {
|
|
550 |
long toplevel = getToplevelWindow(window);
|
|
551 |
|
|
552 |
/*
|
|
553 |
* No window with WM_STATE property is found.
|
|
554 |
* Since the window can be a plugin window reparented to the browser
|
|
555 |
* toplevel, we cannot determine which window will eventually have
|
|
556 |
* WM_STATE property set. So we schedule a timer callback that will
|
|
557 |
* periodically attempt to find an ancestor with WM_STATE and
|
|
558 |
* register the drop site appropriately.
|
|
559 |
*/
|
|
560 |
if (toplevel == 0) {
|
|
561 |
addDelayedRegistrationEntry(window);
|
|
562 |
return;
|
|
563 |
}
|
|
564 |
|
|
565 |
if (toplevel == window) {
|
|
566 |
Iterator dropTargetProtocols =
|
|
567 |
XDragAndDropProtocols.getDropTargetProtocols();
|
|
568 |
|
|
569 |
while (dropTargetProtocols.hasNext()) {
|
|
570 |
XDropTargetProtocol dropTargetProtocol =
|
|
571 |
(XDropTargetProtocol)dropTargetProtocols.next();
|
|
572 |
dropTargetProtocol.registerDropTarget(toplevel);
|
|
573 |
}
|
|
574 |
} else {
|
|
575 |
registerEmbeddedDropSite(toplevel, window);
|
|
576 |
}
|
|
577 |
} finally {
|
|
578 |
XToolkit.awtUnlock();
|
|
579 |
}
|
|
580 |
}
|
|
581 |
|
|
582 |
public void unregisterDropSite(long window) {
|
|
583 |
if (window == 0) {
|
|
584 |
throw new IllegalArgumentException();
|
|
585 |
}
|
|
586 |
|
|
587 |
XToolkit.awtLock();
|
|
588 |
try {
|
|
589 |
long toplevel = getToplevelWindow(window);
|
|
590 |
|
|
591 |
if (toplevel == window) {
|
|
592 |
Iterator dropProtocols =
|
|
593 |
XDragAndDropProtocols.getDropTargetProtocols();
|
|
594 |
|
|
595 |
removeDelayedRegistrationEntry(window);
|
|
596 |
|
|
597 |
while (dropProtocols.hasNext()) {
|
|
598 |
XDropTargetProtocol dropProtocol = (XDropTargetProtocol)dropProtocols.next();
|
|
599 |
dropProtocol.unregisterDropTarget(window);
|
|
600 |
}
|
|
601 |
} else {
|
|
602 |
unregisterEmbeddedDropSite(toplevel, window);
|
|
603 |
}
|
|
604 |
} finally {
|
|
605 |
XToolkit.awtUnlock();
|
|
606 |
}
|
|
607 |
}
|
|
608 |
|
|
609 |
public void registerXEmbedClient(long canvasWindow, long clientWindow) {
|
|
610 |
// If the client has an associated XDnD drop site, add a drop target
|
|
611 |
// to the XEmbedCanvasPeer's target to route drag notifications to the
|
|
612 |
// client.
|
|
613 |
|
|
614 |
XDragSourceProtocol xdndDragProtocol =
|
|
615 |
XDragAndDropProtocols.getDragSourceProtocol(XDragAndDropProtocols.XDnD);
|
|
616 |
XDragSourceProtocol.TargetWindowInfo info =
|
|
617 |
xdndDragProtocol.getTargetWindowInfo(clientWindow);
|
|
618 |
if (info != null &&
|
|
619 |
info.getProtocolVersion() >= XDnDConstants.XDND_MIN_PROTOCOL_VERSION) {
|
|
620 |
|
|
621 |
if (logger.isLoggable(Level.FINE)) {
|
|
622 |
logger.fine(" XEmbed drop site will be registered for " + Long.toHexString(clientWindow));
|
|
623 |
}
|
|
624 |
registerEmbeddedDropSite(canvasWindow, clientWindow);
|
|
625 |
|
|
626 |
Iterator dropTargetProtocols =
|
|
627 |
XDragAndDropProtocols.getDropTargetProtocols();
|
|
628 |
|
|
629 |
while (dropTargetProtocols.hasNext()) {
|
|
630 |
XDropTargetProtocol dropTargetProtocol =
|
|
631 |
(XDropTargetProtocol)dropTargetProtocols.next();
|
|
632 |
dropTargetProtocol.registerEmbeddedDropSite(clientWindow);
|
|
633 |
}
|
|
634 |
|
|
635 |
if (logger.isLoggable(Level.FINE)) {
|
|
636 |
logger.fine(" XEmbed drop site has been registered for " + Long.toHexString(clientWindow));
|
|
637 |
}
|
|
638 |
}
|
|
639 |
}
|
|
640 |
|
|
641 |
public void unregisterXEmbedClient(long canvasWindow, long clientWindow) {
|
|
642 |
if (logger.isLoggable(Level.FINE)) {
|
|
643 |
logger.fine(" XEmbed drop site will be unregistered for " + Long.toHexString(clientWindow));
|
|
644 |
}
|
|
645 |
Iterator dropTargetProtocols =
|
|
646 |
XDragAndDropProtocols.getDropTargetProtocols();
|
|
647 |
|
|
648 |
while (dropTargetProtocols.hasNext()) {
|
|
649 |
XDropTargetProtocol dropTargetProtocol =
|
|
650 |
(XDropTargetProtocol)dropTargetProtocols.next();
|
|
651 |
dropTargetProtocol.unregisterEmbeddedDropSite(clientWindow);
|
|
652 |
}
|
|
653 |
|
|
654 |
unregisterEmbeddedDropSite(canvasWindow, clientWindow);
|
|
655 |
|
|
656 |
if (logger.isLoggable(Level.FINE)) {
|
|
657 |
logger.fine(" XEmbed drop site has beed unregistered for " + Long.toHexString(clientWindow));
|
|
658 |
}
|
|
659 |
}
|
|
660 |
|
|
661 |
/**************** Delayed drop site registration *******************************/
|
|
662 |
|
|
663 |
private void addDelayedRegistrationEntry(final long window) {
|
|
664 |
Long lWindow = Long.valueOf(window);
|
|
665 |
Runnable runnable = new Runnable() {
|
|
666 |
public void run() {
|
|
667 |
removeDelayedRegistrationEntry(window);
|
|
668 |
registerDropSite(window);
|
|
669 |
}
|
|
670 |
};
|
|
671 |
|
|
672 |
XToolkit.awtLock();
|
|
673 |
try {
|
|
674 |
removeDelayedRegistrationEntry(window);
|
|
675 |
delayedRegistrationMap.put(lWindow, runnable);
|
|
676 |
XToolkit.schedule(runnable, DELAYED_REGISTRATION_PERIOD);
|
|
677 |
} finally {
|
|
678 |
XToolkit.awtUnlock();
|
|
679 |
}
|
|
680 |
}
|
|
681 |
|
|
682 |
private void removeDelayedRegistrationEntry(long window) {
|
|
683 |
Long lWindow = Long.valueOf(window);
|
|
684 |
|
|
685 |
XToolkit.awtLock();
|
|
686 |
try {
|
|
687 |
Runnable runnable = delayedRegistrationMap.remove(lWindow);
|
|
688 |
if (runnable != null) {
|
|
689 |
XToolkit.remove(runnable);
|
|
690 |
}
|
|
691 |
} finally {
|
|
692 |
XToolkit.awtUnlock();
|
|
693 |
}
|
|
694 |
}
|
|
695 |
/*******************************************************************************/
|
|
696 |
}
|