0N/A/*
2362N/A * Copyright (c) 1997, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.awt.dnd;
0N/A
0N/Aimport java.awt.Component;
0N/A
0N/Aimport java.awt.datatransfer.DataFlavor;
0N/Aimport java.awt.datatransfer.Transferable;
0N/Aimport java.awt.datatransfer.UnsupportedFlavorException;
0N/A
0N/Aimport java.awt.dnd.peer.DropTargetContextPeer;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.io.Serializable;
0N/A
0N/Aimport java.util.Arrays;
0N/Aimport java.util.List;
0N/A
0N/A
0N/A/**
0N/A * A <code>DropTargetContext</code> is created
0N/A * whenever the logical cursor associated
0N/A * with a Drag and Drop operation coincides with the visible geometry of
0N/A * a <code>Component</code> associated with a <code>DropTarget</code>.
0N/A * The <code>DropTargetContext</code> provides
0N/A * the mechanism for a potential receiver
0N/A * of a drop operation to both provide the end user with the appropriate
0N/A * drag under feedback, but also to effect the subsequent data transfer
0N/A * if appropriate.
0N/A *
0N/A * @since 1.2
0N/A */
0N/A
0N/Apublic class DropTargetContext implements Serializable {
0N/A
0N/A private static final long serialVersionUID = -634158968993743371L;
0N/A
0N/A /**
0N/A * Construct a <code>DropTargetContext</code>
0N/A * given a specified <code>DropTarget</code>.
0N/A * <P>
0N/A * @param dt the DropTarget to associate with
0N/A */
0N/A
0N/A DropTargetContext(DropTarget dt) {
0N/A super();
0N/A
0N/A dropTarget = dt;
0N/A }
0N/A
0N/A /**
0N/A * This method returns the <code>DropTarget</code> associated with this
0N/A * <code>DropTargetContext</code>.
0N/A * <P>
0N/A * @return the <code>DropTarget</code> associated with this <code>DropTargetContext</code>
0N/A */
0N/A
0N/A public DropTarget getDropTarget() { return dropTarget; }
0N/A
0N/A /**
0N/A * This method returns the <code>Component</code> associated with
0N/A * this <code>DropTargetContext</code>.
0N/A * <P>
0N/A * @return the Component associated with this Context
0N/A */
0N/A
0N/A public Component getComponent() { return dropTarget.getComponent(); }
0N/A
0N/A /**
0N/A * Called when associated with the <code>DropTargetContextPeer</code>.
0N/A * <P>
0N/A * @param dtcp the <code>DropTargetContextPeer</code>
0N/A */
0N/A
0N/A public void addNotify(DropTargetContextPeer dtcp) {
0N/A dropTargetContextPeer = dtcp;
0N/A }
0N/A
0N/A /**
0N/A * Called when disassociated with the <code>DropTargetContextPeer</code>.
0N/A */
0N/A
0N/A public void removeNotify() {
0N/A dropTargetContextPeer = null;
0N/A transferable = null;
0N/A }
0N/A
0N/A /**
0N/A * This method sets the current actions acceptable to
0N/A * this <code>DropTarget</code>.
0N/A * <P>
0N/A * @param actions an <code>int</code> representing the supported action(s)
0N/A */
0N/A
0N/A protected void setTargetActions(int actions) {
0N/A DropTargetContextPeer peer = getDropTargetContextPeer();
0N/A if (peer != null) {
0N/A synchronized (peer) {
0N/A peer.setTargetActions(actions);
0N/A getDropTarget().doSetDefaultActions(actions);
0N/A }
0N/A } else {
0N/A getDropTarget().doSetDefaultActions(actions);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * This method returns an <code>int</code> representing the
0N/A * current actions this <code>DropTarget</code> will accept.
0N/A * <P>
0N/A * @return the current actions acceptable to this <code>DropTarget</code>
0N/A */
0N/A
0N/A protected int getTargetActions() {
0N/A DropTargetContextPeer peer = getDropTargetContextPeer();
0N/A return ((peer != null)
0N/A ? peer.getTargetActions()
0N/A : dropTarget.getDefaultActions()
0N/A );
0N/A }
0N/A
0N/A /**
0N/A * This method signals that the drop is completed and
0N/A * if it was successful or not.
0N/A * <P>
0N/A * @param success true for success, false if not
0N/A * <P>
0N/A * @throws InvalidDnDOperationException if a drop is not outstanding/extant
0N/A */
0N/A
0N/A public void dropComplete(boolean success) throws InvalidDnDOperationException{
0N/A DropTargetContextPeer peer = getDropTargetContextPeer();
0N/A if (peer != null) {
0N/A peer.dropComplete(success);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * accept the Drag.
0N/A * <P>
0N/A * @param dragOperation the supported action(s)
0N/A */
0N/A
0N/A protected void acceptDrag(int dragOperation) {
0N/A DropTargetContextPeer peer = getDropTargetContextPeer();
0N/A if (peer != null) {
0N/A peer.acceptDrag(dragOperation);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * reject the Drag.
0N/A */
0N/A
0N/A protected void rejectDrag() {
0N/A DropTargetContextPeer peer = getDropTargetContextPeer();
0N/A if (peer != null) {
0N/A peer.rejectDrag();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * called to signal that the drop is acceptable
0N/A * using the specified operation.
0N/A * must be called during DropTargetListener.drop method invocation.
0N/A * <P>
0N/A * @param dropOperation the supported action(s)
0N/A */
0N/A
0N/A protected void acceptDrop(int dropOperation) {
0N/A DropTargetContextPeer peer = getDropTargetContextPeer();
0N/A if (peer != null) {
0N/A peer.acceptDrop(dropOperation);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * called to signal that the drop is unacceptable.
0N/A * must be called during DropTargetListener.drop method invocation.
0N/A */
0N/A
0N/A protected void rejectDrop() {
0N/A DropTargetContextPeer peer = getDropTargetContextPeer();
0N/A if (peer != null) {
0N/A peer.rejectDrop();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * get the available DataFlavors of the
0N/A * <code>Transferable</code> operand of this operation.
0N/A * <P>
0N/A * @return a <code>DataFlavor[]</code> containing the
0N/A * supported <code>DataFlavor</code>s of the
0N/A * <code>Transferable</code> operand.
0N/A */
0N/A
0N/A protected DataFlavor[] getCurrentDataFlavors() {
0N/A DropTargetContextPeer peer = getDropTargetContextPeer();
0N/A return peer != null ? peer.getTransferDataFlavors() : new DataFlavor[0];
0N/A }
0N/A
0N/A /**
0N/A * This method returns a the currently available DataFlavors
0N/A * of the <code>Transferable</code> operand
0N/A * as a <code>java.util.List</code>.
0N/A * <P>
0N/A * @return the currently available
0N/A * DataFlavors as a <code>java.util.List</code>
0N/A */
0N/A
0N/A protected List<DataFlavor> getCurrentDataFlavorsAsList() {
0N/A return Arrays.asList(getCurrentDataFlavors());
0N/A }
0N/A
0N/A /**
0N/A * This method returns a <code>boolean</code>
0N/A * indicating if the given <code>DataFlavor</code> is
0N/A * supported by this <code>DropTargetContext</code>.
0N/A * <P>
0N/A * @param df the <code>DataFlavor</code>
0N/A * <P>
0N/A * @return if the <code>DataFlavor</code> specified is supported
0N/A */
0N/A
0N/A protected boolean isDataFlavorSupported(DataFlavor df) {
0N/A return getCurrentDataFlavorsAsList().contains(df);
0N/A }
0N/A
0N/A /**
0N/A * get the Transferable (proxy) operand of this operation
0N/A * <P>
0N/A * @throws InvalidDnDOperationException if a drag is not outstanding/extant
0N/A * <P>
0N/A * @return the <code>Transferable</code>
0N/A */
0N/A
0N/A protected Transferable getTransferable() throws InvalidDnDOperationException {
0N/A DropTargetContextPeer peer = getDropTargetContextPeer();
0N/A if (peer == null) {
0N/A throw new InvalidDnDOperationException();
0N/A } else {
0N/A if (transferable == null) {
0N/A Transferable t = peer.getTransferable();
0N/A boolean isLocal = peer.isTransferableJVMLocal();
0N/A synchronized (this) {
0N/A if (transferable == null) {
0N/A transferable = createTransferableProxy(t, isLocal);
0N/A }
0N/A }
0N/A }
0N/A
0N/A return transferable;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get the <code>DropTargetContextPeer</code>
0N/A * <P>
0N/A * @return the platform peer
0N/A */
0N/A
0N/A DropTargetContextPeer getDropTargetContextPeer() {
0N/A return dropTargetContextPeer;
0N/A }
0N/A
0N/A /**
0N/A * Creates a TransferableProxy to proxy for the specified
0N/A * Transferable.
0N/A *
0N/A * @param t the <tt>Transferable</tt> to be proxied
0N/A * @param local <tt>true</tt> if <tt>t</tt> represents
0N/A * the result of a local drag-n-drop operation.
0N/A * @return the new <tt>TransferableProxy</tt> instance.
0N/A */
0N/A protected Transferable createTransferableProxy(Transferable t, boolean local) {
0N/A return new TransferableProxy(t, local);
0N/A }
0N/A
0N/A/****************************************************************************/
0N/A
0N/A
0N/A /**
0N/A * <code>TransferableProxy</code> is a helper inner class that implements
0N/A * <code>Transferable</code> interface and serves as a proxy for another
0N/A * <code>Transferable</code> object which represents data transfer for
0N/A * a particular drag-n-drop operation.
0N/A * <p>
0N/A * The proxy forwards all requests to the encapsulated transferable
0N/A * and automatically performs additional conversion on the data
0N/A * returned by the encapsulated transferable in case of local transfer.
0N/A */
0N/A
0N/A protected class TransferableProxy implements Transferable {
0N/A
0N/A /**
0N/A * Constructs a <code>TransferableProxy</code> given
0N/A * a specified <code>Transferable</code> object representing
0N/A * data transfer for a particular drag-n-drop operation and
0N/A * a <code>boolean</code> which indicates whether the
0N/A * drag-n-drop operation is local (within the same JVM).
0N/A * <p>
0N/A * @param t the <code>Transferable</code> object
0N/A * @param local <code>true</code>, if <code>t</code> represents
0N/A * the result of local drag-n-drop operation
0N/A */
0N/A TransferableProxy(Transferable t, boolean local) {
0N/A proxy = new sun.awt.datatransfer.TransferableProxy(t, local);
0N/A transferable = t;
0N/A isLocal = local;
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of DataFlavor objects indicating the flavors
0N/A * the data can be provided in by the encapsulated transferable.
0N/A * <p>
0N/A * @return an array of data flavors in which the data can be
0N/A * provided by the encapsulated transferable
0N/A */
0N/A public DataFlavor[] getTransferDataFlavors() {
0N/A return proxy.getTransferDataFlavors();
0N/A }
0N/A
0N/A /**
0N/A * Returns whether or not the specified data flavor is supported by
0N/A * the encapsulated transferable.
0N/A * @param flavor the requested flavor for the data
0N/A * @return <code>true</code> if the data flavor is supported,
0N/A * <code>false</code> otherwise
0N/A */
0N/A public boolean isDataFlavorSupported(DataFlavor flavor) {
0N/A return proxy.isDataFlavorSupported(flavor);
0N/A }
0N/A
0N/A /**
0N/A * Returns an object which represents the data provided by
0N/A * the encapsulated transferable for the requested data flavor.
0N/A * <p>
0N/A * In case of local transfer a serialized copy of the object
0N/A * returned by the encapsulated transferable is provided when
0N/A * the data is requested in application/x-java-serialized-object
0N/A * data flavor.
0N/A *
0N/A * @param df the requested flavor for the data
0N/A * @throws IOException if the data is no longer available
0N/A * in the requested flavor.
0N/A * @throws UnsupportedFlavorException if the requested data flavor is
0N/A * not supported.
0N/A */
0N/A public Object getTransferData(DataFlavor df)
0N/A throws UnsupportedFlavorException, IOException
0N/A {
0N/A return proxy.getTransferData(df);
0N/A }
0N/A
0N/A /*
0N/A * fields
0N/A */
0N/A
0N/A // We don't need to worry about client code changing the values of
0N/A // these variables. Since TransferableProxy is a protected class, only
0N/A // subclasses of DropTargetContext can access it. And DropTargetContext
0N/A // cannot be subclassed by client code because it does not have a
0N/A // public constructor.
0N/A
0N/A /**
0N/A * The encapsulated <code>Transferable</code> object.
0N/A */
0N/A protected Transferable transferable;
0N/A
0N/A /**
0N/A * A <code>boolean</code> indicating if the encapsulated
0N/A * <code>Transferable</code> object represents the result
0N/A * of local drag-n-drop operation (within the same JVM).
0N/A */
0N/A protected boolean isLocal;
0N/A
0N/A private sun.awt.datatransfer.TransferableProxy proxy;
0N/A }
0N/A
0N/A/****************************************************************************/
0N/A
0N/A /*
0N/A * fields
0N/A */
0N/A
0N/A /**
0N/A * The DropTarget associated with this DropTargetContext.
0N/A *
0N/A * @serial
0N/A */
0N/A private DropTarget dropTarget;
0N/A
0N/A private transient DropTargetContextPeer dropTargetContextPeer;
0N/A
0N/A private transient Transferable transferable;
0N/A}