1 /* 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.awt.dnd; 27 28 import java.awt.Component; 29 import java.awt.Cursor; 30 import java.awt.GraphicsEnvironment; 31 import java.awt.HeadlessException; 32 import java.awt.Image; 33 import java.awt.Point; 34 import java.awt.Toolkit; 35 import java.awt.datatransfer.FlavorMap; 36 import java.awt.datatransfer.SystemFlavorMap; 37 import java.awt.datatransfer.Transferable; 38 import java.awt.dnd.peer.DragSourceContextPeer; 39 import java.io.IOException; 40 import java.io.ObjectInputStream; 41 import java.io.ObjectOutputStream; 42 import java.io.Serializable; 43 import java.security.AccessController; 44 import java.util.EventListener; 45 import sun.awt.dnd.SunDragSourceContextPeer; 46 import sun.security.action.GetIntegerAction; 47 48 49 /** 50 * The <code>DragSource</code> is the entity responsible 51 * for the initiation of the Drag 52 * and Drop operation, and may be used in a number of scenarios: 53 * <UL> 54 * <LI>1 default instance per JVM for the lifetime of that JVM. 55 * <LI>1 instance per class of potential Drag Initiator object (e.g 56 * TextField). [implementation dependent] 57 * <LI>1 per instance of a particular 58 * <code>Component</code>, or application specific 59 * object associated with a <code>Component</code> 60 * instance in the GUI. [implementation dependent] 61 * <LI>Some other arbitrary association. [implementation dependent] 62 *</UL> 63 * 64 * Once the <code>DragSource</code> is 286 * @throws java.awt.dnd.InvalidDnDOperationException 287 * if the Drag and Drop 288 * system is unable to initiate a drag operation, or if the user 289 * attempts to start a drag while an existing drag operation 290 * is still executing 291 */ 292 293 public void startDrag(DragGestureEvent trigger, 294 Cursor dragCursor, 295 Image dragImage, 296 Point imageOffset, 297 Transferable transferable, 298 DragSourceListener dsl, 299 FlavorMap flavorMap) throws InvalidDnDOperationException { 300 301 SunDragSourceContextPeer.setDragDropInProgress(true); 302 303 try { 304 if (flavorMap != null) this.flavorMap = flavorMap; 305 306 DragSourceContextPeer dscp = Toolkit.getDefaultToolkit().createDragSourceContextPeer(trigger); 307 308 DragSourceContext dsc = createDragSourceContext(dscp, 309 trigger, 310 dragCursor, 311 dragImage, 312 imageOffset, 313 transferable, 314 dsl 315 ); 316 317 if (dsc == null) { 318 throw new InvalidDnDOperationException(); 319 } 320 321 dscp.startDrag(dsc, dsc.getCursor(), dragImage, imageOffset); // may throw 322 } catch (RuntimeException e) { 323 SunDragSourceContextPeer.setDragDropInProgress(false); 324 throw e; 325 } 326 } 327 328 /** 329 * Start a drag, given the <code>DragGestureEvent</code> 330 * that initiated the drag, the initial 331 * <code>Cursor</code> to use, 332 * the <code>Transferable</code> subject data 333 * of the drag, the <code>DragSourceListener</code>, 334 * and the <code>FlavorMap</code>. 335 * 336 * @param trigger the <code>DragGestureEvent</code> that 337 * initiated the drag 338 * @param dragCursor the initial {@code Cursor} for this drag operation 339 * or {@code null} for the default cursor handling; 340 * see <a href="DragSourceContext.html#defaultCursor">DragSourceContext</a> 341 * for more details on the cursor handling mechanism during drag and drop 425 DragSourceListener dsl) throws InvalidDnDOperationException { 426 startDrag(trigger, dragCursor, null, null, transferable, dsl, null); 427 } 428 429 /** 430 * Creates the {@code DragSourceContext} to handle the current drag 431 * operation. 432 * <p> 433 * To incorporate a new <code>DragSourceContext</code> 434 * subclass, subclass <code>DragSource</code> and 435 * override this method. 436 * <p> 437 * If <code>dragImage</code> is <code>null</code>, no image is used 438 * to represent the drag over feedback for this drag operation, but 439 * <code>NullPointerException</code> is not thrown. 440 * <p> 441 * If <code>dsl</code> is <code>null</code>, no drag source listener 442 * is registered with the created <code>DragSourceContext</code>, 443 * but <code>NullPointerException</code> is not thrown. 444 * 445 * @param dscp The <code>DragSourceContextPeer</code> for this drag 446 * @param dgl The <code>DragGestureEvent</code> that triggered the 447 * drag 448 * @param dragCursor The initial {@code Cursor} for this drag operation 449 * or {@code null} for the default cursor handling; 450 * see <a href="DragSourceContext.html#defaultCursor">DragSourceContext</a> class 451 * for more details on the cursor handling mechanism during drag and drop 452 * @param dragImage The <code>Image</code> to drag or <code>null</code> 453 * @param imageOffset The offset of the <code>Image</code> origin from the 454 * hotspot of the cursor at the instant of the trigger 455 * @param t The subject data of the drag 456 * @param dsl The <code>DragSourceListener</code> 457 * 458 * @return the <code>DragSourceContext</code> 459 * 460 * @throws NullPointerException if <code>dscp</code> is <code>null</code> 461 * @throws NullPointerException if <code>dgl</code> is <code>null</code> 462 * @throws NullPointerException if <code>dragImage</code> is not 463 * <code>null</code> and <code>imageOffset</code> is <code>null</code> 464 * @throws NullPointerException if <code>t</code> is <code>null</code> 465 * @throws IllegalArgumentException if the <code>Component</code> 466 * associated with the trigger event is <code>null</code>. 467 * @throws IllegalArgumentException if the <code>DragSource</code> for the 468 * trigger event is <code>null</code>. 469 * @throws IllegalArgumentException if the drag action for the 470 * trigger event is <code>DnDConstants.ACTION_NONE</code>. 471 * @throws IllegalArgumentException if the source actions for the 472 * <code>DragGestureRecognizer</code> associated with the trigger 473 * event are equal to <code>DnDConstants.ACTION_NONE</code>. 474 */ 475 476 protected DragSourceContext createDragSourceContext(DragSourceContextPeer dscp, DragGestureEvent dgl, Cursor dragCursor, Image dragImage, Point imageOffset, Transferable t, DragSourceListener dsl) { 477 return new DragSourceContext(dscp, dgl, dragCursor, dragImage, imageOffset, t, dsl); 478 } 479 480 /** 481 * This method returns the 482 * <code>FlavorMap</code> for this <code>DragSource</code>. 483 * 484 * @return the <code>FlavorMap</code> for this <code>DragSource</code> 485 */ 486 487 public FlavorMap getFlavorMap() { return flavorMap; } 488 489 /** 490 * Creates a new <code>DragGestureRecognizer</code> 491 * that implements the specified 492 * abstract subclass of 493 * <code>DragGestureRecognizer</code>, and 494 * sets the specified <code>Component</code> 495 * and <code>DragGestureListener</code> on 496 * the newly created object. 497 * | 1 /* 2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.awt.dnd; 27 28 import java.awt.Component; 29 import java.awt.Cursor; 30 import java.awt.GraphicsEnvironment; 31 import java.awt.HeadlessException; 32 import java.awt.Image; 33 import java.awt.Point; 34 import java.awt.Toolkit; 35 import java.awt.datatransfer.FlavorMap; 36 import java.awt.datatransfer.SystemFlavorMap; 37 import java.awt.datatransfer.Transferable; 38 import java.io.IOException; 39 import java.io.ObjectInputStream; 40 import java.io.ObjectOutputStream; 41 import java.io.Serializable; 42 import java.security.AccessController; 43 import java.util.EventListener; 44 45 import sun.awt.AWTAccessor; 46 import sun.awt.AWTAccessor.DragSourceContextAccessor; 47 import sun.awt.dnd.SunDragSourceContextPeer; 48 import sun.security.action.GetIntegerAction; 49 50 51 /** 52 * The <code>DragSource</code> is the entity responsible 53 * for the initiation of the Drag 54 * and Drop operation, and may be used in a number of scenarios: 55 * <UL> 56 * <LI>1 default instance per JVM for the lifetime of that JVM. 57 * <LI>1 instance per class of potential Drag Initiator object (e.g 58 * TextField). [implementation dependent] 59 * <LI>1 per instance of a particular 60 * <code>Component</code>, or application specific 61 * object associated with a <code>Component</code> 62 * instance in the GUI. [implementation dependent] 63 * <LI>Some other arbitrary association. [implementation dependent] 64 *</UL> 65 * 66 * Once the <code>DragSource</code> is 288 * @throws java.awt.dnd.InvalidDnDOperationException 289 * if the Drag and Drop 290 * system is unable to initiate a drag operation, or if the user 291 * attempts to start a drag while an existing drag operation 292 * is still executing 293 */ 294 295 public void startDrag(DragGestureEvent trigger, 296 Cursor dragCursor, 297 Image dragImage, 298 Point imageOffset, 299 Transferable transferable, 300 DragSourceListener dsl, 301 FlavorMap flavorMap) throws InvalidDnDOperationException { 302 303 SunDragSourceContextPeer.setDragDropInProgress(true); 304 305 try { 306 if (flavorMap != null) this.flavorMap = flavorMap; 307 308 DragSourceContext dsc = createDragSourceContext(trigger, dragCursor, 309 dragImage, 310 imageOffset, 311 transferable, dsl); 312 313 if (dsc == null) { 314 throw new InvalidDnDOperationException(); 315 } 316 DragSourceContextAccessor acc = AWTAccessor.getDragSourceContextAccessor(); 317 acc.getPeer(dsc).startDrag(dsc, dsc.getCursor(), dragImage, imageOffset); // may throw 318 } catch (RuntimeException e) { 319 SunDragSourceContextPeer.setDragDropInProgress(false); 320 throw e; 321 } 322 } 323 324 /** 325 * Start a drag, given the <code>DragGestureEvent</code> 326 * that initiated the drag, the initial 327 * <code>Cursor</code> to use, 328 * the <code>Transferable</code> subject data 329 * of the drag, the <code>DragSourceListener</code>, 330 * and the <code>FlavorMap</code>. 331 * 332 * @param trigger the <code>DragGestureEvent</code> that 333 * initiated the drag 334 * @param dragCursor the initial {@code Cursor} for this drag operation 335 * or {@code null} for the default cursor handling; 336 * see <a href="DragSourceContext.html#defaultCursor">DragSourceContext</a> 337 * for more details on the cursor handling mechanism during drag and drop 421 DragSourceListener dsl) throws InvalidDnDOperationException { 422 startDrag(trigger, dragCursor, null, null, transferable, dsl, null); 423 } 424 425 /** 426 * Creates the {@code DragSourceContext} to handle the current drag 427 * operation. 428 * <p> 429 * To incorporate a new <code>DragSourceContext</code> 430 * subclass, subclass <code>DragSource</code> and 431 * override this method. 432 * <p> 433 * If <code>dragImage</code> is <code>null</code>, no image is used 434 * to represent the drag over feedback for this drag operation, but 435 * <code>NullPointerException</code> is not thrown. 436 * <p> 437 * If <code>dsl</code> is <code>null</code>, no drag source listener 438 * is registered with the created <code>DragSourceContext</code>, 439 * but <code>NullPointerException</code> is not thrown. 440 * 441 * @param dgl The <code>DragGestureEvent</code> that triggered the 442 * drag 443 * @param dragCursor The initial {@code Cursor} for this drag operation 444 * or {@code null} for the default cursor handling; 445 * see <a href="DragSourceContext.html#defaultCursor">DragSourceContext</a> class 446 * for more details on the cursor handling mechanism during drag and drop 447 * @param dragImage The <code>Image</code> to drag or <code>null</code> 448 * @param imageOffset The offset of the <code>Image</code> origin from the 449 * hotspot of the cursor at the instant of the trigger 450 * @param t The subject data of the drag 451 * @param dsl The <code>DragSourceListener</code> 452 * 453 * @return the <code>DragSourceContext</code> 454 * 455 * @throws NullPointerException if <code>dscp</code> is <code>null</code> 456 * @throws NullPointerException if <code>dgl</code> is <code>null</code> 457 * @throws NullPointerException if <code>dragImage</code> is not 458 * <code>null</code> and <code>imageOffset</code> is <code>null</code> 459 * @throws NullPointerException if <code>t</code> is <code>null</code> 460 * @throws IllegalArgumentException if the <code>Component</code> 461 * associated with the trigger event is <code>null</code>. 462 * @throws IllegalArgumentException if the <code>DragSource</code> for the 463 * trigger event is <code>null</code>. 464 * @throws IllegalArgumentException if the drag action for the 465 * trigger event is <code>DnDConstants.ACTION_NONE</code>. 466 * @throws IllegalArgumentException if the source actions for the 467 * <code>DragGestureRecognizer</code> associated with the trigger 468 * event are equal to <code>DnDConstants.ACTION_NONE</code>. 469 */ 470 471 protected DragSourceContext createDragSourceContext(DragGestureEvent dgl, 472 Cursor dragCursor, 473 Image dragImage, 474 Point imageOffset, 475 Transferable t, 476 DragSourceListener dsl) { 477 return new DragSourceContext(dgl, dragCursor, dragImage, imageOffset, t, dsl); 478 } 479 480 /** 481 * This method returns the 482 * <code>FlavorMap</code> for this <code>DragSource</code>. 483 * 484 * @return the <code>FlavorMap</code> for this <code>DragSource</code> 485 */ 486 487 public FlavorMap getFlavorMap() { return flavorMap; } 488 489 /** 490 * Creates a new <code>DragGestureRecognizer</code> 491 * that implements the specified 492 * abstract subclass of 493 * <code>DragGestureRecognizer</code>, and 494 * sets the specified <code>Component</code> 495 * and <code>DragGestureListener</code> on 496 * the newly created object. 497 * |