author | denis |
Fri, 26 Feb 2010 03:54:52 -0800 | |
changeset 5192 | 506ebd065f86 |
parent 3938 | ef327bd847c0 |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright 2000-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.dnd; |
|
27 |
||
28 |
import java.awt.Component; |
|
29 |
import java.awt.Point; |
|
30 |
||
31 |
import java.awt.datatransfer.DataFlavor; |
|
32 |
import java.awt.datatransfer.Transferable; |
|
33 |
import java.awt.datatransfer.UnsupportedFlavorException; |
|
34 |
||
35 |
import java.awt.dnd.DnDConstants; |
|
36 |
||
37 |
import java.awt.dnd.DropTarget; |
|
38 |
import java.awt.dnd.DropTargetContext; |
|
39 |
import java.awt.dnd.DropTargetListener; |
|
40 |
import java.awt.dnd.DropTargetEvent; |
|
41 |
import java.awt.dnd.DropTargetDragEvent; |
|
42 |
import java.awt.dnd.DropTargetDropEvent; |
|
43 |
import java.awt.dnd.InvalidDnDOperationException; |
|
44 |
||
45 |
import java.awt.dnd.peer.DropTargetContextPeer; |
|
46 |
||
47 |
import java.util.HashSet; |
|
48 |
import java.util.Map; |
|
49 |
import java.util.Arrays; |
|
50 |
||
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
51 |
import sun.util.logging.PlatformLogger; |
2 | 52 |
|
53 |
import java.io.IOException; |
|
54 |
import java.io.InputStream; |
|
55 |
||
56 |
import sun.awt.AppContext; |
|
57 |
import sun.awt.SunToolkit; |
|
58 |
import sun.awt.datatransfer.DataTransferer; |
|
59 |
import sun.awt.datatransfer.ToolkitThreadBlockedHandler; |
|
5192
506ebd065f86
6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents:
3938
diff
changeset
|
60 |
import sun.security.util.SecurityConstants; |
2 | 61 |
|
62 |
/** |
|
63 |
* <p> |
|
64 |
* The SunDropTargetContextPeer class is the generic class responsible for handling |
|
65 |
* the interaction between a windowing systems DnD system and Java. |
|
66 |
* </p> |
|
67 |
* |
|
68 |
* @since JDK1.3.1 |
|
69 |
* |
|
70 |
*/ |
|
71 |
||
72 |
public abstract class SunDropTargetContextPeer implements DropTargetContextPeer, Transferable { |
|
73 |
||
74 |
/* |
|
75 |
* A boolean constant that requires the peer to wait until the |
|
76 |
* SunDropTargetEvent is processed and return the status back |
|
77 |
* to the native code. |
|
78 |
*/ |
|
79 |
public static final boolean DISPATCH_SYNC = true; |
|
80 |
private DropTarget currentDT; |
|
81 |
private DropTargetContext currentDTC; |
|
82 |
private long[] currentT; |
|
83 |
private int currentA; // target actions |
|
84 |
private int currentSA; // source actions |
|
85 |
private int currentDA; // current drop action |
|
86 |
private int previousDA; |
|
87 |
||
88 |
private long nativeDragContext; |
|
89 |
||
90 |
private Transferable local; |
|
91 |
||
92 |
private boolean dragRejected = false; |
|
93 |
||
94 |
protected int dropStatus = STATUS_NONE; |
|
95 |
protected boolean dropComplete = false; |
|
96 |
||
97 |
/* |
|
98 |
* global lock |
|
99 |
*/ |
|
100 |
||
101 |
protected static final Object _globalLock = new Object(); |
|
102 |
||
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
103 |
private static final PlatformLogger dndLog = PlatformLogger.getLogger("sun.awt.dnd.SunDropTargetContextPeer"); |
2 | 104 |
|
105 |
/* |
|
106 |
* a primitive mechanism for advertising intra-JVM Transferables |
|
107 |
*/ |
|
108 |
||
109 |
protected static Transferable currentJVMLocalSourceTransferable = null; |
|
110 |
||
111 |
public static void setCurrentJVMLocalSourceTransferable(Transferable t) throws InvalidDnDOperationException { |
|
112 |
synchronized(_globalLock) { |
|
113 |
if (t != null && currentJVMLocalSourceTransferable != null) { |
|
114 |
throw new InvalidDnDOperationException(); |
|
115 |
} else { |
|
116 |
currentJVMLocalSourceTransferable = t; |
|
117 |
} |
|
118 |
} |
|
119 |
} |
|
120 |
||
121 |
/** |
|
122 |
* obtain the transferable iff the operation is in the same VM |
|
123 |
*/ |
|
124 |
||
125 |
private static Transferable getJVMLocalSourceTransferable() { |
|
126 |
return currentJVMLocalSourceTransferable; |
|
127 |
} |
|
128 |
||
129 |
/* |
|
130 |
* constants used by dropAccept() or dropReject() |
|
131 |
*/ |
|
132 |
||
133 |
protected final static int STATUS_NONE = 0; // none pending |
|
134 |
protected final static int STATUS_WAIT = 1; // drop pending |
|
135 |
protected final static int STATUS_ACCEPT = 2; |
|
136 |
protected final static int STATUS_REJECT = -1; |
|
137 |
||
138 |
/** |
|
139 |
* create the peer |
|
140 |
*/ |
|
141 |
||
142 |
public SunDropTargetContextPeer() { |
|
143 |
super(); |
|
144 |
} |
|
145 |
||
146 |
/** |
|
147 |
* @return the DropTarget associated with this peer |
|
148 |
*/ |
|
149 |
||
150 |
public DropTarget getDropTarget() { return currentDT; } |
|
151 |
||
152 |
/** |
|
153 |
* @param actions set the current actions |
|
154 |
*/ |
|
155 |
||
156 |
public synchronized void setTargetActions(int actions) { |
|
157 |
currentA = actions & |
|
158 |
(DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK); |
|
159 |
} |
|
160 |
||
161 |
/** |
|
162 |
* @return the current target actions |
|
163 |
*/ |
|
164 |
||
165 |
public int getTargetActions() { |
|
166 |
return currentA; |
|
167 |
} |
|
168 |
||
169 |
/** |
|
170 |
* get the Transferable associated with the drop |
|
171 |
*/ |
|
172 |
||
173 |
public Transferable getTransferable() { |
|
174 |
return this; |
|
175 |
} |
|
176 |
||
177 |
/** |
|
178 |
* @return current DataFlavors available |
|
179 |
*/ |
|
180 |
// NOTE: This method may be called by privileged threads. |
|
181 |
// DO NOT INVOKE CLIENT CODE ON THIS THREAD! |
|
182 |
||
183 |
public DataFlavor[] getTransferDataFlavors() { |
|
184 |
final Transferable localTransferable = local; |
|
185 |
||
186 |
if (localTransferable != null) { |
|
187 |
return localTransferable.getTransferDataFlavors(); |
|
188 |
} else { |
|
189 |
return DataTransferer.getInstance().getFlavorsForFormatsAsArray |
|
190 |
(currentT, DataTransferer.adaptFlavorMap |
|
191 |
(currentDT.getFlavorMap())); |
|
192 |
} |
|
193 |
} |
|
194 |
||
195 |
/** |
|
196 |
* @return if the flavor is supported |
|
197 |
*/ |
|
198 |
||
199 |
public boolean isDataFlavorSupported(DataFlavor df) { |
|
200 |
Transferable localTransferable = local; |
|
201 |
||
202 |
if (localTransferable != null) { |
|
203 |
return localTransferable.isDataFlavorSupported(df); |
|
204 |
} else { |
|
205 |
return DataTransferer.getInstance().getFlavorsForFormats |
|
206 |
(currentT, DataTransferer.adaptFlavorMap |
|
207 |
(currentDT.getFlavorMap())). |
|
208 |
containsKey(df); |
|
209 |
} |
|
210 |
} |
|
211 |
||
212 |
/** |
|
213 |
* @return the data |
|
214 |
*/ |
|
215 |
||
216 |
public Object getTransferData(DataFlavor df) |
|
217 |
throws UnsupportedFlavorException, IOException, |
|
218 |
InvalidDnDOperationException |
|
219 |
{ |
|
5192
506ebd065f86
6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents:
3938
diff
changeset
|
220 |
|
506ebd065f86
6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents:
3938
diff
changeset
|
221 |
SecurityManager sm = System.getSecurityManager(); |
506ebd065f86
6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents:
3938
diff
changeset
|
222 |
try { |
506ebd065f86
6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents:
3938
diff
changeset
|
223 |
if (!dropComplete && sm != null) { |
506ebd065f86
6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents:
3938
diff
changeset
|
224 |
sm.checkSystemClipboardAccess(); |
506ebd065f86
6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents:
3938
diff
changeset
|
225 |
} |
506ebd065f86
6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents:
3938
diff
changeset
|
226 |
} catch (Exception e) { |
506ebd065f86
6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents:
3938
diff
changeset
|
227 |
Thread currentThread = Thread.currentThread(); |
506ebd065f86
6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents:
3938
diff
changeset
|
228 |
currentThread.getUncaughtExceptionHandler().uncaughtException(currentThread, e); |
506ebd065f86
6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents:
3938
diff
changeset
|
229 |
return null; |
506ebd065f86
6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents:
3938
diff
changeset
|
230 |
} |
506ebd065f86
6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents:
3938
diff
changeset
|
231 |
|
2 | 232 |
Long lFormat = null; |
233 |
Transferable localTransferable = local; |
|
234 |
||
235 |
if (localTransferable != null) { |
|
236 |
return localTransferable.getTransferData(df); |
|
237 |
} |
|
238 |
||
239 |
if (dropStatus != STATUS_ACCEPT || dropComplete) { |
|
240 |
throw new InvalidDnDOperationException("No drop current"); |
|
241 |
} |
|
242 |
||
243 |
Map flavorMap = DataTransferer.getInstance().getFlavorsForFormats |
|
244 |
(currentT, DataTransferer.adaptFlavorMap |
|
245 |
(currentDT.getFlavorMap())); |
|
246 |
||
247 |
lFormat = (Long)flavorMap.get(df); |
|
248 |
if (lFormat == null) { |
|
249 |
throw new UnsupportedFlavorException(df); |
|
250 |
} |
|
251 |
||
252 |
if (df.isRepresentationClassRemote() && |
|
253 |
currentDA != DnDConstants.ACTION_LINK) { |
|
254 |
throw new InvalidDnDOperationException("only ACTION_LINK is permissable for transfer of java.rmi.Remote objects"); |
|
255 |
} |
|
256 |
||
257 |
final long format = lFormat.longValue(); |
|
258 |
Object ret = getNativeData(format); |
|
259 |
||
260 |
if (ret instanceof byte[]) { |
|
261 |
try { |
|
262 |
return DataTransferer.getInstance(). |
|
263 |
translateBytes((byte[])ret, df, format, this); |
|
264 |
} catch (IOException e) { |
|
265 |
throw new InvalidDnDOperationException(e.getMessage()); |
|
266 |
} |
|
267 |
} else if (ret instanceof InputStream) { |
|
268 |
try { |
|
269 |
return DataTransferer.getInstance(). |
|
270 |
translateStream((InputStream)ret, df, format, this); |
|
271 |
} catch (IOException e) { |
|
272 |
throw new InvalidDnDOperationException(e.getMessage()); |
|
273 |
} |
|
274 |
} else { |
|
275 |
throw new IOException("no native data was transfered"); |
|
276 |
} |
|
277 |
} |
|
278 |
||
279 |
protected abstract Object getNativeData(long format) |
|
280 |
throws IOException; |
|
281 |
||
282 |
/** |
|
283 |
* @return if the transfer is a local one |
|
284 |
*/ |
|
285 |
public boolean isTransferableJVMLocal() { |
|
286 |
return local != null || getJVMLocalSourceTransferable() != null; |
|
287 |
} |
|
288 |
||
289 |
private int handleEnterMessage(final Component component, |
|
290 |
final int x, final int y, |
|
291 |
final int dropAction, |
|
292 |
final int actions, final long[] formats, |
|
293 |
final long nativeCtxt) { |
|
294 |
return postDropTargetEvent(component, x, y, dropAction, actions, |
|
295 |
formats, nativeCtxt, |
|
296 |
SunDropTargetEvent.MOUSE_ENTERED, |
|
297 |
SunDropTargetContextPeer.DISPATCH_SYNC); |
|
298 |
} |
|
299 |
||
300 |
/** |
|
301 |
* actual processing on EventQueue Thread |
|
302 |
*/ |
|
303 |
||
304 |
protected void processEnterMessage(SunDropTargetEvent event) { |
|
305 |
Component c = (Component)event.getSource(); |
|
306 |
DropTarget dt = c.getDropTarget(); |
|
307 |
Point hots = event.getPoint(); |
|
308 |
||
309 |
local = getJVMLocalSourceTransferable(); |
|
310 |
||
311 |
if (currentDTC != null) { // some wreckage from last time |
|
312 |
currentDTC.removeNotify(); |
|
313 |
currentDTC = null; |
|
314 |
} |
|
315 |
||
316 |
if (c.isShowing() && dt != null && dt.isActive()) { |
|
317 |
currentDT = dt; |
|
318 |
currentDTC = currentDT.getDropTargetContext(); |
|
319 |
||
320 |
currentDTC.addNotify(this); |
|
321 |
||
322 |
currentA = dt.getDefaultActions(); |
|
323 |
||
324 |
try { |
|
325 |
((DropTargetListener)dt).dragEnter(new DropTargetDragEvent(currentDTC, |
|
326 |
hots, |
|
327 |
currentDA, |
|
328 |
currentSA)); |
|
329 |
} catch (Exception e) { |
|
330 |
e.printStackTrace(); |
|
331 |
currentDA = DnDConstants.ACTION_NONE; |
|
332 |
} |
|
333 |
} else { |
|
334 |
currentDT = null; |
|
335 |
currentDTC = null; |
|
336 |
currentDA = DnDConstants.ACTION_NONE; |
|
337 |
currentSA = DnDConstants.ACTION_NONE; |
|
338 |
currentA = DnDConstants.ACTION_NONE; |
|
339 |
} |
|
340 |
||
341 |
} |
|
342 |
||
343 |
/** |
|
344 |
* upcall to handle exit messages |
|
345 |
*/ |
|
346 |
||
347 |
private void handleExitMessage(final Component component, |
|
348 |
final long nativeCtxt) { |
|
349 |
/* |
|
350 |
* Even though the return value is irrelevant for this event, it is |
|
351 |
* dispatched synchronously to fix 4393148 properly. |
|
352 |
*/ |
|
353 |
postDropTargetEvent(component, 0, 0, DnDConstants.ACTION_NONE, |
|
354 |
DnDConstants.ACTION_NONE, null, nativeCtxt, |
|
355 |
SunDropTargetEvent.MOUSE_EXITED, |
|
356 |
SunDropTargetContextPeer.DISPATCH_SYNC); |
|
357 |
} |
|
358 |
||
359 |
/** |
|
360 |
* |
|
361 |
*/ |
|
362 |
||
363 |
protected void processExitMessage(SunDropTargetEvent event) { |
|
364 |
Component c = (Component)event.getSource(); |
|
365 |
DropTarget dt = c.getDropTarget(); |
|
366 |
DropTargetContext dtc = null; |
|
367 |
||
368 |
if (dt == null) { |
|
369 |
currentDT = null; |
|
370 |
currentT = null; |
|
371 |
||
372 |
if (currentDTC != null) { |
|
373 |
currentDTC.removeNotify(); |
|
374 |
} |
|
375 |
||
376 |
currentDTC = null; |
|
377 |
||
378 |
return; |
|
379 |
} |
|
380 |
||
381 |
if (dt != currentDT) { |
|
382 |
||
383 |
if (currentDTC != null) { |
|
384 |
currentDTC.removeNotify(); |
|
385 |
} |
|
386 |
||
387 |
currentDT = dt; |
|
388 |
currentDTC = dt.getDropTargetContext(); |
|
389 |
||
390 |
currentDTC.addNotify(this); |
|
391 |
} |
|
392 |
||
393 |
dtc = currentDTC; |
|
394 |
||
395 |
if (dt.isActive()) try { |
|
396 |
((DropTargetListener)dt).dragExit(new DropTargetEvent(dtc)); |
|
397 |
} catch (Exception e) { |
|
398 |
e.printStackTrace(); |
|
399 |
} finally { |
|
400 |
currentA = DnDConstants.ACTION_NONE; |
|
401 |
currentSA = DnDConstants.ACTION_NONE; |
|
402 |
currentDA = DnDConstants.ACTION_NONE; |
|
403 |
currentDT = null; |
|
404 |
currentT = null; |
|
405 |
||
406 |
currentDTC.removeNotify(); |
|
407 |
currentDTC = null; |
|
408 |
||
409 |
local = null; |
|
410 |
||
411 |
dragRejected = false; |
|
412 |
} |
|
413 |
} |
|
414 |
||
415 |
private int handleMotionMessage(final Component component, |
|
416 |
final int x, final int y, |
|
417 |
final int dropAction, |
|
418 |
final int actions, final long[] formats, |
|
419 |
final long nativeCtxt) { |
|
420 |
return postDropTargetEvent(component, x, y, dropAction, actions, |
|
421 |
formats, nativeCtxt, |
|
422 |
SunDropTargetEvent.MOUSE_DRAGGED, |
|
423 |
SunDropTargetContextPeer.DISPATCH_SYNC); |
|
424 |
} |
|
425 |
||
426 |
/** |
|
427 |
* |
|
428 |
*/ |
|
429 |
||
430 |
protected void processMotionMessage(SunDropTargetEvent event, |
|
431 |
boolean operationChanged) { |
|
432 |
Component c = (Component)event.getSource(); |
|
433 |
Point hots = event.getPoint(); |
|
434 |
int id = event.getID(); |
|
435 |
DropTarget dt = c.getDropTarget(); |
|
436 |
DropTargetContext dtc = null; |
|
437 |
||
438 |
if (c.isShowing() && (dt != null) && dt.isActive()) { |
|
439 |
if (currentDT != dt) { |
|
440 |
if (currentDTC != null) { |
|
441 |
currentDTC.removeNotify(); |
|
442 |
} |
|
443 |
||
444 |
currentDT = dt; |
|
445 |
currentDTC = null; |
|
446 |
} |
|
447 |
||
448 |
dtc = currentDT.getDropTargetContext(); |
|
449 |
if (dtc != currentDTC) { |
|
450 |
if (currentDTC != null) { |
|
451 |
currentDTC.removeNotify(); |
|
452 |
} |
|
453 |
||
454 |
currentDTC = dtc; |
|
455 |
currentDTC.addNotify(this); |
|
456 |
} |
|
457 |
||
458 |
currentA = currentDT.getDefaultActions(); |
|
459 |
||
460 |
try { |
|
461 |
DropTargetDragEvent dtde = new DropTargetDragEvent(dtc, |
|
462 |
hots, |
|
463 |
currentDA, |
|
464 |
currentSA); |
|
465 |
DropTargetListener dtl = (DropTargetListener)dt; |
|
466 |
if (operationChanged) { |
|
467 |
dtl.dropActionChanged(dtde); |
|
468 |
} else { |
|
469 |
dtl.dragOver(dtde); |
|
470 |
} |
|
471 |
||
472 |
if (dragRejected) { |
|
473 |
currentDA = DnDConstants.ACTION_NONE; |
|
474 |
} |
|
475 |
} catch (Exception e) { |
|
476 |
e.printStackTrace(); |
|
477 |
currentDA = DnDConstants.ACTION_NONE; |
|
478 |
} |
|
479 |
} else { |
|
480 |
currentDA = DnDConstants.ACTION_NONE; |
|
481 |
} |
|
482 |
} |
|
483 |
||
484 |
/** |
|
485 |
* upcall to handle the Drop message |
|
486 |
*/ |
|
487 |
||
488 |
private void handleDropMessage(final Component component, |
|
489 |
final int x, final int y, |
|
490 |
final int dropAction, final int actions, |
|
491 |
final long[] formats, |
|
492 |
final long nativeCtxt) { |
|
493 |
postDropTargetEvent(component, x, y, dropAction, actions, |
|
494 |
formats, nativeCtxt, |
|
495 |
SunDropTargetEvent.MOUSE_DROPPED, |
|
496 |
!SunDropTargetContextPeer.DISPATCH_SYNC); |
|
497 |
} |
|
498 |
||
499 |
/** |
|
500 |
* |
|
501 |
*/ |
|
502 |
||
503 |
protected void processDropMessage(SunDropTargetEvent event) { |
|
504 |
Component c = (Component)event.getSource(); |
|
505 |
Point hots = event.getPoint(); |
|
506 |
DropTarget dt = c.getDropTarget(); |
|
507 |
||
508 |
dropStatus = STATUS_WAIT; // drop pending ACK |
|
509 |
dropComplete = false; |
|
510 |
||
511 |
if (c.isShowing() && dt != null && dt.isActive()) { |
|
512 |
DropTargetContext dtc = dt.getDropTargetContext(); |
|
513 |
||
514 |
currentDT = dt; |
|
515 |
||
516 |
if (currentDTC != null) { |
|
517 |
currentDTC.removeNotify(); |
|
518 |
} |
|
519 |
||
520 |
currentDTC = dtc; |
|
521 |
currentDTC.addNotify(this); |
|
522 |
currentA = dt.getDefaultActions(); |
|
523 |
||
524 |
synchronized(_globalLock) { |
|
525 |
if ((local = getJVMLocalSourceTransferable()) != null) |
|
526 |
setCurrentJVMLocalSourceTransferable(null); |
|
527 |
} |
|
528 |
||
529 |
try { |
|
530 |
((DropTargetListener)dt).drop(new DropTargetDropEvent(dtc, |
|
531 |
hots, |
|
532 |
currentDA, |
|
533 |
currentSA, |
|
534 |
local != null)); |
|
535 |
} finally { |
|
536 |
if (dropStatus == STATUS_WAIT) { |
|
537 |
rejectDrop(); |
|
538 |
} else if (dropComplete == false) { |
|
539 |
dropComplete(false); |
|
540 |
} |
|
541 |
} |
|
542 |
} else { |
|
543 |
rejectDrop(); |
|
544 |
} |
|
545 |
} |
|
546 |
||
547 |
protected int postDropTargetEvent(final Component component, |
|
548 |
final int x, final int y, |
|
549 |
final int dropAction, |
|
550 |
final int actions, |
|
551 |
final long[] formats, |
|
552 |
final long nativeCtxt, |
|
553 |
final int eventID, |
|
554 |
final boolean dispatchType) { |
|
555 |
AppContext appContext = SunToolkit.targetToAppContext(component); |
|
556 |
||
557 |
EventDispatcher dispatcher = |
|
558 |
new EventDispatcher(this, dropAction, actions, formats, nativeCtxt, |
|
559 |
dispatchType); |
|
560 |
||
561 |
SunDropTargetEvent event = |
|
562 |
new SunDropTargetEvent(component, eventID, x, y, dispatcher); |
|
563 |
||
564 |
if (dispatchType == SunDropTargetContextPeer.DISPATCH_SYNC) { |
|
565 |
DataTransferer.getInstance().getToolkitThreadBlockedHandler().lock(); |
|
566 |
} |
|
567 |
||
568 |
// schedule callback |
|
569 |
SunToolkit.postEvent(appContext, event); |
|
570 |
||
571 |
eventPosted(event); |
|
572 |
||
573 |
if (dispatchType == SunDropTargetContextPeer.DISPATCH_SYNC) { |
|
574 |
while (!dispatcher.isDone()) { |
|
575 |
DataTransferer.getInstance().getToolkitThreadBlockedHandler().enter(); |
|
576 |
} |
|
577 |
||
578 |
DataTransferer.getInstance().getToolkitThreadBlockedHandler().unlock(); |
|
579 |
||
580 |
// return target's response |
|
581 |
return dispatcher.getReturnValue(); |
|
582 |
} else { |
|
583 |
return 0; |
|
584 |
} |
|
585 |
} |
|
586 |
||
587 |
/** |
|
588 |
* acceptDrag |
|
589 |
*/ |
|
590 |
||
591 |
public synchronized void acceptDrag(int dragOperation) { |
|
592 |
if (currentDT == null) { |
|
593 |
throw new InvalidDnDOperationException("No Drag pending"); |
|
594 |
} |
|
595 |
currentDA = mapOperation(dragOperation); |
|
596 |
if (currentDA != DnDConstants.ACTION_NONE) { |
|
597 |
dragRejected = false; |
|
598 |
} |
|
599 |
} |
|
600 |
||
601 |
/** |
|
602 |
* rejectDrag |
|
603 |
*/ |
|
604 |
||
605 |
public synchronized void rejectDrag() { |
|
606 |
if (currentDT == null) { |
|
607 |
throw new InvalidDnDOperationException("No Drag pending"); |
|
608 |
} |
|
609 |
currentDA = DnDConstants.ACTION_NONE; |
|
610 |
dragRejected = true; |
|
611 |
} |
|
612 |
||
613 |
/** |
|
614 |
* acceptDrop |
|
615 |
*/ |
|
616 |
||
617 |
public synchronized void acceptDrop(int dropOperation) { |
|
618 |
if (dropOperation == DnDConstants.ACTION_NONE) |
|
619 |
throw new IllegalArgumentException("invalid acceptDrop() action"); |
|
620 |
||
621 |
if (dropStatus != STATUS_WAIT) { |
|
622 |
throw new InvalidDnDOperationException("invalid acceptDrop()"); |
|
623 |
} |
|
624 |
||
625 |
currentDA = currentA = mapOperation(dropOperation & currentSA); |
|
626 |
||
627 |
dropStatus = STATUS_ACCEPT; |
|
628 |
dropComplete = false; |
|
629 |
} |
|
630 |
||
631 |
/** |
|
632 |
* reject Drop |
|
633 |
*/ |
|
634 |
||
635 |
public synchronized void rejectDrop() { |
|
636 |
if (dropStatus != STATUS_WAIT) { |
|
637 |
throw new InvalidDnDOperationException("invalid rejectDrop()"); |
|
638 |
} |
|
639 |
dropStatus = STATUS_REJECT; |
|
640 |
/* |
|
641 |
* Fix for 4285634. |
|
642 |
* The target rejected the drop means that it doesn't perform any |
|
643 |
* drop action. This change is to make Solaris behavior consistent |
|
644 |
* with Win32. |
|
645 |
*/ |
|
646 |
currentDA = DnDConstants.ACTION_NONE; |
|
647 |
dropComplete(false); |
|
648 |
} |
|
649 |
||
650 |
/** |
|
651 |
* mapOperation |
|
652 |
*/ |
|
653 |
||
654 |
private int mapOperation(int operation) { |
|
655 |
int[] operations = { |
|
656 |
DnDConstants.ACTION_MOVE, |
|
657 |
DnDConstants.ACTION_COPY, |
|
658 |
DnDConstants.ACTION_LINK, |
|
659 |
}; |
|
660 |
int ret = DnDConstants.ACTION_NONE; |
|
661 |
||
662 |
for (int i = 0; i < operations.length; i++) { |
|
663 |
if ((operation & operations[i]) == operations[i]) { |
|
664 |
ret = operations[i]; |
|
665 |
break; |
|
666 |
} |
|
667 |
} |
|
668 |
||
669 |
return ret; |
|
670 |
} |
|
671 |
||
672 |
/** |
|
673 |
* signal drop complete |
|
674 |
*/ |
|
675 |
||
676 |
public synchronized void dropComplete(boolean success) { |
|
677 |
if (dropStatus == STATUS_NONE) { |
|
678 |
throw new InvalidDnDOperationException("No Drop pending"); |
|
679 |
} |
|
680 |
||
681 |
if (currentDTC != null) currentDTC.removeNotify(); |
|
682 |
||
683 |
currentDT = null; |
|
684 |
currentDTC = null; |
|
685 |
currentT = null; |
|
686 |
currentA = DnDConstants.ACTION_NONE; |
|
687 |
||
688 |
synchronized(_globalLock) { |
|
689 |
currentJVMLocalSourceTransferable = null; |
|
690 |
} |
|
691 |
||
692 |
dropStatus = STATUS_NONE; |
|
693 |
dropComplete = true; |
|
694 |
||
695 |
try { |
|
696 |
doDropDone(success, currentDA, local != null); |
|
697 |
} finally { |
|
698 |
currentDA = DnDConstants.ACTION_NONE; |
|
699 |
// The native context is invalid after the drop is done. |
|
700 |
// Clear the reference to prohibit access. |
|
701 |
nativeDragContext = 0; |
|
702 |
} |
|
703 |
} |
|
704 |
||
705 |
protected abstract void doDropDone(boolean success, |
|
706 |
int dropAction, boolean isLocal); |
|
707 |
||
708 |
protected synchronized long getNativeDragContext() { |
|
709 |
return nativeDragContext; |
|
710 |
} |
|
711 |
||
712 |
protected void eventPosted(SunDropTargetEvent e) {} |
|
713 |
||
714 |
protected void eventProcessed(SunDropTargetEvent e, int returnValue, |
|
715 |
boolean dispatcherDone) {} |
|
716 |
||
717 |
protected static class EventDispatcher { |
|
718 |
||
719 |
private final SunDropTargetContextPeer peer; |
|
720 |
||
721 |
// context fields |
|
722 |
private final int dropAction; |
|
723 |
private final int actions; |
|
724 |
private final long[] formats; |
|
725 |
private long nativeCtxt; |
|
726 |
private final boolean dispatchType; |
|
727 |
private boolean dispatcherDone = false; |
|
728 |
||
729 |
// dispatcher state fields |
|
730 |
private int returnValue = 0; |
|
731 |
// set of events to be dispatched by this dispatcher |
|
732 |
private final HashSet eventSet = new HashSet(3); |
|
733 |
||
734 |
static final ToolkitThreadBlockedHandler handler = |
|
735 |
DataTransferer.getInstance().getToolkitThreadBlockedHandler(); |
|
736 |
||
737 |
EventDispatcher(SunDropTargetContextPeer peer, |
|
738 |
int dropAction, |
|
739 |
int actions, |
|
740 |
long[] formats, |
|
741 |
long nativeCtxt, |
|
742 |
boolean dispatchType) { |
|
743 |
||
744 |
this.peer = peer; |
|
745 |
this.nativeCtxt = nativeCtxt; |
|
746 |
this.dropAction = dropAction; |
|
747 |
this.actions = actions; |
|
748 |
this.formats = |
|
749 |
(null == formats) ? null : Arrays.copyOf(formats, formats.length); |
|
750 |
this.dispatchType = dispatchType; |
|
751 |
} |
|
752 |
||
753 |
void dispatchEvent(SunDropTargetEvent e) { |
|
754 |
int id = e.getID(); |
|
755 |
||
756 |
switch (id) { |
|
757 |
case SunDropTargetEvent.MOUSE_ENTERED: |
|
758 |
dispatchEnterEvent(e); |
|
759 |
break; |
|
760 |
case SunDropTargetEvent.MOUSE_DRAGGED: |
|
761 |
dispatchMotionEvent(e); |
|
762 |
break; |
|
763 |
case SunDropTargetEvent.MOUSE_EXITED: |
|
764 |
dispatchExitEvent(e); |
|
765 |
break; |
|
766 |
case SunDropTargetEvent.MOUSE_DROPPED: |
|
767 |
dispatchDropEvent(e); |
|
768 |
break; |
|
769 |
default: |
|
770 |
throw new InvalidDnDOperationException(); |
|
771 |
} |
|
772 |
} |
|
773 |
||
774 |
private void dispatchEnterEvent(SunDropTargetEvent e) { |
|
775 |
synchronized (peer) { |
|
776 |
||
777 |
// store the drop action here to track operation changes |
|
778 |
peer.previousDA = dropAction; |
|
779 |
||
780 |
// setup peer context |
|
781 |
peer.nativeDragContext = nativeCtxt; |
|
782 |
peer.currentT = formats; |
|
783 |
peer.currentSA = actions; |
|
784 |
peer.currentDA = dropAction; |
|
785 |
// To allow data retrieval. |
|
786 |
peer.dropStatus = STATUS_ACCEPT; |
|
787 |
peer.dropComplete = false; |
|
788 |
||
789 |
try { |
|
790 |
peer.processEnterMessage(e); |
|
791 |
} finally { |
|
792 |
peer.dropStatus = STATUS_NONE; |
|
793 |
} |
|
794 |
||
795 |
setReturnValue(peer.currentDA); |
|
796 |
} |
|
797 |
} |
|
798 |
||
799 |
private void dispatchMotionEvent(SunDropTargetEvent e) { |
|
800 |
synchronized (peer) { |
|
801 |
||
802 |
boolean operationChanged = peer.previousDA != dropAction; |
|
803 |
peer.previousDA = dropAction; |
|
804 |
||
805 |
// setup peer context |
|
806 |
peer.nativeDragContext = nativeCtxt; |
|
807 |
peer.currentT = formats; |
|
808 |
peer.currentSA = actions; |
|
809 |
peer.currentDA = dropAction; |
|
810 |
// To allow data retrieval. |
|
811 |
peer.dropStatus = STATUS_ACCEPT; |
|
812 |
peer.dropComplete = false; |
|
813 |
||
814 |
try { |
|
815 |
peer.processMotionMessage(e, operationChanged); |
|
816 |
} finally { |
|
817 |
peer.dropStatus = STATUS_NONE; |
|
818 |
} |
|
819 |
||
820 |
setReturnValue(peer.currentDA); |
|
821 |
} |
|
822 |
} |
|
823 |
||
824 |
private void dispatchExitEvent(SunDropTargetEvent e) { |
|
825 |
synchronized (peer) { |
|
826 |
||
827 |
// setup peer context |
|
828 |
peer.nativeDragContext = nativeCtxt; |
|
829 |
||
830 |
peer.processExitMessage(e); |
|
831 |
} |
|
832 |
} |
|
833 |
||
834 |
private void dispatchDropEvent(SunDropTargetEvent e) { |
|
835 |
synchronized (peer) { |
|
836 |
||
837 |
// setup peer context |
|
838 |
peer.nativeDragContext = nativeCtxt; |
|
839 |
peer.currentT = formats; |
|
840 |
peer.currentSA = actions; |
|
841 |
peer.currentDA = dropAction; |
|
842 |
||
843 |
peer.processDropMessage(e); |
|
844 |
} |
|
845 |
} |
|
846 |
||
847 |
void setReturnValue(int ret) { |
|
848 |
returnValue = ret; |
|
849 |
} |
|
850 |
||
851 |
int getReturnValue() { |
|
852 |
return returnValue; |
|
853 |
} |
|
854 |
||
855 |
boolean isDone() { |
|
856 |
return eventSet.isEmpty(); |
|
857 |
} |
|
858 |
||
859 |
void registerEvent(SunDropTargetEvent e) { |
|
860 |
handler.lock(); |
|
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
861 |
if (!eventSet.add(e) && dndLog.isLoggable(PlatformLogger.FINE)) { |
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
862 |
dndLog.fine("Event is already registered: " + e); |
2 | 863 |
} |
864 |
handler.unlock(); |
|
865 |
} |
|
866 |
||
867 |
void unregisterEvent(SunDropTargetEvent e) { |
|
868 |
handler.lock(); |
|
869 |
try { |
|
870 |
if (!eventSet.remove(e)) { |
|
871 |
// This event has already been unregistered. |
|
872 |
return; |
|
873 |
} |
|
874 |
if (eventSet.isEmpty()) { |
|
875 |
if (!dispatcherDone && dispatchType == DISPATCH_SYNC) { |
|
876 |
handler.exit(); |
|
877 |
} |
|
878 |
dispatcherDone = true; |
|
879 |
} |
|
880 |
} finally { |
|
881 |
handler.unlock(); |
|
882 |
} |
|
883 |
||
884 |
try { |
|
885 |
peer.eventProcessed(e, returnValue, dispatcherDone); |
|
886 |
} finally { |
|
887 |
/* |
|
888 |
* Clear the reference to the native context if all copies of |
|
889 |
* the original event are processed. |
|
890 |
*/ |
|
891 |
if (dispatcherDone) { |
|
892 |
nativeCtxt = 0; |
|
893 |
// Fix for 6342381 |
|
894 |
peer.nativeDragContext = 0; |
|
895 |
||
896 |
} |
|
897 |
} |
|
898 |
} |
|
899 |
||
900 |
public void unregisterAllEvents() { |
|
901 |
Object[] events = null; |
|
902 |
handler.lock(); |
|
903 |
try { |
|
904 |
events = eventSet.toArray(); |
|
905 |
} finally { |
|
906 |
handler.unlock(); |
|
907 |
} |
|
908 |
||
909 |
if (events != null) { |
|
910 |
for (int i = 0; i < events.length; i++) { |
|
911 |
unregisterEvent((SunDropTargetEvent)events[i]); |
|
912 |
} |
|
913 |
} |
|
914 |
} |
|
915 |
} |
|
916 |
} |