4
* Copyright 2009 Sun Microsystems, Inc., 4150 Network Circle,
5
* Santa Clara, California 95054, U.S.A. All rights reserved.
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.
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.
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
22
package org.jdesktop.swingx.plaf.basic.core;
25
* @(#)DragRecognitionSupport.java 1.2 05/11/17
27
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
28
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
31
import java.awt.Toolkit;
32
import java.awt.event.*;
33
import java.awt.dnd.DragSource;
36
import org.jdesktop.swingx.SwingXUtilities;
37
//import sun.awt.dnd.SunDragSourceContextPeer;
38
//import sun.awt.AppContext;
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>
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.
50
* <li> a static method of SunDragSourceContextPeer has been copied into SwingXUtilities
52
* <li> the shared instance of this class is maintained in the UIManager instead of
56
* @author Shannon Hickey
57
* @version 1.2 11/17/05
59
public class DragRecognitionSupport {
60
private int motionThreshold;
61
private MouseEvent dndArmedEvent;
62
private JComponent component;
65
* This interface allows us to pass in a handler to mouseDragged,
66
* so that we can be notified immediately before a drag begins.
68
public static interface BeforeDrag {
69
public void dragStarting(MouseEvent me);
73
* Returns the DragRecognitionSupport for the caller's AppContext.
75
private static DragRecognitionSupport getDragRecognitionSupport() {
76
// DragRecognitionSupport support =
77
// (DragRecognitionSupport)AppContext.getAppContext().
78
// get(DragRecognitionSupport.class);
80
// if (support == null) {
81
// support = new DragRecognitionSupport();
82
// AppContext.getAppContext().put(DragRecognitionSupport.class, support);
85
DragRecognitionSupport support = (DragRecognitionSupport)
86
UIManager.get("sharedInstance.dragRecognitionSupport");
87
if (support == null) {
88
support = new DragRecognitionSupport();
89
UIManager.put("sharedInstance.dragRecognitionSupport", support);
95
* Returns whether or not the event is potentially part of a drag sequence.
97
public static boolean mousePressed(MouseEvent me) {
98
return ((DragRecognitionSupport)getDragRecognitionSupport()).
103
* If a dnd recognition has been going on, return the MouseEvent
104
* that started the recognition. Otherwise, return null.
106
public static MouseEvent mouseReleased(MouseEvent me) {
107
return ((DragRecognitionSupport)getDragRecognitionSupport()).
108
mouseReleasedImpl(me);
112
* Returns whether or not a drag gesture recognition is ongoing.
114
public static boolean mouseDragged(MouseEvent me, BeforeDrag bd) {
115
return ((DragRecognitionSupport)getDragRecognitionSupport()).
116
mouseDraggedImpl(me, bd);
119
private void clearState() {
120
dndArmedEvent = null;
124
private int mapDragOperationFromModifiers(MouseEvent me,
125
TransferHandler th) {
127
if (th == null || !SwingUtilities.isLeftMouseButton(me)) {
128
return TransferHandler.NONE;
130
// PENDING JW: c'p from SunDragSourceContextPeer
131
return SwingXUtilities.
132
convertModifiersToDropAction(me.getModifiersEx(),
133
th.getSourceActions(component));
137
* Returns whether or not the event is potentially part of a drag sequence.
139
private boolean mousePressedImpl(MouseEvent me) {
140
component = (JComponent)me.getSource();
142
if (mapDragOperationFromModifiers(me, component.getTransferHandler())
143
!= TransferHandler.NONE) {
145
motionThreshold = DragSource.getDragThreshold();
155
* If a dnd recognition has been going on, return the MouseEvent
156
* that started the recognition. Otherwise, return null.
158
private MouseEvent mouseReleasedImpl(MouseEvent me) {
159
/* no recognition has been going on */
160
if (dndArmedEvent == null) {
164
MouseEvent retEvent = null;
166
if (me.getSource() == component) {
167
retEvent = dndArmedEvent;
168
} // else component has changed unexpectedly, so return null
175
* Returns whether or not a drag gesture recognition is ongoing.
177
private boolean mouseDraggedImpl(MouseEvent me, BeforeDrag bd) {
178
/* no recognition is in progress */
179
if (dndArmedEvent == null) {
183
/* component has changed unexpectedly, so bail */
184
if (me.getSource() != component) {
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 */
197
bd.dragStarting(dndArmedEvent);
199
th.exportAsDrag(component, dndArmedEvent, action);