KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > basic > DragRecognitionSupport


1 /*
2  * @(#)DragRecognitionSupport.java 1.1 05/05/02
3  *
4  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.plaf.basic;
8
9 import java.awt.Toolkit JavaDoc;
10 import java.awt.event.*;
11 import java.awt.dnd.DragSource JavaDoc;
12 import javax.swing.*;
13 import sun.awt.dnd.SunDragSourceContextPeer;
14 import sun.awt.AppContext;
15
16 /**
17  * Drag gesture recognition support for classes that have a
18  * <code>TransferHandler</code>. The gesture for a drag in this class is a mouse
19  * press followed by movement by <code>DragSource.getDragThreshold()</code>
20  * pixels. An instance of this class is maintained per AppContext, and the
21  * public static methods call into the appropriate instance.
22  *
23  * @author Shannon Hickey
24  * @version 1.1 05/02/05
25  */

26 class DragRecognitionSupport {
27     private int motionThreshold;
28     private MouseEvent dndArmedEvent;
29     private JComponent component;
30
31     /**
32      * This interface allows us to pass in a handler to mouseDragged,
33      * so that we can be notified immediately before a drag begins.
34      */

35     public static interface BeforeDrag {
36         public void dragStarting(MouseEvent me);
37     }
38
39     /**
40      * Returns the DragRecognitionSupport for the caller's AppContext.
41      */

42     private static DragRecognitionSupport JavaDoc getDragRecognitionSupport() {
43         DragRecognitionSupport JavaDoc support =
44             (DragRecognitionSupport JavaDoc)AppContext.getAppContext().
45                 get(DragRecognitionSupport JavaDoc.class);
46
47         if (support == null) {
48             support = new DragRecognitionSupport JavaDoc();
49             AppContext.getAppContext().put(DragRecognitionSupport JavaDoc.class, support);
50         }
51
52         return support;
53     }
54
55     /**
56      * Returns whether or not the event is potentially part of a drag sequence.
57      */

58     public static boolean mousePressed(MouseEvent me) {
59         return ((DragRecognitionSupport JavaDoc)getDragRecognitionSupport()).
60             mousePressedImpl(me);
61     }
62
63     /**
64      * If a dnd recognition has been going on, return the MouseEvent
65      * that started the recognition. Otherwise, return null.
66      */

67     public static MouseEvent mouseReleased(MouseEvent me) {
68         return ((DragRecognitionSupport JavaDoc)getDragRecognitionSupport()).
69             mouseReleasedImpl(me);
70     }
71
72     /**
73      * Returns whether or not a drag gesture recognition is ongoing.
74      */

75     public static boolean mouseDragged(MouseEvent me, BeforeDrag bd) {
76         return ((DragRecognitionSupport JavaDoc)getDragRecognitionSupport()).
77             mouseDraggedImpl(me, bd);
78     }
79
80     private void clearState() {
81         dndArmedEvent = null;
82         component = null;
83     }
84
85     private int mapDragOperationFromModifiers(MouseEvent me,
86                                               TransferHandler th) {
87
88         if (th == null || !SwingUtilities.isLeftMouseButton(me)) {
89             return TransferHandler.NONE;
90         }
91
92         return SunDragSourceContextPeer.
93             convertModifiersToDropAction(me.getModifiersEx(),
94                                          th.getSourceActions(component));
95     }
96
97     /**
98      * Returns whether or not the event is potentially part of a drag sequence.
99      */

100     private boolean mousePressedImpl(MouseEvent me) {
101         component = (JComponent)me.getSource();
102
103         if (mapDragOperationFromModifiers(me, component.getTransferHandler())
104                 != TransferHandler.NONE) {
105
106             motionThreshold = DragSource.getDragThreshold();
107             dndArmedEvent = me;
108             return true;
109         }
110
111         clearState();
112         return false;
113     }
114
115     /**
116      * If a dnd recognition has been going on, return the MouseEvent
117      * that started the recognition. Otherwise, return null.
118      */

119     private MouseEvent mouseReleasedImpl(MouseEvent me) {
120         /* no recognition has been going on */
121         if (dndArmedEvent == null) {
122             return null;
123         }
124
125         MouseEvent retEvent = null;
126
127         if (me.getSource() == component) {
128             retEvent = dndArmedEvent;
129         } // else component has changed unexpectedly, so return null
130

131         clearState();
132         return retEvent;
133     }
134
135     /**
136      * Returns whether or not a drag gesture recognition is ongoing.
137      */

138     private boolean mouseDraggedImpl(MouseEvent me, BeforeDrag bd) {
139         /* no recognition is in progress */
140         if (dndArmedEvent == null) {
141             return false;
142         }
143
144         /* component has changed unexpectedly, so bail */
145         if (me.getSource() != component) {
146             clearState();
147             return false;
148         }
149
150         int dx = Math.abs(me.getX() - dndArmedEvent.getX());
151         int dy = Math.abs(me.getY() - dndArmedEvent.getY());
152         if ((dx > motionThreshold) || (dy > motionThreshold)) {
153             TransferHandler th = component.getTransferHandler();
154             int action = mapDragOperationFromModifiers(me, th);
155             if (action != TransferHandler.NONE) {
156                 /* notify the BeforeDrag instance */
157                 if (bd != null) {
158                     bd.dragStarting(dndArmedEvent);
159                 }
160                 th.exportAsDrag(component, dndArmedEvent, action);
161                 clearState();
162             }
163         }
164
165         return true;
166     }
167 }
168
Popular Tags