1 7 8 package java.awt.dnd; 9 10 import java.awt.event.InputEvent ; 11 import java.awt.Component ; 12 import java.awt.Cursor ; 13 import java.awt.Image ; 14 import java.awt.Point ; 15 16 import java.awt.datatransfer.DataFlavor ; 17 import java.awt.datatransfer.Transferable ; 18 import java.awt.datatransfer.UnsupportedFlavorException ; 19 20 import java.awt.dnd.DragGestureEvent ; 21 import java.awt.dnd.DragSource ; 22 import java.awt.dnd.DragSourceListener ; 23 import java.awt.dnd.InvalidDnDOperationException ; 24 25 import java.awt.dnd.peer.DragSourceContextPeer; 26 27 import java.io.IOException ; 28 import java.io.ObjectOutputStream ; 29 import java.io.ObjectInputStream ; 30 import java.io.Serializable ; 31 32 import java.util.TooManyListenersException ; 33 34 58 59 public class DragSourceContext 60 implements DragSourceListener , DragSourceMotionListener , Serializable { 61 62 private static final long serialVersionUID = -115407898692194719L; 63 64 66 71 protected static final int DEFAULT = 0; 72 73 78 protected static final int ENTER = 1; 79 80 85 protected static final int OVER = 2; 86 87 91 92 protected static final int CHANGED = 3; 93 94 147 public DragSourceContext(DragSourceContextPeer dscp, 148 DragGestureEvent trigger, Cursor dragCursor, 149 Image dragImage, Point offset, Transferable t, 150 DragSourceListener dsl) { 151 if (dscp == null) { 152 throw new NullPointerException ("DragSourceContextPeer"); 153 } 154 155 if (trigger == null) { 156 throw new NullPointerException ("Trigger"); 157 } 158 159 if (trigger.getDragSource() == null) { 160 throw new IllegalArgumentException ("DragSource"); 161 } 162 163 if (trigger.getComponent() == null) { 164 throw new IllegalArgumentException ("Component"); 165 } 166 167 if (trigger.getSourceAsDragGestureRecognizer().getSourceActions() == 168 DnDConstants.ACTION_NONE) { 169 throw new IllegalArgumentException ("source actions"); 170 } 171 172 if (trigger.getDragAction() == DnDConstants.ACTION_NONE) { 173 throw new IllegalArgumentException ("no drag action"); 174 } 175 176 if (t == null) { 177 throw new NullPointerException ("Transferable"); 178 } 179 180 if (dragImage != null && offset == null) { 181 throw new NullPointerException ("offset"); 182 } 183 184 peer = dscp; 185 this.trigger = trigger; 186 cursor = dragCursor; 187 transferable = t; 188 listener = dsl; 189 sourceActions = 190 trigger.getSourceAsDragGestureRecognizer().getSourceActions(); 191 192 useCustomCursor = (dragCursor != null); 193 194 updateCurrentCursor(trigger.getDragAction(), getSourceActions(), DEFAULT); 195 } 196 197 204 205 public DragSource getDragSource() { return trigger.getDragSource(); } 206 207 213 214 public Component getComponent() { return trigger.getComponent(); } 215 216 222 223 public DragGestureEvent getTrigger() { return trigger; } 224 225 232 public int getSourceActions() { 233 return sourceActions; 234 } 235 236 247 248 public synchronized void setCursor(Cursor c) { 249 useCustomCursor = (c != null); 250 setCursorImpl(c); 251 } 252 253 258 259 public Cursor getCursor() { return cursor; } 260 261 274 275 public synchronized void addDragSourceListener(DragSourceListener dsl) throws TooManyListenersException { 276 if (dsl == null) return; 277 278 if (equals(dsl)) throw new IllegalArgumentException ("DragSourceContext may not be its own listener"); 279 280 if (listener != null) 281 throw new TooManyListenersException (); 282 else 283 listener = dsl; 284 } 285 286 294 295 public synchronized void removeDragSourceListener(DragSourceListener dsl) { 296 if (listener != null && listener.equals(dsl)) { 297 listener = null; 298 } else 299 throw new IllegalArgumentException (); 300 } 301 302 306 307 public void transferablesFlavorsChanged() { 308 if (peer != null) peer.transferablesFlavorsChanged(); 309 } 310 311 320 public void dragEnter(DragSourceDragEvent dsde) { 321 DragSourceListener dsl = listener; 322 if (dsl != null) { 323 dsl.dragEnter(dsde); 324 } 325 getDragSource().processDragEnter(dsde); 326 327 updateCurrentCursor(dsde.getDropAction(), dsde.getTargetActions(), ENTER); 328 } 329 330 339 public void dragOver(DragSourceDragEvent dsde) { 340 DragSourceListener dsl = listener; 341 if (dsl != null) { 342 dsl.dragOver(dsde); 343 } 344 getDragSource().processDragOver(dsde); 345 346 updateCurrentCursor(dsde.getDropAction(), dsde.getTargetActions(), OVER); 347 } 348 349 358 public void dragExit(DragSourceEvent dse) { 359 DragSourceListener dsl = listener; 360 if (dsl != null) { 361 dsl.dragExit(dse); 362 } 363 getDragSource().processDragExit(dse); 364 365 updateCurrentCursor(DnDConstants.ACTION_NONE, DnDConstants.ACTION_NONE, DEFAULT); 366 } 367 368 377 public void dropActionChanged(DragSourceDragEvent dsde) { 378 DragSourceListener dsl = listener; 379 if (dsl != null) { 380 dsl.dropActionChanged(dsde); 381 } 382 getDragSource().processDropActionChanged(dsde); 383 384 updateCurrentCursor(dsde.getDropAction(), dsde.getTargetActions(), CHANGED); 385 } 386 387 396 public void dragDropEnd(DragSourceDropEvent dsde) { 397 DragSourceListener dsl = listener; 398 if (dsl != null) { 399 dsl.dragDropEnd(dsde); 400 } 401 getDragSource().processDragDropEnd(dsde); 402 } 403 404 414 public void dragMouseMoved(DragSourceDragEvent dsde) { 415 getDragSource().processDragMouseMoved(dsde); 416 } 417 418 424 public Transferable getTransferable() { return transferable; } 425 426 436 437 protected synchronized void updateCurrentCursor(int dropOp, int targetAct, int status) { 438 439 442 if (useCustomCursor) { 443 return; 444 } 445 446 448 Cursor c = null; 449 450 switch (status) { 451 default: 452 targetAct = DnDConstants.ACTION_NONE; 453 case ENTER: 454 case OVER: 455 case CHANGED: 456 int ra = dropOp & targetAct; 457 458 if (ra == DnDConstants.ACTION_NONE) { if ((dropOp & DnDConstants.ACTION_LINK) == DnDConstants.ACTION_LINK) 460 c = DragSource.DefaultLinkNoDrop; 461 else if ((dropOp & DnDConstants.ACTION_MOVE) == DnDConstants.ACTION_MOVE) 462 c = DragSource.DefaultMoveNoDrop; 463 else 464 c = DragSource.DefaultCopyNoDrop; 465 } else { if ((ra & DnDConstants.ACTION_LINK) == DnDConstants.ACTION_LINK) 467 c = DragSource.DefaultLinkDrop; 468 else if ((ra & DnDConstants.ACTION_MOVE) == DnDConstants.ACTION_MOVE) 469 c = DragSource.DefaultMoveDrop; 470 else 471 c = DragSource.DefaultCopyDrop; 472 } 473 } 474 475 setCursorImpl(c); 476 } 477 478 private void setCursorImpl(Cursor c) { 479 if (cursor == null || !cursor.equals(c)) { 480 cursor = c; 481 if (peer != null) peer.setCursor(cursor); 482 } 483 } 484 485 503 private void writeObject(ObjectOutputStream s) throws IOException { 504 s.defaultWriteObject(); 505 506 s.writeObject(SerializationTester.test(transferable) 507 ? transferable : null); 508 s.writeObject(SerializationTester.test(listener) 509 ? listener : null); 510 } 511 512 524 private void readObject(ObjectInputStream s) 525 throws ClassNotFoundException , IOException 526 { 527 s.defaultReadObject(); 528 529 transferable = (Transferable )s.readObject(); 530 listener = (DragSourceListener )s.readObject(); 531 532 if (transferable == null) { 534 if (emptyTransferable == null) { 535 emptyTransferable = new Transferable () { 536 public DataFlavor [] getTransferDataFlavors() { 537 return new DataFlavor [0]; 538 } 539 public boolean isDataFlavorSupported(DataFlavor flavor) 540 { 541 return false; 542 } 543 public Object getTransferData(DataFlavor flavor) 544 throws UnsupportedFlavorException 545 { 546 throw new UnsupportedFlavorException (flavor); 547 } 548 }; 549 } 550 transferable = emptyTransferable; 551 } 552 } 553 554 private static Transferable emptyTransferable; 555 556 559 560 private transient DragSourceContextPeer peer; 561 562 567 private DragGestureEvent trigger; 568 569 574 private Cursor cursor; 575 576 private transient Transferable transferable; 577 578 private transient DragSourceListener listener; 579 580 586 private boolean useCustomCursor; 587 588 595 private final int sourceActions; 596 } 597 | Popular Tags |