home *** CD-ROM | disk | FTP | other *** search
Wrap
Java Source | 1998-03-20 | 7.4 KB | 298 lines
/* * @(#)DragSourceContext.java 1.13 98/03/18 * * Copyright 1997, 1998 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. */ package java.awt.dnd; import java.awt.AWTEvent; import java.awt.Component; import java.awt.Cursor; import java.awt.Image; import java.awt.Point; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.dnd.DragSource; import java.awt.dnd.DragSourceListener; import java.awt.dnd.InvalidDnDOperationException; import java.awt.dnd.peer.DragSourceContextPeer; import java.io.IOException; import java.util.TooManyListenersException; /** * <p> * The DragSourceContext class is responsible for managing the initiator side * of the Drag and Drop protocol. In particular it is responsible for managing * event notifications to the DragSourceListener, and providing the * Transferable state to enable the data transfer. * </p> * * @version 1.13 * @since JDK1.2 * */ public class DragSourceContext implements DragSourceListener { // used by checkCurrentCursor protected static final int DEFAULT = 0; protected static final int ENTER = 1; protected static final int OVER = 2; protected static final int CHANGED = 3; /** * construct a DragSourceContext (called from DragSource) * * @param ds The DragSource that originated this operation * @param dscp The DragSourceContextPeer for this operation * @param a The operation(s) * @param dragCursor The initial Cursor * @param dragImage The image to drag (or null) * @param offset The offset of the image origin from the hotspot * at the instant of the triggering event * @param t The Transferable * @param dsl The DragSourceListener * * @throw IllegalArgumentException * @throw NullPointerException */ public DragSourceContext(DragSource ds, DragSourceContextPeer dscp, Component c, int a, Cursor dragCursor, Image dragImage, Point offset, Transferable t, DragSourceListener dsl) { if (ds == null) throw new NullPointerException("DragSource"); if (dscp == null) throw new NullPointerException("DragSourceContextPeer"); if (c == null) throw new NullPointerException("Component"); if (a == DnDConstants.ACTION_NONE) throw new IllegalArgumentException("actions == DnDConstants.ACTION_NONE"); if (t == null) throw new NullPointerException("Transferable"); dragSource = ds; peer = dscp; component = c; actions = a; cursor = dragCursor; image = dragImage; offset = offset; transferable = t; listener = dsl; checkCurrentCursor(DnDConstants.ACTION_NONE, DEFAULT); } /** * @return the DragSource that instantiated this DragSourceContext */ public DragSource getDragSource() { return dragSource; } /** * @return the Component that started the Drag */ public Component getComponent() { return component; } /** * @return the Event that triggered the Drag */ public AWTEvent getTrigger() { return peer.getTrigger(); } /** * cancel the operation */ public void cancelDrag() throws InvalidDnDOperationException { peer.cancelDrag(); } /** * @return the current actions */ public int getSourceActions() { return actions; } /** * change the drag cursor */ public void setCursor(Cursor c) { if (cursor == null || !cursor.equals(c)) { cursorDirty = true; peer.setCursor(cursor = c); } } /** * @return the current drag cursor */ public Cursor getCursor() { return cursor; } /** * @return the drag image */ public Image getDragImage() { return image; } /** * @return the image offset */ public Point getDragImageOffset() { return offset; } /** * change the DragSourceListener */ public synchronized void addDragSourceListener(DragSourceListener dsl) throws TooManyListenersException { if (listener != null) throw new TooManyListenersException(); else listener = dsl; } /** * change the DragSourceListener */ public synchronized void removeDragSourceListener(DragSourceListener dsl) { if (listener != null && listener.equals(dsl)) { listener = null; } else throw new IllegalArgumentException(); } /** * intercept the dragEnter event from the peer */ public synchronized void dragEnter(DragSourceDragEvent dsde) { if (listener != null) listener.dragEnter(dsde); checkCurrentCursor(dsde.getDropAction(), ENTER); } /** * intercept the dragOver event from the peer */ public synchronized void dragOver(DragSourceDragEvent dsde) { if (listener != null) listener.dragOver(dsde); checkCurrentCursor(dsde.getDropAction(), OVER); } /** * intercept the dragExit event from the peer */ public synchronized void dragExit(DragSourceEvent dse) { if (listener != null) listener.dragExit(dse); checkCurrentCursor(DnDConstants.ACTION_NONE, DEFAULT); } /** * intercept the dragGestureChanged event from the peer */ public synchronized void dropActionChanged(DragSourceDragEvent dsde) { if (listener != null) listener.dropActionChanged(dsde); checkCurrentCursor(dsde.getDropAction(), CHANGED); } /** * intercept the dragDropEnd event from the peer */ public synchronized void dragDropEnd(DragSourceDropEvent dsde) { if (listener != null) listener.dragDropEnd(dsde); } public Transferable getTransferable() { return transferable; } /** * check the cursor for updates and implement defaults check the cursor for updates and implement defaults */ protected void checkCurrentCursor(int target, int status) { if (cursorDirty && cursor != null) { cursorDirty = false; return; } Cursor c = null; switch (status) { default: target = DnDConstants.ACTION_NONE; case ENTER: case OVER: case CHANGED: int ra = actions & target; if (ra == DnDConstants.ACTION_NONE) { if ((actions & DnDConstants.ACTION_LINK) == DnDConstants.ACTION_LINK) c = DragSource.DefaultLinkNoDrop; else if ((actions & DnDConstants.ACTION_MOVE) == DnDConstants.ACTION_MOVE) c = DragSource.DefaultMoveNoDrop; else c = DragSource.DefaultCopyNoDrop; } else { if ((ra & DnDConstants.ACTION_LINK) == DnDConstants.ACTION_LINK) c = DragSource.DefaultLinkDrop; else if ((ra & DnDConstants.ACTION_MOVE) == DnDConstants.ACTION_MOVE) c = DragSource.DefaultMoveDrop; else c = DragSource.DefaultCopyDrop; } } setCursor(c); cursorDirty = false; } /* * fields */ private DragSource dragSource; private DragSourceContextPeer peer; private Cursor cursor; private Component component; private int actions; private Image image; private Point offset; private Transferable transferable; private DragSourceListener listener; private boolean cursorDirty = true; }