📄 droptarget.java
字号:
/* * @(#)DropTarget.java 1.46 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package java.awt.dnd;import java.util.TooManyListenersException;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.awt.AWTEvent;import java.awt.Component;import java.awt.Dimension;import java.awt.GraphicsEnvironment;import java.awt.HeadlessException;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.datatransfer.FlavorMap;import java.awt.datatransfer.SystemFlavorMap;import javax.swing.Timer;import java.awt.peer.ComponentPeer;import java.awt.peer.LightweightPeer;import java.awt.dnd.peer.DropTargetPeer;/** * The <code>DropTarget</code> is associated * with a <code>Component</code> when that <code>Component</code> * wishes * to accept drops during Drag and Drop operations. * <P> * Each * <code>DropTarget</code> is associated with a <code>FlavorMap</code>. * The default <code>FlavorMap</code> hereafter designates the * <code>FlavorMap</code> returned by <code>SystemFlavorMap.getDefaultFlavorMap()</code>. * * @version 1.46, 01/23/03 * @since 1.2 */public class DropTarget implements DropTargetListener, Serializable { private static final long serialVersionUID = -6283860791671019047L; /** * Creates a new DropTarget given the <code>Component</code> * to associate itself with, an <code>int</code> representing * the default acceptable action(s) to * support, a <code>DropTargetListener</code> * to handle event processing, a <code>boolean</code> indicating * if the <code>DropTarget</code> is currently accepting drops, and * a <code>FlavorMap</code> to use (or null for the default <CODE>FlavorMap</CODE>). * <P> * The Component will receive drops only if it is enabled. * @param c The <code>Component</code> with which this <code>DropTarget</code> is associated * @param ops The default acceptable actions for this <code>DropTarget</code> * @param dtl The <code>DropTargetListener</code> for this <code>DropTarget</code> * @param act Is the <code>DropTarget</code> accepting drops. * @param fm The <code>FlavorMap</code> to use, or null for the default <CODE>FlavorMap</CODE> * @exception HeadlessException if GraphicsEnvironment.isHeadless() * returns true * @see java.awt.GraphicsEnvironment#isHeadless */ public DropTarget(Component c, int ops, DropTargetListener dtl, boolean act, FlavorMap fm) throws HeadlessException { if (GraphicsEnvironment.isHeadless()) { throw new HeadlessException(); } component = c; setDefaultActions(ops); if (dtl != null) try { addDropTargetListener(dtl); } catch (TooManyListenersException tmle) { // do nothing! } if (c != null) { c.setDropTarget(this); setActive(act); } if (fm != null) flavorMap = fm; } /** * Creates a <code>DropTarget</code> given the <code>Component</code> * to associate itself with, an <code>int</code> representing * the default acceptable action(s) * to support, a <code>DropTargetListener</code> * to handle event processing, and a <code>boolean</code> indicating * if the <code>DropTarget</code> is currently accepting drops. * <P> * The Component will receive drops only if it is enabled. * @param c The <code>Component</code> with which this <code>DropTarget</code> is associated * @param ops The default acceptable actions for this <code>DropTarget</code> * @param dtl The <code>DropTargetListener</code> for this <code>DropTarget</code> * @param act Is the <code>DropTarget</code> accepting drops. * @exception HeadlessException if GraphicsEnvironment.isHeadless() * returns true * @see java.awt.GraphicsEnvironment#isHeadless */ public DropTarget(Component c, int ops, DropTargetListener dtl, boolean act) throws HeadlessException { this(c, ops, dtl, act, null); } /** * Creates a <code>DropTarget</code>. * @exception HeadlessException if GraphicsEnvironment.isHeadless() * returns true * @see java.awt.GraphicsEnvironment#isHeadless */ public DropTarget() throws HeadlessException { this(null, DnDConstants.ACTION_COPY_OR_MOVE, null, true, null); } /** * Creates a <code>DropTarget</code> given the <code>Component</code> * to associate itself with, and the <code>DropTargetListener</code> * to handle event processing. * <P> * The Component will receive drops only if it is enabled. * @param c The <code>Component</code> with which this <code>DropTarget</code> is associated * @param dtl The <code>DropTargetListener</code> for this <code>DropTarget</code> * @exception HeadlessException if GraphicsEnvironment.isHeadless() * returns true * @see java.awt.GraphicsEnvironment#isHeadless */ public DropTarget(Component c, DropTargetListener dtl) throws HeadlessException { this(c, DnDConstants.ACTION_COPY_OR_MOVE, dtl, true, null); } /** * Creates a <code>DropTarget</code> given the <code>Component</code> * to associate itself with, an <code>int</code> representing * the default acceptable action(s) to support, and a * <code>DropTargetListener</code> to handle event processing. * <P> * The Component will receive drops only if it is enabled. * @param c The <code>Component</code> with which this <code>DropTarget</code> is associated * @param ops The default acceptable actions for this <code>DropTarget</code> * @param dtl The <code>DropTargetListener</code> for this <code>DropTarget</code> * @exception HeadlessException if GraphicsEnvironment.isHeadless() * returns true * @see java.awt.GraphicsEnvironment#isHeadless */ public DropTarget(Component c, int ops, DropTargetListener dtl) throws HeadlessException { 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> * <P> * The Component will receive drops only if it is enabled. * @param c The new <code>Component</code> this <code>DropTarget</code> * is to be associated with.<P> */ public synchronized void setComponent(Component c) { if (component == c || component != null && component.equals(c)) return; 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); } } } /** * Gets the <code>Component</code> associated * with this <code>DropTarget</code>. * <P> * @return the current </code>Component</code> */ public synchronized Component getComponent() { return component; } /** * Sets the default acceptable actions for this <code>DropTarget</code> * <P> * @param ops the default actions * <P> * @see java.awt.dnd.DnDConstants */ public void setDefaultActions(int ops) { getDropTargetContext().setTargetActions(ops & (DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_REFERENCE)); } /* * Called by DropTargetContext.setTargetActions() * with appropriate synchronization. */ void doSetDefaultActions(int ops) { actions = ops; } /** * Gets an <code>int</code> representing the * current action(s) supported by this <code>DropTarget</code>. * <P> * @return the current default actions */ public int getDefaultActions() { return actions; } /** * Sets the DropTarget active if <code>true</code>, * inactive if <code>false</code>. * <P> * @param isActive sets the <code>DropTarget</code> (in)active. */ public synchronized void setActive(boolean isActive) { if (isActive != active) { active = isActive; } if (!active) clearAutoscroll(); } /** * Reports whether or not * this <code>DropTarget</code> * is currently active (ready to accept drops). * <P> * @return <CODE>true</CODE> if active, <CODE>false</CODE> if not */ public boolean isActive() { return active; } /** * Adds a new <code>DropTargetListener</code> (UNICAST SOURCE). * <P> * @param dtl The new <code>DropTargetListener</code> * <P> * @throws <code>TooManyListenersException</code> if a * <code>DropTargetListener</code> is already added to this * <code>DropTarget</code>. */ public synchronized void addDropTargetListener(DropTargetListener dtl) throws TooManyListenersException { if (dtl == null) return; if (equals(dtl)) throw new IllegalArgumentException("DropTarget may not be its own Listener"); if (dtListener == null) dtListener = dtl; else throw new TooManyListenersException(); } /** * Removes the current <code>DropTargetListener</code> (UNICAST SOURCE). * <P> * @param dtl the DropTargetListener to deregister. */ public synchronized void removeDropTargetListener(DropTargetListener dtl) { if (dtl != null && dtListener != null) { if(dtListener.equals(dtl)) dtListener = null; else throw new IllegalArgumentException("listener mismatch"); } } /** * The <code>DropTarget</code> intercepts * dragEnter() notifications before the * registered <code>DropTargetListener</code> gets them. * <P> * @param dtde the <code>DropTargetDragEvent</code> */ 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 <code>DropTarget</code> * intercepts dragOver() notifications before the * registered <code>DropTargetListener</code> gets them. * <P> * @param dtde the <code>DropTargetDragEvent</code> */ public synchronized void dragOver(DropTargetDragEvent dtde) { if (!active) return; if (dtListener != null && active) dtListener.dragOver(dtde); updateAutoscroll(dtde.getLocation()); } /** * The <code>DropTarget</code> intercepts * dropActionChanged() notifications before the * registered <code>DropTargetListener</code> gets them. * <P> * @param dtde the DropTargetDragEvent */ public synchronized void dropActionChanged(DropTargetDragEvent dtde) { if (!active) return; if (dtListener != null) dtListener.dropActionChanged(dtde); updateAutoscroll(dtde.getLocation()); } /** * The <code>DropTarget</code> intercepts * dragExit() notifications before the * registered <code>DropTargetListener</code> gets them. * <P> * @param dte the <code>DropTargetEvent</code> */ public synchronized void dragExit(DropTargetEvent dte) { if (!active) return; if (dtListener != null && active) dtListener.dragExit(dte); clearAutoscroll(); } /** * The <code>DropTarget</code> intercepts drop() notifications before the * registered <code>DropTargetListener</code> gets them. * <P> * @param dtde the <code>DropTargetDropEvent</code> */ public synchronized void drop(DropTargetDropEvent dtde) { clearAutoscroll(); if (dtListener != null && active) dtListener.drop(dtde); else { // we should'nt get here ... dtde.rejectDrop(); } } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -