001: /*
002: * Copyright 1998-2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt.motif;
027:
028: import java.awt.Toolkit;
029: import java.awt.Component;
030:
031: import java.awt.Point;
032: import java.awt.dnd.DnDConstants;
033: import java.awt.dnd.DragSource;
034: import java.awt.dnd.MouseDragGestureRecognizer;
035: import java.awt.dnd.DragGestureListener;
036:
037: import java.awt.event.InputEvent;
038: import java.awt.event.MouseEvent;
039: import java.awt.event.MouseListener;
040: import java.awt.event.MouseMotionListener;
041:
042: import java.lang.reflect.*;
043:
044: import sun.awt.dnd.SunDragSourceContextPeer;
045:
046: /**
047: * <p>
048: * This subclass of MouseDragGestureRecognizer defines a DragGestureRecognizer
049: * for Mouse based gestures on OSF/Motif.
050: * </p>
051: *
052: * @author Laurence P. G. Cable
053: * @version 1.22
054: *
055: * @see java.awt.dnd.DragGestureListener
056: * @see java.awt.dnd.DragGestureEvent
057: * @see java.awt.dnd.DragSource
058: */
059:
060: class MMouseDragGestureRecognizer extends MouseDragGestureRecognizer {
061:
062: private static final long serialVersionUID = -841711780352520383L;
063:
064: /*
065: * constant for number of pixels hysterisis before drag is determined
066: * to have started
067: */
068:
069: protected static int motionThreshold;
070:
071: protected static final int ButtonMask = InputEvent.BUTTON1_DOWN_MASK
072: | InputEvent.BUTTON2_DOWN_MASK
073: | InputEvent.BUTTON3_DOWN_MASK;
074:
075: /**
076: * construct a new MMouseDragGestureRecognizer
077: *
078: * @param ds The DragSource for the Component c
079: * @param c The Component to observe
080: * @param act The actions permitted for this Drag
081: * @param dgl The DragGestureRecognizer to notify when a gesture is detected
082: *
083: */
084:
085: protected MMouseDragGestureRecognizer(DragSource ds, Component c,
086: int act, DragGestureListener dgl) {
087: super (ds, c, act, dgl);
088: }
089:
090: /**
091: * construct a new MMouseDragGestureRecognizer
092: *
093: * @param ds The DragSource for the Component c
094: * @param c The Component to observe
095: * @param act The actions permitted for this Drag
096: */
097:
098: protected MMouseDragGestureRecognizer(DragSource ds, Component c,
099: int act) {
100: this (ds, c, act, null);
101: }
102:
103: /**
104: * construct a new MMouseDragGestureRecognizer
105: *
106: * @param ds The DragSource for the Component c
107: * @param c The Component to observe
108: */
109:
110: protected MMouseDragGestureRecognizer(DragSource ds, Component c) {
111: this (ds, c, DnDConstants.ACTION_NONE);
112: }
113:
114: /**
115: * construct a new MMouseDragGestureRecognizer
116: *
117: * @param ds The DragSource for the Component c
118: */
119:
120: protected MMouseDragGestureRecognizer(DragSource ds) {
121: this (ds, null);
122: }
123:
124: /**
125: * determine the drop action from the event
126: */
127:
128: protected int mapDragOperationFromModifiers(MouseEvent e) {
129: int mods = e.getModifiersEx();
130: int btns = mods & ButtonMask;
131:
132: // Do not allow right mouse button drag since Motif DnD does not
133: // terminate drag operation on right mouse button release.
134: if (!(btns == InputEvent.BUTTON1_DOWN_MASK || btns == InputEvent.BUTTON2_DOWN_MASK)) {
135: return DnDConstants.ACTION_NONE;
136: }
137:
138: return SunDragSourceContextPeer.convertModifiersToDropAction(
139: mods, getSourceActions());
140: }
141:
142: /**
143: * Invoked when the mouse has been clicked on a component.
144: */
145:
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:
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:
171: public void mouseReleased(MouseEvent e) {
172: events.clear();
173: }
174:
175: /**
176: * Invoked when the mouse enters a component.
177: */
178:
179: public void mouseEntered(MouseEvent e) {
180: events.clear();
181: }
182:
183: /**
184: * Invoked when the mouse exits a component.
185: */
186:
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:
201: public void mouseDragged(MouseEvent e) {
202: if (!events.isEmpty()) { // gesture pending
203: int dop = mapDragOperationFromModifiers(e);
204:
205: if (dop == DnDConstants.ACTION_NONE) {
206: return;
207: }
208:
209: MouseEvent trigger = (MouseEvent) events.get(0);
210:
211: Point origin = trigger.getPoint();
212: Point current = e.getPoint();
213:
214: int dx = Math.abs(origin.x - current.x);
215: int dy = Math.abs(origin.y - current.y);
216:
217: if (dx > motionThreshold || dy > motionThreshold) {
218: fireDragGestureRecognized(dop,
219: ((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:
230: public void mouseMoved(MouseEvent e) {
231: // do nothing
232: }
233: }
|