jdk/src/share/classes/sun/awt/dnd/SunDragSourceContextPeer.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 4831 85d01a4fe115
permissions -rw-r--r--
Initial load
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.AWTEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.EventQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.Image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.Point;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.datatransfer.Transferable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.dnd.DnDConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.dnd.DragSourceContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.dnd.DragSourceEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.dnd.DragSourceDropEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.dnd.DragSourceDragEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.dnd.DragGestureEvent;
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.DragSourceContextPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.awt.event.InputEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.awt.event.MouseEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.util.SortedMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import sun.awt.SunToolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import sun.awt.datatransfer.DataTransferer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * TBC
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @since JDK1.3.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
public abstract class SunDragSourceContextPeer implements DragSourceContextPeer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private DragGestureEvent  trigger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private Component         component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private Cursor            cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private long              nativeCtxt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private DragSourceContext dragSourceContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private int               sourceActions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static boolean    dragDropInProgress = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static boolean    discardingMouseEvents = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * dispatch constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    protected final static int DISPATCH_ENTER   = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    protected final static int DISPATCH_MOTION  = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    protected final static int DISPATCH_CHANGED = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    protected final static int DISPATCH_EXIT    = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    protected final static int DISPATCH_FINISH  = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    protected final static int DISPATCH_MOUSE_MOVED  = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * construct a new SunDragSourceContextPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public SunDragSourceContextPeer(DragGestureEvent dge) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        trigger = dge;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (trigger != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            component = trigger.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            component = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Synchro messages in AWT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public void startSecondaryEventLoop(){}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public void quitSecondaryEventLoop(){}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * initiate a DnD operation ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public void startDrag(DragSourceContext dsc, Cursor c, Image di, Point p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
      throws InvalidDnDOperationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        /* Fix for 4354044: don't initiate a drag if event sequence provided by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
         * DragGestureRecognizer is empty */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (getTrigger().getTriggerEvent() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            throw new InvalidDnDOperationException("DragGestureEvent has a null trigger");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        dragSourceContext = dsc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        cursor            = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        sourceActions     = getDragSourceContext().getSourceActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        Transferable transferable  = getDragSourceContext().getTransferable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        SortedMap formatMap = DataTransferer.getInstance().getFormatsForTransferable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
             (transferable, DataTransferer.adaptFlavorMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                 (getTrigger().getDragSource().getFlavorMap()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        long[] formats = DataTransferer.getInstance().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            keysToLongArray(formatMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        startDrag(transferable, formats, formatMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
         * Fix for 4613903.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
         * Filter out all mouse events that are currently on the event queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        discardingMouseEvents = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        EventQueue.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    discardingMouseEvents = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    protected abstract void startDrag(Transferable trans,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                      long[] formats, Map formatMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * set cursor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public void setCursor(Cursor c) throws InvalidDnDOperationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            if (cursor == null || !cursor.equals(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                cursor = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                // NOTE: native context can be null at this point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                // setNativeCursor() should handle it properly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                setNativeCursor(getNativeContext(), c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                c != null ? c.getType() : 0);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * return cursor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public Cursor getCursor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * downcall into native code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    protected abstract void setNativeCursor(long nativeCtxt, Cursor c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                            int cType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    protected synchronized void setTrigger(DragGestureEvent dge) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        trigger = dge;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (trigger != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            component = trigger.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            component = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    protected DragGestureEvent getTrigger() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return trigger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    protected Component getComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    protected synchronized void setNativeContext(long ctxt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        nativeCtxt = ctxt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    protected synchronized long getNativeContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return nativeCtxt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    protected DragSourceContext getDragSourceContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return dragSourceContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Notify the peer that the transferables' DataFlavors have changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * No longer useful as the transferables are determined at the time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * of the drag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public void transferablesFlavorsChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    protected final void postDragSourceDragEvent(final int targetAction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                                 final int modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                                 final int x, final int y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                                 final int dispatchType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        final int dropAction =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            SunDragSourceContextPeer.convertModifiersToDropAction(modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                                                                  sourceActions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        DragSourceDragEvent event =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            new DragSourceDragEvent(getDragSourceContext(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                                    dropAction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                                    targetAction & sourceActions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                    modifiers, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        EventDispatcher dispatcher = new EventDispatcher(dispatchType, event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        SunToolkit.invokeLaterOnAppContext(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            SunToolkit.targetToAppContext(getComponent()), dispatcher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        startSecondaryEventLoop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * upcall from native code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    private void dragEnter(final int targetActions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                           final int modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                           final int x, final int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        postDragSourceDragEvent(targetActions, modifiers, x, y, DISPATCH_ENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * upcall from native code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    private void dragMotion(final int targetActions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                            final int modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                            final int x, final int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        postDragSourceDragEvent(targetActions, modifiers, x, y, DISPATCH_MOTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * upcall from native code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    private void operationChanged(final int targetActions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                                  final int modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                                  final int x, final int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        postDragSourceDragEvent(targetActions, modifiers, x, y, DISPATCH_CHANGED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * upcall from native code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    protected final void dragExit(final int x, final int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        DragSourceEvent event =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            new DragSourceEvent(getDragSourceContext(), x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        EventDispatcher dispatcher =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            new EventDispatcher(DISPATCH_EXIT, event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        SunToolkit.invokeLaterOnAppContext(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            SunToolkit.targetToAppContext(getComponent()), dispatcher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        startSecondaryEventLoop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * upcall from native code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    private void dragMouseMoved(final int targetActions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                                final int modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                final int x, final int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        postDragSourceDragEvent(targetActions, modifiers, x, y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                DISPATCH_MOUSE_MOVED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * upcall from native code via implemented class (do)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    protected final void dragDropFinished(final boolean success,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                          final int operations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                                          final int x, final int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        DragSourceEvent event =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            new DragSourceDropEvent(getDragSourceContext(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                                    operations & sourceActions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                    success, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        EventDispatcher dispatcher =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            new EventDispatcher(DISPATCH_FINISH, event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        SunToolkit.invokeLaterOnAppContext(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            SunToolkit.targetToAppContext(getComponent()), dispatcher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        startSecondaryEventLoop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        setNativeContext(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public static void setDragDropInProgress(boolean b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
      throws InvalidDnDOperationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (dragDropInProgress == b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            throw new InvalidDnDOperationException(getExceptionMessage(b));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        synchronized (SunDragSourceContextPeer.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            if (dragDropInProgress == b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                throw new InvalidDnDOperationException(getExceptionMessage(b));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            dragDropInProgress = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * Filters out all mouse events that were on the java event queue when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * startDrag was called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public static boolean checkEvent(AWTEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if (discardingMouseEvents && event instanceof MouseEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            MouseEvent mouseEvent = (MouseEvent)event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            if (!(mouseEvent instanceof SunDropTargetEvent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public static void checkDragDropInProgress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
      throws InvalidDnDOperationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if (dragDropInProgress) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            throw new InvalidDnDOperationException(getExceptionMessage(true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    private static String getExceptionMessage(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        return b ? "Drag and drop in progress" : "No drag in progress";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public static int convertModifiersToDropAction(final int modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                                                   final int supportedActions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        int dropAction = DnDConstants.ACTION_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
         * Fix for 4285634.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
         * Calculate the drop action to match Motif DnD behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
         * If the user selects an operation (by pressing a modifier key),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
         * return the selected operation or ACTION_NONE if the selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
         * operation is not supported by the drag source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
         * If the user doesn't select an operation search the set of operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
         * supported by the drag source for ACTION_MOVE, then for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
         * ACTION_COPY, then for ACTION_LINK and return the first operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
         * found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        switch (modifiers & (InputEvent.SHIFT_DOWN_MASK |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                             InputEvent.CTRL_DOWN_MASK)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        case InputEvent.SHIFT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            dropAction = DnDConstants.ACTION_LINK; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        case InputEvent.CTRL_DOWN_MASK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            dropAction = DnDConstants.ACTION_COPY; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        case InputEvent.SHIFT_DOWN_MASK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            dropAction = DnDConstants.ACTION_MOVE; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            if ((supportedActions & DnDConstants.ACTION_MOVE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                dropAction = DnDConstants.ACTION_MOVE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            } else if ((supportedActions & DnDConstants.ACTION_COPY) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                dropAction = DnDConstants.ACTION_COPY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            } else if ((supportedActions & DnDConstants.ACTION_LINK) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                dropAction = DnDConstants.ACTION_LINK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        return dropAction & supportedActions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    private void cleanup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        trigger = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        component = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        cursor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        dragSourceContext = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        SunDropTargetContextPeer.setCurrentJVMLocalSourceTransferable(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        SunDragSourceContextPeer.setDragDropInProgress(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    private class EventDispatcher implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        private final int dispatchType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        private final DragSourceEvent event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        EventDispatcher(int dispatchType, DragSourceEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            switch (dispatchType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            case DISPATCH_ENTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            case DISPATCH_MOTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            case DISPATCH_CHANGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            case DISPATCH_MOUSE_MOVED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                if (!(event instanceof DragSourceDragEvent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    throw new IllegalArgumentException("Event: " + event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            case DISPATCH_EXIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            case DISPATCH_FINISH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                if (!(event instanceof DragSourceDropEvent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    throw new IllegalArgumentException("Event: " + event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                throw new IllegalArgumentException("Dispatch type: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                                                   dispatchType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            this.dispatchType  = dispatchType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            this.event         = event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            DragSourceContext dragSourceContext =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                SunDragSourceContextPeer.this.getDragSourceContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                switch (dispatchType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                case DISPATCH_ENTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    dragSourceContext.dragEnter((DragSourceDragEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                case DISPATCH_MOTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    dragSourceContext.dragOver((DragSourceDragEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                case DISPATCH_CHANGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    dragSourceContext.dropActionChanged((DragSourceDragEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                case DISPATCH_EXIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    dragSourceContext.dragExit(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                case DISPATCH_MOUSE_MOVED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    dragSourceContext.dragMouseMoved((DragSourceDragEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                case DISPATCH_FINISH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                        dragSourceContext.dragDropEnd((DragSourceDropEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                        SunDragSourceContextPeer.this.cleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    throw new IllegalStateException("Dispatch type: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                                                    dispatchType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                 SunDragSourceContextPeer.this.quitSecondaryEventLoop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
}