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 / DragSourceContext.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  7.4 KB  |  298 lines

  1. /*
  2.  * @(#)DragSourceContext.java    1.13 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.AWTEvent;
  18. import java.awt.Component;
  19. import java.awt.Cursor;
  20. import java.awt.Image;
  21. import java.awt.Point;
  22.  
  23. import java.awt.datatransfer.DataFlavor;
  24. import java.awt.datatransfer.Transferable;
  25. import java.awt.datatransfer.UnsupportedFlavorException;
  26.  
  27. import java.awt.dnd.DragSource;
  28. import java.awt.dnd.DragSourceListener;
  29. import java.awt.dnd.InvalidDnDOperationException;
  30.  
  31. import java.awt.dnd.peer.DragSourceContextPeer;
  32.  
  33. import java.io.IOException;
  34.  
  35. import java.util.TooManyListenersException;
  36.  
  37. /**
  38.  * <p>
  39.  * The DragSourceContext class is responsible for managing the initiator side
  40.  * of the Drag and Drop protocol. In particular it is responsible for managing
  41.  * event notifications to the DragSourceListener, and providing the
  42.  * Transferable state to enable the data transfer.
  43.  * </p>
  44.  *
  45.  * @version 1.13
  46.  * @since JDK1.2
  47.  *
  48.  */
  49.  
  50. public class DragSourceContext implements DragSourceListener {
  51.  
  52.     // used by checkCurrentCursor
  53.  
  54.     protected static final int DEFAULT = 0;
  55.     protected static final int ENTER   = 1;
  56.     protected static final int OVER    = 2;
  57.     protected static final int CHANGED = 3;
  58.     
  59.     /**
  60.      * construct a DragSourceContext (called from DragSource)
  61.      *
  62.      * @param ds     The DragSource that originated this operation
  63.      * @param dscp     The DragSourceContextPeer for this operation
  64.      * @param a         The operation(s)
  65.      * @param dragCursor The initial Cursor
  66.      * @param dragImage  The image to drag (or null)
  67.      * @param offset     The offset of the image origin from the hotspot
  68.      *             at the instant of the triggering event
  69.      * @param t         The Transferable
  70.      * @param dsl     The DragSourceListener
  71.      *
  72.      * @throw IllegalArgumentException
  73.      * @throw NullPointerException
  74.      */
  75.  
  76.     public DragSourceContext(DragSource ds, DragSourceContextPeer dscp, Component c, int a, Cursor dragCursor, Image dragImage, Point offset, Transferable t, DragSourceListener dsl) {
  77.     if (ds == null) 
  78.         throw new NullPointerException("DragSource");
  79.  
  80.     if (dscp == null)
  81.         throw new NullPointerException("DragSourceContextPeer");
  82.  
  83.     if (c == null)
  84.         throw new NullPointerException("Component");
  85.  
  86.     if (a == DnDConstants.ACTION_NONE)
  87.         throw new IllegalArgumentException("actions == DnDConstants.ACTION_NONE");
  88.  
  89.     if (t == null)
  90.         throw new NullPointerException("Transferable");
  91.  
  92.     dragSource   = ds;
  93.     peer         = dscp;
  94.     component    = c;
  95.     actions      = a;
  96.     cursor         = dragCursor;
  97.     image        = dragImage;
  98.     offset       = offset;
  99.     transferable = t;
  100.     listener     = dsl;
  101.  
  102.     checkCurrentCursor(DnDConstants.ACTION_NONE, DEFAULT);
  103.     }
  104.  
  105.     /**
  106.      * @return the DragSource that instantiated this DragSourceContext
  107.      */
  108.  
  109.     public DragSource   getDragSource() { return dragSource; }
  110.  
  111.     /**
  112.      * @return the Component that started the Drag
  113.      */
  114.  
  115.     public Component    getComponent() { return component; }
  116.  
  117.     /**
  118.      * @return the Event that triggered the Drag
  119.      */
  120.  
  121.     public AWTEvent getTrigger() { return peer.getTrigger(); }
  122.  
  123.     /**
  124.      * cancel the operation
  125.      */
  126.  
  127.     public void cancelDrag() throws InvalidDnDOperationException {
  128.     peer.cancelDrag();
  129.     }
  130.  
  131.     /**
  132.      * @return the current actions
  133.      */
  134.  
  135.     public int  getSourceActions() {
  136.     return actions;
  137.     }
  138.  
  139.     /**
  140.      * change the drag cursor
  141.      */
  142.  
  143.     public void setCursor(Cursor c) {
  144.     if (cursor == null || !cursor.equals(c)) {
  145.         cursorDirty = true;
  146.         peer.setCursor(cursor = c);
  147.     }
  148.     }
  149.  
  150.     /**
  151.      * @return the current drag cursor
  152.      */
  153.  
  154.     public Cursor getCursor() { return cursor; }
  155.  
  156.     /**
  157.      * @return the drag image
  158.      */
  159.  
  160.     public Image getDragImage() { return image; }
  161.  
  162.     /**
  163.      * @return the image offset
  164.      */
  165.  
  166.     public Point getDragImageOffset() { return offset; }
  167.  
  168.     /**
  169.      * change the DragSourceListener
  170.      */
  171.  
  172.     public synchronized void addDragSourceListener(DragSourceListener dsl) throws TooManyListenersException {
  173.     if (listener != null)
  174.         throw new TooManyListenersException();
  175.     else
  176.         listener = dsl;
  177.     }
  178.  
  179.     /**
  180.      * change the DragSourceListener
  181.      */
  182.  
  183.     public synchronized void removeDragSourceListener(DragSourceListener dsl) {
  184.     if (listener != null && listener.equals(dsl)) {
  185.         listener = null;
  186.     } else
  187.         throw new IllegalArgumentException();
  188.     }
  189.  
  190.     /**
  191.      * intercept the dragEnter event from the peer
  192.      */
  193.  
  194.     public synchronized void dragEnter(DragSourceDragEvent dsde) {
  195.     if (listener != null) listener.dragEnter(dsde);
  196.  
  197.     checkCurrentCursor(dsde.getDropAction(), ENTER);
  198.     }
  199.  
  200.     /**
  201.      * intercept the dragOver event from the peer
  202.      */
  203.  
  204.     public synchronized void dragOver(DragSourceDragEvent dsde) {
  205.     if (listener != null) listener.dragOver(dsde);
  206.  
  207.     checkCurrentCursor(dsde.getDropAction(), OVER);
  208.     }
  209.  
  210.     /**
  211.      * intercept the dragExit event from the peer
  212.      */
  213.  
  214.     public synchronized void dragExit(DragSourceEvent dse) {
  215.     if (listener != null) listener.dragExit(dse);
  216.  
  217.     checkCurrentCursor(DnDConstants.ACTION_NONE, DEFAULT);
  218.     }
  219.  
  220.     /**
  221.      * intercept the dragGestureChanged event from the peer
  222.      */
  223.  
  224.     public synchronized void dropActionChanged(DragSourceDragEvent dsde) {
  225.     if (listener != null) listener.dropActionChanged(dsde);
  226.  
  227.     checkCurrentCursor(dsde.getDropAction(), CHANGED);
  228.     }
  229.  
  230.     /**
  231.      * intercept the dragDropEnd event from the peer
  232.      */
  233.  
  234.     public synchronized void dragDropEnd(DragSourceDropEvent dsde) {
  235.     if (listener != null) listener.dragDropEnd(dsde);
  236.     }
  237.  
  238.     public Transferable getTransferable() { return transferable; }
  239.    
  240.     /**
  241.      * check the cursor for updates and implement defaults check the cursor for updates and implement defaults
  242.      */
  243.  
  244.     protected void checkCurrentCursor(int target, int status) {
  245.     if (cursorDirty && cursor != null) {
  246.         cursorDirty = false;
  247.         return;
  248.     }
  249.  
  250.     Cursor c = null;
  251.  
  252.     switch (status) {
  253.         default:
  254.         target = DnDConstants.ACTION_NONE;
  255.         case ENTER:
  256.         case OVER:
  257.         case CHANGED:
  258.         int    ra = actions & target;
  259.  
  260.         if (ra == DnDConstants.ACTION_NONE) {
  261.             if ((actions & DnDConstants.ACTION_LINK) == DnDConstants.ACTION_LINK)
  262.                 c = DragSource.DefaultLinkNoDrop;
  263.             else if ((actions & DnDConstants.ACTION_MOVE) == DnDConstants.ACTION_MOVE)
  264.                 c = DragSource.DefaultMoveNoDrop;
  265.             else
  266.                 c = DragSource.DefaultCopyNoDrop;
  267.         } else {
  268.             if ((ra & DnDConstants.ACTION_LINK) == DnDConstants.ACTION_LINK)
  269.                 c = DragSource.DefaultLinkDrop;
  270.             else if ((ra & DnDConstants.ACTION_MOVE) == DnDConstants.ACTION_MOVE)
  271.                 c = DragSource.DefaultMoveDrop;
  272.             else
  273.                 c = DragSource.DefaultCopyDrop;
  274.         }
  275.     }
  276.  
  277.     setCursor(c);
  278.     cursorDirty = false;
  279.     }
  280.  
  281.     /*
  282.      * fields
  283.      */
  284.  
  285.     private DragSource              dragSource;
  286.     private DragSourceContextPeer peer;
  287.  
  288.     private Cursor          cursor;
  289.  
  290.     private Component          component;
  291.     private int                  actions;
  292.     private Image          image;
  293.     private Point          offset;
  294.     private Transferable      transferable;
  295.     private DragSourceListener    listener;
  296.     private boolean          cursorDirty = true;
  297. }
  298.