1 7 8 package java.awt.dnd; 9 10 import java.awt.AWTError ; 11 import java.awt.AWTException ; 12 import java.awt.event.InputEvent ; 13 import java.awt.AWTPermission ; 14 import java.awt.Component ; 15 import java.awt.Cursor ; 16 import java.awt.GraphicsEnvironment ; 17 import java.awt.HeadlessException ; 18 import java.awt.Image ; 19 import java.awt.Point ; 20 import java.awt.Toolkit ; 21 import java.awt.datatransfer.FlavorMap ; 22 import java.awt.datatransfer.SystemFlavorMap ; 23 import java.awt.datatransfer.Transferable ; 24 import java.awt.dnd.peer.DragSourceContextPeer; 25 import java.io.Serializable ; 26 import java.io.IOException ; 27 import java.io.ObjectInputStream ; 28 import java.io.ObjectOutputStream ; 29 import java.io.Serializable ; 30 import java.security.AccessController ; 31 import java.util.EventListener ; 32 import sun.awt.dnd.SunDragSourceContextPeer; 33 import sun.security.action.GetIntegerAction; 34 35 36 104 105 public class DragSource implements Serializable { 106 107 private static final long serialVersionUID = 6236096958971414066L; 108 109 112 113 private static Cursor load(String name) { 114 if (GraphicsEnvironment.isHeadless()) { 115 return null; 116 } 117 118 try { 119 return (Cursor )Toolkit.getDefaultToolkit().getDesktopProperty(name); 120 } catch (Exception e) { 121 e.printStackTrace(); 122 123 throw new RuntimeException ("failed to load system cursor: " + name + " : " + e.getMessage()); 124 } 125 } 126 127 128 135 public static final Cursor DefaultCopyDrop = 136 load("DnD.Cursor.CopyDrop"); 137 138 145 public static final Cursor DefaultMoveDrop = 146 load("DnD.Cursor.MoveDrop"); 147 148 155 public static final Cursor DefaultLinkDrop = 156 load("DnD.Cursor.LinkDrop"); 157 158 165 public static final Cursor DefaultCopyNoDrop = 166 load("DnD.Cursor.CopyNoDrop"); 167 168 175 public static final Cursor DefaultMoveNoDrop = 176 load("DnD.Cursor.MoveNoDrop"); 177 178 185 public static final Cursor DefaultLinkNoDrop = 186 load("DnD.Cursor.LinkNoDrop"); 187 188 private static final DragSource dflt = 189 (GraphicsEnvironment.isHeadless()) ? null : new DragSource (); 190 191 194 static final String dragSourceListenerK = "dragSourceL"; 195 static final String dragSourceMotionListenerK = "dragSourceMotionL"; 196 197 206 public static DragSource getDefaultDragSource() { 207 if (GraphicsEnvironment.isHeadless()) { 208 throw new HeadlessException (); 209 } else { 210 return dflt; 211 } 212 } 213 214 222 223 public static boolean isDragImageSupported() { 224 Toolkit t = Toolkit.getDefaultToolkit(); 225 226 Boolean supported; 227 228 try { 229 supported = (Boolean )Toolkit.getDefaultToolkit().getDesktopProperty("DnD.isDragImageSupported"); 230 231 return supported.booleanValue(); 232 } catch (Exception e) { 233 return false; 234 } 235 } 236 237 244 public DragSource() throws HeadlessException { 245 if (GraphicsEnvironment.isHeadless()) { 246 throw new HeadlessException (); 247 } 248 } 249 250 277 278 public void startDrag(DragGestureEvent trigger, 279 Cursor dragCursor, 280 Image dragImage, 281 Point imageOffset, 282 Transferable transferable, 283 DragSourceListener dsl, 284 FlavorMap flavorMap) throws InvalidDnDOperationException { 285 286 SunDragSourceContextPeer.setDragDropInProgress(true); 287 288 try { 289 if (flavorMap != null) this.flavorMap = flavorMap; 290 291 DragSourceContextPeer dscp = Toolkit.getDefaultToolkit().createDragSourceContextPeer(trigger); 292 293 DragSourceContext dsc = createDragSourceContext(dscp, 294 trigger, 295 dragCursor, 296 dragImage, 297 imageOffset, 298 transferable, 299 dsl 300 ); 301 302 if (dsc == null) { 303 throw new InvalidDnDOperationException (); 304 } 305 306 dscp.startDrag(dsc, dsc.getCursor(), dragImage, imageOffset); } catch (RuntimeException e) { 308 SunDragSourceContextPeer.setDragDropInProgress(false); 309 throw e; 310 } 311 } 312 313 335 336 public void startDrag(DragGestureEvent trigger, 337 Cursor dragCursor, 338 Transferable transferable, 339 DragSourceListener dsl, 340 FlavorMap flavorMap) throws InvalidDnDOperationException { 341 startDrag(trigger, dragCursor, null, null, transferable, dsl, flavorMap); 342 } 343 344 369 370 public void startDrag(DragGestureEvent trigger, 371 Cursor dragCursor, 372 Image dragImage, 373 Point dragOffset, 374 Transferable transferable, 375 DragSourceListener dsl) throws InvalidDnDOperationException { 376 startDrag(trigger, dragCursor, dragImage, dragOffset, transferable, dsl, null); 377 } 378 379 398 399 public void startDrag(DragGestureEvent trigger, 400 Cursor dragCursor, 401 Transferable transferable, 402 DragSourceListener dsl) throws InvalidDnDOperationException { 403 startDrag(trigger, dragCursor, null, null, transferable, dsl, null); 404 } 405 406 452 453 protected DragSourceContext createDragSourceContext(DragSourceContextPeer dscp, DragGestureEvent dgl, Cursor dragCursor, Image dragImage, Point imageOffset, Transferable t, DragSourceListener dsl) { 454 return new DragSourceContext (dscp, dgl, dragCursor, dragImage, imageOffset, t, dsl); 455 } 456 457 463 464 public FlavorMap getFlavorMap() { return flavorMap; } 465 466 486 487 public <T extends DragGestureRecognizer > T 488 createDragGestureRecognizer(Class <T> recognizerAbstractClass, 489 Component c, int actions, 490 DragGestureListener dgl) 491 { 492 return Toolkit.getDefaultToolkit().createDragGestureRecognizer(recognizerAbstractClass, this, c, actions, dgl); 493 } 494 495 496 518 519 public DragGestureRecognizer createDefaultDragGestureRecognizer(Component c, int actions, DragGestureListener dgl) { 520 return Toolkit.getDefaultToolkit().createDragGestureRecognizer(MouseDragGestureRecognizer .class, this, c, actions, dgl); 521 } 522 523 536 public void addDragSourceListener(DragSourceListener dsl) { 537 if (dsl != null) { 538 synchronized (this) { 539 listener = DnDEventMulticaster.add(listener, dsl); 540 } 541 } 542 } 543 544 559 public void removeDragSourceListener(DragSourceListener dsl) { 560 if (dsl != null) { 561 synchronized (this) { 562 listener = DnDEventMulticaster.remove(listener, dsl); 563 } 564 } 565 } 566 567 579 public DragSourceListener [] getDragSourceListeners() { 580 return (DragSourceListener [])getListeners(DragSourceListener .class); 581 } 582 583 596 public void addDragSourceMotionListener(DragSourceMotionListener dsml) { 597 if (dsml != null) { 598 synchronized (this) { 599 motionListener = DnDEventMulticaster.add(motionListener, dsml); 600 } 601 } 602 } 603 604 619 public void removeDragSourceMotionListener(DragSourceMotionListener dsml) { 620 if (dsml != null) { 621 synchronized (this) { 622 motionListener = DnDEventMulticaster.remove(motionListener, dsml); 623 } 624 } 625 } 626 627 639 public DragSourceMotionListener [] getDragSourceMotionListeners() { 640 return (DragSourceMotionListener []) 641 getListeners(DragSourceMotionListener .class); 642 } 643 644 665 public <T extends EventListener > T[] getListeners(Class <T> listenerType) { 666 EventListener l = null; 667 if (listenerType == DragSourceListener .class) { 668 l = listener; 669 } else if (listenerType == DragSourceMotionListener .class) { 670 l = motionListener; 671 } 672 return DnDEventMulticaster.getListeners(l, listenerType); 673 } 674 675 683 void processDragEnter(DragSourceDragEvent dsde) { 684 DragSourceListener dsl = listener; 685 if (dsl != null) { 686 dsl.dragEnter(dsde); 687 } 688 } 689 690 698 void processDragOver(DragSourceDragEvent dsde) { 699 DragSourceListener dsl = listener; 700 if (dsl != null) { 701 dsl.dragOver(dsde); 702 } 703 } 704 705 713 void processDropActionChanged(DragSourceDragEvent dsde) { 714 DragSourceListener dsl = listener; 715 if (dsl != null) { 716 dsl.dropActionChanged(dsde); 717 } 718 } 719 720 728 void processDragExit(DragSourceEvent dse) { 729 DragSourceListener dsl = listener; 730 if (dsl != null) { 731 dsl.dragExit(dse); 732 } 733 } 734 735 743 void processDragDropEnd(DragSourceDropEvent dsde) { 744 DragSourceListener dsl = listener; 745 if (dsl != null) { 746 dsl.dragDropEnd(dsde); 747 } 748 } 749 750 758 void processDragMouseMoved(DragSourceDragEvent dsde) { 759 DragSourceMotionListener dsml = motionListener; 760 if (dsml != null) { 761 dsml.dragMouseMoved(dsde); 762 } 763 } 764 765 796 private void writeObject(ObjectOutputStream s) throws IOException { 797 s.defaultWriteObject(); 798 799 s.writeObject(SerializationTester.test(flavorMap) ? flavorMap : null); 800 801 DnDEventMulticaster.save(s, dragSourceListenerK, listener); 802 DnDEventMulticaster.save(s, dragSourceMotionListenerK, motionListener); 803 s.writeObject(null); 804 } 805 806 831 private void readObject(ObjectInputStream s) 832 throws ClassNotFoundException , IOException { 833 s.defaultReadObject(); 834 835 flavorMap = (FlavorMap )s.readObject(); 837 838 if (flavorMap == null) { 840 flavorMap = SystemFlavorMap.getDefaultFlavorMap(); 841 } 842 843 Object keyOrNull; 844 while (null != (keyOrNull = s.readObject())) { 845 String key = ((String )keyOrNull).intern(); 846 847 if (dragSourceListenerK == key) { 848 addDragSourceListener((DragSourceListener )(s.readObject())); 849 } else if (dragSourceMotionListenerK == key) { 850 addDragSourceMotionListener( 851 (DragSourceMotionListener )(s.readObject())); 852 } else { 853 s.readObject(); 855 } 856 } 857 } 858 859 875 public static int getDragThreshold() { 876 int ts = ((Integer )AccessController.doPrivileged( 877 new GetIntegerAction("awt.dnd.drag.threshold", 0))).intValue(); 878 if (ts > 0) { 879 return ts; 880 } else { 881 Integer td = (Integer )Toolkit.getDefaultToolkit(). 882 getDesktopProperty("DnD.gestureMotionThreshold"); 883 if (td != null) { 884 return td.intValue(); 885 } 886 } 887 return 5; 888 } 889 890 893 894 private transient FlavorMap flavorMap = SystemFlavorMap.getDefaultFlavorMap(); 895 896 private transient DragSourceListener listener; 897 898 private transient DragSourceMotionListener motionListener; 899 } 900 | Popular Tags |