0N/A/*
3909N/A * Copyright (c) 2000, 2011, 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/Apackage javax.swing;
0N/A
0N/Aimport java.awt.*;
0N/Aimport java.awt.event.*;
0N/Aimport java.awt.datatransfer.*;
0N/Aimport java.awt.dnd.*;
0N/Aimport java.beans.*;
0N/Aimport java.lang.reflect.*;
0N/Aimport java.io.*;
0N/Aimport java.util.TooManyListenersException;
0N/Aimport javax.swing.plaf.UIResource;
0N/Aimport javax.swing.event.*;
0N/Aimport javax.swing.text.JTextComponent;
0N/A
0N/Aimport sun.reflect.misc.MethodUtil;
0N/Aimport sun.swing.SwingUtilities2;
0N/Aimport sun.awt.AppContext;
0N/Aimport sun.swing.*;
1338N/Aimport sun.awt.SunToolkit;
0N/A
3787N/Aimport java.security.AccessController;
3787N/Aimport java.security.PrivilegedAction;
3787N/A
3787N/Aimport java.security.AccessControlContext;
3787N/Aimport java.security.ProtectionDomain;
3787N/Aimport sun.misc.SharedSecrets;
3787N/Aimport sun.misc.JavaSecurityAccess;
3787N/A
3787N/Aimport sun.awt.AWTAccessor;
3787N/A
0N/A/**
0N/A * This class is used to handle the transfer of a <code>Transferable</code>
0N/A * to and from Swing components. The <code>Transferable</code> is used to
0N/A * represent data that is exchanged via a cut, copy, or paste
0N/A * to/from a clipboard. It is also used in drag-and-drop operations
0N/A * to represent a drag from a component, and a drop to a component.
0N/A * Swing provides functionality that automatically supports cut, copy,
0N/A * and paste keyboard bindings that use the functionality provided by
0N/A * an implementation of this class. Swing also provides functionality
0N/A * that automatically supports drag and drop that uses the functionality
0N/A * provided by an implementation of this class. The Swing developer can
0N/A * concentrate on specifying the semantics of a transfer primarily by setting
0N/A * the <code>transferHandler</code> property on a Swing component.
0N/A * <p>
0N/A * This class is implemented to provide a default behavior of transferring
0N/A * a component property simply by specifying the name of the property in
0N/A * the constructor. For example, to transfer the foreground color from
0N/A * one component to another either via the clipboard or a drag and drop operation
0N/A * a <code>TransferHandler</code> can be constructed with the string "foreground". The
0N/A * built in support will use the color returned by <code>getForeground</code> as the source
0N/A * of the transfer, and <code>setForeground</code> for the target of a transfer.
0N/A * <p>
0N/A * Please see
0N/A * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/dnd.html">
0N/A * How to Use Drag and Drop and Data Transfer</a>,
0N/A * a section in <em>The Java Tutorial</em>, for more information.
0N/A *
0N/A *
0N/A * @author Timothy Prinzing
0N/A * @author Shannon Hickey
0N/A * @since 1.4
0N/A */
0N/A@SuppressWarnings("serial")
0N/Apublic class TransferHandler implements Serializable {
0N/A
0N/A /**
0N/A * An <code>int</code> representing no transfer action.
0N/A */
0N/A public static final int NONE = DnDConstants.ACTION_NONE;
0N/A
0N/A /**
0N/A * An <code>int</code> representing a &quot;copy&quot; transfer action.
0N/A * This value is used when data is copied to a clipboard
0N/A * or copied elsewhere in a drag and drop operation.
0N/A */
0N/A public static final int COPY = DnDConstants.ACTION_COPY;
0N/A
0N/A /**
0N/A * An <code>int</code> representing a &quot;move&quot; transfer action.
0N/A * This value is used when data is moved to a clipboard (i.e. a cut)
0N/A * or moved elsewhere in a drag and drop operation.
0N/A */
0N/A public static final int MOVE = DnDConstants.ACTION_MOVE;
0N/A
0N/A /**
0N/A * An <code>int</code> representing a source action capability of either
0N/A * &quot;copy&quot; or &quot;move&quot;.
0N/A */
0N/A public static final int COPY_OR_MOVE = DnDConstants.ACTION_COPY_OR_MOVE;
0N/A
0N/A /**
0N/A * An <code>int</code> representing a &quot;link&quot; transfer action.
0N/A * This value is used to specify that data should be linked in a drag
0N/A * and drop operation.
0N/A *
0N/A * @see java.awt.dnd.DnDConstants#ACTION_LINK
0N/A * @since 1.6
0N/A */
0N/A public static final int LINK = DnDConstants.ACTION_LINK;
0N/A
0N/A /**
0N/A * An interface to tag things with a {@code getTransferHandler} method.
0N/A */
0N/A interface HasGetTransferHandler {
0N/A
0N/A /** Returns the {@code TransferHandler}.
0N/A *
0N/A * @return The {@code TransferHandler} or {@code null}
0N/A */
0N/A public TransferHandler getTransferHandler();
0N/A }
0N/A
0N/A /**
0N/A * Represents a location where dropped data should be inserted.
0N/A * This is a base class that only encapsulates a point.
0N/A * Components supporting drop may provide subclasses of this
0N/A * containing more information.
0N/A * <p>
0N/A * Developers typically shouldn't create instances of, or extend, this
0N/A * class. Instead, these are something provided by the DnD
0N/A * implementation by <code>TransferSupport</code> instances and by
0N/A * components with a <code>getDropLocation()</code> method.
0N/A *
0N/A * @see javax.swing.TransferHandler.TransferSupport#getDropLocation
0N/A * @since 1.6
0N/A */
0N/A public static class DropLocation {
0N/A private final Point dropPoint;
0N/A
0N/A /**
0N/A * Constructs a drop location for the given point.
0N/A *
0N/A * @param dropPoint the drop point, representing the mouse's
0N/A * current location within the component.
0N/A * @throws IllegalArgumentException if the point
0N/A * is <code>null</code>
0N/A */
0N/A protected DropLocation(Point dropPoint) {
0N/A if (dropPoint == null) {
0N/A throw new IllegalArgumentException("Point cannot be null");
0N/A }
0N/A
0N/A this.dropPoint = new Point(dropPoint);
0N/A }
0N/A
0N/A /**
0N/A * Returns the drop point, representing the mouse's
0N/A * current location within the component.
0N/A *
0N/A * @return the drop point.
0N/A */
0N/A public final Point getDropPoint() {
0N/A return new Point(dropPoint);
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representation of this drop location.
0N/A * This method is intended to be used for debugging purposes,
0N/A * and the content and format of the returned string may vary
0N/A * between implementations.
0N/A *
0N/A * @return a string representation of this drop location
0N/A */
0N/A public String toString() {
0N/A return getClass().getName() + "[dropPoint=" + dropPoint + "]";
0N/A }
0N/A };
0N/A
0N/A /**
0N/A * This class encapsulates all relevant details of a clipboard
0N/A * or drag and drop transfer, and also allows for customizing
0N/A * aspects of the drag and drop experience.
0N/A * <p>
0N/A * The main purpose of this class is to provide the information
0N/A * needed by a developer to determine the suitability of a
0N/A * transfer or to import the data contained within. But it also
0N/A * doubles as a controller for customizing properties during drag
0N/A * and drop, such as whether or not to show the drop location,
0N/A * and which drop action to use.
0N/A * <p>
0N/A * Developers typically need not create instances of this
0N/A * class. Instead, they are something provided by the DnD
0N/A * implementation to certain methods in <code>TransferHandler</code>.
0N/A *
0N/A * @see #canImport(TransferHandler.TransferSupport)
0N/A * @see #importData(TransferHandler.TransferSupport)
0N/A * @since 1.6
0N/A */
0N/A public final static class TransferSupport {
0N/A private boolean isDrop;
0N/A private Component component;
0N/A
0N/A private boolean showDropLocationIsSet;
0N/A private boolean showDropLocation;
0N/A
0N/A private int dropAction = -1;
0N/A
0N/A /**
0N/A * The source is a {@code DropTargetDragEvent} or
0N/A * {@code DropTargetDropEvent} for drops,
0N/A * and a {@code Transferable} otherwise
0N/A */
0N/A private Object source;
0N/A
0N/A private DropLocation dropLocation;
0N/A
0N/A /**
0N/A * Create a <code>TransferSupport</code> with <code>isDrop()</code>
0N/A * <code>true</code> for the given component, event, and index.
0N/A *
0N/A * @param component the target component
0N/A * @param event a <code>DropTargetEvent</code>
0N/A */
0N/A private TransferSupport(Component component,
0N/A DropTargetEvent event) {
0N/A
0N/A isDrop = true;
0N/A setDNDVariables(component, event);
0N/A }
0N/A
0N/A /**
0N/A * Create a <code>TransferSupport</code> with <code>isDrop()</code>
0N/A * <code>false</code> for the given component and
0N/A * <code>Transferable</code>.
0N/A *
0N/A * @param component the target component
0N/A * @param transferable the transferable
0N/A * @throws NullPointerException if either parameter
0N/A * is <code>null</code>
0N/A */
0N/A public TransferSupport(Component component, Transferable transferable) {
0N/A if (component == null) {
0N/A throw new NullPointerException("component is null");
0N/A }
0N/A
0N/A if (transferable == null) {
0N/A throw new NullPointerException("transferable is null");
0N/A }
0N/A
0N/A isDrop = false;
0N/A this.component = component;
0N/A this.source = transferable;
0N/A }
0N/A
0N/A /**
0N/A * Allows for a single instance to be reused during DnD.
0N/A *
0N/A * @param component the target component
0N/A * @param event a <code>DropTargetEvent</code>
0N/A */
0N/A private void setDNDVariables(Component component,
0N/A DropTargetEvent event) {
0N/A
0N/A assert isDrop;
0N/A
0N/A this.component = component;
0N/A this.source = event;
0N/A dropLocation = null;
0N/A dropAction = -1;
0N/A showDropLocationIsSet = false;
0N/A
0N/A if (source == null) {
0N/A return;
0N/A }
0N/A
0N/A assert source instanceof DropTargetDragEvent ||
0N/A source instanceof DropTargetDropEvent;
0N/A
0N/A Point p = source instanceof DropTargetDragEvent
0N/A ? ((DropTargetDragEvent)source).getLocation()
0N/A : ((DropTargetDropEvent)source).getLocation();
0N/A
1338N/A if (SunToolkit.isInstanceOf(component, "javax.swing.text.JTextComponent")) {
1338N/A dropLocation = SwingAccessor.getJTextComponentAccessor().
1338N/A dropLocationForPoint((JTextComponent)component, p);
0N/A } else if (component instanceof JComponent) {
0N/A dropLocation = ((JComponent)component).dropLocationForPoint(p);
0N/A }
0N/A
0N/A /*
0N/A * The drop location may be null at this point if the component
0N/A * doesn't return custom drop locations. In this case, a point-only
0N/A * drop location will be created lazily when requested.
0N/A */
0N/A }
0N/A
0N/A /**
0N/A * Returns whether or not this <code>TransferSupport</code>
0N/A * represents a drop operation.
0N/A *
0N/A * @return <code>true</code> if this is a drop operation,
0N/A * <code>false</code> otherwise.
0N/A */
0N/A public boolean isDrop() {
0N/A return isDrop;
0N/A }
0N/A
0N/A /**
0N/A * Returns the target component of this transfer.
0N/A *
0N/A * @return the target component
0N/A */
0N/A public Component getComponent() {
0N/A return component;
0N/A }
0N/A
0N/A /**
0N/A * Checks that this is a drop and throws an
0N/A * {@code IllegalStateException} if it isn't.
0N/A *
0N/A * @throws IllegalStateException if {@code isDrop} is false.
0N/A */
0N/A private void assureIsDrop() {
0N/A if (!isDrop) {
0N/A throw new IllegalStateException("Not a drop");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the current (non-{@code null}) drop location for the component,
0N/A * when this {@code TransferSupport} represents a drop.
0N/A * <p>
0N/A * Note: For components with built-in drop support, this location
0N/A * will be a subclass of {@code DropLocation} of the same type
0N/A * returned by that component's {@code getDropLocation} method.
0N/A * <p>
0N/A * This method is only for use with drag and drop transfers.
0N/A * Calling it when {@code isDrop()} is {@code false} results
0N/A * in an {@code IllegalStateException}.
0N/A *
0N/A * @return the drop location
0N/A * @throws IllegalStateException if this is not a drop
3370N/A * @see #isDrop()
0N/A */
0N/A public DropLocation getDropLocation() {
0N/A assureIsDrop();
0N/A
0N/A if (dropLocation == null) {
0N/A /*
0N/A * component didn't give us a custom drop location,
0N/A * so lazily create a point-only location
0N/A */
0N/A Point p = source instanceof DropTargetDragEvent
0N/A ? ((DropTargetDragEvent)source).getLocation()
0N/A : ((DropTargetDropEvent)source).getLocation();
0N/A
0N/A dropLocation = new DropLocation(p);
0N/A }
0N/A
0N/A return dropLocation;
0N/A }
0N/A
0N/A /**
0N/A * Sets whether or not the drop location should be visually indicated
0N/A * for the transfer - which must represent a drop. This is applicable to
0N/A * those components that automatically
0N/A * show the drop location when appropriate during a drag and drop
0N/A * operation). By default, the drop location is shown only when the
0N/A * {@code TransferHandler} has said it can accept the import represented
0N/A * by this {@code TransferSupport}. With this method you can force the
0N/A * drop location to always be shown, or always not be shown.
0N/A * <p>
0N/A * This method is only for use with drag and drop transfers.
0N/A * Calling it when {@code isDrop()} is {@code false} results
0N/A * in an {@code IllegalStateException}.
0N/A *
0N/A * @param showDropLocation whether or not to indicate the drop location
0N/A * @throws IllegalStateException if this is not a drop
3370N/A * @see #isDrop()
0N/A */
0N/A public void setShowDropLocation(boolean showDropLocation) {
0N/A assureIsDrop();
0N/A
0N/A this.showDropLocation = showDropLocation;
0N/A this.showDropLocationIsSet = true;
0N/A }
0N/A
0N/A /**
0N/A * Sets the drop action for the transfer - which must represent a drop
0N/A * - to the given action,
0N/A * instead of the default user drop action. The action must be
0N/A * supported by the source's drop actions, and must be one
0N/A * of {@code COPY}, {@code MOVE} or {@code LINK}.
0N/A * <p>
0N/A * This method is only for use with drag and drop transfers.
0N/A * Calling it when {@code isDrop()} is {@code false} results
0N/A * in an {@code IllegalStateException}.
0N/A *
0N/A * @param dropAction the drop action
0N/A * @throws IllegalStateException if this is not a drop
0N/A * @throws IllegalArgumentException if an invalid action is specified
0N/A * @see #getDropAction
0N/A * @see #getUserDropAction
0N/A * @see #getSourceDropActions
3370N/A * @see #isDrop()
0N/A */
0N/A public void setDropAction(int dropAction) {
0N/A assureIsDrop();
0N/A
0N/A int action = dropAction & getSourceDropActions();
0N/A
0N/A if (!(action == COPY || action == MOVE || action == LINK)) {
0N/A throw new IllegalArgumentException("unsupported drop action: " + dropAction);
0N/A }
0N/A
0N/A this.dropAction = dropAction;
0N/A }
0N/A
0N/A /**
0N/A * Returns the action chosen for the drop, when this
0N/A * {@code TransferSupport} represents a drop.
0N/A * <p>
0N/A * Unless explicitly chosen by way of {@code setDropAction},
0N/A * this returns the user drop action provided by
0N/A * {@code getUserDropAction}.
0N/A * <p>
0N/A * You may wish to query this in {@code TransferHandler}'s
0N/A * {@code importData} method to customize processing based
0N/A * on the action.
0N/A * <p>
0N/A * This method is only for use with drag and drop transfers.
0N/A * Calling it when {@code isDrop()} is {@code false} results
0N/A * in an {@code IllegalStateException}.
0N/A *
0N/A * @return the action chosen for the drop
0N/A * @throws IllegalStateException if this is not a drop
0N/A * @see #setDropAction
0N/A * @see #getUserDropAction
3370N/A * @see #isDrop()
0N/A */
0N/A public int getDropAction() {
0N/A return dropAction == -1 ? getUserDropAction() : dropAction;
0N/A }
0N/A
0N/A /**
0N/A * Returns the user drop action for the drop, when this
0N/A * {@code TransferSupport} represents a drop.
0N/A * <p>
0N/A * The user drop action is chosen for a drop as described in the
0N/A * documentation for {@link java.awt.dnd.DropTargetDragEvent} and
0N/A * {@link java.awt.dnd.DropTargetDropEvent}. A different action
0N/A * may be chosen as the drop action by way of the {@code setDropAction}
0N/A * method.
0N/A * <p>
0N/A * You may wish to query this in {@code TransferHandler}'s
0N/A * {@code canImport} method when determining the suitability of a
0N/A * drop or when deciding on a drop action to explicitly choose.
0N/A * <p>
0N/A * This method is only for use with drag and drop transfers.
0N/A * Calling it when {@code isDrop()} is {@code false} results
0N/A * in an {@code IllegalStateException}.
0N/A *
0N/A * @return the user drop action
0N/A * @throws IllegalStateException if this is not a drop
0N/A * @see #setDropAction
0N/A * @see #getDropAction
3370N/A * @see #isDrop()
0N/A */
0N/A public int getUserDropAction() {
0N/A assureIsDrop();
0N/A
0N/A return (source instanceof DropTargetDragEvent)
0N/A ? ((DropTargetDragEvent)source).getDropAction()
0N/A : ((DropTargetDropEvent)source).getDropAction();
0N/A }
0N/A
0N/A /**
0N/A * Returns the drag source's supported drop actions, when this
0N/A * {@code TransferSupport} represents a drop.
0N/A * <p>
0N/A * The source actions represent the set of actions supported by the
0N/A * source of this transfer, and are represented as some bitwise-OR
0N/A * combination of {@code COPY}, {@code MOVE} and {@code LINK}.
0N/A * You may wish to query this in {@code TransferHandler}'s
0N/A * {@code canImport} method when determining the suitability of a drop
0N/A * or when deciding on a drop action to explicitly choose. To determine
0N/A * if a particular action is supported by the source, bitwise-AND
0N/A * the action with the source drop actions, and then compare the result
0N/A * against the original action. For example:
0N/A * <pre>
0N/A * boolean copySupported = (COPY & getSourceDropActions()) == COPY;
0N/A * </pre>
0N/A * <p>
0N/A * This method is only for use with drag and drop transfers.
0N/A * Calling it when {@code isDrop()} is {@code false} results
0N/A * in an {@code IllegalStateException}.
0N/A *
0N/A * @return the drag source's supported drop actions
0N/A * @throws IllegalStateException if this is not a drop
3370N/A * @see #isDrop()
0N/A */
0N/A public int getSourceDropActions() {
0N/A assureIsDrop();
0N/A
0N/A return (source instanceof DropTargetDragEvent)
0N/A ? ((DropTargetDragEvent)source).getSourceActions()
0N/A : ((DropTargetDropEvent)source).getSourceActions();
0N/A }
0N/A
0N/A /**
0N/A * Returns the data flavors for this transfer.
0N/A *
0N/A * @return the data flavors for this transfer
0N/A */
0N/A public DataFlavor[] getDataFlavors() {
0N/A if (isDrop) {
0N/A if (source instanceof DropTargetDragEvent) {
0N/A return ((DropTargetDragEvent)source).getCurrentDataFlavors();
0N/A } else {
0N/A return ((DropTargetDropEvent)source).getCurrentDataFlavors();
0N/A }
0N/A }
0N/A
0N/A return ((Transferable)source).getTransferDataFlavors();
0N/A }
0N/A
0N/A /**
0N/A * Returns whether or not the given data flavor is supported.
0N/A *
0N/A * @param df the <code>DataFlavor</code> to test
0N/A * @return whether or not the given flavor is supported.
0N/A */
0N/A public boolean isDataFlavorSupported(DataFlavor df) {
0N/A if (isDrop) {
0N/A if (source instanceof DropTargetDragEvent) {
0N/A return ((DropTargetDragEvent)source).isDataFlavorSupported(df);
0N/A } else {
0N/A return ((DropTargetDropEvent)source).isDataFlavorSupported(df);
0N/A }
0N/A }
0N/A
0N/A return ((Transferable)source).isDataFlavorSupported(df);
0N/A }
0N/A
0N/A /**
0N/A * Returns the <code>Transferable</code> associated with this transfer.
0N/A * <p>
0N/A * Note: Unless it is necessary to fetch the <code>Transferable</code>
0N/A * directly, use one of the other methods on this class to inquire about
0N/A * the transfer. This may perform better than fetching the
0N/A * <code>Transferable</code> and asking it directly.
0N/A *
0N/A * @return the <code>Transferable</code> associated with this transfer
0N/A */
0N/A public Transferable getTransferable() {
0N/A if (isDrop) {
0N/A if (source instanceof DropTargetDragEvent) {
0N/A return ((DropTargetDragEvent)source).getTransferable();
0N/A } else {
0N/A return ((DropTargetDropEvent)source).getTransferable();
0N/A }
0N/A }
0N/A
0N/A return (Transferable)source;
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns an {@code Action} that performs cut operations to the
0N/A * clipboard. When performed, this action operates on the {@code JComponent}
0N/A * source of the {@code ActionEvent} by invoking {@code exportToClipboard},
0N/A * with a {@code MOVE} action, on the component's {@code TransferHandler}.
0N/A *
0N/A * @return an {@code Action} for performing cuts to the clipboard
0N/A */
0N/A public static Action getCutAction() {
0N/A return cutAction;
0N/A }
0N/A
0N/A /**
0N/A * Returns an {@code Action} that performs copy operations to the
0N/A * clipboard. When performed, this action operates on the {@code JComponent}
0N/A * source of the {@code ActionEvent} by invoking {@code exportToClipboard},
0N/A * with a {@code COPY} action, on the component's {@code TransferHandler}.
0N/A *
0N/A * @return an {@code Action} for performing copies to the clipboard
0N/A */
0N/A public static Action getCopyAction() {
0N/A return copyAction;
0N/A }
0N/A
0N/A /**
0N/A * Returns an {@code Action} that performs paste operations from the
0N/A * clipboard. When performed, this action operates on the {@code JComponent}
0N/A * source of the {@code ActionEvent} by invoking {@code importData},
0N/A * with the clipboard contents, on the component's {@code TransferHandler}.
0N/A *
0N/A * @return an {@code Action} for performing pastes from the clipboard
0N/A */
0N/A public static Action getPasteAction() {
0N/A return pasteAction;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Constructs a transfer handler that can transfer a Java Bean property
0N/A * from one component to another via the clipboard or a drag and drop
0N/A * operation.
0N/A *
0N/A * @param property the name of the property to transfer; this can
0N/A * be <code>null</code> if there is no property associated with the transfer
0N/A * handler (a subclass that performs some other kind of transfer, for example)
0N/A */
0N/A public TransferHandler(String property) {
0N/A propertyName = property;
0N/A }
0N/A
0N/A /**
0N/A * Convenience constructor for subclasses.
0N/A */
0N/A protected TransferHandler() {
0N/A this(null);
0N/A }
0N/A
2129N/A
2129N/A /**
2129N/A * image for the {@code startDrag} method
2129N/A *
2129N/A * @see java.awt.dnd.DragGestureEvent#startDrag(Cursor dragCursor, Image dragImage, Point imageOffset, Transferable transferable, DragSourceListener dsl)
2129N/A */
2129N/A private Image dragImage;
2129N/A
2129N/A /**
2129N/A * anchor offset for the {@code startDrag} method
2129N/A *
2129N/A * @see java.awt.dnd.DragGestureEvent#startDrag(Cursor dragCursor, Image dragImage, Point imageOffset, Transferable transferable, DragSourceListener dsl)
2129N/A */
2129N/A private Point dragImageOffset;
2129N/A
2129N/A /**
2129N/A * Sets the drag image parameter. The image has to be prepared
2129N/A * for rendering by the moment of the call. The image is stored
2129N/A * by reference because of some performance reasons.
2129N/A *
2129N/A * @param img an image to drag
2129N/A */
2129N/A public void setDragImage(Image img) {
2129N/A dragImage = img;
2129N/A }
2129N/A
2129N/A /**
2129N/A * Returns the drag image. If there is no image to drag,
2129N/A * the returned value is {@code null}.
2129N/A *
2129N/A * @return the reference to the drag image
2129N/A */
2129N/A public Image getDragImage() {
2129N/A return dragImage;
2129N/A }
2129N/A
2129N/A /**
2129N/A * Sets an anchor offset for the image to drag.
2129N/A * It can not be {@code null}.
2129N/A *
2129N/A * @param p a {@code Point} object that corresponds
2129N/A * to coordinates of an anchor offset of the image
2129N/A * relative to the upper left corner of the image
2129N/A */
2129N/A public void setDragImageOffset(Point p) {
2129N/A dragImageOffset = new Point(p);
2129N/A }
2129N/A
2129N/A /**
2129N/A * Returns an anchor offset for the image to drag.
2129N/A *
2129N/A * @return a {@code Point} object that corresponds
2129N/A * to coordinates of an anchor offset of the image
2129N/A * relative to the upper left corner of the image.
2129N/A * The point {@code (0,0)} returns by default.
2129N/A */
2129N/A public Point getDragImageOffset() {
2129N/A if (dragImageOffset == null) {
2129N/A return new Point(0,0);
2129N/A }
2129N/A return new Point(dragImageOffset);
2129N/A }
2129N/A
0N/A /**
0N/A * Causes the Swing drag support to be initiated. This is called by
0N/A * the various UI implementations in the <code>javax.swing.plaf.basic</code>
0N/A * package if the dragEnabled property is set on the component.
0N/A * This can be called by custom UI
0N/A * implementations to use the Swing drag support. This method can also be called
0N/A * by a Swing extension written as a subclass of <code>JComponent</code>
0N/A * to take advantage of the Swing drag support.
0N/A * <p>
0N/A * The transfer <em>will not necessarily</em> have been completed at the
0N/A * return of this call (i.e. the call does not block waiting for the drop).
0N/A * The transfer will take place through the Swing implementation of the
0N/A * <code>java.awt.dnd</code> mechanism, requiring no further effort
0N/A * from the developer. The <code>exportDone</code> method will be called
0N/A * when the transfer has completed.
0N/A *
0N/A * @param comp the component holding the data to be transferred;
0N/A * provided to enable sharing of <code>TransferHandler</code>s
0N/A * @param e the event that triggered the transfer
0N/A * @param action the transfer action initially requested;
0N/A * either {@code COPY}, {@code MOVE} or {@code LINK};
0N/A * the DnD system may change the action used during the
0N/A * course of the drag operation
0N/A */
0N/A public void exportAsDrag(JComponent comp, InputEvent e, int action) {
0N/A int srcActions = getSourceActions(comp);
0N/A
0N/A // only mouse events supported for drag operations
0N/A if (!(e instanceof MouseEvent)
0N/A // only support known actions
0N/A || !(action == COPY || action == MOVE || action == LINK)
0N/A // only support valid source actions
0N/A || (srcActions & action) == 0) {
0N/A
0N/A action = NONE;
0N/A }
0N/A
0N/A if (action != NONE && !GraphicsEnvironment.isHeadless()) {
0N/A if (recognizer == null) {
0N/A recognizer = new SwingDragGestureRecognizer(new DragHandler());
0N/A }
0N/A recognizer.gestured(comp, (MouseEvent)e, srcActions, action);
0N/A } else {
0N/A exportDone(comp, null, NONE);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Causes a transfer from the given component to the
0N/A * given clipboard. This method is called by the default cut and
0N/A * copy actions registered in a component's action map.
0N/A * <p>
0N/A * The transfer will take place using the <code>java.awt.datatransfer</code>
0N/A * mechanism, requiring no further effort from the developer. Any data
0N/A * transfer <em>will</em> be complete and the <code>exportDone</code>
0N/A * method will be called with the action that occurred, before this method
0N/A * returns. Should the clipboard be unavailable when attempting to place
0N/A * data on it, the <code>IllegalStateException</code> thrown by
0N/A * {@link Clipboard#setContents(Transferable, ClipboardOwner)} will
0N/A * be propogated through this method. However,
0N/A * <code>exportDone</code> will first be called with an action
0N/A * of <code>NONE</code> for consistency.
0N/A *
0N/A * @param comp the component holding the data to be transferred;
0N/A * provided to enable sharing of <code>TransferHandler</code>s
0N/A * @param clip the clipboard to transfer the data into
0N/A * @param action the transfer action requested; this should
0N/A * be a value of either <code>COPY</code> or <code>MOVE</code>;
0N/A * the operation performed is the intersection of the transfer
0N/A * capabilities given by getSourceActions and the requested action;
0N/A * the intersection may result in an action of <code>NONE</code>
0N/A * if the requested action isn't supported
0N/A * @throws IllegalStateException if the clipboard is currently unavailable
0N/A * @see Clipboard#setContents(Transferable, ClipboardOwner)
0N/A */
0N/A public void exportToClipboard(JComponent comp, Clipboard clip, int action)
0N/A throws IllegalStateException {
0N/A
0N/A if ((action == COPY || action == MOVE)
0N/A && (getSourceActions(comp) & action) != 0) {
0N/A
0N/A Transferable t = createTransferable(comp);
0N/A if (t != null) {
0N/A try {
0N/A clip.setContents(t, null);
0N/A exportDone(comp, t, action);
0N/A return;
0N/A } catch (IllegalStateException ise) {
0N/A exportDone(comp, t, NONE);
0N/A throw ise;
0N/A }
0N/A }
0N/A }
0N/A
0N/A exportDone(comp, null, NONE);
0N/A }
0N/A
0N/A /**
0N/A * Causes a transfer to occur from a clipboard or a drag and
0N/A * drop operation. The <code>Transferable</code> to be
0N/A * imported and the component to transfer to are contained
0N/A * within the <code>TransferSupport</code>.
0N/A * <p>
0N/A * While the drag and drop implementation calls {@code canImport}
0N/A * to determine the suitability of a transfer before calling this
0N/A * method, the implementation of paste does not. As such, it cannot
0N/A * be assumed that the transfer is acceptable upon a call to
0N/A * this method for paste. It is recommended that {@code canImport} be
0N/A * explicitly called to cover this case.
0N/A * <p>
0N/A * Note: The <code>TransferSupport</code> object passed to this method
0N/A * is only valid for the duration of the method call. It is undefined
0N/A * what values it may contain after this method returns.
0N/A *
0N/A * @param support the object containing the details of
0N/A * the transfer, not <code>null</code>.
0N/A * @return true if the data was inserted into the component,
0N/A * false otherwise
0N/A * @throws NullPointerException if <code>support</code> is {@code null}
0N/A * @see #canImport(TransferHandler.TransferSupport)
0N/A * @since 1.6
0N/A */
0N/A public boolean importData(TransferSupport support) {
0N/A return support.getComponent() instanceof JComponent
0N/A ? importData((JComponent)support.getComponent(), support.getTransferable())
0N/A : false;
0N/A }
0N/A
0N/A /**
0N/A * Causes a transfer to a component from a clipboard or a
0N/A * DND drop operation. The <code>Transferable</code> represents
0N/A * the data to be imported into the component.
0N/A * <p>
0N/A * Note: Swing now calls the newer version of <code>importData</code>
0N/A * that takes a <code>TransferSupport</code>, which in turn calls this
0N/A * method (if the component in the {@code TransferSupport} is a
0N/A * {@code JComponent}). Developers are encouraged to call and override the
0N/A * newer version as it provides more information (and is the only
0N/A * version that supports use with a {@code TransferHandler} set directly
0N/A * on a {@code JFrame} or other non-{@code JComponent}).
0N/A *
0N/A * @param comp the component to receive the transfer;
0N/A * provided to enable sharing of <code>TransferHandler</code>s
0N/A * @param t the data to import
0N/A * @return true if the data was inserted into the component, false otherwise
0N/A * @see #importData(TransferHandler.TransferSupport)
0N/A */
0N/A public boolean importData(JComponent comp, Transferable t) {
0N/A PropertyDescriptor prop = getPropertyDescriptor(comp);
0N/A if (prop != null) {
0N/A Method writer = prop.getWriteMethod();
0N/A if (writer == null) {
0N/A // read-only property. ignore
0N/A return false;
0N/A }
0N/A Class<?>[] params = writer.getParameterTypes();
0N/A if (params.length != 1) {
0N/A // zero or more than one argument, ignore
0N/A return false;
0N/A }
0N/A DataFlavor flavor = getPropertyDataFlavor(params[0], t.getTransferDataFlavors());
0N/A if (flavor != null) {
0N/A try {
0N/A Object value = t.getTransferData(flavor);
0N/A Object[] args = { value };
0N/A MethodUtil.invoke(writer, comp, args);
0N/A return true;
0N/A } catch (Exception ex) {
0N/A System.err.println("Invocation failed");
0N/A // invocation code
0N/A }
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * This method is called repeatedly during a drag and drop operation
0N/A * to allow the developer to configure properties of, and to return
0N/A * the acceptability of transfers; with a return value of {@code true}
0N/A * indicating that the transfer represented by the given
0N/A * {@code TransferSupport} (which contains all of the details of the
0N/A * transfer) is acceptable at the current time, and a value of {@code false}
0N/A * rejecting the transfer.
0N/A * <p>
0N/A * For those components that automatically display a drop location during
0N/A * drag and drop, accepting the transfer, by default, tells them to show
0N/A * the drop location. This can be changed by calling
0N/A * {@code setShowDropLocation} on the {@code TransferSupport}.
0N/A * <p>
0N/A * By default, when the transfer is accepted, the chosen drop action is that
0N/A * picked by the user via their drag gesture. The developer can override
0N/A * this and choose a different action, from the supported source
0N/A * actions, by calling {@code setDropAction} on the {@code TransferSupport}.
0N/A * <p>
0N/A * On every call to {@code canImport}, the {@code TransferSupport} contains
0N/A * fresh state. As such, any properties set on it must be set on every
0N/A * call. Upon a drop, {@code canImport} is called one final time before
0N/A * calling into {@code importData}. Any state set on the
0N/A * {@code TransferSupport} during that last call will be available in
0N/A * {@code importData}.
0N/A * <p>
0N/A * This method is not called internally in response to paste operations.
0N/A * As such, it is recommended that implementations of {@code importData}
0N/A * explicitly call this method for such cases and that this method
0N/A * be prepared to return the suitability of paste operations as well.
0N/A * <p>
0N/A * Note: The <code>TransferSupport</code> object passed to this method
0N/A * is only valid for the duration of the method call. It is undefined
0N/A * what values it may contain after this method returns.
0N/A *
0N/A * @param support the object containing the details of
0N/A * the transfer, not <code>null</code>.
0N/A * @return <code>true</code> if the import can happen,
0N/A * <code>false</code> otherwise
0N/A * @throws NullPointerException if <code>support</code> is {@code null}
0N/A * @see #importData(TransferHandler.TransferSupport)
0N/A * @see javax.swing.TransferHandler.TransferSupport#setShowDropLocation
0N/A * @see javax.swing.TransferHandler.TransferSupport#setDropAction
0N/A * @since 1.6
0N/A */
0N/A public boolean canImport(TransferSupport support) {
0N/A return support.getComponent() instanceof JComponent
0N/A ? canImport((JComponent)support.getComponent(), support.getDataFlavors())
0N/A : false;
0N/A }
0N/A
0N/A /**
0N/A * Indicates whether a component will accept an import of the given
0N/A * set of data flavors prior to actually attempting to import it.
0N/A * <p>
0N/A * Note: Swing now calls the newer version of <code>canImport</code>
0N/A * that takes a <code>TransferSupport</code>, which in turn calls this
0N/A * method (only if the component in the {@code TransferSupport} is a
0N/A * {@code JComponent}). Developers are encouraged to call and override the
0N/A * newer version as it provides more information (and is the only
0N/A * version that supports use with a {@code TransferHandler} set directly
0N/A * on a {@code JFrame} or other non-{@code JComponent}).
0N/A *
0N/A * @param comp the component to receive the transfer;
0N/A * provided to enable sharing of <code>TransferHandler</code>s
0N/A * @param transferFlavors the data formats available
0N/A * @return true if the data can be inserted into the component, false otherwise
0N/A * @see #canImport(TransferHandler.TransferSupport)
0N/A */
0N/A public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
0N/A PropertyDescriptor prop = getPropertyDescriptor(comp);
0N/A if (prop != null) {
0N/A Method writer = prop.getWriteMethod();
0N/A if (writer == null) {
0N/A // read-only property. ignore
0N/A return false;
0N/A }
0N/A Class<?>[] params = writer.getParameterTypes();
0N/A if (params.length != 1) {
0N/A // zero or more than one argument, ignore
0N/A return false;
0N/A }
0N/A DataFlavor flavor = getPropertyDataFlavor(params[0], transferFlavors);
0N/A if (flavor != null) {
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Returns the type of transfer actions supported by the source;
0N/A * any bitwise-OR combination of {@code COPY}, {@code MOVE}
0N/A * and {@code LINK}.
0N/A * <p>
0N/A * Some models are not mutable, so a transfer operation of {@code MOVE}
0N/A * should not be advertised in that case. Returning {@code NONE}
0N/A * disables transfers from the component.
0N/A *
0N/A * @param c the component holding the data to be transferred;
0N/A * provided to enable sharing of <code>TransferHandler</code>s
0N/A * @return {@code COPY} if the transfer property can be found,
0N/A * otherwise returns <code>NONE</code>
0N/A */
0N/A public int getSourceActions(JComponent c) {
0N/A PropertyDescriptor prop = getPropertyDescriptor(c);
0N/A if (prop != null) {
0N/A return COPY;
0N/A }
0N/A return NONE;
0N/A }
0N/A
0N/A /**
0N/A * Returns an object that establishes the look of a transfer. This is
0N/A * useful for both providing feedback while performing a drag operation and for
0N/A * representing the transfer in a clipboard implementation that has a visual
0N/A * appearance. The implementation of the <code>Icon</code> interface should
0N/A * not alter the graphics clip or alpha level.
0N/A * The icon implementation need not be rectangular or paint all of the
0N/A * bounding rectangle and logic that calls the icons paint method should
0N/A * not assume the all bits are painted. <code>null</code> is a valid return value
0N/A * for this method and indicates there is no visual representation provided.
0N/A * In that case, the calling logic is free to represent the
0N/A * transferable however it wants.
0N/A * <p>
0N/A * The default Swing logic will not do an alpha blended drag animation if
0N/A * the return is <code>null</code>.
0N/A *
0N/A * @param t the data to be transferred; this value is expected to have been
0N/A * created by the <code>createTransferable</code> method
0N/A * @return <code>null</code>, indicating
0N/A * there is no default visual representation
0N/A */
0N/A public Icon getVisualRepresentation(Transferable t) {
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Creates a <code>Transferable</code> to use as the source for
0N/A * a data transfer. Returns the representation of the data to
0N/A * be transferred, or <code>null</code> if the component's
0N/A * property is <code>null</code>
0N/A *
0N/A * @param c the component holding the data to be transferred;
0N/A * provided to enable sharing of <code>TransferHandler</code>s
0N/A * @return the representation of the data to be transferred, or
0N/A * <code>null</code> if the property associated with <code>c</code>
0N/A * is <code>null</code>
0N/A *
0N/A */
0N/A protected Transferable createTransferable(JComponent c) {
0N/A PropertyDescriptor property = getPropertyDescriptor(c);
0N/A if (property != null) {
0N/A return new PropertyTransferable(property, c);
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Invoked after data has been exported. This method should remove
0N/A * the data that was transferred if the action was <code>MOVE</code>.
0N/A * <p>
0N/A * This method is implemented to do nothing since <code>MOVE</code>
0N/A * is not a supported action of this implementation
0N/A * (<code>getSourceActions</code> does not include <code>MOVE</code>).
0N/A *
0N/A * @param source the component that was the source of the data
0N/A * @param data The data that was transferred or possibly null
0N/A * if the action is <code>NONE</code>.
0N/A * @param action the actual action that was performed
0N/A */
0N/A protected void exportDone(JComponent source, Transferable data, int action) {
0N/A }
0N/A
0N/A /**
0N/A * Fetches the property descriptor for the property assigned to this transfer
0N/A * handler on the given component (transfer handler may be shared). This
0N/A * returns <code>null</code> if the property descriptor can't be found
0N/A * or there is an error attempting to fetch the property descriptor.
0N/A */
0N/A private PropertyDescriptor getPropertyDescriptor(JComponent comp) {
0N/A if (propertyName == null) {
0N/A return null;
0N/A }
0N/A Class<?> k = comp.getClass();
0N/A BeanInfo bi;
0N/A try {
0N/A bi = Introspector.getBeanInfo(k);
0N/A } catch (IntrospectionException ex) {
0N/A return null;
0N/A }
0N/A PropertyDescriptor props[] = bi.getPropertyDescriptors();
0N/A for (int i=0; i < props.length; i++) {
0N/A if (propertyName.equals(props[i].getName())) {
0N/A Method reader = props[i].getReadMethod();
0N/A
0N/A if (reader != null) {
0N/A Class<?>[] params = reader.getParameterTypes();
0N/A
0N/A if (params == null || params.length == 0) {
0N/A // found the desired descriptor
0N/A return props[i];
0N/A }
0N/A }
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Fetches the data flavor from the array of possible flavors that
0N/A * has data of the type represented by property type. Null is
0N/A * returned if there is no match.
0N/A */
0N/A private DataFlavor getPropertyDataFlavor(Class<?> k, DataFlavor[] flavors) {
0N/A for(int i = 0; i < flavors.length; i++) {
0N/A DataFlavor flavor = flavors[i];
0N/A if ("application".equals(flavor.getPrimaryType()) &&
0N/A "x-java-jvm-local-objectref".equals(flavor.getSubType()) &&
0N/A k.isAssignableFrom(flavor.getRepresentationClass())) {
0N/A
0N/A return flavor;
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A
0N/A private String propertyName;
0N/A private static SwingDragGestureRecognizer recognizer = null;
0N/A
0N/A private static DropTargetListener getDropTargetListener() {
0N/A synchronized(DropHandler.class) {
0N/A DropHandler handler =
0N/A (DropHandler)AppContext.getAppContext().get(DropHandler.class);
0N/A
0N/A if (handler == null) {
0N/A handler = new DropHandler();
0N/A AppContext.getAppContext().put(DropHandler.class, handler);
0N/A }
0N/A
0N/A return handler;
0N/A }
0N/A }
0N/A
0N/A static class PropertyTransferable implements Transferable {
0N/A
0N/A PropertyTransferable(PropertyDescriptor p, JComponent c) {
0N/A property = p;
0N/A component = c;
0N/A }
0N/A
0N/A // --- Transferable methods ----------------------------------------------
0N/A
0N/A /**
0N/A * Returns an array of <code>DataFlavor</code> objects indicating the flavors the data
0N/A * can be provided in. The array should be ordered according to preference
0N/A * for providing the data (from most richly descriptive to least descriptive).
0N/A * @return an array of data flavors in which this data can be transferred
0N/A */
0N/A public DataFlavor[] getTransferDataFlavors() {
0N/A DataFlavor[] flavors = new DataFlavor[1];
0N/A Class<?> propertyType = property.getPropertyType();
0N/A String mimeType = DataFlavor.javaJVMLocalObjectMimeType + ";class=" + propertyType.getName();
0N/A try {
0N/A flavors[0] = new DataFlavor(mimeType);
0N/A } catch (ClassNotFoundException cnfe) {
0N/A flavors = new DataFlavor[0];
0N/A }
0N/A return flavors;
0N/A }
0N/A
0N/A /**
0N/A * Returns whether the specified data flavor is supported for
0N/A * this object.
0N/A * @param flavor the requested flavor for the data
0N/A * @return true if this <code>DataFlavor</code> is supported,
0N/A * otherwise false
0N/A */
0N/A public boolean isDataFlavorSupported(DataFlavor flavor) {
0N/A Class<?> propertyType = property.getPropertyType();
0N/A if ("application".equals(flavor.getPrimaryType()) &&
0N/A "x-java-jvm-local-objectref".equals(flavor.getSubType()) &&
0N/A flavor.getRepresentationClass().isAssignableFrom(propertyType)) {
0N/A
0N/A return true;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Returns an object which represents the data to be transferred. The class
0N/A * of the object returned is defined by the representation class of the flavor.
0N/A *
0N/A * @param flavor the requested flavor for the data
0N/A * @see DataFlavor#getRepresentationClass
0N/A * @exception IOException if the data is no longer available
0N/A * in the requested flavor.
0N/A * @exception UnsupportedFlavorException if the requested data flavor is
0N/A * not supported.
0N/A */
0N/A public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
0N/A if (! isDataFlavorSupported(flavor)) {
0N/A throw new UnsupportedFlavorException(flavor);
0N/A }
0N/A Method reader = property.getReadMethod();
0N/A Object value = null;
0N/A try {
0N/A value = MethodUtil.invoke(reader, component, (Object[])null);
0N/A } catch (Exception ex) {
0N/A throw new IOException("Property read failed: " + property.getName());
0N/A }
0N/A return value;
0N/A }
0N/A
0N/A JComponent component;
0N/A PropertyDescriptor property;
0N/A }
0N/A
0N/A /**
0N/A * This is the default drop target for drag and drop operations if
0N/A * one isn't provided by the developer. <code>DropTarget</code>
0N/A * only supports one <code>DropTargetListener</code> and doesn't
0N/A * function properly if it isn't set.
0N/A * This class sets the one listener as the linkage of drop handling
0N/A * to the <code>TransferHandler</code>, and adds support for
0N/A * additional listeners which some of the <code>ComponentUI</code>
0N/A * implementations install to manipulate a drop insertion location.
0N/A */
0N/A static class SwingDropTarget extends DropTarget implements UIResource {
0N/A
0N/A SwingDropTarget(Component c) {
0N/A super(c, COPY_OR_MOVE | LINK, null);
0N/A try {
0N/A // addDropTargetListener is overridden
0N/A // we specifically need to add to the superclass
0N/A super.addDropTargetListener(getDropTargetListener());
0N/A } catch (TooManyListenersException tmle) {}
0N/A }
0N/A
0N/A public void addDropTargetListener(DropTargetListener dtl) throws TooManyListenersException {
0N/A // Since the super class only supports one DropTargetListener,
0N/A // and we add one from the constructor, we always add to the
0N/A // extended list.
0N/A if (listenerList == null) {
0N/A listenerList = new EventListenerList();
0N/A }
0N/A listenerList.add(DropTargetListener.class, dtl);
0N/A }
0N/A
0N/A public void removeDropTargetListener(DropTargetListener dtl) {
0N/A if (listenerList != null) {
0N/A listenerList.remove(DropTargetListener.class, dtl);
0N/A }
0N/A }
0N/A
0N/A // --- DropTargetListener methods (multicast) --------------------------
0N/A
0N/A public void dragEnter(DropTargetDragEvent e) {
0N/A super.dragEnter(e);
0N/A if (listenerList != null) {
0N/A Object[] listeners = listenerList.getListenerList();
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==DropTargetListener.class) {
0N/A ((DropTargetListener)listeners[i+1]).dragEnter(e);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void dragOver(DropTargetDragEvent e) {
0N/A super.dragOver(e);
0N/A if (listenerList != null) {
0N/A Object[] listeners = listenerList.getListenerList();
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==DropTargetListener.class) {
0N/A ((DropTargetListener)listeners[i+1]).dragOver(e);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void dragExit(DropTargetEvent e) {
0N/A super.dragExit(e);
0N/A if (listenerList != null) {
0N/A Object[] listeners = listenerList.getListenerList();
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==DropTargetListener.class) {
0N/A ((DropTargetListener)listeners[i+1]).dragExit(e);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void drop(DropTargetDropEvent e) {
0N/A super.drop(e);
0N/A if (listenerList != null) {
0N/A Object[] listeners = listenerList.getListenerList();
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==DropTargetListener.class) {
0N/A ((DropTargetListener)listeners[i+1]).drop(e);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void dropActionChanged(DropTargetDragEvent e) {
0N/A super.dropActionChanged(e);
0N/A if (listenerList != null) {
0N/A Object[] listeners = listenerList.getListenerList();
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==DropTargetListener.class) {
0N/A ((DropTargetListener)listeners[i+1]).dropActionChanged(e);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A private EventListenerList listenerList;
0N/A }
0N/A
0N/A private static class DropHandler implements DropTargetListener,
0N/A Serializable,
0N/A ActionListener {
0N/A
0N/A private Timer timer;
0N/A private Point lastPosition;
0N/A private Rectangle outer = new Rectangle();
0N/A private Rectangle inner = new Rectangle();
0N/A private int hysteresis = 10;
0N/A
0N/A private Component component;
0N/A private Object state;
0N/A private TransferSupport support =
0N/A new TransferSupport(null, (DropTargetEvent)null);
0N/A
0N/A private static final int AUTOSCROLL_INSET = 10;
0N/A
0N/A /**
0N/A * Update the geometry of the autoscroll region. The geometry is
0N/A * maintained as a pair of rectangles. The region can cause
0N/A * a scroll if the pointer sits inside it for the duration of the
0N/A * timer. The region that causes the timer countdown is the area
0N/A * between the two rectangles.
0N/A * <p>
0N/A * This is implemented to use the visible area of the component
0N/A * as the outer rectangle, and the insets are fixed at 10. Should
0N/A * the component be smaller than a total of 20 in any direction,
0N/A * autoscroll will not occur in that direction.
0N/A */
0N/A private void updateAutoscrollRegion(JComponent c) {
0N/A // compute the outer
0N/A Rectangle visible = c.getVisibleRect();
0N/A outer.setBounds(visible.x, visible.y, visible.width, visible.height);
0N/A
0N/A // compute the insets
0N/A Insets i = new Insets(0, 0, 0, 0);
0N/A if (c instanceof Scrollable) {
0N/A int minSize = 2 * AUTOSCROLL_INSET;
0N/A
0N/A if (visible.width >= minSize) {
0N/A i.left = i.right = AUTOSCROLL_INSET;
0N/A }
0N/A
0N/A if (visible.height >= minSize) {
0N/A i.top = i.bottom = AUTOSCROLL_INSET;
0N/A }
0N/A }
0N/A
0N/A // set the inner from the insets
0N/A inner.setBounds(visible.x + i.left,
0N/A visible.y + i.top,
0N/A visible.width - (i.left + i.right),
0N/A visible.height - (i.top + i.bottom));
0N/A }
0N/A
0N/A /**
0N/A * Perform an autoscroll operation. This is implemented to scroll by the
0N/A * unit increment of the Scrollable using scrollRectToVisible. If the
0N/A * cursor is in a corner of the autoscroll region, more than one axis will
0N/A * scroll.
0N/A */
0N/A private void autoscroll(JComponent c, Point pos) {
0N/A if (c instanceof Scrollable) {
0N/A Scrollable s = (Scrollable) c;
0N/A if (pos.y < inner.y) {
0N/A // scroll upward
0N/A int dy = s.getScrollableUnitIncrement(outer, SwingConstants.VERTICAL, -1);
0N/A Rectangle r = new Rectangle(inner.x, outer.y - dy, inner.width, dy);
0N/A c.scrollRectToVisible(r);
0N/A } else if (pos.y > (inner.y + inner.height)) {
0N/A // scroll downard
0N/A int dy = s.getScrollableUnitIncrement(outer, SwingConstants.VERTICAL, 1);
0N/A Rectangle r = new Rectangle(inner.x, outer.y + outer.height, inner.width, dy);
0N/A c.scrollRectToVisible(r);
0N/A }
0N/A
0N/A if (pos.x < inner.x) {
0N/A // scroll left
0N/A int dx = s.getScrollableUnitIncrement(outer, SwingConstants.HORIZONTAL, -1);
0N/A Rectangle r = new Rectangle(outer.x - dx, inner.y, dx, inner.height);
0N/A c.scrollRectToVisible(r);
0N/A } else if (pos.x > (inner.x + inner.width)) {
0N/A // scroll right
0N/A int dx = s.getScrollableUnitIncrement(outer, SwingConstants.HORIZONTAL, 1);
0N/A Rectangle r = new Rectangle(outer.x + outer.width, inner.y, dx, inner.height);
0N/A c.scrollRectToVisible(r);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Initializes the internal properties if they haven't been already
0N/A * inited. This is done lazily to avoid loading of desktop properties.
0N/A */
0N/A private void initPropertiesIfNecessary() {
0N/A if (timer == null) {
0N/A Toolkit t = Toolkit.getDefaultToolkit();
0N/A Integer prop;
0N/A
0N/A prop = (Integer)
0N/A t.getDesktopProperty("DnD.Autoscroll.interval");
0N/A
0N/A timer = new Timer(prop == null ? 100 : prop.intValue(), this);
0N/A
0N/A prop = (Integer)
0N/A t.getDesktopProperty("DnD.Autoscroll.initialDelay");
0N/A
0N/A timer.setInitialDelay(prop == null ? 100 : prop.intValue());
0N/A
0N/A prop = (Integer)
0N/A t.getDesktopProperty("DnD.Autoscroll.cursorHysteresis");
0N/A
0N/A if (prop != null) {
0N/A hysteresis = prop.intValue();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * The timer fired, perform autoscroll if the pointer is within the
0N/A * autoscroll region.
0N/A * <P>
0N/A * @param e the <code>ActionEvent</code>
0N/A */
0N/A public void actionPerformed(ActionEvent e) {
0N/A updateAutoscrollRegion((JComponent)component);
0N/A if (outer.contains(lastPosition) && !inner.contains(lastPosition)) {
0N/A autoscroll((JComponent)component, lastPosition);
0N/A }
0N/A }
0N/A
0N/A // --- DropTargetListener methods -----------------------------------
0N/A
0N/A private void setComponentDropLocation(TransferSupport support,
0N/A boolean forDrop) {
0N/A
0N/A DropLocation dropLocation = (support == null)
0N/A ? null
0N/A : support.getDropLocation();
0N/A
1338N/A if (SunToolkit.isInstanceOf(component, "javax.swing.text.JTextComponent")) {
1338N/A state = SwingAccessor.getJTextComponentAccessor().
1338N/A setDropLocation((JTextComponent)component, dropLocation, state, forDrop);
0N/A } else if (component instanceof JComponent) {
0N/A state = ((JComponent)component).setDropLocation(dropLocation, state, forDrop);
0N/A }
0N/A }
0N/A
0N/A private void handleDrag(DropTargetDragEvent e) {
0N/A TransferHandler importer =
0N/A ((HasGetTransferHandler)component).getTransferHandler();
0N/A
0N/A if (importer == null) {
0N/A e.rejectDrag();
0N/A setComponentDropLocation(null, false);
0N/A return;
0N/A }
0N/A
0N/A support.setDNDVariables(component, e);
0N/A boolean canImport = importer.canImport(support);
0N/A
0N/A if (canImport) {
0N/A e.acceptDrag(support.getDropAction());
0N/A } else {
0N/A e.rejectDrag();
0N/A }
0N/A
0N/A boolean showLocation = support.showDropLocationIsSet ?
0N/A support.showDropLocation :
0N/A canImport;
0N/A
0N/A setComponentDropLocation(showLocation ? support : null, false);
0N/A }
0N/A
0N/A public void dragEnter(DropTargetDragEvent e) {
0N/A state = null;
0N/A component = e.getDropTargetContext().getComponent();
0N/A
0N/A handleDrag(e);
0N/A
0N/A if (component instanceof JComponent) {
0N/A lastPosition = e.getLocation();
0N/A updateAutoscrollRegion((JComponent)component);
0N/A initPropertiesIfNecessary();
0N/A }
0N/A }
0N/A
0N/A public void dragOver(DropTargetDragEvent e) {
0N/A handleDrag(e);
0N/A
0N/A if (!(component instanceof JComponent)) {
0N/A return;
0N/A }
0N/A
0N/A Point p = e.getLocation();
0N/A
0N/A if (Math.abs(p.x - lastPosition.x) > hysteresis
0N/A || Math.abs(p.y - lastPosition.y) > hysteresis) {
0N/A // no autoscroll
0N/A if (timer.isRunning()) timer.stop();
0N/A } else {
0N/A if (!timer.isRunning()) timer.start();
0N/A }
0N/A
0N/A lastPosition = p;
0N/A }
0N/A
0N/A public void dragExit(DropTargetEvent e) {
0N/A cleanup(false);
0N/A }
0N/A
0N/A public void drop(DropTargetDropEvent e) {
0N/A TransferHandler importer =
0N/A ((HasGetTransferHandler)component).getTransferHandler();
0N/A
0N/A if (importer == null) {
0N/A e.rejectDrop();
0N/A cleanup(false);
0N/A return;
0N/A }
0N/A
0N/A support.setDNDVariables(component, e);
0N/A boolean canImport = importer.canImport(support);
0N/A
0N/A if (canImport) {
0N/A e.acceptDrop(support.getDropAction());
0N/A
0N/A boolean showLocation = support.showDropLocationIsSet ?
0N/A support.showDropLocation :
0N/A canImport;
0N/A
0N/A setComponentDropLocation(showLocation ? support : null, false);
0N/A
0N/A boolean success;
0N/A
0N/A try {
0N/A success = importer.importData(support);
0N/A } catch (RuntimeException re) {
0N/A success = false;
0N/A }
0N/A
0N/A e.dropComplete(success);
0N/A cleanup(success);
0N/A } else {
0N/A e.rejectDrop();
0N/A cleanup(false);
0N/A }
0N/A }
0N/A
0N/A public void dropActionChanged(DropTargetDragEvent e) {
0N/A /*
0N/A * Work-around for Linux bug where dropActionChanged
0N/A * is called before dragEnter.
0N/A */
0N/A if (component == null) {
0N/A return;
0N/A }
0N/A
0N/A handleDrag(e);
0N/A }
0N/A
0N/A private void cleanup(boolean forDrop) {
0N/A setComponentDropLocation(null, forDrop);
0N/A if (component instanceof JComponent) {
0N/A ((JComponent)component).dndDone();
0N/A }
0N/A
0N/A if (timer != null) {
0N/A timer.stop();
0N/A }
0N/A
0N/A state = null;
0N/A component = null;
0N/A lastPosition = null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * This is the default drag handler for drag and drop operations that
0N/A * use the <code>TransferHandler</code>.
0N/A */
0N/A private static class DragHandler implements DragGestureListener, DragSourceListener {
0N/A
0N/A private boolean scrolls;
0N/A
0N/A // --- DragGestureListener methods -----------------------------------
0N/A
0N/A /**
0N/A * a Drag gesture has been recognized
0N/A */
0N/A public void dragGestureRecognized(DragGestureEvent dge) {
0N/A JComponent c = (JComponent) dge.getComponent();
0N/A TransferHandler th = c.getTransferHandler();
0N/A Transferable t = th.createTransferable(c);
0N/A if (t != null) {
0N/A scrolls = c.getAutoscrolls();
0N/A c.setAutoscrolls(false);
0N/A try {
2129N/A Image im = th.getDragImage();
2129N/A if (im == null) {
2129N/A dge.startDrag(null, t, this);
2129N/A } else {
2129N/A dge.startDrag(null, im, th.getDragImageOffset(), t, this);
2129N/A }
0N/A return;
0N/A } catch (RuntimeException re) {
0N/A c.setAutoscrolls(scrolls);
0N/A }
0N/A }
0N/A
0N/A th.exportDone(c, t, NONE);
0N/A }
0N/A
0N/A // --- DragSourceListener methods -----------------------------------
0N/A
0N/A /**
0N/A * as the hotspot enters a platform dependent drop site
0N/A */
0N/A public void dragEnter(DragSourceDragEvent dsde) {
0N/A }
0N/A
0N/A /**
0N/A * as the hotspot moves over a platform dependent drop site
0N/A */
0N/A public void dragOver(DragSourceDragEvent dsde) {
0N/A }
0N/A
0N/A /**
0N/A * as the hotspot exits a platform dependent drop site
0N/A */
0N/A public void dragExit(DragSourceEvent dsde) {
0N/A }
0N/A
0N/A /**
0N/A * as the operation completes
0N/A */
0N/A public void dragDropEnd(DragSourceDropEvent dsde) {
0N/A DragSourceContext dsc = dsde.getDragSourceContext();
0N/A JComponent c = (JComponent)dsc.getComponent();
0N/A if (dsde.getDropSuccess()) {
0N/A c.getTransferHandler().exportDone(c, dsc.getTransferable(), dsde.getDropAction());
0N/A } else {
0N/A c.getTransferHandler().exportDone(c, dsc.getTransferable(), NONE);
0N/A }
0N/A c.setAutoscrolls(scrolls);
0N/A }
0N/A
0N/A public void dropActionChanged(DragSourceDragEvent dsde) {
0N/A }
0N/A }
0N/A
0N/A private static class SwingDragGestureRecognizer extends DragGestureRecognizer {
0N/A
0N/A SwingDragGestureRecognizer(DragGestureListener dgl) {
0N/A super(DragSource.getDefaultDragSource(), null, NONE, dgl);
0N/A }
0N/A
0N/A void gestured(JComponent c, MouseEvent e, int srcActions, int action) {
0N/A setComponent(c);
0N/A setSourceActions(srcActions);
0N/A appendEvent(e);
0N/A fireDragGestureRecognized(action, e.getPoint());
0N/A }
0N/A
0N/A /**
0N/A * register this DragGestureRecognizer's Listeners with the Component
0N/A */
0N/A protected void registerListeners() {
0N/A }
0N/A
0N/A /**
0N/A * unregister this DragGestureRecognizer's Listeners with the Component
0N/A *
0N/A * subclasses must override this method
0N/A */
0N/A protected void unregisterListeners() {
0N/A }
0N/A
0N/A }
0N/A
0N/A static final Action cutAction = new TransferAction("cut");
0N/A static final Action copyAction = new TransferAction("copy");
0N/A static final Action pasteAction = new TransferAction("paste");
0N/A
0N/A static class TransferAction extends UIAction implements UIResource {
0N/A
0N/A TransferAction(String name) {
0N/A super(name);
0N/A }
0N/A
0N/A public boolean isEnabled(Object sender) {
0N/A if (sender instanceof JComponent
0N/A && ((JComponent)sender).getTransferHandler() == null) {
0N/A return false;
0N/A }
0N/A
0N/A return true;
0N/A }
0N/A
3787N/A private static final JavaSecurityAccess javaSecurityAccess =
3787N/A SharedSecrets.getJavaSecurityAccess();
3787N/A
3787N/A public void actionPerformed(final ActionEvent e) {
3787N/A final Object src = e.getSource();
3787N/A
3787N/A final PrivilegedAction<Void> action = new PrivilegedAction<Void>() {
3787N/A public Void run() {
3787N/A actionPerformedImpl(e);
3787N/A return null;
3787N/A }
3787N/A };
3787N/A
3787N/A final AccessControlContext stack = AccessController.getContext();
3787N/A final AccessControlContext srcAcc = AWTAccessor.getComponentAccessor().getAccessControlContext((Component)src);
3787N/A final AccessControlContext eventAcc = AWTAccessor.getAWTEventAccessor().getAccessControlContext(e);
3787N/A
3787N/A if (srcAcc == null) {
3787N/A javaSecurityAccess.doIntersectionPrivilege(action, stack, eventAcc);
3787N/A } else {
3787N/A javaSecurityAccess.doIntersectionPrivilege(
3787N/A new PrivilegedAction<Void>() {
3787N/A public Void run() {
3787N/A javaSecurityAccess.doIntersectionPrivilege(action, eventAcc);
3787N/A return null;
3787N/A }
3787N/A }, stack, srcAcc);
3787N/A }
3787N/A }
3787N/A
3787N/A private void actionPerformedImpl(ActionEvent e) {
0N/A Object src = e.getSource();
0N/A if (src instanceof JComponent) {
0N/A JComponent c = (JComponent) src;
0N/A TransferHandler th = c.getTransferHandler();
0N/A Clipboard clipboard = getClipboard(c);
0N/A String name = (String) getValue(Action.NAME);
0N/A
0N/A Transferable trans = null;
0N/A
0N/A // any of these calls may throw IllegalStateException
0N/A try {
0N/A if ((clipboard != null) && (th != null) && (name != null)) {
0N/A if ("cut".equals(name)) {
0N/A th.exportToClipboard(c, clipboard, MOVE);
0N/A } else if ("copy".equals(name)) {
0N/A th.exportToClipboard(c, clipboard, COPY);
0N/A } else if ("paste".equals(name)) {
0N/A trans = clipboard.getContents(null);
0N/A }
0N/A }
0N/A } catch (IllegalStateException ise) {
0N/A // clipboard was unavailable
0N/A UIManager.getLookAndFeel().provideErrorFeedback(c);
0N/A return;
0N/A }
0N/A
0N/A // this is a paste action, import data into the component
0N/A if (trans != null) {
0N/A th.importData(new TransferSupport(c, trans));
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the clipboard to use for cut/copy/paste.
0N/A */
0N/A private Clipboard getClipboard(JComponent c) {
0N/A if (SwingUtilities2.canAccessSystemClipboard()) {
0N/A return c.getToolkit().getSystemClipboard();
0N/A }
0N/A Clipboard clipboard = (Clipboard)sun.awt.AppContext.getAppContext().
0N/A get(SandboxClipboardKey);
0N/A if (clipboard == null) {
0N/A clipboard = new Clipboard("Sandboxed Component Clipboard");
0N/A sun.awt.AppContext.getAppContext().put(SandboxClipboardKey,
0N/A clipboard);
0N/A }
0N/A return clipboard;
0N/A }
0N/A
0N/A /**
0N/A * Key used in app context to lookup Clipboard to use if access to
0N/A * System clipboard is denied.
0N/A */
0N/A private static Object SandboxClipboardKey = new Object();
0N/A
0N/A }
0N/A
0N/A}