jdk/src/share/classes/sun/awt/dnd/SunDropTargetContextPeer.java
author denis
Fri, 26 Feb 2010 03:54:52 -0800
changeset 5192 506ebd065f86
parent 3938 ef327bd847c0
child 5506 202f599c92aa
permissions -rw-r--r--
6887703: Unsigned applet can retrieve the dragged information before drop action occur Reviewed-by: uta
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-2007 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt.dnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Point;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.datatransfer.DataFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.datatransfer.Transferable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.datatransfer.UnsupportedFlavorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.dnd.DnDConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.dnd.DropTarget;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.dnd.DropTargetContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.dnd.DropTargetListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.dnd.DropTargetEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.dnd.DropTargetDragEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.dnd.DropTargetDropEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.dnd.InvalidDnDOperationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.awt.dnd.peer.DropTargetContextPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    51
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import sun.awt.SunToolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import sun.awt.datatransfer.DataTransferer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import sun.awt.datatransfer.ToolkitThreadBlockedHandler;
5192
506ebd065f86 6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents: 3938
diff changeset
    60
import sun.security.util.SecurityConstants;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * The SunDropTargetContextPeer class is the generic class responsible for handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * the interaction between a windowing systems DnD system and Java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @since JDK1.3.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
public abstract class SunDropTargetContextPeer implements DropTargetContextPeer, Transferable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * A boolean constant that requires the peer to wait until the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * SunDropTargetEvent is processed and return the status back
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * to the native code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public static final boolean DISPATCH_SYNC = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private   DropTarget              currentDT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private   DropTargetContext       currentDTC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private   long[]                  currentT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private   int                     currentA;   // target actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private   int                     currentSA;  // source actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private   int                     currentDA;  // current drop action
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private   int                     previousDA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private   long                    nativeDragContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private   Transferable            local;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private boolean                   dragRejected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    protected int                     dropStatus   = STATUS_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    protected boolean                 dropComplete = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * global lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    protected static final Object _globalLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   103
    private static final PlatformLogger dndLog = PlatformLogger.getLogger("sun.awt.dnd.SunDropTargetContextPeer");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * a primitive mechanism for advertising intra-JVM Transferables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    protected static Transferable         currentJVMLocalSourceTransferable = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public static void setCurrentJVMLocalSourceTransferable(Transferable t) throws InvalidDnDOperationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        synchronized(_globalLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (t != null && currentJVMLocalSourceTransferable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    throw new InvalidDnDOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                currentJVMLocalSourceTransferable = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * obtain the transferable iff the operation is in the same VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private static Transferable getJVMLocalSourceTransferable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return currentJVMLocalSourceTransferable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * constants used by dropAccept() or dropReject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    protected final static int STATUS_NONE   =  0; // none pending
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    protected final static int STATUS_WAIT   =  1; // drop pending
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    protected final static int STATUS_ACCEPT =  2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    protected final static int STATUS_REJECT = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * create the peer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public SunDropTargetContextPeer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @return the DropTarget associated with this peer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public DropTarget getDropTarget() { return currentDT; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param actions set the current actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public synchronized void setTargetActions(int actions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        currentA = actions &
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            (DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @return the current target actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public int getTargetActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return currentA;
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
     * get the Transferable associated with the drop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public Transferable getTransferable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @return current DataFlavors available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    // NOTE: This method may be called by privileged threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    //       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public DataFlavor[] getTransferDataFlavors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        final Transferable    localTransferable = local;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (localTransferable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return localTransferable.getTransferDataFlavors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            return DataTransferer.getInstance().getFlavorsForFormatsAsArray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                (currentT, DataTransferer.adaptFlavorMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    (currentDT.getFlavorMap()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @return if the flavor is supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public boolean isDataFlavorSupported(DataFlavor df) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        Transferable localTransferable = local;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (localTransferable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            return localTransferable.isDataFlavorSupported(df);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            return DataTransferer.getInstance().getFlavorsForFormats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                (currentT, DataTransferer.adaptFlavorMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    (currentDT.getFlavorMap())).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                containsKey(df);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @return the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public Object getTransferData(DataFlavor df)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
      throws UnsupportedFlavorException, IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        InvalidDnDOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    {
5192
506ebd065f86 6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents: 3938
diff changeset
   220
506ebd065f86 6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents: 3938
diff changeset
   221
        SecurityManager sm = System.getSecurityManager();
506ebd065f86 6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents: 3938
diff changeset
   222
        try {
506ebd065f86 6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents: 3938
diff changeset
   223
            if (!dropComplete && sm != null) {
506ebd065f86 6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents: 3938
diff changeset
   224
                sm.checkSystemClipboardAccess();
506ebd065f86 6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents: 3938
diff changeset
   225
            }
506ebd065f86 6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents: 3938
diff changeset
   226
        } catch (Exception e) {
506ebd065f86 6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents: 3938
diff changeset
   227
            Thread currentThread = Thread.currentThread();
506ebd065f86 6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents: 3938
diff changeset
   228
            currentThread.getUncaughtExceptionHandler().uncaughtException(currentThread, e);
506ebd065f86 6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents: 3938
diff changeset
   229
            return null;
506ebd065f86 6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents: 3938
diff changeset
   230
        }
506ebd065f86 6887703: Unsigned applet can retrieve the dragged information before drop action occur
denis
parents: 3938
diff changeset
   231
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        Long lFormat = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        Transferable localTransferable = local;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (localTransferable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return localTransferable.getTransferData(df);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (dropStatus != STATUS_ACCEPT || dropComplete) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            throw new InvalidDnDOperationException("No drop current");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        Map flavorMap = DataTransferer.getInstance().getFlavorsForFormats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            (currentT, DataTransferer.adaptFlavorMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                (currentDT.getFlavorMap()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        lFormat = (Long)flavorMap.get(df);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (lFormat == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            throw new UnsupportedFlavorException(df);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (df.isRepresentationClassRemote() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            currentDA != DnDConstants.ACTION_LINK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            throw new InvalidDnDOperationException("only ACTION_LINK is permissable for transfer of java.rmi.Remote objects");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        final long format = lFormat.longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        Object ret = getNativeData(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (ret instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                return DataTransferer.getInstance().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    translateBytes((byte[])ret, df, format, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                throw new InvalidDnDOperationException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        } else if (ret instanceof InputStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                return DataTransferer.getInstance().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    translateStream((InputStream)ret, df, format, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                throw new InvalidDnDOperationException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            throw new IOException("no native data was transfered");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    protected abstract Object getNativeData(long format)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
      throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @return if the transfer is a local one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public boolean isTransferableJVMLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        return local != null || getJVMLocalSourceTransferable() != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    private int handleEnterMessage(final Component component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                                   final int x, final int y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                                   final int dropAction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                                   final int actions, final long[] formats,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                                   final long nativeCtxt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        return postDropTargetEvent(component, x, y, dropAction, actions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                                   formats, nativeCtxt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                   SunDropTargetEvent.MOUSE_ENTERED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                   SunDropTargetContextPeer.DISPATCH_SYNC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * actual processing on EventQueue Thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    protected void processEnterMessage(SunDropTargetEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        Component  c    = (Component)event.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        DropTarget dt   = c.getDropTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        Point      hots = event.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        local = getJVMLocalSourceTransferable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (currentDTC != null) { // some wreckage from last time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            currentDTC.removeNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            currentDTC = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (c.isShowing() && dt != null && dt.isActive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            currentDT  = dt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            currentDTC = currentDT.getDropTargetContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            currentDTC.addNotify(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            currentA   = dt.getDefaultActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                ((DropTargetListener)dt).dragEnter(new DropTargetDragEvent(currentDTC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                                                           hots,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                                                           currentDA,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                                                                           currentSA));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                currentDA = DnDConstants.ACTION_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            currentDT  = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            currentDTC = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            currentDA   = DnDConstants.ACTION_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            currentSA   = DnDConstants.ACTION_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            currentA   = DnDConstants.ACTION_NONE;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * upcall to handle exit messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    private void handleExitMessage(final Component component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                                   final long nativeCtxt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
         * Even though the return value is irrelevant for this event, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
         * dispatched synchronously to fix 4393148 properly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        postDropTargetEvent(component, 0, 0, DnDConstants.ACTION_NONE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                            DnDConstants.ACTION_NONE, null, nativeCtxt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                            SunDropTargetEvent.MOUSE_EXITED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                            SunDropTargetContextPeer.DISPATCH_SYNC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    protected void processExitMessage(SunDropTargetEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        Component         c   = (Component)event.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        DropTarget        dt  = c.getDropTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        DropTargetContext dtc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (dt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            currentDT = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            currentT  = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            if (currentDTC != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                currentDTC.removeNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            currentDTC = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (dt != currentDT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            if (currentDTC != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                currentDTC.removeNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            currentDT  = dt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            currentDTC = dt.getDropTargetContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            currentDTC.addNotify(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        dtc = currentDTC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        if (dt.isActive()) try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            ((DropTargetListener)dt).dragExit(new DropTargetEvent(dtc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            currentA  = DnDConstants.ACTION_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            currentSA = DnDConstants.ACTION_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            currentDA = DnDConstants.ACTION_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            currentDT = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            currentT  = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            currentDTC.removeNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            currentDTC = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            local = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            dragRejected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    private int handleMotionMessage(final Component component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                                    final int x, final int y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                                    final int dropAction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                                    final int actions, final long[] formats,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                                    final long nativeCtxt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        return postDropTargetEvent(component, x, y, dropAction, actions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                                   formats, nativeCtxt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                                   SunDropTargetEvent.MOUSE_DRAGGED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                                   SunDropTargetContextPeer.DISPATCH_SYNC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    protected void processMotionMessage(SunDropTargetEvent event,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                                      boolean operationChanged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        Component         c    = (Component)event.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        Point             hots = event.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        int               id   = event.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        DropTarget        dt   = c.getDropTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        DropTargetContext dtc  = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        if (c.isShowing() && (dt != null) && dt.isActive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            if (currentDT != dt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                if (currentDTC != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    currentDTC.removeNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                currentDT  = dt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                currentDTC = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            dtc = currentDT.getDropTargetContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            if (dtc != currentDTC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                if (currentDTC != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    currentDTC.removeNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                currentDTC = dtc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                currentDTC.addNotify(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            currentA = currentDT.getDefaultActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                DropTargetDragEvent dtde = new DropTargetDragEvent(dtc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                                                                   hots,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                                                                   currentDA,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                                                                   currentSA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                DropTargetListener dtl = (DropTargetListener)dt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                if (operationChanged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    dtl.dropActionChanged(dtde);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    dtl.dragOver(dtde);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                if (dragRejected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    currentDA = DnDConstants.ACTION_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                currentDA = DnDConstants.ACTION_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            currentDA = DnDConstants.ACTION_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * upcall to handle the Drop message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    private void handleDropMessage(final Component component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                                   final int x, final int y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                                   final int dropAction, final int actions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                                   final long[] formats,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                                   final long nativeCtxt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        postDropTargetEvent(component, x, y, dropAction, actions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                            formats, nativeCtxt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                            SunDropTargetEvent.MOUSE_DROPPED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                            !SunDropTargetContextPeer.DISPATCH_SYNC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    protected void processDropMessage(SunDropTargetEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        Component  c    = (Component)event.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        Point      hots = event.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        DropTarget dt   = c.getDropTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        dropStatus   = STATUS_WAIT; // drop pending ACK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        dropComplete = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        if (c.isShowing() && dt != null && dt.isActive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            DropTargetContext dtc = dt.getDropTargetContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            currentDT = dt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            if (currentDTC != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                currentDTC.removeNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            currentDTC = dtc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            currentDTC.addNotify(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            currentA = dt.getDefaultActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            synchronized(_globalLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                if ((local = getJVMLocalSourceTransferable()) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                    setCurrentJVMLocalSourceTransferable(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                ((DropTargetListener)dt).drop(new DropTargetDropEvent(dtc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                                                                      hots,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                                                                      currentDA,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                                                                      currentSA,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                                                                      local != null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                if (dropStatus == STATUS_WAIT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                    rejectDrop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                } else if (dropComplete == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                    dropComplete(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            rejectDrop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    protected int postDropTargetEvent(final Component component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                                      final int x, final int y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                                      final int dropAction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                                      final int actions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                                      final long[] formats,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                                      final long nativeCtxt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                                      final int eventID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                                      final boolean dispatchType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        AppContext appContext = SunToolkit.targetToAppContext(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        EventDispatcher dispatcher =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            new EventDispatcher(this, dropAction, actions, formats, nativeCtxt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                                dispatchType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        SunDropTargetEvent event =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            new SunDropTargetEvent(component, eventID, x, y, dispatcher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        if (dispatchType == SunDropTargetContextPeer.DISPATCH_SYNC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            DataTransferer.getInstance().getToolkitThreadBlockedHandler().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        // schedule callback
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        SunToolkit.postEvent(appContext, event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        eventPosted(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        if (dispatchType == SunDropTargetContextPeer.DISPATCH_SYNC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            while (!dispatcher.isDone()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                DataTransferer.getInstance().getToolkitThreadBlockedHandler().enter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            DataTransferer.getInstance().getToolkitThreadBlockedHandler().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            // return target's response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            return dispatcher.getReturnValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * acceptDrag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    public synchronized void acceptDrag(int dragOperation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        if (currentDT == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            throw new InvalidDnDOperationException("No Drag pending");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        currentDA = mapOperation(dragOperation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        if (currentDA != DnDConstants.ACTION_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            dragRejected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * rejectDrag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    public synchronized void rejectDrag() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        if (currentDT == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            throw new InvalidDnDOperationException("No Drag pending");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        currentDA = DnDConstants.ACTION_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        dragRejected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * acceptDrop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    public synchronized void acceptDrop(int dropOperation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        if (dropOperation == DnDConstants.ACTION_NONE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            throw new IllegalArgumentException("invalid acceptDrop() action");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        if (dropStatus != STATUS_WAIT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            throw new InvalidDnDOperationException("invalid acceptDrop()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        currentDA = currentA = mapOperation(dropOperation & currentSA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        dropStatus   = STATUS_ACCEPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        dropComplete = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * reject Drop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    public synchronized void rejectDrop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        if (dropStatus != STATUS_WAIT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            throw new InvalidDnDOperationException("invalid rejectDrop()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        dropStatus = STATUS_REJECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
         * Fix for 4285634.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
         * The target rejected the drop means that it doesn't perform any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
         * drop action. This change is to make Solaris behavior consistent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
         * with Win32.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        currentDA = DnDConstants.ACTION_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        dropComplete(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * mapOperation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    private int mapOperation(int operation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        int[] operations = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                DnDConstants.ACTION_MOVE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                DnDConstants.ACTION_COPY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                DnDConstants.ACTION_LINK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        int   ret = DnDConstants.ACTION_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        for (int i = 0; i < operations.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            if ((operation & operations[i]) == operations[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                    ret = operations[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * signal drop complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    public synchronized void dropComplete(boolean success) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        if (dropStatus == STATUS_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            throw new InvalidDnDOperationException("No Drop pending");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        if (currentDTC != null) currentDTC.removeNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        currentDT  = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        currentDTC = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        currentT   = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        currentA   = DnDConstants.ACTION_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        synchronized(_globalLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            currentJVMLocalSourceTransferable = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        dropStatus   = STATUS_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        dropComplete = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            doDropDone(success, currentDA, local != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            currentDA = DnDConstants.ACTION_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            // The native context is invalid after the drop is done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            // Clear the reference to prohibit access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            nativeDragContext = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    protected abstract void doDropDone(boolean success,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                                       int dropAction, boolean isLocal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    protected synchronized long getNativeDragContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        return nativeDragContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    protected void eventPosted(SunDropTargetEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    protected void eventProcessed(SunDropTargetEvent e, int returnValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                                  boolean dispatcherDone) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    protected static class EventDispatcher {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        private final SunDropTargetContextPeer peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        // context fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        private final int dropAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        private final int actions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        private final long[] formats;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        private long nativeCtxt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        private final boolean dispatchType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        private boolean dispatcherDone = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        // dispatcher state fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        private int returnValue = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        // set of events to be dispatched by this dispatcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        private final HashSet eventSet = new HashSet(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        static final ToolkitThreadBlockedHandler handler =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            DataTransferer.getInstance().getToolkitThreadBlockedHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        EventDispatcher(SunDropTargetContextPeer peer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                        int dropAction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                        int actions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                        long[] formats,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                        long nativeCtxt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                        boolean dispatchType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            this.peer         = peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            this.nativeCtxt   = nativeCtxt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            this.dropAction   = dropAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            this.actions      = actions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            this.formats =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                     (null == formats) ? null : Arrays.copyOf(formats, formats.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            this.dispatchType = dispatchType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        void dispatchEvent(SunDropTargetEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            int id = e.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            switch (id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            case SunDropTargetEvent.MOUSE_ENTERED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                dispatchEnterEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            case SunDropTargetEvent.MOUSE_DRAGGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                dispatchMotionEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            case SunDropTargetEvent.MOUSE_EXITED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                dispatchExitEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            case SunDropTargetEvent.MOUSE_DROPPED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                dispatchDropEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                throw new InvalidDnDOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        private void dispatchEnterEvent(SunDropTargetEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            synchronized (peer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                // store the drop action here to track operation changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                peer.previousDA = dropAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                // setup peer context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                peer.nativeDragContext = nativeCtxt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                peer.currentT          = formats;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                peer.currentSA         = actions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                peer.currentDA         = dropAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                // To allow data retrieval.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                peer.dropStatus        = STATUS_ACCEPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                peer.dropComplete      = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                    peer.processEnterMessage(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                    peer.dropStatus        = STATUS_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                setReturnValue(peer.currentDA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        private void dispatchMotionEvent(SunDropTargetEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            synchronized (peer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                boolean operationChanged = peer.previousDA != dropAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                peer.previousDA = dropAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                // setup peer context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                peer.nativeDragContext = nativeCtxt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                peer.currentT          = formats;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                peer.currentSA         = actions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                peer.currentDA         = dropAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                // To allow data retrieval.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                peer.dropStatus        = STATUS_ACCEPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                peer.dropComplete      = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                    peer.processMotionMessage(e, operationChanged);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                    peer.dropStatus        = STATUS_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                setReturnValue(peer.currentDA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        private void dispatchExitEvent(SunDropTargetEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            synchronized (peer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                // setup peer context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                peer.nativeDragContext = nativeCtxt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                peer.processExitMessage(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        private void dispatchDropEvent(SunDropTargetEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            synchronized (peer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                // setup peer context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                peer.nativeDragContext = nativeCtxt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                peer.currentT          = formats;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                peer.currentSA         = actions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                peer.currentDA         = dropAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                peer.processDropMessage(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        void setReturnValue(int ret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            returnValue = ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        int getReturnValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            return returnValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        boolean isDone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            return eventSet.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        void registerEvent(SunDropTargetEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            handler.lock();
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   861
            if (!eventSet.add(e) && dndLog.isLoggable(PlatformLogger.FINE)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   862
                dndLog.fine("Event is already registered: " + e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            handler.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        void unregisterEvent(SunDropTargetEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            handler.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                if (!eventSet.remove(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                    // This event has already been unregistered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                if (eventSet.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                    if (!dispatcherDone && dispatchType == DISPATCH_SYNC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                        handler.exit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                    dispatcherDone = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                handler.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                peer.eventProcessed(e, returnValue, dispatcherDone);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                 * Clear the reference to the native context if all copies of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                 * the original event are processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                if (dispatcherDone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                    nativeCtxt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                    // Fix for 6342381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                    peer.nativeDragContext = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        public void unregisterAllEvents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            Object[] events = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            handler.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                events = eventSet.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                handler.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            if (events != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                for (int i = 0; i < events.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                    unregisterEvent((SunDropTargetEvent)events[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
}