1 /*
2  * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package sun.awt.X11;
27 
28 import java.awt.Toolkit;
29 import java.awt.Component;
30 
31 import java.awt.Point;
32 import java.awt.dnd.DnDConstants;
33 import java.awt.dnd.DragSource;
34 import java.awt.dnd.MouseDragGestureRecognizer;
35 import java.awt.dnd.DragGestureListener;
36 
37 import java.awt.event.InputEvent;
38 import java.awt.event.MouseEvent;
39 import java.awt.event.MouseListener;
40 import java.awt.event.MouseMotionListener;
41 
42 import java.lang.reflect.*;
43 
44 import sun.awt.dnd.SunDragSourceContextPeer;
45 
46 /**
47  * <p>
48  * This subclass of MouseDragGestureRecognizer defines a DragGestureRecognizer
49  * for Mouse based gestures on OSF/Motif.
50  * </p>
51  *
52  * @author Laurence P. G. Cable
53  *
54  * @see java.awt.dnd.DragGestureListener
55  * @see java.awt.dnd.DragGestureEvent
56  * @see java.awt.dnd.DragSource
57  */
58 
59 class XMouseDragGestureRecognizer extends MouseDragGestureRecognizer {
60 
61     private static final long serialVersionUID = -841711780352520383L;
62 
63     /*
64      * constant for number of pixels hysterisis before drag is determined
65      * to have started
66      */
67 
68     protected static int motionThreshold;
69 
70 
71     protected static final int ButtonMask = InputEvent.BUTTON1_DOWN_MASK |
72                                             InputEvent.BUTTON2_DOWN_MASK |
73                                             InputEvent.BUTTON3_DOWN_MASK;
74 
75     /**
76      * construct a new XMouseDragGestureRecognizer
77      *
78      * @param ds  The DragSource for the Component c
79      * @param c   The Component to observe
80      * @param act The actions permitted for this Drag
81      * @param dgl The DragGestureRecognizer to notify when a gesture is detected
82      *
83      */
84 
XMouseDragGestureRecognizer(DragSource ds, Component c, int act, DragGestureListener dgl)85     protected XMouseDragGestureRecognizer(DragSource ds, Component c, int act, DragGestureListener dgl) {
86         super(ds, c, act, dgl);
87     }
88 
89     /**
90      * construct a new XMouseDragGestureRecognizer
91      *
92      * @param ds  The DragSource for the Component c
93      * @param c   The Component to observe
94      * @param act The actions permitted for this Drag
95      */
96 
XMouseDragGestureRecognizer(DragSource ds, Component c, int act)97     protected XMouseDragGestureRecognizer(DragSource ds, Component c, int act) {
98         this(ds, c, act, null);
99     }
100 
101     /**
102      * construct a new XMouseDragGestureRecognizer
103      *
104      * @param ds  The DragSource for the Component c
105      * @param c   The Component to observe
106      */
107 
XMouseDragGestureRecognizer(DragSource ds, Component c)108     protected XMouseDragGestureRecognizer(DragSource ds, Component c) {
109         this(ds, c, DnDConstants.ACTION_NONE);
110     }
111 
112     /**
113      * construct a new XMouseDragGestureRecognizer
114      *
115      * @param ds  The DragSource for the Component c
116      */
117 
XMouseDragGestureRecognizer(DragSource ds)118     protected XMouseDragGestureRecognizer(DragSource ds) {
119         this(ds, null);
120     }
121 
122     /**
123      * determine the drop action from the event
124      */
125 
mapDragOperationFromModifiers(MouseEvent e)126     protected int mapDragOperationFromModifiers(MouseEvent e) {
127         int mods = e.getModifiersEx();
128         int btns = mods & ButtonMask;
129 
130         // Do not allow right mouse button drag since Motif DnD does not
131         // terminate drag operation on right mouse button release.
132         if (!(btns == InputEvent.BUTTON1_DOWN_MASK ||
133               btns == InputEvent.BUTTON2_DOWN_MASK)) {
134             return DnDConstants.ACTION_NONE;
135         }
136 
137         return
138             SunDragSourceContextPeer.convertModifiersToDropAction(mods,
139                                                                   getSourceActions());
140     }
141 
142     /**
143      * Invoked when the mouse has been clicked on a component.
144      */
145 
mouseClicked(MouseEvent e)146     public void mouseClicked(MouseEvent e) {
147         // do nothing
148     }
149 
150     /**
151      * Invoked when a mouse button has been pressed on a component.
152      */
153 
mousePressed(MouseEvent e)154     public void mousePressed(MouseEvent e) {
155         events.clear();
156 
157         if (mapDragOperationFromModifiers(e) != DnDConstants.ACTION_NONE) {
158             try {
159                 motionThreshold = DragSource.getDragThreshold();
160             } catch (Exception exc) {
161                 motionThreshold = 5;
162             }
163             appendEvent(e);
164         }
165     }
166 
167     /**
168      * Invoked when a mouse button has been released on a component.
169      */
170 
mouseReleased(MouseEvent e)171     public void mouseReleased(MouseEvent e) {
172         events.clear();
173     }
174 
175     /**
176      * Invoked when the mouse enters a component.
177      */
178 
mouseEntered(MouseEvent e)179     public void mouseEntered(MouseEvent e) {
180         events.clear();
181     }
182 
183     /**
184      * Invoked when the mouse exits a component.
185      */
186 
mouseExited(MouseEvent e)187     public void mouseExited(MouseEvent e) {
188         if (!events.isEmpty()) { // gesture pending
189             int dragAction = mapDragOperationFromModifiers(e);
190 
191             if (dragAction == DnDConstants.ACTION_NONE) {
192                 events.clear();
193             }
194         }
195     }
196 
197     /**
198      * Invoked when a mouse button is pressed on a component.
199      */
200 
mouseDragged(MouseEvent e)201     public void mouseDragged(MouseEvent e) {
202         if (!events.isEmpty()) { // gesture pending
203             int dop = mapDragOperationFromModifiers(e);
204 
205 
206             if (dop == DnDConstants.ACTION_NONE) {
207                 return;
208             }
209 
210             MouseEvent trigger = (MouseEvent)events.get(0);
211 
212             Point      origin  = trigger.getPoint();
213             Point      current = e.getPoint();
214 
215             int        dx      = Math.abs(origin.x - current.x);
216             int        dy      = Math.abs(origin.y - current.y);
217 
218             if (dx > motionThreshold || dy > motionThreshold) {
219                 fireDragGestureRecognized(dop, ((MouseEvent)getTriggerEvent()).getPoint());
220             } else
221                 appendEvent(e);
222         }
223     }
224 
225     /**
226      * Invoked when the mouse button has been moved on a component
227      * (with no buttons no down).
228      */
229 
mouseMoved(MouseEvent e)230     public void mouseMoved(MouseEvent e) {
231         // do nothing
232     }
233 }
234