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