jdk/src/java.desktop/share/classes/javax/swing/TransferHandler.java
author darcy
Fri, 03 Apr 2015 10:41:34 -0700
changeset 29894 3e16b51732f5
parent 25859 3317bb8137f4
child 32834 e1dca5fe4de3
child 32865 f9cb6e427f9e
permissions -rw-r--r--
8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}} Reviewed-by: alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21278
diff changeset
     2
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4831
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4831
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4831
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4831
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4831
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.datatransfer.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.dnd.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.TooManyListenersException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.plaf.UIResource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.text.JTextComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.reflect.misc.MethodUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.swing.SwingUtilities2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.swing.*;
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2
diff changeset
    43
import sun.awt.SunToolkit;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
8816
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
    45
import java.security.AccessController;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
    46
import java.security.PrivilegedAction;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
    47
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
    48
import java.security.AccessControlContext;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
    49
import java.security.ProtectionDomain;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
    50
import sun.misc.SharedSecrets;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
    51
import sun.misc.JavaSecurityAccess;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
    52
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
    53
import sun.awt.AWTAccessor;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
    54
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * This class is used to handle the transfer of a <code>Transferable</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * to and from Swing components.  The <code>Transferable</code> is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * represent data that is exchanged via a cut, copy, or paste
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * to/from a clipboard.  It is also used in drag-and-drop operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * to represent a drag from a component, and a drop to a component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Swing provides functionality that automatically supports cut, copy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * and paste keyboard bindings that use the functionality provided by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * an implementation of this class.  Swing also provides functionality
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * that automatically supports drag and drop that uses the functionality
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * provided by an implementation of this class.  The Swing developer can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * concentrate on specifying the semantics of a transfer primarily by setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * the <code>transferHandler</code> property on a Swing component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * This class is implemented to provide a default behavior of transferring
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * a component property simply by specifying the name of the property in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * the constructor.  For example, to transfer the foreground color from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * one component to another either via the clipboard or a drag and drop operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * a <code>TransferHandler</code> can be constructed with the string "foreground".  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * built in support will use the color returned by <code>getForeground</code> as the source
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * of the transfer, and <code>setForeground</code> for the target of a transfer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * Please see
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 20169
diff changeset
    78
 * <a href="http://docs.oracle.com/javase/tutorial/uiswing/dnd/index.html">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * How to Use Drag and Drop and Data Transfer</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * a section in <em>The Java Tutorial</em>, for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @author Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @author Shannon Hickey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
@SuppressWarnings("serial")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
public class TransferHandler implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * An <code>int</code> representing no transfer action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public static final int NONE = DnDConstants.ACTION_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * An <code>int</code> representing a &quot;copy&quot; transfer action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * This value is used when data is copied to a clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * or copied elsewhere in a drag and drop operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static final int COPY = DnDConstants.ACTION_COPY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * An <code>int</code> representing a &quot;move&quot; transfer action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * This value is used when data is moved to a clipboard (i.e. a cut)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * or moved elsewhere in a drag and drop operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public static final int MOVE = DnDConstants.ACTION_MOVE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * An <code>int</code> representing a source action capability of either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * &quot;copy&quot; or &quot;move&quot;.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public static final int COPY_OR_MOVE = DnDConstants.ACTION_COPY_OR_MOVE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * An <code>int</code> representing a &quot;link&quot; transfer action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * This value is used to specify that data should be linked in a drag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * and drop operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @see java.awt.dnd.DnDConstants#ACTION_LINK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public static final int LINK = DnDConstants.ACTION_LINK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * An interface to tag things with a {@code getTransferHandler} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    interface HasGetTransferHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        /** Returns the {@code TransferHandler}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
         * @return The {@code TransferHandler} or {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        public TransferHandler getTransferHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Represents a location where dropped data should be inserted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * This is a base class that only encapsulates a point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Components supporting drop may provide subclasses of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * containing more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Developers typically shouldn't create instances of, or extend, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * class. Instead, these are something provided by the DnD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * implementation by <code>TransferSupport</code> instances and by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * components with a <code>getDropLocation()</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @see javax.swing.TransferHandler.TransferSupport#getDropLocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public static class DropLocation {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        private final Point dropPoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
         * Constructs a drop location for the given point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
         * @param dropPoint the drop point, representing the mouse's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
         *        current location within the component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         * @throws IllegalArgumentException if the point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         *         is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        protected DropLocation(Point dropPoint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if (dropPoint == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                throw new IllegalArgumentException("Point cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            this.dropPoint = new Point(dropPoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
         * Returns the drop point, representing the mouse's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
         * current location within the component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
         * @return the drop point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        public final Point getDropPoint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            return new Point(dropPoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
         * Returns a string representation of this drop location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
         * This method is intended to be used for debugging purposes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
         * and the content and format of the returned string may vary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
         * between implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
         * @return a string representation of this drop location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            return getClass().getName() + "[dropPoint=" + dropPoint + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * This class encapsulates all relevant details of a clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * or drag and drop transfer, and also allows for customizing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * aspects of the drag and drop experience.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * The main purpose of this class is to provide the information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * needed by a developer to determine the suitability of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * transfer or to import the data contained within. But it also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * doubles as a controller for customizing properties during drag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * and drop, such as whether or not to show the drop location,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * and which drop action to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Developers typically need not create instances of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * class. Instead, they are something provided by the DnD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * implementation to certain methods in <code>TransferHandler</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @see #canImport(TransferHandler.TransferSupport)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @see #importData(TransferHandler.TransferSupport)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public final static class TransferSupport {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        private boolean isDrop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        private Component component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        private boolean showDropLocationIsSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        private boolean showDropLocation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        private int dropAction = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
         * The source is a {@code DropTargetDragEvent} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
         * {@code DropTargetDropEvent} for drops,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
         * and a {@code Transferable} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        private Object source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        private DropLocation dropLocation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
         * Create a <code>TransferSupport</code> with <code>isDrop()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
         * <code>true</code> for the given component, event, and index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
         * @param component the target component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
         * @param event a <code>DropTargetEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        private TransferSupport(Component component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                             DropTargetEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            isDrop = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            setDNDVariables(component, event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
         * Create a <code>TransferSupport</code> with <code>isDrop()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
         * <code>false</code> for the given component and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
         * <code>Transferable</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
         * @param component the target component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
         * @param transferable the transferable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
         * @throws NullPointerException if either parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
         *         is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        public TransferSupport(Component component, Transferable transferable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if (component == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                throw new NullPointerException("component is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            if (transferable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                throw new NullPointerException("transferable is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            isDrop = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            this.component = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            this.source = transferable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
         * Allows for a single instance to be reused during DnD.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         * @param component the target component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
         * @param event a <code>DropTargetEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        private void setDNDVariables(Component component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                                     DropTargetEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            assert isDrop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            this.component = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            this.source = event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            dropLocation = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            dropAction = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            showDropLocationIsSet = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            if (source == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            assert source instanceof DropTargetDragEvent ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                   source instanceof DropTargetDropEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            Point p = source instanceof DropTargetDragEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                          ? ((DropTargetDragEvent)source).getLocation()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                          : ((DropTargetDropEvent)source).getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2
diff changeset
   297
            if (SunToolkit.isInstanceOf(component, "javax.swing.text.JTextComponent")) {
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2
diff changeset
   298
                dropLocation = SwingAccessor.getJTextComponentAccessor().
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2
diff changeset
   299
                                   dropLocationForPoint((JTextComponent)component, p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            } else if (component instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                dropLocation = ((JComponent)component).dropLocationForPoint(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
             * The drop location may be null at this point if the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
             * doesn't return custom drop locations. In this case, a point-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
             * drop location will be created lazily when requested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
         * Returns whether or not this <code>TransferSupport</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
         * represents a drop operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
         * @return <code>true</code> if this is a drop operation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
         *         <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        public boolean isDrop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            return isDrop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
         * Returns the target component of this transfer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
         * @return the target component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        public Component getComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            return component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
         * Checks that this is a drop and throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
         * {@code IllegalStateException} if it isn't.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
         * @throws IllegalStateException if {@code isDrop} is false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        private void assureIsDrop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            if (!isDrop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                throw new IllegalStateException("Not a drop");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
         * Returns the current (non-{@code null}) drop location for the component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
         * when this {@code TransferSupport} represents a drop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
         * Note: For components with built-in drop support, this location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
         * will be a subclass of {@code DropLocation} of the same type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
         * returned by that component's {@code getDropLocation} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
         * This method is only for use with drag and drop transfers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
         * Calling it when {@code isDrop()} is {@code false} results
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
         * in an {@code IllegalStateException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
         * @return the drop location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
         * @throws IllegalStateException if this is not a drop
7959
2e05332a8f5c 6589952: Swing: dead links in API documentation
rupashka
parents: 5506
diff changeset
   357
         * @see #isDrop()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        public DropLocation getDropLocation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            assureIsDrop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            if (dropLocation == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                 * component didn't give us a custom drop location,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                 * so lazily create a point-only location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                Point p = source instanceof DropTargetDragEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                              ? ((DropTargetDragEvent)source).getLocation()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                              : ((DropTargetDropEvent)source).getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                dropLocation = new DropLocation(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            return dropLocation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
         * Sets whether or not the drop location should be visually indicated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
         * for the transfer - which must represent a drop. This is applicable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
         * those components that automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
         * show the drop location when appropriate during a drag and drop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
         * operation). By default, the drop location is shown only when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
         * {@code TransferHandler} has said it can accept the import represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
         * by this {@code TransferSupport}. With this method you can force the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
         * drop location to always be shown, or always not be shown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
         * This method is only for use with drag and drop transfers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
         * Calling it when {@code isDrop()} is {@code false} results
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
         * in an {@code IllegalStateException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
         * @param showDropLocation whether or not to indicate the drop location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
         * @throws IllegalStateException if this is not a drop
7959
2e05332a8f5c 6589952: Swing: dead links in API documentation
rupashka
parents: 5506
diff changeset
   393
         * @see #isDrop()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        public void setShowDropLocation(boolean showDropLocation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            assureIsDrop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            this.showDropLocation = showDropLocation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            this.showDropLocationIsSet = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
         * Sets the drop action for the transfer - which must represent a drop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
         * - to the given action,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
         * instead of the default user drop action. The action must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
         * supported by the source's drop actions, and must be one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
         * of {@code COPY}, {@code MOVE} or {@code LINK}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
         * This method is only for use with drag and drop transfers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
         * Calling it when {@code isDrop()} is {@code false} results
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
         * in an {@code IllegalStateException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
         * @param dropAction the drop action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
         * @throws IllegalStateException if this is not a drop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
         * @throws IllegalArgumentException if an invalid action is specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
         * @see #getDropAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
         * @see #getUserDropAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
         * @see #getSourceDropActions
7959
2e05332a8f5c 6589952: Swing: dead links in API documentation
rupashka
parents: 5506
diff changeset
   419
         * @see #isDrop()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        public void setDropAction(int dropAction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            assureIsDrop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            int action = dropAction & getSourceDropActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            if (!(action == COPY || action == MOVE || action == LINK)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                throw new IllegalArgumentException("unsupported drop action: " + dropAction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            this.dropAction = dropAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
         * Returns the action chosen for the drop, when this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
         * {@code TransferSupport} represents a drop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
         * Unless explicitly chosen by way of {@code setDropAction},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
         * this returns the user drop action provided by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
         * {@code getUserDropAction}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
         * You may wish to query this in {@code TransferHandler}'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
         * {@code importData} method to customize processing based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
         * on the action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
         * This method is only for use with drag and drop transfers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
         * Calling it when {@code isDrop()} is {@code false} results
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
         * in an {@code IllegalStateException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
         * @return the action chosen for the drop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
         * @throws IllegalStateException if this is not a drop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
         * @see #setDropAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
         * @see #getUserDropAction
7959
2e05332a8f5c 6589952: Swing: dead links in API documentation
rupashka
parents: 5506
diff changeset
   453
         * @see #isDrop()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        public int getDropAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            return dropAction == -1 ? getUserDropAction() : dropAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
         * Returns the user drop action for the drop, when this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
         * {@code TransferSupport} represents a drop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
         * The user drop action is chosen for a drop as described in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
         * documentation for {@link java.awt.dnd.DropTargetDragEvent} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
         * {@link java.awt.dnd.DropTargetDropEvent}. A different action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
         * may be chosen as the drop action by way of the {@code setDropAction}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
         * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
         * You may wish to query this in {@code TransferHandler}'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
         * {@code canImport} method when determining the suitability of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
         * drop or when deciding on a drop action to explicitly choose.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
         * This method is only for use with drag and drop transfers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
         * Calling it when {@code isDrop()} is {@code false} results
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
         * in an {@code IllegalStateException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
         * @return the user drop action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
         * @throws IllegalStateException if this is not a drop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
         * @see #setDropAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
         * @see #getDropAction
7959
2e05332a8f5c 6589952: Swing: dead links in API documentation
rupashka
parents: 5506
diff changeset
   481
         * @see #isDrop()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        public int getUserDropAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            assureIsDrop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            return (source instanceof DropTargetDragEvent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                ? ((DropTargetDragEvent)source).getDropAction()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                : ((DropTargetDropEvent)source).getDropAction();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
         * Returns the drag source's supported drop actions, when this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
         * {@code TransferSupport} represents a drop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
         * The source actions represent the set of actions supported by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
         * source of this transfer, and are represented as some bitwise-OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
         * combination of {@code COPY}, {@code MOVE} and {@code LINK}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
         * You may wish to query this in {@code TransferHandler}'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
         * {@code canImport} method when determining the suitability of a drop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
         * or when deciding on a drop action to explicitly choose. To determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
         * if a particular action is supported by the source, bitwise-AND
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
         * the action with the source drop actions, and then compare the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
         * against the original action. For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
         * <pre>
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 20130
diff changeset
   505
         * boolean copySupported = (COPY &amp; getSourceDropActions()) == COPY;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
         * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
         * This method is only for use with drag and drop transfers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
         * Calling it when {@code isDrop()} is {@code false} results
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
         * in an {@code IllegalStateException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
         * @return the drag source's supported drop actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
         * @throws IllegalStateException if this is not a drop
7959
2e05332a8f5c 6589952: Swing: dead links in API documentation
rupashka
parents: 5506
diff changeset
   514
         * @see #isDrop()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        public int getSourceDropActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            assureIsDrop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            return (source instanceof DropTargetDragEvent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                ? ((DropTargetDragEvent)source).getSourceActions()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                : ((DropTargetDropEvent)source).getSourceActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
         * Returns the data flavors for this transfer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
         * @return the data flavors for this transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        public DataFlavor[] getDataFlavors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            if (isDrop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                if (source instanceof DropTargetDragEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    return ((DropTargetDragEvent)source).getCurrentDataFlavors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    return ((DropTargetDropEvent)source).getCurrentDataFlavors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            return ((Transferable)source).getTransferDataFlavors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
         * Returns whether or not the given data flavor is supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
         * @param df the <code>DataFlavor</code> to test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
         * @return whether or not the given flavor is supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        public boolean isDataFlavorSupported(DataFlavor df) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            if (isDrop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                if (source instanceof DropTargetDragEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    return ((DropTargetDragEvent)source).isDataFlavorSupported(df);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                    return ((DropTargetDropEvent)source).isDataFlavorSupported(df);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            return ((Transferable)source).isDataFlavorSupported(df);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
         * Returns the <code>Transferable</code> associated with this transfer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
         * Note: Unless it is necessary to fetch the <code>Transferable</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
         * directly, use one of the other methods on this class to inquire about
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
         * the transfer. This may perform better than fetching the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
         * <code>Transferable</code> and asking it directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
         * @return the <code>Transferable</code> associated with this transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        public Transferable getTransferable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            if (isDrop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                if (source instanceof DropTargetDragEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                    return ((DropTargetDragEvent)source).getTransferable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    return ((DropTargetDropEvent)source).getTransferable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            return (Transferable)source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * Returns an {@code Action} that performs cut operations to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * clipboard. When performed, this action operates on the {@code JComponent}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * source of the {@code ActionEvent} by invoking {@code exportToClipboard},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * with a {@code MOVE} action, on the component's {@code TransferHandler}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @return an {@code Action} for performing cuts to the clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    public static Action getCutAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        return cutAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * Returns an {@code Action} that performs copy operations to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * clipboard. When performed, this action operates on the {@code JComponent}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * source of the {@code ActionEvent} by invoking {@code exportToClipboard},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * with a {@code COPY} action, on the component's {@code TransferHandler}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @return an {@code Action} for performing copies to the clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    public static Action getCopyAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        return copyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * Returns an {@code Action} that performs paste operations from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * clipboard. When performed, this action operates on the {@code JComponent}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * source of the {@code ActionEvent} by invoking {@code importData},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * with the clipboard contents, on the component's {@code TransferHandler}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * @return an {@code Action} for performing pastes from the clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    public static Action getPasteAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        return pasteAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * Constructs a transfer handler that can transfer a Java Bean property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * from one component to another via the clipboard or a drag and drop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * @param property  the name of the property to transfer; this can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     *  be <code>null</code> if there is no property associated with the transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *  handler (a subclass that performs some other kind of transfer, for example)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    public TransferHandler(String property) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        propertyName = property;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * Convenience constructor for subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    protected TransferHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        this(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
4831
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   640
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   641
    /**
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   642
     * image for the {@code startDrag} method
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   643
     *
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   644
     * @see java.awt.dnd.DragGestureEvent#startDrag(Cursor dragCursor, Image dragImage, Point imageOffset, Transferable transferable, DragSourceListener dsl)
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   645
     */
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   646
    private  Image dragImage;
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   647
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   648
    /**
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   649
     * anchor offset for the {@code startDrag} method
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   650
     *
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   651
     * @see java.awt.dnd.DragGestureEvent#startDrag(Cursor dragCursor, Image dragImage, Point imageOffset, Transferable transferable, DragSourceListener dsl)
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   652
     */
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   653
    private  Point dragImageOffset;
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   654
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   655
    /**
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   656
     * Sets the drag image parameter. The image has to be prepared
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   657
     * for rendering by the moment of the call. The image is stored
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   658
     * by reference because of some performance reasons.
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   659
     *
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   660
     * @param img an image to drag
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   661
     */
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   662
    public void setDragImage(Image img) {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   663
        dragImage = img;
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   664
    }
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   665
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   666
    /**
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   667
     * Returns the drag image. If there is no image to drag,
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   668
     * the returned value is {@code null}.
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   669
     *
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   670
     * @return the reference to the drag image
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   671
     */
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   672
    public Image getDragImage() {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   673
        return dragImage;
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   674
    }
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   675
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   676
    /**
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   677
     * Sets an anchor offset for the image to drag.
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   678
     * It can not be {@code null}.
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   679
     *
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   680
     * @param p a {@code Point} object that corresponds
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   681
     * to coordinates of an anchor offset of the image
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   682
     * relative to the upper left corner of the image
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   683
     */
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   684
    public void setDragImageOffset(Point p) {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   685
        dragImageOffset = new Point(p);
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   686
    }
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   687
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   688
    /**
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   689
     * Returns an anchor offset for the image to drag.
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   690
     *
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   691
     * @return a {@code Point} object that corresponds
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   692
     * to coordinates of an anchor offset of the image
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   693
     * relative to the upper left corner of the image.
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   694
     * The point {@code (0,0)} returns by default.
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   695
     */
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   696
    public Point getDragImageOffset() {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   697
        if (dragImageOffset == null) {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   698
            return new Point(0,0);
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   699
        }
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   700
        return new Point(dragImageOffset);
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   701
    }
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
   702
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * Causes the Swing drag support to be initiated.  This is called by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * the various UI implementations in the <code>javax.swing.plaf.basic</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * package if the dragEnabled property is set on the component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * This can be called by custom UI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * implementations to use the Swing drag support.  This method can also be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * by a Swing extension written as a subclass of <code>JComponent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * to take advantage of the Swing drag support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * The transfer <em>will not necessarily</em> have been completed at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * return of this call (i.e. the call does not block waiting for the drop).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * The transfer will take place through the Swing implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * <code>java.awt.dnd</code> mechanism, requiring no further effort
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * from the developer. The <code>exportDone</code> method will be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * when the transfer has completed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * @param comp  the component holding the data to be transferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     *              provided to enable sharing of <code>TransferHandler</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * @param e     the event that triggered the transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @param action the transfer action initially requested;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *               either {@code COPY}, {@code MOVE} or {@code LINK};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     *               the DnD system may change the action used during the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     *               course of the drag operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    public void exportAsDrag(JComponent comp, InputEvent e, int action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        int srcActions = getSourceActions(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        // only mouse events supported for drag operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        if (!(e instanceof MouseEvent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                // only support known actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                || !(action == COPY || action == MOVE || action == LINK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                // only support valid source actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                || (srcActions & action) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            action = NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        if (action != NONE && !GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            if (recognizer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                recognizer = new SwingDragGestureRecognizer(new DragHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            recognizer.gestured(comp, (MouseEvent)e, srcActions, action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            exportDone(comp, null, NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * Causes a transfer from the given component to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * given clipboard.  This method is called by the default cut and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * copy actions registered in a component's action map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * The transfer will take place using the <code>java.awt.datatransfer</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * mechanism, requiring no further effort from the developer. Any data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * transfer <em>will</em> be complete and the <code>exportDone</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * method will be called with the action that occurred, before this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * returns. Should the clipboard be unavailable when attempting to place
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * data on it, the <code>IllegalStateException</code> thrown by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * {@link Clipboard#setContents(Transferable, ClipboardOwner)} will
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20455
diff changeset
   762
     * be propagated through this method. However,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * <code>exportDone</code> will first be called with an action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * of <code>NONE</code> for consistency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * @param comp  the component holding the data to be transferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     *              provided to enable sharing of <code>TransferHandler</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * @param clip  the clipboard to transfer the data into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * @param action the transfer action requested; this should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     *  be a value of either <code>COPY</code> or <code>MOVE</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     *  the operation performed is the intersection  of the transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     *  capabilities given by getSourceActions and the requested action;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     *  the intersection may result in an action of <code>NONE</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     *  if the requested action isn't supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * @throws IllegalStateException if the clipboard is currently unavailable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * @see Clipboard#setContents(Transferable, ClipboardOwner)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    public void exportToClipboard(JComponent comp, Clipboard clip, int action)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                                                  throws IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        if ((action == COPY || action == MOVE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                && (getSourceActions(comp) & action) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            Transferable t = createTransferable(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            if (t != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                    clip.setContents(t, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                    exportDone(comp, t, action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                } catch (IllegalStateException ise) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                    exportDone(comp, t, NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                    throw ise;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        exportDone(comp, null, NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * Causes a transfer to occur from a clipboard or a drag and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * drop operation. The <code>Transferable</code> to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * imported and the component to transfer to are contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * within the <code>TransferSupport</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * While the drag and drop implementation calls {@code canImport}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * to determine the suitability of a transfer before calling this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * method, the implementation of paste does not. As such, it cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * be assumed that the transfer is acceptable upon a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * this method for paste. It is recommended that {@code canImport} be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * explicitly called to cover this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * Note: The <code>TransferSupport</code> object passed to this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * is only valid for the duration of the method call. It is undefined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * what values it may contain after this method returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * @param support the object containing the details of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     *        the transfer, not <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * @return true if the data was inserted into the component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     *         false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @throws NullPointerException if <code>support</code> is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * @see #canImport(TransferHandler.TransferSupport)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    public boolean importData(TransferSupport support) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        return support.getComponent() instanceof JComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            ? importData((JComponent)support.getComponent(), support.getTransferable())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * Causes a transfer to a component from a clipboard or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * DND drop operation.  The <code>Transferable</code> represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * the data to be imported into the component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * Note: Swing now calls the newer version of <code>importData</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * that takes a <code>TransferSupport</code>, which in turn calls this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * method (if the component in the {@code TransferSupport} is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * {@code JComponent}). Developers are encouraged to call and override the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * newer version as it provides more information (and is the only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * version that supports use with a {@code TransferHandler} set directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * on a {@code JFrame} or other non-{@code JComponent}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * @param comp  the component to receive the transfer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     *              provided to enable sharing of <code>TransferHandler</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * @param t     the data to import
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * @return  true if the data was inserted into the component, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * @see #importData(TransferHandler.TransferSupport)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    public boolean importData(JComponent comp, Transferable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        PropertyDescriptor prop = getPropertyDescriptor(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        if (prop != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            Method writer = prop.getWriteMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            if (writer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                // read-only property. ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            Class<?>[] params = writer.getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            if (params.length != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                // zero or more than one argument, ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            DataFlavor flavor = getPropertyDataFlavor(params[0], t.getTransferDataFlavors());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            if (flavor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                    Object value = t.getTransferData(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                    Object[] args = { value };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                    MethodUtil.invoke(writer, comp, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                    System.err.println("Invocation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                    // invocation code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * This method is called repeatedly during a drag and drop operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * to allow the developer to configure properties of, and to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * the acceptability of transfers; with a return value of {@code true}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * indicating that the transfer represented by the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * {@code TransferSupport} (which contains all of the details of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * transfer) is acceptable at the current time, and a value of {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * rejecting the transfer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * For those components that automatically display a drop location during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * drag and drop, accepting the transfer, by default, tells them to show
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * the drop location. This can be changed by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * {@code setShowDropLocation} on the {@code TransferSupport}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * By default, when the transfer is accepted, the chosen drop action is that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * picked by the user via their drag gesture. The developer can override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * this and choose a different action, from the supported source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * actions, by calling {@code setDropAction} on the {@code TransferSupport}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * On every call to {@code canImport}, the {@code TransferSupport} contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * fresh state. As such, any properties set on it must be set on every
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * call. Upon a drop, {@code canImport} is called one final time before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * calling into {@code importData}. Any state set on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * {@code TransferSupport} during that last call will be available in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * {@code importData}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * This method is not called internally in response to paste operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * As such, it is recommended that implementations of {@code importData}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * explicitly call this method for such cases and that this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * be prepared to return the suitability of paste operations as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * Note: The <code>TransferSupport</code> object passed to this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * is only valid for the duration of the method call. It is undefined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * what values it may contain after this method returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * @param support the object containing the details of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     *        the transfer, not <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * @return <code>true</code> if the import can happen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     *         <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * @throws NullPointerException if <code>support</code> is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * @see #importData(TransferHandler.TransferSupport)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * @see javax.swing.TransferHandler.TransferSupport#setShowDropLocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * @see javax.swing.TransferHandler.TransferSupport#setDropAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    public boolean canImport(TransferSupport support) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        return support.getComponent() instanceof JComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            ? canImport((JComponent)support.getComponent(), support.getDataFlavors())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * Indicates whether a component will accept an import of the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * set of data flavors prior to actually attempting to import it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * Note: Swing now calls the newer version of <code>canImport</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * that takes a <code>TransferSupport</code>, which in turn calls this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * method (only if the component in the {@code TransferSupport} is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * {@code JComponent}). Developers are encouraged to call and override the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * newer version as it provides more information (and is the only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * version that supports use with a {@code TransferHandler} set directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * on a {@code JFrame} or other non-{@code JComponent}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * @param comp  the component to receive the transfer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     *              provided to enable sharing of <code>TransferHandler</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * @param transferFlavors  the data formats available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * @return  true if the data can be inserted into the component, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * @see #canImport(TransferHandler.TransferSupport)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        PropertyDescriptor prop = getPropertyDescriptor(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        if (prop != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            Method writer = prop.getWriteMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            if (writer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                // read-only property. ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            Class<?>[] params = writer.getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            if (params.length != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                // zero or more than one argument, ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            DataFlavor flavor = getPropertyDataFlavor(params[0], transferFlavors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            if (flavor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * Returns the type of transfer actions supported by the source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * any bitwise-OR combination of {@code COPY}, {@code MOVE}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * and {@code LINK}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * Some models are not mutable, so a transfer operation of {@code MOVE}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * should not be advertised in that case. Returning {@code NONE}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * disables transfers from the component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * @param c  the component holding the data to be transferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     *           provided to enable sharing of <code>TransferHandler</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * @return {@code COPY} if the transfer property can be found,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     *          otherwise returns <code>NONE</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    public int getSourceActions(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        PropertyDescriptor prop = getPropertyDescriptor(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        if (prop != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            return COPY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        return NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * Returns an object that establishes the look of a transfer.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * useful for both providing feedback while performing a drag operation and for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * representing the transfer in a clipboard implementation that has a visual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * appearance.  The implementation of the <code>Icon</code> interface should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * not alter the graphics clip or alpha level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * The icon implementation need not be rectangular or paint all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * bounding rectangle and logic that calls the icons paint method should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * not assume the all bits are painted. <code>null</code> is a valid return value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * for this method and indicates there is no visual representation provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * In that case, the calling logic is free to represent the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * transferable however it wants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * The default Swing logic will not do an alpha blended drag animation if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * the return is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * @param t  the data to be transferred; this value is expected to have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     *  created by the <code>createTransferable</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * @return  <code>null</code>, indicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     *    there is no default visual representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    public Icon getVisualRepresentation(Transferable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * Creates a <code>Transferable</code> to use as the source for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * a data transfer. Returns the representation of the data to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * be transferred, or <code>null</code> if the component's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * property is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * @param c  the component holding the data to be transferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     *              provided to enable sharing of <code>TransferHandler</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * @return  the representation of the data to be transferred, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     *  <code>null</code> if the property associated with <code>c</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     *  is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    protected Transferable createTransferable(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        PropertyDescriptor property = getPropertyDescriptor(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        if (property != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            return new PropertyTransferable(property, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * Invoked after data has been exported.  This method should remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * the data that was transferred if the action was <code>MOVE</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * This method is implemented to do nothing since <code>MOVE</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * is not a supported action of this implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * (<code>getSourceActions</code> does not include <code>MOVE</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * @param source the component that was the source of the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * @param data   The data that was transferred or possibly null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     *               if the action is <code>NONE</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * @param action the actual action that was performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    protected void exportDone(JComponent source, Transferable data, int action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * Fetches the property descriptor for the property assigned to this transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * handler on the given component (transfer handler may be shared).  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * returns <code>null</code> if the property descriptor can't be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * or there is an error attempting to fetch the property descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    private PropertyDescriptor getPropertyDescriptor(JComponent comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        if (propertyName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        Class<?> k = comp.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        BeanInfo bi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            bi = Introspector.getBeanInfo(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        } catch (IntrospectionException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        PropertyDescriptor props[] = bi.getPropertyDescriptors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        for (int i=0; i < props.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            if (propertyName.equals(props[i].getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                Method reader = props[i].getReadMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                if (reader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                    Class<?>[] params = reader.getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                    if (params == null || params.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                        // found the desired descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                        return props[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * Fetches the data flavor from the array of possible flavors that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * has data of the type represented by property type.  Null is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * returned if there is no match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    private DataFlavor getPropertyDataFlavor(Class<?> k, DataFlavor[] flavors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        for(int i = 0; i < flavors.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            DataFlavor flavor = flavors[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            if ("application".equals(flavor.getPrimaryType()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                "x-java-jvm-local-objectref".equals(flavor.getSubType()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                k.isAssignableFrom(flavor.getRepresentationClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                return flavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    private String propertyName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
    private static SwingDragGestureRecognizer recognizer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    private static DropTargetListener getDropTargetListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        synchronized(DropHandler.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            DropHandler handler =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                (DropHandler)AppContext.getAppContext().get(DropHandler.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            if (handler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                handler = new DropHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                AppContext.getAppContext().put(DropHandler.class, handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    static class PropertyTransferable implements Transferable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        PropertyTransferable(PropertyDescriptor p, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            property = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            component = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        // --- Transferable methods ----------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
         * Returns an array of <code>DataFlavor</code> objects indicating the flavors the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
         * can be provided in.  The array should be ordered according to preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
         * for providing the data (from most richly descriptive to least descriptive).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
         * @return an array of data flavors in which this data can be transferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        public DataFlavor[] getTransferDataFlavors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            DataFlavor[] flavors = new DataFlavor[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            Class<?> propertyType = property.getPropertyType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            String mimeType = DataFlavor.javaJVMLocalObjectMimeType + ";class=" + propertyType.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                flavors[0] = new DataFlavor(mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            } catch (ClassNotFoundException cnfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                flavors = new DataFlavor[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            return flavors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
         * Returns whether the specified data flavor is supported for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
         * this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
         * @param flavor the requested flavor for the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
         * @return true if this <code>DataFlavor</code> is supported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
         *   otherwise false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        public boolean isDataFlavorSupported(DataFlavor flavor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            Class<?> propertyType = property.getPropertyType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            if ("application".equals(flavor.getPrimaryType()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                "x-java-jvm-local-objectref".equals(flavor.getSubType()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                flavor.getRepresentationClass().isAssignableFrom(propertyType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
         * Returns an object which represents the data to be transferred.  The class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
         * of the object returned is defined by the representation class of the flavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
         * @param flavor the requested flavor for the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
         * @see DataFlavor#getRepresentationClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
         * @exception IOException                if the data is no longer available
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
         *              in the requested flavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
         * @exception UnsupportedFlavorException if the requested data flavor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
         *              not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            if (! isDataFlavorSupported(flavor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                throw new UnsupportedFlavorException(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            Method reader = property.getReadMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            Object value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                value = MethodUtil.invoke(reader, component, (Object[])null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                throw new IOException("Property read failed: " + property.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        JComponent component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        PropertyDescriptor property;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     * This is the default drop target for drag and drop operations if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * one isn't provided by the developer.  <code>DropTarget</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * only supports one <code>DropTargetListener</code> and doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * function properly if it isn't set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * This class sets the one listener as the linkage of drop handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * to the <code>TransferHandler</code>, and adds support for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * additional listeners which some of the <code>ComponentUI</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * implementations install to manipulate a drop insertion location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    static class SwingDropTarget extends DropTarget implements UIResource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        SwingDropTarget(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            super(c, COPY_OR_MOVE | LINK, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                // addDropTargetListener is overridden
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                // we specifically need to add to the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                super.addDropTargetListener(getDropTargetListener());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            } catch (TooManyListenersException tmle) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        public void addDropTargetListener(DropTargetListener dtl) throws TooManyListenersException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            // Since the super class only supports one DropTargetListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            // and we add one from the constructor, we always add to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            // extended list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            if (listenerList == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                listenerList = new EventListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            listenerList.add(DropTargetListener.class, dtl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        public void removeDropTargetListener(DropTargetListener dtl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            if (listenerList != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                listenerList.remove(DropTargetListener.class, dtl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        // --- DropTargetListener methods (multicast) --------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        public void dragEnter(DropTargetDragEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            super.dragEnter(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            if (listenerList != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                    if (listeners[i]==DropTargetListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                        ((DropTargetListener)listeners[i+1]).dragEnter(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        public void dragOver(DropTargetDragEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            super.dragOver(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            if (listenerList != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                    if (listeners[i]==DropTargetListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                        ((DropTargetListener)listeners[i+1]).dragOver(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        public void dragExit(DropTargetEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            super.dragExit(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            if (listenerList != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                    if (listeners[i]==DropTargetListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                        ((DropTargetListener)listeners[i+1]).dragExit(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
            }
20130
5e4ae22bef47 8015455: java/awt/dnd/DisposeFrameOnDragCrash/DisposeFrameOnDragTest.java hangs on windows
pchelko
parents: 9035
diff changeset
  1271
            if (!isActive()) {
5e4ae22bef47 8015455: java/awt/dnd/DisposeFrameOnDragCrash/DisposeFrameOnDragTest.java hangs on windows
pchelko
parents: 9035
diff changeset
  1272
                // If the Drop target is inactive the dragExit will not be dispatched to the dtListener,
5e4ae22bef47 8015455: java/awt/dnd/DisposeFrameOnDragCrash/DisposeFrameOnDragTest.java hangs on windows
pchelko
parents: 9035
diff changeset
  1273
                // so make sure that we clean up the dtListener anyway.
5e4ae22bef47 8015455: java/awt/dnd/DisposeFrameOnDragCrash/DisposeFrameOnDragTest.java hangs on windows
pchelko
parents: 9035
diff changeset
  1274
                DropTargetListener dtListener = getDropTargetListener();
5e4ae22bef47 8015455: java/awt/dnd/DisposeFrameOnDragCrash/DisposeFrameOnDragTest.java hangs on windows
pchelko
parents: 9035
diff changeset
  1275
                    if (dtListener != null && dtListener instanceof DropHandler) {
5e4ae22bef47 8015455: java/awt/dnd/DisposeFrameOnDragCrash/DisposeFrameOnDragTest.java hangs on windows
pchelko
parents: 9035
diff changeset
  1276
                        ((DropHandler)dtListener).cleanup(false);
5e4ae22bef47 8015455: java/awt/dnd/DisposeFrameOnDragCrash/DisposeFrameOnDragTest.java hangs on windows
pchelko
parents: 9035
diff changeset
  1277
                    }
5e4ae22bef47 8015455: java/awt/dnd/DisposeFrameOnDragCrash/DisposeFrameOnDragTest.java hangs on windows
pchelko
parents: 9035
diff changeset
  1278
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        public void drop(DropTargetDropEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
            super.drop(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
            if (listenerList != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                    if (listeners[i]==DropTargetListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                        ((DropTargetListener)listeners[i+1]).drop(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        public void dropActionChanged(DropTargetDragEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
            super.dropActionChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            if (listenerList != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                    if (listeners[i]==DropTargetListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                        ((DropTargetListener)listeners[i+1]).dropActionChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        private EventListenerList listenerList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
    private static class DropHandler implements DropTargetListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
                                                Serializable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                                                ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        private Timer timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        private Point lastPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        private Rectangle outer = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        private Rectangle inner = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        private int hysteresis = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        private Component component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        private Object state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        private TransferSupport support =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
            new TransferSupport(null, (DropTargetEvent)null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        private static final int AUTOSCROLL_INSET = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
         * Update the geometry of the autoscroll region.  The geometry is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
         * maintained as a pair of rectangles.  The region can cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
         * a scroll if the pointer sits inside it for the duration of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
         * timer.  The region that causes the timer countdown is the area
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
         * between the two rectangles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
         * This is implemented to use the visible area of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
         * as the outer rectangle, and the insets are fixed at 10. Should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
         * the component be smaller than a total of 20 in any direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
         * autoscroll will not occur in that direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        private void updateAutoscrollRegion(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            // compute the outer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
            Rectangle visible = c.getVisibleRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            outer.setBounds(visible.x, visible.y, visible.width, visible.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
            // compute the insets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            Insets i = new Insets(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
            if (c instanceof Scrollable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                int minSize = 2 * AUTOSCROLL_INSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                if (visible.width >= minSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                    i.left = i.right = AUTOSCROLL_INSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                if (visible.height >= minSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                    i.top = i.bottom = AUTOSCROLL_INSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
            // set the inner from the insets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            inner.setBounds(visible.x + i.left,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                          visible.y + i.top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                          visible.width - (i.left + i.right),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                          visible.height - (i.top  + i.bottom));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
         * Perform an autoscroll operation.  This is implemented to scroll by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
         * unit increment of the Scrollable using scrollRectToVisible.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
         * cursor is in a corner of the autoscroll region, more than one axis will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
         * scroll.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        private void autoscroll(JComponent c, Point pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
            if (c instanceof Scrollable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                Scrollable s = (Scrollable) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                if (pos.y < inner.y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                    // scroll upward
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                    int dy = s.getScrollableUnitIncrement(outer, SwingConstants.VERTICAL, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                    Rectangle r = new Rectangle(inner.x, outer.y - dy, inner.width, dy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                    c.scrollRectToVisible(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                } else if (pos.y > (inner.y + inner.height)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                    // scroll downard
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                    int dy = s.getScrollableUnitIncrement(outer, SwingConstants.VERTICAL, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                    Rectangle r = new Rectangle(inner.x, outer.y + outer.height, inner.width, dy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                    c.scrollRectToVisible(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                if (pos.x < inner.x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                    // scroll left
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                    int dx = s.getScrollableUnitIncrement(outer, SwingConstants.HORIZONTAL, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                    Rectangle r = new Rectangle(outer.x - dx, inner.y, dx, inner.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                    c.scrollRectToVisible(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                } else if (pos.x > (inner.x + inner.width)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                    // scroll right
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                    int dx = s.getScrollableUnitIncrement(outer, SwingConstants.HORIZONTAL, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                    Rectangle r = new Rectangle(outer.x + outer.width, inner.y, dx, inner.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                    c.scrollRectToVisible(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
         * Initializes the internal properties if they haven't been already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
         * inited. This is done lazily to avoid loading of desktop properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        private void initPropertiesIfNecessary() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            if (timer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                Toolkit t = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                Integer prop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                prop = (Integer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                    t.getDesktopProperty("DnD.Autoscroll.interval");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                timer = new Timer(prop == null ? 100 : prop.intValue(), this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
                prop = (Integer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
                    t.getDesktopProperty("DnD.Autoscroll.initialDelay");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
                timer.setInitialDelay(prop == null ? 100 : prop.intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                prop = (Integer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
                    t.getDesktopProperty("DnD.Autoscroll.cursorHysteresis");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
                if (prop != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
                    hysteresis = prop.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
         * The timer fired, perform autoscroll if the pointer is within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
         * autoscroll region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
         * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
         * @param e the <code>ActionEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
            updateAutoscrollRegion((JComponent)component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            if (outer.contains(lastPosition) && !inner.contains(lastPosition)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                autoscroll((JComponent)component, lastPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
        // --- DropTargetListener methods -----------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
        private void setComponentDropLocation(TransferSupport support,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                                              boolean forDrop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
            DropLocation dropLocation = (support == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                                        ? null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                                        : support.getDropLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2
diff changeset
  1448
            if (SunToolkit.isInstanceOf(component, "javax.swing.text.JTextComponent")) {
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2
diff changeset
  1449
                state = SwingAccessor.getJTextComponentAccessor().
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 2
diff changeset
  1450
                            setDropLocation((JTextComponent)component, dropLocation, state, forDrop);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
            } else if (component instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                state = ((JComponent)component).setDropLocation(dropLocation, state, forDrop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        private void handleDrag(DropTargetDragEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
            TransferHandler importer =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
                ((HasGetTransferHandler)component).getTransferHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
            if (importer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                e.rejectDrag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
                setComponentDropLocation(null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
            support.setDNDVariables(component, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
            boolean canImport = importer.canImport(support);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
            if (canImport) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
                e.acceptDrag(support.getDropAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
                e.rejectDrag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
            boolean showLocation = support.showDropLocationIsSet ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
                                   support.showDropLocation :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
                                   canImport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
            setComponentDropLocation(showLocation ? support : null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        public void dragEnter(DropTargetDragEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
            state = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
            component = e.getDropTargetContext().getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
            handleDrag(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
            if (component instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                lastPosition = e.getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
                updateAutoscrollRegion((JComponent)component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                initPropertiesIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        public void dragOver(DropTargetDragEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
            handleDrag(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
            if (!(component instanceof JComponent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
            Point p = e.getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
            if (Math.abs(p.x - lastPosition.x) > hysteresis
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
                    || Math.abs(p.y - lastPosition.y) > hysteresis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                // no autoscroll
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
                if (timer.isRunning()) timer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
                if (!timer.isRunning()) timer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
            lastPosition = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        public void dragExit(DropTargetEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
            cleanup(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        public void drop(DropTargetDropEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
            TransferHandler importer =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
                ((HasGetTransferHandler)component).getTransferHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
            if (importer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                e.rejectDrop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                cleanup(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
            support.setDNDVariables(component, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
            boolean canImport = importer.canImport(support);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
            if (canImport) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                e.acceptDrop(support.getDropAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                boolean showLocation = support.showDropLocationIsSet ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                                       support.showDropLocation :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                                       canImport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                setComponentDropLocation(showLocation ? support : null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                boolean success;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                    success = importer.importData(support);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                } catch (RuntimeException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                    success = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                e.dropComplete(success);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                cleanup(success);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                e.rejectDrop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                cleanup(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        public void dropActionChanged(DropTargetDragEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
             * Work-around for Linux bug where dropActionChanged
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
             * is called before dragEnter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
            if (component == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
            handleDrag(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
        private void cleanup(boolean forDrop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
            setComponentDropLocation(null, forDrop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            if (component instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                ((JComponent)component).dndDone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
            if (timer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                timer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
            state = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
            component = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
            lastPosition = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
     * This is the default drag handler for drag and drop operations that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
     * use the <code>TransferHandler</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
    private static class DragHandler implements DragGestureListener, DragSourceListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
        private boolean scrolls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
        // --- DragGestureListener methods -----------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
         * a Drag gesture has been recognized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
        public void dragGestureRecognized(DragGestureEvent dge) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
            JComponent c = (JComponent) dge.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            TransferHandler th = c.getTransferHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
            Transferable t = th.createTransferable(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            if (t != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                scrolls = c.getAutoscrolls();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                c.setAutoscrolls(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                try {
4831
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
  1606
                    Image im = th.getDragImage();
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
  1607
                    if (im == null) {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
  1608
                        dge.startDrag(null, t, this);
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
  1609
                    } else {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
  1610
                        dge.startDrag(null, im, th.getDragImageOffset(), t, this);
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents: 3084
diff changeset
  1611
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                } catch (RuntimeException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                    c.setAutoscrolls(scrolls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
            th.exportDone(c, t, NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
        // --- DragSourceListener methods -----------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
         * as the hotspot enters a platform dependent drop site
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        public void dragEnter(DragSourceDragEvent dsde) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
         * as the hotspot moves over a platform dependent drop site
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        public void dragOver(DragSourceDragEvent dsde) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
         * as the hotspot exits a platform dependent drop site
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
        public void dragExit(DragSourceEvent dsde) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
         * as the operation completes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
        public void dragDropEnd(DragSourceDropEvent dsde) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
            DragSourceContext dsc = dsde.getDragSourceContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
            JComponent c = (JComponent)dsc.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
            if (dsde.getDropSuccess()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                c.getTransferHandler().exportDone(c, dsc.getTransferable(), dsde.getDropAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                c.getTransferHandler().exportDone(c, dsc.getTransferable(), NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
            c.setAutoscrolls(scrolls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
        public void dropActionChanged(DragSourceDragEvent dsde) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
    private static class SwingDragGestureRecognizer extends DragGestureRecognizer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
        SwingDragGestureRecognizer(DragGestureListener dgl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
            super(DragSource.getDefaultDragSource(), null, NONE, dgl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
        void gestured(JComponent c, MouseEvent e, int srcActions, int action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
            setComponent(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
            setSourceActions(srcActions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
            appendEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
            fireDragGestureRecognized(action, e.getPoint());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
         * register this DragGestureRecognizer's Listeners with the Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        protected void registerListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
         * unregister this DragGestureRecognizer's Listeners with the Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
         * subclasses must override this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
        protected void unregisterListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
    static final Action cutAction = new TransferAction("cut");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
    static final Action copyAction = new TransferAction("copy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
    static final Action pasteAction = new TransferAction("paste");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
    static class TransferAction extends UIAction implements UIResource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        TransferAction(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
        public boolean isEnabled(Object sender) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
            if (sender instanceof JComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
                && ((JComponent)sender).getTransferHandler() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
8816
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1707
        private static final JavaSecurityAccess javaSecurityAccess =
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1708
            SharedSecrets.getJavaSecurityAccess();
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1709
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1710
        public void actionPerformed(final ActionEvent e) {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1711
            final Object src = e.getSource();
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1712
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1713
            final PrivilegedAction<Void> action = new PrivilegedAction<Void>() {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1714
                public Void run() {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1715
                    actionPerformedImpl(e);
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1716
                    return null;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1717
                }
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1718
            };
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1719
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1720
            final AccessControlContext stack = AccessController.getContext();
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1721
            final AccessControlContext srcAcc = AWTAccessor.getComponentAccessor().getAccessControlContext((Component)src);
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1722
            final AccessControlContext eventAcc = AWTAccessor.getAWTEventAccessor().getAccessControlContext(e);
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1723
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1724
                if (srcAcc == null) {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1725
                    javaSecurityAccess.doIntersectionPrivilege(action, stack, eventAcc);
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1726
                } else {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1727
                    javaSecurityAccess.doIntersectionPrivilege(
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1728
                        new PrivilegedAction<Void>() {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1729
                            public Void run() {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1730
                                javaSecurityAccess.doIntersectionPrivilege(action, eventAcc);
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1731
                                return null;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1732
                             }
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1733
                    }, stack, srcAcc);
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1734
                }
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1735
        }
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1736
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7959
diff changeset
  1737
        private void actionPerformedImpl(ActionEvent e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
            Object src = e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
            if (src instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                JComponent c = (JComponent) src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
                TransferHandler th = c.getTransferHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                Clipboard clipboard = getClipboard(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
                String name = (String) getValue(Action.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
                Transferable trans = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
                // any of these calls may throw IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                    if ((clipboard != null) && (th != null) && (name != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                        if ("cut".equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
                            th.exportToClipboard(c, clipboard, MOVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                        } else if ("copy".equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                            th.exportToClipboard(c, clipboard, COPY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
                        } else if ("paste".equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                            trans = clipboard.getContents(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
                } catch (IllegalStateException ise) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                    // clipboard was unavailable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
                    UIManager.getLookAndFeel().provideErrorFeedback(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
                // this is a paste action, import data into the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
                if (trans != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
                    th.importData(new TransferSupport(c, trans));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
         * Returns the clipboard to use for cut/copy/paste.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
        private Clipboard getClipboard(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
            if (SwingUtilities2.canAccessSystemClipboard()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
                return c.getToolkit().getSystemClipboard();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
            Clipboard clipboard = (Clipboard)sun.awt.AppContext.getAppContext().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
                get(SandboxClipboardKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
            if (clipboard == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
                clipboard = new Clipboard("Sandboxed Component Clipboard");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
                sun.awt.AppContext.getAppContext().put(SandboxClipboardKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
                                                       clipboard);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
            return clipboard;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
         * Key used in app context to lookup Clipboard to use if access to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
         * System clipboard is denied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
        private static Object SandboxClipboardKey = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
}