KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)BasicDragGestureRecognizer.java 1.7 03/12/19
3  *
4  * Copyright 2004 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
15 /**
16  * Default drag gesture recognition for drag operations performed by classses
17  * that have a <code>dragEnabled</code> property. The gesture for a drag in
18  * this package is a mouse press over a selection followed by some movement
19  * by enough pixels to keep it from being treated as a click.
20  *
21  * @author Timothy Prinzing
22  * @version 1.7 12/19/03
23  */

24 class BasicDragGestureRecognizer implements MouseListener, MouseMotionListener {
25     
26     private MouseEvent dndArmedEvent = null;
27     
28     private static int getMotionThreshold() {
29         return DragSource.getDragThreshold();
30     }
31     
32     protected int mapDragOperationFromModifiers(MouseEvent e) {
33         int mods = e.getModifiersEx();
34         
35         if ((mods & InputEvent.BUTTON1_DOWN_MASK) != InputEvent.BUTTON1_DOWN_MASK) {
36             return TransferHandler.NONE;
37         }
38
39         JComponent c = getComponent(e);
40         TransferHandler th = c.getTransferHandler();
41         return SunDragSourceContextPeer.convertModifiersToDropAction(mods, th.getSourceActions(c));
42     }
43     
44     public void mouseClicked(MouseEvent e) {
45     }
46
47     public void mousePressed(MouseEvent e) {
48         dndArmedEvent = null;
49
50     if (isDragPossible(e) && mapDragOperationFromModifiers(e) != TransferHandler.NONE) {
51             dndArmedEvent = e;
52         e.consume();
53     }
54     }
55     
56     public void mouseReleased(MouseEvent e) {
57         dndArmedEvent = null;
58     }
59     
60     public void mouseEntered(MouseEvent e) {
61         //dndArmedEvent = null;
62
}
63     
64     public void mouseExited(MouseEvent e) {
65         //if (dndArmedEvent != null && mapDragOperationFromModifiers(e) == TransferHandler.NONE) {
66
// dndArmedEvent = null;
67
//}
68
}
69
70     public void mouseDragged(MouseEvent e) {
71     if (dndArmedEvent != null) {
72             e.consume();
73             
74             int action = mapDragOperationFromModifiers(e);
75             
76             if (action == TransferHandler.NONE) {
77                 return;
78             }
79             
80         int dx = Math.abs(e.getX() - dndArmedEvent.getX());
81         int dy = Math.abs(e.getY() - dndArmedEvent.getY());
82             if ((dx > getMotionThreshold()) || (dy > getMotionThreshold())) {
83         // start transfer... shouldn't be a click at this point
84
JComponent c = getComponent(e);
85         TransferHandler th = c.getTransferHandler();
86         th.exportAsDrag(c, dndArmedEvent, action);
87         dndArmedEvent = null;
88         }
89     }
90     }
91     
92     public void mouseMoved(MouseEvent e) {
93     }
94     
95     private TransferHandler getTransferHandler(MouseEvent e) {
96         JComponent c = getComponent(e);
97         return c == null ? null : c.getTransferHandler();
98     }
99     
100     /**
101      * Determines if the following are true:
102      * <ul>
103      * <li>the press event is located over a selection
104      * <li>the dragEnabled property is true
105      * <li>A TranferHandler is installed
106      * </ul>
107      * <p>
108      * This is implemented to check for a TransferHandler.
109      * Subclasses should perform the remaining conditions.
110      */

111     protected boolean isDragPossible(MouseEvent e) {
112         JComponent c = getComponent(e);
113         return (c == null) ? true : (c.getTransferHandler() != null);
114     }
115
116     protected JComponent getComponent(MouseEvent e) {
117     Object JavaDoc src = e.getSource();
118     if (src instanceof JComponent) {
119         JComponent c = (JComponent) src;
120         return c;
121     }
122     return null;
123     }
124
125 }
126
Popular Tags