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