~ubuntu-branches/ubuntu/trusty/libswingx-java/trusty

« back to all changes in this revision

Viewing changes to src/java/org/jdesktop/swingx/plaf/basic/core/DragRecognitionSupport.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2011-03-06 00:28:45 UTC
  • mfrom: (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20110306002845-escned3cbqp5qx0t
Tags: 1:1.6.2-1
* New upstream release.
* Switch to maven as build system:
  - d/control: drop ant, add maven-debian-helper
  - d/rules: use maven.mk
* d/patches/pom.diff: drop, uneeded since upstream fixed its dependencies.
* d/watch: update to use java.net directly.
* d/rules: force debian version for JARs (Closes: #603495).
* d/copyright: Update to lastest DEP-5 r166.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * $Id$
3
 
 *
4
 
 * Copyright 2009 Sun Microsystems, Inc., 4150 Network Circle,
5
 
 * Santa Clara, California 95054, U.S.A. All rights reserved.
6
 
 *
7
 
 * This library is free software; you can redistribute it and/or
8
 
 * modify it under the terms of the GNU Lesser General Public
9
 
 * License as published by the Free Software Foundation; either
10
 
 * version 2.1 of the License, or (at your option) any later version.
11
 
 * 
12
 
 * This library is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 
 * Lesser General Public License for more details.
16
 
 * 
17
 
 * You should have received a copy of the GNU Lesser General Public
18
 
 * License along with this library; if not, write to the Free Software
19
 
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
 
 *
21
 
 */
22
 
package org.jdesktop.swingx.plaf.basic.core;
23
 
 
24
 
/*
25
 
 * @(#)DragRecognitionSupport.java      1.2 05/11/17
26
 
 *
27
 
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
28
 
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
29
 
 */
30
 
 
31
 
import java.awt.Toolkit;
32
 
import java.awt.event.*;
33
 
import java.awt.dnd.DragSource;
34
 
import javax.swing.*;
35
 
 
36
 
import org.jdesktop.swingx.SwingXUtilities;
37
 
//import sun.awt.dnd.SunDragSourceContextPeer;
38
 
//import sun.awt.AppContext;
39
 
 
40
 
/**
41
 
 * Drag gesture recognition support for classes that have a
42
 
 * <code>TransferHandler</code>. The gesture for a drag in this class is a mouse
43
 
 * press followed by movement by <code>DragSource.getDragThreshold()</code>
44
 
 * pixels. An instance of this class is maintained per AppContext, and the
45
 
 * public static methods call into the appropriate instance. <p>
46
 
 * 
47
 
 * This is a c&p of core (package private) needed for BasicXListUI. It differs from
48
 
 * core in that references to sun packages have been replaced.
49
 
 * <ul>
50
 
 * <li> a static method of SunDragSourceContextPeer has been copied into SwingXUtilities
51
 
 *    and is used here
52
 
 * <li> the shared instance of this class is maintained in the UIManager instead of
53
 
 *   per appContext.
54
 
 * </ul>
55
 
 * 
56
 
 * @author Shannon Hickey
57
 
 * @version 1.2 11/17/05
58
 
 */
59
 
public class DragRecognitionSupport {
60
 
    private int motionThreshold;
61
 
    private MouseEvent dndArmedEvent;
62
 
    private JComponent component;
63
 
 
64
 
    /**
65
 
     * This interface allows us to pass in a handler to mouseDragged,
66
 
     * so that we can be notified immediately before a drag begins.
67
 
     */
68
 
    public static interface BeforeDrag {
69
 
        public void dragStarting(MouseEvent me);
70
 
    }
71
 
 
72
 
    /**
73
 
     * Returns the DragRecognitionSupport for the caller's AppContext.
74
 
     */
75
 
    private static DragRecognitionSupport getDragRecognitionSupport() {
76
 
//        DragRecognitionSupport support =
77
 
//            (DragRecognitionSupport)AppContext.getAppContext().
78
 
//                get(DragRecognitionSupport.class);
79
 
//
80
 
//        if (support == null) {
81
 
//            support = new DragRecognitionSupport();
82
 
//            AppContext.getAppContext().put(DragRecognitionSupport.class, support);
83
 
//        }
84
 
 
85
 
        DragRecognitionSupport support = (DragRecognitionSupport) 
86
 
            UIManager.get("sharedInstance.dragRecognitionSupport");
87
 
        if (support == null) {
88
 
            support = new DragRecognitionSupport();
89
 
            UIManager.put("sharedInstance.dragRecognitionSupport", support);
90
 
        }
91
 
        return support;
92
 
    }
93
 
 
94
 
    /**
95
 
     * Returns whether or not the event is potentially part of a drag sequence.
96
 
     */
97
 
    public static boolean mousePressed(MouseEvent me) {
98
 
        return ((DragRecognitionSupport)getDragRecognitionSupport()).
99
 
            mousePressedImpl(me);
100
 
    }
101
 
 
102
 
    /**
103
 
     * If a dnd recognition has been going on, return the MouseEvent
104
 
     * that started the recognition. Otherwise, return null.
105
 
     */
106
 
    public static MouseEvent mouseReleased(MouseEvent me) {
107
 
        return ((DragRecognitionSupport)getDragRecognitionSupport()).
108
 
            mouseReleasedImpl(me);
109
 
    }
110
 
 
111
 
    /**
112
 
     * Returns whether or not a drag gesture recognition is ongoing.
113
 
     */
114
 
    public static boolean mouseDragged(MouseEvent me, BeforeDrag bd) {
115
 
        return ((DragRecognitionSupport)getDragRecognitionSupport()).
116
 
            mouseDraggedImpl(me, bd);
117
 
    }
118
 
 
119
 
    private void clearState() {
120
 
        dndArmedEvent = null;
121
 
        component = null;
122
 
    }
123
 
 
124
 
    private int mapDragOperationFromModifiers(MouseEvent me,
125
 
                                              TransferHandler th) {
126
 
 
127
 
        if (th == null || !SwingUtilities.isLeftMouseButton(me)) {
128
 
            return TransferHandler.NONE;
129
 
        }
130
 
        // PENDING JW: c'p from SunDragSourceContextPeer
131
 
        return SwingXUtilities.
132
 
            convertModifiersToDropAction(me.getModifiersEx(),
133
 
                                         th.getSourceActions(component));
134
 
    }
135
 
 
136
 
    /**
137
 
     * Returns whether or not the event is potentially part of a drag sequence.
138
 
     */
139
 
    private boolean mousePressedImpl(MouseEvent me) {
140
 
        component = (JComponent)me.getSource();
141
 
 
142
 
        if (mapDragOperationFromModifiers(me, component.getTransferHandler())
143
 
                != TransferHandler.NONE) {
144
 
 
145
 
            motionThreshold = DragSource.getDragThreshold();
146
 
            dndArmedEvent = me;
147
 
            return true;
148
 
        }
149
 
 
150
 
        clearState();
151
 
        return false;
152
 
    }
153
 
 
154
 
    /**
155
 
     * If a dnd recognition has been going on, return the MouseEvent
156
 
     * that started the recognition. Otherwise, return null.
157
 
     */
158
 
    private MouseEvent mouseReleasedImpl(MouseEvent me) {
159
 
        /* no recognition has been going on */
160
 
        if (dndArmedEvent == null) {
161
 
            return null;
162
 
        }
163
 
 
164
 
        MouseEvent retEvent = null;
165
 
 
166
 
        if (me.getSource() == component) {
167
 
            retEvent = dndArmedEvent;
168
 
        } // else component has changed unexpectedly, so return null
169
 
 
170
 
        clearState();
171
 
        return retEvent;
172
 
    }
173
 
 
174
 
    /**
175
 
     * Returns whether or not a drag gesture recognition is ongoing.
176
 
     */
177
 
    private boolean mouseDraggedImpl(MouseEvent me, BeforeDrag bd) {
178
 
        /* no recognition is in progress */
179
 
        if (dndArmedEvent == null) {
180
 
            return false;
181
 
        }
182
 
 
183
 
        /* component has changed unexpectedly, so bail */
184
 
        if (me.getSource() != component) {
185
 
            clearState();
186
 
            return false;
187
 
        }
188
 
 
189
 
        int dx = Math.abs(me.getX() - dndArmedEvent.getX());
190
 
        int dy = Math.abs(me.getY() - dndArmedEvent.getY());
191
 
        if ((dx > motionThreshold) || (dy > motionThreshold)) {
192
 
            TransferHandler th = component.getTransferHandler();
193
 
            int action = mapDragOperationFromModifiers(me, th);
194
 
            if (action != TransferHandler.NONE) {
195
 
                /* notify the BeforeDrag instance */
196
 
                if (bd != null) {
197
 
                    bd.dragStarting(dndArmedEvent);
198
 
                }
199
 
                th.exportAsDrag(component, dndArmedEvent, action);
200
 
                clearState();
201
 
            }
202
 
        }
203
 
 
204
 
        return true;
205
 
    }
206
 
}