jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/DragRecognitionSupport.java
author weijun
Thu, 11 Dec 2014 15:19:12 +0800
changeset 28228 be83f404724d
parent 25859 3317bb8137f4
permissions -rw-r--r--
8067207: Replace concat String to append in StringBuilder parameters (client) Reviewed-by: serb Contributed-by: Otavio Santana <otaviojava@java.net>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     2
 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.plaf.basic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.Toolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.dnd.DragSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.awt.dnd.SunDragSourceContextPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Drag gesture recognition support for classes that have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <code>TransferHandler</code>. The gesture for a drag in this class is a mouse
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * press followed by movement by <code>DragSource.getDragThreshold()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * pixels. An instance of this class is maintained per AppContext, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * public static methods call into the appropriate instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Shannon Hickey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
class DragRecognitionSupport {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private int motionThreshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private MouseEvent dndArmedEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private JComponent component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * This interface allows us to pass in a handler to mouseDragged,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * so that we can be notified immediately before a drag begins.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static interface BeforeDrag {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        public void dragStarting(MouseEvent me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Returns the DragRecognitionSupport for the caller's AppContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static DragRecognitionSupport getDragRecognitionSupport() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        DragRecognitionSupport support =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            (DragRecognitionSupport)AppContext.getAppContext().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                get(DragRecognitionSupport.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (support == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            support = new DragRecognitionSupport();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            AppContext.getAppContext().put(DragRecognitionSupport.class, support);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return support;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Returns whether or not the event is potentially part of a drag sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public static boolean mousePressed(MouseEvent me) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
    76
        return getDragRecognitionSupport().mousePressedImpl(me);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * If a dnd recognition has been going on, return the MouseEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * that started the recognition. Otherwise, return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public static MouseEvent mouseReleased(MouseEvent me) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
    84
        return getDragRecognitionSupport().mouseReleasedImpl(me);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Returns whether or not a drag gesture recognition is ongoing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public static boolean mouseDragged(MouseEvent me, BeforeDrag bd) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
    91
        return getDragRecognitionSupport().mouseDraggedImpl(me, bd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private void clearState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        dndArmedEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        component = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private int mapDragOperationFromModifiers(MouseEvent me,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                              TransferHandler th) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (th == null || !SwingUtilities.isLeftMouseButton(me)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return TransferHandler.NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return SunDragSourceContextPeer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            convertModifiersToDropAction(me.getModifiersEx(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                                         th.getSourceActions(component));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Returns whether or not the event is potentially part of a drag sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private boolean mousePressedImpl(MouseEvent me) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        component = (JComponent)me.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (mapDragOperationFromModifiers(me, component.getTransferHandler())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                != TransferHandler.NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            motionThreshold = DragSource.getDragThreshold();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            dndArmedEvent = me;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        clearState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return false;
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
     * If a dnd recognition has been going on, return the MouseEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * that started the recognition. Otherwise, return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private MouseEvent mouseReleasedImpl(MouseEvent me) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        /* no recognition has been going on */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (dndArmedEvent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        MouseEvent retEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (me.getSource() == component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            retEvent = dndArmedEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        } // else component has changed unexpectedly, so return null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        clearState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return retEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Returns whether or not a drag gesture recognition is ongoing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private boolean mouseDraggedImpl(MouseEvent me, BeforeDrag bd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        /* no recognition is in progress */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (dndArmedEvent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        /* component has changed unexpectedly, so bail */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (me.getSource() != component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            clearState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        int dx = Math.abs(me.getX() - dndArmedEvent.getX());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        int dy = Math.abs(me.getY() - dndArmedEvent.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if ((dx > motionThreshold) || (dy > motionThreshold)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            TransferHandler th = component.getTransferHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            int action = mapDragOperationFromModifiers(me, th);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            if (action != TransferHandler.NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                /* notify the BeforeDrag instance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                if (bd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    bd.dragStarting(dndArmedEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                th.exportAsDrag(component, dndArmedEvent, action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                clearState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
}