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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
|
/*
* Copyright (c) 1997, 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 java.awt.dnd;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.datatransfer.FlavorMap;
import java.awt.datatransfer.SystemFlavorMap;
import java.awt.datatransfer.Transferable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serial;
import java.io.Serializable;
import java.security.AccessController;
import java.util.EventListener;
import sun.awt.AWTAccessor;
import sun.awt.AWTAccessor.DragSourceContextAccessor;
import sun.awt.dnd.SunDragSourceContextPeer;
import sun.security.action.GetIntegerAction;
/**
* The {@code DragSource} is the entity responsible
* for the initiation of the Drag
* and Drop operation, and may be used in a number of scenarios:
* <UL>
* <LI>1 default instance per JVM for the lifetime of that JVM.
* <LI>1 instance per class of potential Drag Initiator object (e.g
* TextField). [implementation dependent]
* <LI>1 per instance of a particular
* {@code Component}, or application specific
* object associated with a {@code Component}
* instance in the GUI. [implementation dependent]
* <LI>Some other arbitrary association. [implementation dependent]
*</UL>
*
* Once the {@code DragSource} is
* obtained, a {@code DragGestureRecognizer} should
* also be obtained to associate the {@code DragSource}
* with a particular
* {@code Component}.
* <P>
* The initial interpretation of the user's gesture,
* and the subsequent starting of the drag operation
* are the responsibility of the implementing
* {@code Component}, which is usually
* implemented by a {@code DragGestureRecognizer}.
*<P>
* When a drag gesture occurs, the
* {@code DragSource}'s
* startDrag() method shall be
* invoked in order to cause processing
* of the user's navigational
* gestures and delivery of Drag and Drop
* protocol notifications. A
* {@code DragSource} shall only
* permit a single Drag and Drop operation to be
* current at any one time, and shall
* reject any further startDrag() requests
* by throwing an {@code IllegalDnDOperationException}
* until such time as the extant operation is complete.
* <P>
* The startDrag() method invokes the
* createDragSourceContext() method to
* instantiate an appropriate
* {@code DragSourceContext}
* and associate the {@code DragSourceContextPeer}
* with that.
* <P>
* If the Drag and Drop System is
* unable to initiate a drag operation for
* some reason, the startDrag() method throws
* a {@code java.awt.dnd.InvalidDnDOperationException}
* to signal such a condition. Typically this
* exception is thrown when the underlying platform
* system is either not in a state to
* initiate a drag, or the parameters specified are invalid.
* <P>
* Note that during the drag, the
* set of operations exposed by the source
* at the start of the drag operation may not change
* until the operation is complete.
* The operation(s) are constant for the
* duration of the operation with respect to the
* {@code DragSource}.
*
* @since 1.2
*/
public class DragSource implements Serializable {
/**
* Use serialVersionUID from JDK 1.4 for interoperability.
*/
@Serial
private static final long serialVersionUID = 6236096958971414066L;
/*
* load a system default cursor
*/
private static Cursor load(String name) {
if (GraphicsEnvironment.isHeadless()) {
return null;
}
try {
return (Cursor)Toolkit.getDefaultToolkit().getDesktopProperty(name);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("failed to load system cursor: " + name + " : " + e.getMessage());
}
}
/**
* The default {@code Cursor} to use with a copy operation indicating
* that a drop is currently allowed. {@code null} if
* {@code GraphicsEnvironment.isHeadless()} returns {@code true}.
*
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public static final Cursor DefaultCopyDrop =
load("DnD.Cursor.CopyDrop");
/**
* The default {@code Cursor} to use with a move operation indicating
* that a drop is currently allowed. {@code null} if
* {@code GraphicsEnvironment.isHeadless()} returns {@code true}.
*
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public static final Cursor DefaultMoveDrop =
load("DnD.Cursor.MoveDrop");
/**
* The default {@code Cursor} to use with a link operation indicating
* that a drop is currently allowed. {@code null} if
* {@code GraphicsEnvironment.isHeadless()} returns {@code true}.
*
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public static final Cursor DefaultLinkDrop =
load("DnD.Cursor.LinkDrop");
/**
* The default {@code Cursor} to use with a copy operation indicating
* that a drop is currently not allowed. {@code null} if
* {@code GraphicsEnvironment.isHeadless()} returns {@code true}.
*
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public static final Cursor DefaultCopyNoDrop =
load("DnD.Cursor.CopyNoDrop");
/**
* The default {@code Cursor} to use with a move operation indicating
* that a drop is currently not allowed. {@code null} if
* {@code GraphicsEnvironment.isHeadless()} returns {@code true}.
*
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public static final Cursor DefaultMoveNoDrop =
load("DnD.Cursor.MoveNoDrop");
/**
* The default {@code Cursor} to use with a link operation indicating
* that a drop is currently not allowed. {@code null} if
* {@code GraphicsEnvironment.isHeadless()} returns {@code true}.
*
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public static final Cursor DefaultLinkNoDrop =
load("DnD.Cursor.LinkNoDrop");
private static final DragSource dflt =
(GraphicsEnvironment.isHeadless()) ? null : new DragSource();
/**
* Internal constants for serialization.
*/
static final String dragSourceListenerK = "dragSourceL";
static final String dragSourceMotionListenerK = "dragSourceMotionL";
/**
* Gets the {@code DragSource} object associated with
* the underlying platform.
*
* @return the platform DragSource
* @exception HeadlessException if GraphicsEnvironment.isHeadless()
* returns true
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public static DragSource getDefaultDragSource() {
if (GraphicsEnvironment.isHeadless()) {
throw new HeadlessException();
} else {
return dflt;
}
}
/**
* Reports
* whether or not drag
* {@code Image} support
* is available on the underlying platform.
*
* @return if the Drag Image support is available on this platform
*/
public static boolean isDragImageSupported() {
Toolkit t = Toolkit.getDefaultToolkit();
Boolean supported;
try {
supported = (Boolean)Toolkit.getDefaultToolkit().getDesktopProperty("DnD.isDragImageSupported");
return supported.booleanValue();
} catch (Exception e) {
return false;
}
}
/**
* Creates a new {@code DragSource}.
*
* @exception HeadlessException if GraphicsEnvironment.isHeadless()
* returns true
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public DragSource() throws HeadlessException {
if (GraphicsEnvironment.isHeadless()) {
throw new HeadlessException();
}
}
/**
* Start a drag, given the {@code DragGestureEvent}
* that initiated the drag, the initial
* {@code Cursor} to use,
* the {@code Image} to drag,
* the offset of the {@code Image} origin
* from the hotspot of the {@code Cursor} at
* the instant of the trigger,
* the {@code Transferable} subject data
* of the drag, the {@code DragSourceListener},
* and the {@code FlavorMap}.
*
* @param trigger the {@code DragGestureEvent} that initiated the drag
* @param dragCursor the initial {@code Cursor} for this drag operation
* or {@code null} for the default cursor handling;
* see <a href="DragSourceContext.html#defaultCursor">DragSourceContext</a>
* for more details on the cursor handling mechanism during drag and drop
* @param dragImage the image to drag or {@code null}
* @param imageOffset the offset of the {@code Image} origin from the hotspot
* of the {@code Cursor} at the instant of the trigger
* @param transferable the subject data of the drag
* @param dsl the {@code DragSourceListener}
* @param flavorMap the {@code FlavorMap} to use, or {@code null}
*
* @throws java.awt.dnd.InvalidDnDOperationException
* if the Drag and Drop
* system is unable to initiate a drag operation, or if the user
* attempts to start a drag while an existing drag operation
* is still executing
*/
public void startDrag(DragGestureEvent trigger,
Cursor dragCursor,
Image dragImage,
Point imageOffset,
Transferable transferable,
DragSourceListener dsl,
FlavorMap flavorMap) throws InvalidDnDOperationException {
SunDragSourceContextPeer.setDragDropInProgress(true);
try {
if (flavorMap != null) this.flavorMap = flavorMap;
DragSourceContext dsc = createDragSourceContext(trigger, dragCursor,
dragImage,
imageOffset,
transferable, dsl);
if (dsc == null) {
throw new InvalidDnDOperationException();
}
DragSourceContextAccessor acc = AWTAccessor.getDragSourceContextAccessor();
acc.getPeer(dsc).startDrag(dsc, dsc.getCursor(), dragImage, imageOffset); // may throw
} catch (RuntimeException e) {
SunDragSourceContextPeer.setDragDropInProgress(false);
throw e;
}
}
/**
* Start a drag, given the {@code DragGestureEvent}
* that initiated the drag, the initial
* {@code Cursor} to use,
* the {@code Transferable} subject data
* of the drag, the {@code DragSourceListener},
* and the {@code FlavorMap}.
*
* @param trigger the {@code DragGestureEvent} that
* initiated the drag
* @param dragCursor the initial {@code Cursor} for this drag operation
* or {@code null} for the default cursor handling;
* see <a href="DragSourceContext.html#defaultCursor">DragSourceContext</a>
* for more details on the cursor handling mechanism during drag and drop
* @param transferable the subject data of the drag
* @param dsl the {@code DragSourceListener}
* @param flavorMap the {@code FlavorMap} to use or {@code null}
*
* @throws java.awt.dnd.InvalidDnDOperationException
* if the Drag and Drop
* system is unable to initiate a drag operation, or if the user
* attempts to start a drag while an existing drag operation
* is still executing
*/
public void startDrag(DragGestureEvent trigger,
Cursor dragCursor,
Transferable transferable,
DragSourceListener dsl,
FlavorMap flavorMap) throws InvalidDnDOperationException {
startDrag(trigger, dragCursor, null, null, transferable, dsl, flavorMap);
}
/**
* Start a drag, given the {@code DragGestureEvent}
* that initiated the drag, the initial {@code Cursor}
* to use,
* the {@code Image} to drag,
* the offset of the {@code Image} origin
* from the hotspot of the {@code Cursor}
* at the instant of the trigger,
* the subject data of the drag, and
* the {@code DragSourceListener}.
*
* @param trigger the {@code DragGestureEvent} that initiated the drag
* @param dragCursor the initial {@code Cursor} for this drag operation
* or {@code null} for the default cursor handling;
* see <a href="DragSourceContext.html#defaultCursor">DragSourceContext</a>
* for more details on the cursor handling mechanism during drag and drop
* @param dragImage the {@code Image} to drag or {@code null}
* @param dragOffset the offset of the {@code Image} origin from the hotspot
* of the {@code Cursor} at the instant of the trigger
* @param transferable the subject data of the drag
* @param dsl the {@code DragSourceListener}
*
* @throws java.awt.dnd.InvalidDnDOperationException
* if the Drag and Drop
* system is unable to initiate a drag operation, or if the user
* attempts to start a drag while an existing drag operation
* is still executing
*/
public void startDrag(DragGestureEvent trigger,
Cursor dragCursor,
Image dragImage,
Point dragOffset,
Transferable transferable,
DragSourceListener dsl) throws InvalidDnDOperationException {
startDrag(trigger, dragCursor, dragImage, dragOffset, transferable, dsl, null);
}
/**
* Start a drag, given the {@code DragGestureEvent}
* that initiated the drag, the initial
* {@code Cursor} to
* use,
* the {@code Transferable} subject data
* of the drag, and the {@code DragSourceListener}.
*
* @param trigger the {@code DragGestureEvent} that initiated the drag
* @param dragCursor the initial {@code Cursor} for this drag operation
* or {@code null} for the default cursor handling;
* see <a href="DragSourceContext.html#defaultCursor">DragSourceContext</a> class
* for more details on the cursor handling mechanism during drag and drop
* @param transferable the subject data of the drag
* @param dsl the {@code DragSourceListener}
*
* @throws java.awt.dnd.InvalidDnDOperationException
* if the Drag and Drop
* system is unable to initiate a drag operation, or if the user
* attempts to start a drag while an existing drag operation
* is still executing
*/
public void startDrag(DragGestureEvent trigger,
Cursor dragCursor,
Transferable transferable,
DragSourceListener dsl) throws InvalidDnDOperationException {
startDrag(trigger, dragCursor, null, null, transferable, dsl, null);
}
/**
* Creates the {@code DragSourceContext} to handle the current drag
* operation.
* <p>
* To incorporate a new {@code DragSourceContext}
* subclass, subclass {@code DragSource} and
* override this method.
* <p>
* If {@code dragImage} is {@code null}, no image is used
* to represent the drag over feedback for this drag operation, but
* {@code NullPointerException} is not thrown.
* <p>
* If {@code dsl} is {@code null}, no drag source listener
* is registered with the created {@code DragSourceContext},
* but {@code NullPointerException} is not thrown.
*
* @param dgl The {@code DragGestureEvent} that triggered the
* drag
* @param dragCursor The initial {@code Cursor} for this drag operation
* or {@code null} for the default cursor handling;
* see <a href="DragSourceContext.html#defaultCursor">DragSourceContext</a> class
* for more details on the cursor handling mechanism during drag and drop
* @param dragImage The {@code Image} to drag or {@code null}
* @param imageOffset The offset of the {@code Image} origin from the
* hotspot of the cursor at the instant of the trigger
* @param t The subject data of the drag
* @param dsl The {@code DragSourceListener}
*
* @return the {@code DragSourceContext}
*
* @throws NullPointerException if {@code dscp} is {@code null}
* @throws NullPointerException if {@code dgl} is {@code null}
* @throws NullPointerException if {@code dragImage} is not
* {@code null} and {@code imageOffset} is {@code null}
* @throws NullPointerException if {@code t} is {@code null}
* @throws IllegalArgumentException if the {@code Component}
* associated with the trigger event is {@code null}.
* @throws IllegalArgumentException if the {@code DragSource} for the
* trigger event is {@code null}.
* @throws IllegalArgumentException if the drag action for the
* trigger event is {@code DnDConstants.ACTION_NONE}.
* @throws IllegalArgumentException if the source actions for the
* {@code DragGestureRecognizer} associated with the trigger
* event are equal to {@code DnDConstants.ACTION_NONE}.
*/
protected DragSourceContext createDragSourceContext(DragGestureEvent dgl,
Cursor dragCursor,
Image dragImage,
Point imageOffset,
Transferable t,
DragSourceListener dsl) {
return new DragSourceContext(dgl, dragCursor, dragImage, imageOffset, t, dsl);
}
/**
* This method returns the
* {@code FlavorMap} for this {@code DragSource}.
*
* @return the {@code FlavorMap} for this {@code DragSource}
*/
public FlavorMap getFlavorMap() { return flavorMap; }
/**
* Creates a new {@code DragGestureRecognizer}
* that implements the specified
* abstract subclass of
* {@code DragGestureRecognizer}, and
* sets the specified {@code Component}
* and {@code DragGestureListener} on
* the newly created object.
*
* @param <T> the type of {@code DragGestureRecognizer} to create
* @param recognizerAbstractClass the requested abstract type
* @param actions the permitted source drag actions
* @param c the {@code Component} target
* @param dgl the {@code DragGestureListener} to notify
*
* @return the new {@code DragGestureRecognizer} or {@code null}
* if the {@code Toolkit.createDragGestureRecognizer} method
* has no implementation available for
* the requested {@code DragGestureRecognizer}
* subclass and returns {@code null}
*/
public <T extends DragGestureRecognizer> T
createDragGestureRecognizer(Class<T> recognizerAbstractClass,
Component c, int actions,
DragGestureListener dgl)
{
return Toolkit.getDefaultToolkit().createDragGestureRecognizer(recognizerAbstractClass, this, c, actions, dgl);
}
/**
* Creates a new {@code DragGestureRecognizer}
* that implements the default
* abstract subclass of {@code DragGestureRecognizer}
* for this {@code DragSource},
* and sets the specified {@code Component}
* and {@code DragGestureListener} on the
* newly created object.
*
* For this {@code DragSource}
* the default is {@code MouseDragGestureRecognizer}.
*
* @param c the {@code Component} target for the recognizer
* @param actions the permitted source actions
* @param dgl the {@code DragGestureListener} to notify
*
* @return the new {@code DragGestureRecognizer} or {@code null}
* if the {@code Toolkit.createDragGestureRecognizer} method
* has no implementation available for
* the requested {@code DragGestureRecognizer}
* subclass and returns {@code null}
*/
public DragGestureRecognizer createDefaultDragGestureRecognizer(Component c, int actions, DragGestureListener dgl) {
return Toolkit.getDefaultToolkit().createDragGestureRecognizer(MouseDragGestureRecognizer.class, this, c, actions, dgl);
}
/**
* Adds the specified {@code DragSourceListener} to this
* {@code DragSource} to receive drag source events during drag
* operations initiated with this {@code DragSource}.
* If a {@code null} listener is specified, no action is taken and no
* exception is thrown.
*
* @param dsl the {@code DragSourceListener} to add
*
* @see #removeDragSourceListener
* @see #getDragSourceListeners
* @since 1.4
*/
public void addDragSourceListener(DragSourceListener dsl) {
if (dsl != null) {
synchronized (this) {
listener = DnDEventMulticaster.add(listener, dsl);
}
}
}
/**
* Removes the specified {@code DragSourceListener} from this
* {@code DragSource}.
* If a {@code null} listener is specified, no action is taken and no
* exception is thrown.
* If the listener specified by the argument was not previously added to
* this {@code DragSource}, no action is taken and no exception
* is thrown.
*
* @param dsl the {@code DragSourceListener} to remove
*
* @see #addDragSourceListener
* @see #getDragSourceListeners
* @since 1.4
*/
public void removeDragSourceListener(DragSourceListener dsl) {
if (dsl != null) {
synchronized (this) {
listener = DnDEventMulticaster.remove(listener, dsl);
}
}
}
/**
* Gets all the {@code DragSourceListener}s
* registered with this {@code DragSource}.
*
* @return all of this {@code DragSource}'s
* {@code DragSourceListener}s or an empty array if no
* such listeners are currently registered
*
* @see #addDragSourceListener
* @see #removeDragSourceListener
* @since 1.4
*/
public DragSourceListener[] getDragSourceListeners() {
return getListeners(DragSourceListener.class);
}
/**
* Adds the specified {@code DragSourceMotionListener} to this
* {@code DragSource} to receive drag motion events during drag
* operations initiated with this {@code DragSource}.
* If a {@code null} listener is specified, no action is taken and no
* exception is thrown.
*
* @param dsml the {@code DragSourceMotionListener} to add
*
* @see #removeDragSourceMotionListener
* @see #getDragSourceMotionListeners
* @since 1.4
*/
public void addDragSourceMotionListener(DragSourceMotionListener dsml) {
if (dsml != null) {
synchronized (this) {
motionListener = DnDEventMulticaster.add(motionListener, dsml);
}
}
}
/**
* Removes the specified {@code DragSourceMotionListener} from this
* {@code DragSource}.
* If a {@code null} listener is specified, no action is taken and no
* exception is thrown.
* If the listener specified by the argument was not previously added to
* this {@code DragSource}, no action is taken and no exception
* is thrown.
*
* @param dsml the {@code DragSourceMotionListener} to remove
*
* @see #addDragSourceMotionListener
* @see #getDragSourceMotionListeners
* @since 1.4
*/
public void removeDragSourceMotionListener(DragSourceMotionListener dsml) {
if (dsml != null) {
synchronized (this) {
motionListener = DnDEventMulticaster.remove(motionListener, dsml);
}
}
}
/**
* Gets all of the {@code DragSourceMotionListener}s
* registered with this {@code DragSource}.
*
* @return all of this {@code DragSource}'s
* {@code DragSourceMotionListener}s or an empty array if no
* such listeners are currently registered
*
* @see #addDragSourceMotionListener
* @see #removeDragSourceMotionListener
* @since 1.4
*/
public DragSourceMotionListener[] getDragSourceMotionListeners() {
return getListeners(DragSourceMotionListener.class);
}
/**
* Gets all the objects currently registered as
* <code><em>Foo</em>Listener</code>s upon this {@code DragSource}.
* <code><em>Foo</em>Listener</code>s are registered using the
* <code>add<em>Foo</em>Listener</code> method.
*
* @param <T> the type of listener objects
* @param listenerType the type of listeners requested; this parameter
* should specify an interface that descends from
* {@code java.util.EventListener}
* @return an array of all objects registered as
* <code><em>Foo</em>Listener</code>s on this
* {@code DragSource}, or an empty array if no such listeners
* have been added
* @exception ClassCastException if {@code listenerType}
* doesn't specify a class or interface that implements
* {@code java.util.EventListener}
*
* @see #getDragSourceListeners
* @see #getDragSourceMotionListeners
* @since 1.4
*/
public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
EventListener l = null;
if (listenerType == DragSourceListener.class) {
l = listener;
} else if (listenerType == DragSourceMotionListener.class) {
l = motionListener;
}
return DnDEventMulticaster.getListeners(l, listenerType);
}
/**
* This method calls {@code dragEnter} on the
* {@code DragSourceListener}s registered with this
* {@code DragSource}, and passes them the specified
* {@code DragSourceDragEvent}.
*
* @param dsde the {@code DragSourceDragEvent}
*/
void processDragEnter(DragSourceDragEvent dsde) {
DragSourceListener dsl = listener;
if (dsl != null) {
dsl.dragEnter(dsde);
}
}
/**
* This method calls {@code dragOver} on the
* {@code DragSourceListener}s registered with this
* {@code DragSource}, and passes them the specified
* {@code DragSourceDragEvent}.
*
* @param dsde the {@code DragSourceDragEvent}
*/
void processDragOver(DragSourceDragEvent dsde) {
DragSourceListener dsl = listener;
if (dsl != null) {
dsl.dragOver(dsde);
}
}
/**
* This method calls {@code dropActionChanged} on the
* {@code DragSourceListener}s registered with this
* {@code DragSource}, and passes them the specified
* {@code DragSourceDragEvent}.
*
* @param dsde the {@code DragSourceDragEvent}
*/
void processDropActionChanged(DragSourceDragEvent dsde) {
DragSourceListener dsl = listener;
if (dsl != null) {
dsl.dropActionChanged(dsde);
}
}
/**
* This method calls {@code dragExit} on the
* {@code DragSourceListener}s registered with this
* {@code DragSource}, and passes them the specified
* {@code DragSourceEvent}.
*
* @param dse the {@code DragSourceEvent}
*/
void processDragExit(DragSourceEvent dse) {
DragSourceListener dsl = listener;
if (dsl != null) {
dsl.dragExit(dse);
}
}
/**
* This method calls {@code dragDropEnd} on the
* {@code DragSourceListener}s registered with this
* {@code DragSource}, and passes them the specified
* {@code DragSourceDropEvent}.
*
* @param dsde the {@code DragSourceEvent}
*/
void processDragDropEnd(DragSourceDropEvent dsde) {
DragSourceListener dsl = listener;
if (dsl != null) {
dsl.dragDropEnd(dsde);
}
}
/**
* This method calls {@code dragMouseMoved} on the
* {@code DragSourceMotionListener}s registered with this
* {@code DragSource}, and passes them the specified
* {@code DragSourceDragEvent}.
*
* @param dsde the {@code DragSourceEvent}
*/
void processDragMouseMoved(DragSourceDragEvent dsde) {
DragSourceMotionListener dsml = motionListener;
if (dsml != null) {
dsml.dragMouseMoved(dsde);
}
}
/**
* Serializes this {@code DragSource}. This method first performs
* default serialization. Next, it writes out this object's
* {@code FlavorMap} if and only if it can be serialized. If not,
* {@code null} is written instead. Next, it writes out
* {@code Serializable} listeners registered with this
* object. Listeners are written in a {@code null}-terminated sequence
* of 0 or more pairs. The pair consists of a {@code String} and an
* {@code Object}; the {@code String} indicates the type of the
* {@code Object} and is one of the following:
* <ul>
* <li>{@code dragSourceListenerK} indicating a
* {@code DragSourceListener} object;
* <li>{@code dragSourceMotionListenerK} indicating a
* {@code DragSourceMotionListener} object.
* </ul>
*
* @param s the {@code ObjectOutputStream} to write
* @throws IOException if an I/O error occurs
* @serialData Either a {@code FlavorMap} instance, or
* {@code null}, followed by a {@code null}-terminated
* sequence of 0 or more pairs; the pair consists of a
* {@code String} and an {@code Object}; the
* {@code String} indicates the type of the {@code Object}
* and is one of the following:
* <ul>
* <li>{@code dragSourceListenerK} indicating a
* {@code DragSourceListener} object;
* <li>{@code dragSourceMotionListenerK} indicating a
* {@code DragSourceMotionListener} object.
* </ul>.
* @since 1.4
*/
@Serial
private void writeObject(ObjectOutputStream s) throws IOException {
s.defaultWriteObject();
s.writeObject(SerializationTester.test(flavorMap) ? flavorMap : null);
DnDEventMulticaster.save(s, dragSourceListenerK, listener);
DnDEventMulticaster.save(s, dragSourceMotionListenerK, motionListener);
s.writeObject(null);
}
/**
* Deserializes this {@code DragSource}. This method first performs
* default deserialization. Next, this object's {@code FlavorMap} is
* deserialized by using the next object in the stream.
* If the resulting {@code FlavorMap} is {@code null}, this
* object's {@code FlavorMap} is set to the default FlavorMap for
* this thread's {@code ClassLoader}.
* Next, this object's listeners are deserialized by reading a
* {@code null}-terminated sequence of 0 or more key/value pairs
* from the stream:
* <ul>
* <li>If a key object is a {@code String} equal to
* {@code dragSourceListenerK}, a {@code DragSourceListener} is
* deserialized using the corresponding value object and added to this
* {@code DragSource}.
* <li>If a key object is a {@code String} equal to
* {@code dragSourceMotionListenerK}, a
* {@code DragSourceMotionListener} is deserialized using the
* corresponding value object and added to this {@code DragSource}.
* <li>Otherwise, the key/value pair is skipped.
* </ul>
*
* @param s the {@code ObjectInputStream} to read
* @throws ClassNotFoundException if the class of a serialized object could
* not be found
* @throws IOException if an I/O error occurs
* @see java.awt.datatransfer.SystemFlavorMap#getDefaultFlavorMap
* @since 1.4
*/
@Serial
private void readObject(ObjectInputStream s)
throws ClassNotFoundException, IOException {
s.defaultReadObject();
// 'flavorMap' was written explicitly
flavorMap = (FlavorMap)s.readObject();
// Implementation assumes 'flavorMap' is never null.
if (flavorMap == null) {
flavorMap = SystemFlavorMap.getDefaultFlavorMap();
}
Object keyOrNull;
while (null != (keyOrNull = s.readObject())) {
String key = ((String)keyOrNull).intern();
if (dragSourceListenerK == key) {
addDragSourceListener((DragSourceListener)(s.readObject()));
} else if (dragSourceMotionListenerK == key) {
addDragSourceMotionListener(
(DragSourceMotionListener)(s.readObject()));
} else {
// skip value for unrecognized key
s.readObject();
}
}
}
/**
* Returns the drag gesture motion threshold. The drag gesture motion threshold
* defines the recommended behavior for {@link MouseDragGestureRecognizer}s.
* <p>
* If the system property {@code awt.dnd.drag.threshold} is set to
* a positive integer, this method returns the value of the system property;
* otherwise if a pertinent desktop property is available and supported by
* the implementation of the Java platform, this method returns the value of
* that property; otherwise this method returns some default value.
* The pertinent desktop property can be queried using
* {@code java.awt.Toolkit.getDesktopProperty("DnD.gestureMotionThreshold")}.
*
* @return the drag gesture motion threshold
* @see MouseDragGestureRecognizer
* @since 1.5
*/
public static int getDragThreshold() {
@SuppressWarnings("removal")
int ts = AccessController.doPrivileged(
new GetIntegerAction("awt.dnd.drag.threshold", 0)).intValue();
if (ts > 0) {
return ts;
} else {
Integer td = (Integer)Toolkit.getDefaultToolkit().
getDesktopProperty("DnD.gestureMotionThreshold");
if (td != null) {
return td.intValue();
}
}
return 5;
}
/*
* fields
*/
private transient FlavorMap flavorMap = SystemFlavorMap.getDefaultFlavorMap();
private transient DragSourceListener listener;
private transient DragSourceMotionListener motionListener;
}
|