home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / dnd / DragSource.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  5.6 KB  |  198 lines

  1. /*
  2.  * @(#)DragSource.java    1.12 98/03/18
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt.dnd;
  16.  
  17. import java.awt.AWTError;
  18. import java.awt.AWTException;
  19. import java.awt.AWTEvent;
  20. import java.awt.AWTPermission;
  21. import java.awt.Component;
  22. import java.awt.Cursor;
  23. import java.awt.Image;
  24. import java.awt.Point;
  25. import java.awt.Toolkit;
  26.  
  27. import java.awt.datatransfer.Transferable;
  28.  
  29. import java.awt.dnd.DnDConstants;
  30. import java.awt.dnd.DragSourceContext;
  31. import java.awt.dnd.DragSourceListener;
  32. import java.awt.dnd.FlavorMap;
  33. import java.awt.dnd.SystemFlavorMap;
  34. import java.awt.dnd.InvalidDnDOperationException;
  35.  
  36. import java.awt.dnd.peer.DragSourceContextPeer;
  37.  
  38. import java.security.AccessController;
  39.  
  40. /**
  41.  * <p>
  42.  * The DragSource class is a small class responsible for originating a
  43.  * Drag and Drop operation.
  44.  * </p>
  45.  *
  46.  * @version 1.12
  47.  * @since JDK1.2
  48.  *
  49.  */
  50.  
  51. public class DragSource {
  52.  
  53.     /*
  54.      * load a default cursor
  55.      */
  56.  
  57.     private static Cursor load(String name) {
  58.     try {
  59.         return (Cursor)Toolkit.getDefaultToolkit().getDesktopProperty(name);
  60.     } catch (Exception e) {
  61.         e.printStackTrace();
  62.  
  63.         throw new RuntimeException("failed to load system cursor: " + name + " : " + e.getMessage());
  64.     }
  65.     }
  66.  
  67.     /**
  68.      * Default Cursor Constants
  69.      */
  70.  
  71.     public static final Cursor DefaultCopyDrop = load("DnD.Cursor.CopyDrop");
  72.     public static final Cursor DefaultMoveDrop = load("DnD.Cursor.MoveDrop");
  73.     public static final Cursor DefaultLinkDrop = load("DnD.Cursor.LinkDrop");
  74.  
  75.     public static final Cursor DefaultCopyNoDrop = load("DnD.Cursor.CopyNoDrop");
  76.     public static final Cursor DefaultMoveNoDrop = load("DnD.Cursor.MoveNoDrop");
  77.     public static final Cursor DefaultLinkNoDrop = load("DnD.Cursor.LinkNoDrop");
  78.  
  79.     /*
  80.      * The System FlavorMap
  81.      */
  82.  
  83.     private static final FlavorMap flavorMap = SystemFlavorMap.getDefaultFlavorMap();
  84.  
  85.     private static DragSource dflt;
  86.  
  87.     /**
  88.      * @return the platform DragSource
  89.      */
  90.  
  91.     public static DragSource getDefaultDragSource() {
  92.     if (dflt == null) dflt = new DragSource();
  93.  
  94.     return dflt;
  95.     }
  96.  
  97.     /**
  98.      * @return if the Drag Image support is available on this platform
  99.      */
  100.  
  101.     public static boolean isDragImageSupported() {
  102.     Toolkit t = Toolkit.getDefaultToolkit();
  103.  
  104.     Boolean supported;
  105.  
  106.     try {
  107.         supported = (Boolean)Toolkit.getDefaultToolkit().getDesktopProperty("DnD.isDragImageSupported");
  108.  
  109.         return supported.booleanValue();
  110.     } catch (Exception e) {
  111.         return false;
  112.     }
  113.     }
  114.  
  115.     /**
  116.      * construct a DragSource
  117.      */
  118.  
  119.     public DragSource() { super(); }
  120.  
  121.     /**
  122.      * start a Drag operation.
  123.      *
  124.      * @param c            The Component the Drag trigger occurred in
  125.      * @param trigger        The AWTEvent that initiated the operation
  126.      * @param actions        The drag "verbs" appropriate
  127.      * @param dragCursor    The initial cursor or null for defaults
  128.      * @param dragImage        The image to drag or null
  129.      * @param imageOffset    The offset of the image origin from the hotspot
  130.      *                of the cursor at the instant of the trigger
  131.      * @param transferable    The subject data of the operation
  132.      * @param dsl        The DragSourceListener
  133.      *
  134.      * Caller must have AWTPermission startDrag to succeed.
  135.      *
  136.      * @throw java.awt.dnd.InvalidDnDOperationException
  137.      * @throw java.lang.SecurityException
  138.      */
  139.  
  140.     public void startDrag(Component         c,
  141.                   AWTEvent         trigger,
  142.               int             actions,
  143.               Cursor         dragCursor,
  144.               Image             dragImage,
  145.               Point             imageOffset,
  146.               Transferable         transferable,
  147.               DragSourceListener dsl) throws InvalidDnDOperationException, SecurityException {
  148.  
  149.     /*
  150.      * FIX THIS FOR BETA4
  151.      */
  152.  
  153.     // java.security.AccessController.checkPermission(new AWTPermission("startDrag"));
  154.  
  155.     DragSourceContextPeer dscp = Toolkit.getDefaultToolkit().createDragSourceContextPeer(this, c);
  156.     DragSourceContext     dsc = createDragSourceContext(dscp,
  157.                                 c,
  158.                                 actions,
  159.                                 dragCursor,
  160.                                 dragImage,
  161.                                 imageOffset,
  162.                                 transferable,
  163.                                 dsl
  164.                     );
  165.  
  166.     if (dsc == null) throw new InvalidDnDOperationException();
  167.                                 
  168.     dscp.startDrag(dsc, trigger, dsc.getCursor(), actions); // may throw
  169. }
  170.  
  171.     /**
  172.      * Create the DragSourceContext to handle this Drag.
  173.      * 
  174.      * To incorporate a new DragSourceContext subclass, subclass DragSource and
  175.      * override this method.
  176.      *
  177.      * @param dscp         The DragSourceContextPeer for this operation
  178.      * @param c            The Component the drag started in
  179.      * @param actions        The drag "verbs" appropriate
  180.      * @param dragCursor    The initial cursor
  181.      * @param dragImage        The image to drag or null
  182.      * @param imageOffset    The offset of the image origin from the hotspot
  183.      *                of the cursor at the instant of the trigger
  184.      * @param transferable    The subject data of the operation
  185.      * @param dsl        The DragSourceListener
  186.      */
  187.  
  188.     protected DragSourceContext createDragSourceContext(DragSourceContextPeer dscp, Component c, int actions, Cursor dragCursor, Image dragImage, Point imageOffset, Transferable t, DragSourceListener dsl) {
  189.     return new DragSourceContext(this, dscp, c, actions, dragCursor, dragImage, imageOffset, t, dsl);
  190.     }
  191.  
  192.     /**
  193.      * @return the FlavorMap for this DragSource
  194.      */
  195.  
  196.     public FlavorMap getFlavorMap() { return flavorMap; }
  197. }
  198.