KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > presentations > PresentationUtil


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.presentations;
12
13 import org.eclipse.jface.util.Geometry;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.graphics.Point;
16 import org.eclipse.swt.widgets.Control;
17 import org.eclipse.swt.widgets.Event;
18 import org.eclipse.swt.widgets.Listener;
19 import org.eclipse.ui.internal.dnd.DragUtil;
20
21 /**
22  * Contains various utility methods for Presentation authors
23  *
24  * @since 3.0
25  */

26 public class PresentationUtil {
27     private static Point anchor;
28
29     private final static int HYSTERESIS = 16;
30     
31     private static int initialMouseButton = 0;
32
33     private final static String JavaDoc LISTENER_ID = PresentationUtil.class.getName()
34             + ".dragListener"; //$NON-NLS-1$
35

36     private static Event dragEvent;
37
38     private static Listener currentListener = null;
39
40     private static Control dragSource;
41
42     private static Listener dragListener = new Listener() {
43         public void handleEvent(Event event) {
44             dragEvent = event;
45             if (dragSource != event.widget) {
46                 dragSource = null;
47                 currentListener = null;
48             }
49         }
50     };
51
52     /**
53      * Returns whether the mouse has moved enough to warrant
54      * opening a tracker.
55      */

56     private static boolean hasMovedEnough(Event event) {
57         return Geometry.distanceSquared(DragUtil.getEventLoc(event), anchor) >= HYSTERESIS
58                 * HYSTERESIS;
59     }
60
61     private static Listener moveListener = new Listener() {
62         public void handleEvent(Event event) {
63             handleMouseMove(event);
64         }
65     };
66
67     private static Listener clickListener = new Listener() {
68         public void handleEvent(Event e) {
69             handleMouseClick(e);
70         }
71     };
72
73     private static Listener mouseDownListener = new Listener() {
74         public void handleEvent(Event event) {
75             if (event.widget instanceof Control) {
76                 // Remember the button that started the drag so we
77
// can forward it on the call to the 'externalDragListener'
78
initialMouseButton = event.button;
79                 
80                 dragSource = (Control) event.widget;
81                 currentListener = (Listener) dragSource.getData(LISTENER_ID);
82                 anchor = DragUtil.getEventLoc(event);
83
84                 if (dragEvent != null && (dragEvent.widget != dragSource)) {
85                     dragEvent = null;
86                 }
87             }
88         }
89     };
90
91     private static void handleMouseClick(Event event) {
92         cancelDrag();
93     }
94
95     private static void handleMouseMove(Event e) {
96         if (currentListener != null && dragEvent != null && hasMovedEnough(e)) {
97             if (dragSource != null && !dragSource.isDisposed()
98                     && dragSource == e.widget) {
99                 Event de = dragEvent;
100                 
101                 // cache the current value so we can restore it later
102
int originalMouseButton = de.button;
103                 
104                 // Update the button field so that the drag listener
105
// can detect whether or not it's a 'right button' drag
106
de.button = initialMouseButton;
107                 
108                 Listener l = currentListener;
109                 cancelDrag();
110                 l.handleEvent(de);
111                 
112                 // Restore the event's state so that other listeners see
113
// the original values
114
de.button = originalMouseButton;
115             } else {
116                 cancelDrag();
117             }
118         }
119     }
120
121     private static void cancelDrag() {
122         currentListener = null;
123         dragEvent = null;
124         dragSource = null;
125
126         initialMouseButton = 0;
127     }
128
129     /**
130      * Adds a drag listener to the given control. The behavior is very similar
131      * to control.addListener(SWT.DragDetect, dragListener), however the listener
132      * attached by this method is less sensitive. The drag event is only fired
133      * once the user moves the cursor more than HYSTERESIS pixels.
134      * <p>
135      * This is useful for registering a listener that will trigger an editor or
136      * view drag, since an overly sensitive drag listener can cause users to accidentally
137      * drag views when trying to select a tab.</p>
138      * <p>
139      * Currently, only one such drag listener can be registered at a time. </p>
140      *
141      * @param control the control containing the drag listener
142      * @param externalDragListener the drag listener to attach
143      */

144     public static void addDragListener(Control control,
145             Listener externalDragListener) {
146         control.addListener(SWT.DragDetect, dragListener);
147         control.addListener(SWT.MouseUp, clickListener);
148         control.addListener(SWT.MouseDoubleClick, clickListener);
149         control.addListener(SWT.MouseDown, mouseDownListener);
150         control.addListener(SWT.MouseMove, moveListener);
151         control.setData(LISTENER_ID, externalDragListener);
152     }
153
154     /**
155      * Removes a drag listener that was previously attached using addDragListener
156      *
157      * @param control the control containing the drag listener
158      * @param externalDragListener the drag listener to remove
159      */

160     public static void removeDragListener(Control control,
161             Listener externalDragListener) {
162         control.removeListener(SWT.DragDetect, dragListener);
163         control.removeListener(SWT.MouseUp, clickListener);
164         control.removeListener(SWT.MouseDoubleClick, clickListener);
165         control.removeListener(SWT.MouseDown, mouseDownListener);
166         control.removeListener(SWT.MouseMove, moveListener);
167         control.setData(LISTENER_ID, null);
168         if (externalDragListener == currentListener) {
169             cancelDrag();
170         }
171     }
172
173 }
174
Popular Tags