home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 12.7 KB | 514 lines |
- /*
- * @(#)DropTarget.java 1.16 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.util.TooManyListenersException;
-
- import java.io.Serializable;
-
- import java.awt.AWTEvent;
- import java.awt.AWTPermission;
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.Insets;
- import java.awt.Point;
- import java.awt.Rectangle;
- import java.awt.Toolkit;
-
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
-
- import java.awt.dnd.DnDConstants;
- import java.awt.dnd.DropTargetContext;
- import java.awt.dnd.DropTargetDragEvent;
- import java.awt.dnd.DropTargetDropEvent;
- import java.awt.dnd.DropTargetListener;
- import java.awt.dnd.FlavorMap;
- import java.awt.dnd.SystemFlavorMap;
- import java.awt.dnd.Autoscroll;
-
- import java.awt.peer.ComponentPeer;
- import java.awt.peer.LightweightPeer;
-
- import java.awt.dnd.peer.DropTargetPeer;
-
- import java.security.AccessController;
-
- /**
- * <p>
- * The DropTarget is associated with a Component, when that Component wishes
- * to accept Drops during Drag and Drop operations.
- * </p>
- *
- * @version 1.16
- * @since JDK1.2
- *
- */
-
- public class DropTarget implements DropTargetListener, Serializable {
-
- static final long serialVersionUID = -6283860791671019047L;
-
- /*
- * default FlavorMap for the system
- */
-
- static private final FlavorMap flavorMap = SystemFlavorMap.getDefaultFlavorMap();
-
- /**
- * Construct a DropTarget
- *
- * @param c The Component with which this DropTarget is associated
- * @param ops The default acceptable actions for this DropTarget
- * @param dtl The DropTargetListener for this DropTarget
- * @param act Is the DropTarget accepting drops.
- *
- * @throw java.lang.SecurityException
- */
-
- public DropTarget(Component c, int ops, DropTargetListener dtl, boolean act) {
- super();
-
- setDefaultActions(ops);
-
- if (dtl != null) try {
- addDropTargetListener(dtl);
- } catch (TooManyListenersException tmle) {
- // do nothing!
- }
-
- if (c != null) c.setDropTarget(this);
-
- setActive(act);
- }
-
- /**
- * Construct a DropTarget
- *
- * @throw java.lang.SecurityException
- */
-
- public DropTarget() throws SecurityException {
- this(null, DnDConstants.ACTION_COPY_OR_MOVE, null, true);
- }
-
- /**
- * Construct a DropTarget
- *
- * @param c The Component with which this DropTarget is associated
- * @param dtl The DropTargetListener for this DropTarget
- *
- * @throw java.lang.SecurityException
- */
-
- public DropTarget(Component c, DropTargetListener dtl) throws SecurityException {
- this(c, DnDConstants.ACTION_COPY_OR_MOVE, dtl, true);
- }
-
- /**
- * Construct a DropTarget
- *
- * @param c The Component with which this DropTarget is associated
- * @param ops The default acceptable actions for this DropTarget
- * @param dtl The DropTargetListener for this DropTarget
- *
- * @throw java.lang.SecurityException
- */
-
- public DropTarget(Component c, int ops, DropTargetListener dtl) throws SecurityException {
- this(c, ops, dtl, true);
- }
-
- /**
- * Note: this interface is required to permit the safe association
- * of a DropTarget with a Component in one of two ways, either:
- * <code> component.setDropTarget(droptarget); </code>
- * or <code> droptarget.setComponent(component); </code>
- *
- * The caller must have AWTPermission.setDropTarget to succeed.
- *
- * @param c The new Component this DropTarget is to be associated with.
- *
- * @throw SecurityException
- * @throw IllegalArgumentException
- * @throw UnsupportedOperationException
- */
-
- public synchronized void setComponent(Component c) {
- if (component == c || component != null && component.equals(c))
- return;
-
-
- /*
- * FIX THIS FOR BETA4
- */
-
- // java.security.AccessController.checkPermission(new AWTPermission("setDropTarget"));
-
- Component old;
- ComponentPeer oldPeer = null;
-
- if ((old = component) != null) {
- clearAutoscroll();
-
- component = null;
-
- if (componentPeer != null) {
- oldPeer = componentPeer;
- removeNotify(componentPeer);
- }
-
- old.setDropTarget(null);
-
- }
-
- if ((component = c) != null) try {
- c.setDropTarget(this);
- } catch (Exception e) { // undo the change
- if (old != null) {
- old.setDropTarget(this);
- addNotify(oldPeer);
- }
- }
- }
-
- /**
- * @return the current Component
- */
-
- public synchronized Component getComponent() {
- return component;
- }
-
- /**
- * Sets the default acceptable actions for this DropTarget
- *
- * @param ops the default actions.
- *
- * @see java.awt.dnd.DnDConstants
- */
-
- public synchronized void setDefaultActions(int ops) {
- actions = ops & (DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_REFERENCE);
-
- if (dropTargetContext != null) dropTargetContext.setTargetActions(actions);
-
- }
-
- /**
- * @return the current default actions
- */
-
- public synchronized int getDefaultActions() {
- return actions;
- }
-
- /**
- * set the DropTarget (in)active.
- *
- * @param isActive
- */
-
- public synchronized void setActive(boolean isActive) {
- if (isActive != active) {
- active = isActive;
- }
-
- if (!active) clearAutoscroll();
- }
-
- /**
- * @return is the DropTarget active?
- */
-
- public synchronized boolean isActive() {
- return active;
- }
-
- /**
- * Add a new DropTargetListener (UNICAST SOURCE)
- *
- * @param dte The new DropTargetListener
- *
- * @throw TooManyListenersExceptio
- */
-
- public synchronized void addDropTargetListener(DropTargetListener dte) throws TooManyListenersException {
- if (dte == null) return;
-
- if (dtListener == null)
- dtListener = dte;
- else
- throw new TooManyListenersException();
- }
-
- /**
- * Remove the current DropTargetListener (UNICAST SOURCE)
- *
- * @param dte the DropTargetListener to deregister.
- *
- * @throw IllegalArgumentException
- */
-
- public synchronized void removeDropTargetListener(DropTargetListener dte) {
- if (dte != null && dtListener != null) {
- if(dtListener.equals(dte))
- dtListener = null;
- else
- throw new IllegalArgumentException();
- }
- }
-
-
- /**
- * The DropTarget intercepts dragEnter() notifications before the
- * registered DropTargetListener gets them.
- */
-
- public synchronized void dragEnter(DropTargetDragEvent dtde) {
- if (!active) return;
-
- if (dtListener != null) {
- dtListener.dragEnter(dtde);
- } else
- dtde.getDropTargetContext().setTargetActions(DnDConstants.ACTION_NONE);
-
- initializeAutoscrolling(dtde.getLocation());
- }
-
- /**
- * The DropTarget intercepts dragOver() notifications before the
- * registered DropTargetListener gets them.
- */
-
- public synchronized void dragOver(DropTargetDragEvent dtde) {
- if (!active) return;
-
- if (dtListener != null && active) dtListener.dragOver(dtde);
-
- updateAutoscroll(dtde.getLocation());
- }
-
- /**
- * The DropTarget intercepts dropActionChanged() notifications before the
- * registered DropTargetListener gets them.
- */
-
- public void dropActionChanged(DropTargetDragEvent dtde) {
- if (!active) return;
-
- if (dtListener != null) dtListener.dropActionChanged(dtde);
-
- updateAutoscroll(dtde.getLocation());
- }
-
- /**
- * The DropTarget intercepts dragExit() notifications before the
- * registered DropTargetListener gets them.
- */
-
- public synchronized void dragExit(DropTargetEvent dte) {
- if (!active) return;
-
- if (dtListener != null && active) dtListener.dragExit(dte);
-
- clearAutoscroll();
- }
-
- /**
- * The DropTarget intercepts drop() notifications before the
- * registered DropTargetListener gets them.
- */
-
- public synchronized void drop(DropTargetDropEvent dtde) {
- if (dtListener != null && active)
- dtListener.drop(dtde);
- else { // we should'nt get here ...
- dtde.rejectDrop();
- }
- }
-
- /**
- * @return the FlavorMap for this DropTarget
- */
-
- public FlavorMap getFlavorMap() { return flavorMap; }
-
- /**
- * Notify the DropTarget that it has been associated with a Component
- *
- **********************************************************************
- * This method is usually called from java.awt.Component.addNotify() of
- * the Component associated with this DropTarget to notify the DropTarget
- * that a ComponentPeer has been associated with that Component.
- *
- * Calling this method, other than to notify this DropTarget of the
- * association of the ComponentPeer with the Component may result in
- * a malfunction of the DnD system.
- **********************************************************************
- *
- * @param peer The Peer of the Component we are associated with!
- *
- * @throw SecurityException
- */
-
- public void addNotify(ComponentPeer peer) throws SecurityException {
- /*
- * FIX THIS FOR BETA4
- */
-
- // java.security.AccessController.checkPermission(new AWTPermission("setDTarget"));
- if (peer == componentPeer) return;
-
- componentPeer = peer;
-
- for (Component c = component; peer instanceof LightweightPeer; c = c.getParent())
- peer = c.getPeer();
-
- try {
- ((DropTargetPeer)(nativePeer = peer)).addDropTarget(this);
- } catch (ClassCastException cce) {
- nativePeer = null;
- // throw new InvalidDnDOperationException("No Native Peer support");
- }
- }
-
- /**
- * Notify the DropTarget that it has been disassociated from a Component
- *
- **********************************************************************
- * This method is usually called from java.awt.Component.removeNotify() of
- * the Component associated with this DropTarget to notify the DropTarget
- * that a ComponentPeer has been disassociated with that Component.
- *
- * Calling this method, other than to notify this DropTarget of the
- * disassociation of the ComponentPeer from the Component may result in
- * a malfunction of the DnD system.
- **********************************************************************
- *
- * @param peer The Peer of the Component we are being disassociated froe!
- */
-
- public void removeNotify(ComponentPeer peer) {
- if (nativePeer != null)
- ((DropTargetPeer)nativePeer).removeDropTarget(this);
-
- componentPeer = nativePeer = null;
- }
-
- /**
- * @return the DropTargetContext associated with this DropTarget.
- */
-
- public DropTargetContext getDropTargetContext() {
- if (dropTargetContext == null) dropTargetContext = createDropTargetContext();
-
- return dropTargetContext;
- }
-
- /**
- * Create the DropTargetContext associated with this DropTarget.
- * Subclasses may override this method to instantiate their own
- * DropTargetContext subclass.
- *
- * This call is typically *only* called by the platform's
- * DropTargetContextPeer as a drag operation encounters this
- * DropTarget. Accessing the Context while no Drag is current
- * has undefined results.
- *
- * @param owner the owner of the DropTargetContext
- */
-
- protected DropTargetContext createDropTargetContext() {
- return new DropTargetContext(this);
- }
-
- /*********************************************************************/
-
- /*********************************************************************/
-
- /**
- * initialize autoscrolling
- */
-
- protected void initializeAutoscrolling(Point p) {
- if (component == null || !(component instanceof Autoscroll)) return;
-
- // TODO
- }
-
- /**
- * update autoscrolling with current cursor locn
- */
-
- protected void updateAutoscroll(Point dragCursorLocn) {
- // TODO
- }
-
- /**
- * clear autoscrolling
- */
-
- protected void clearAutoscroll() {
- // TODO
- }
-
- /**
- * The DropTargetContext associated with this DropTarget
- */
-
- private transient DropTargetContext dropTargetContext;
-
- /**
- * The Component associated with this DropTarget
- */
-
- private Component component;
-
- /**
- * That Component's Peer
- */
-
- private transient ComponentPeer componentPeer;
-
- /**
- * That Component's "native" Peer
- */
-
- private transient ComponentPeer nativePeer;
-
-
- /**
- * Default permissable actions supported by this DropTarget
- *
- * @see #setDefaultActions
- * @see #getDefaultActions
- */
-
- int actions = DnDConstants.ACTION_COPY_OR_MOVE;
-
- /**
- * Is the Target accepting DND ops ...
- */
-
- boolean active = true;
-
- /**
- * The delegate
- */
-
- private DropTargetListener dtListener;
- }
-