1 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 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 LISTENER_ID = PresentationUtil.class.getName() 34 + ".dragListener"; 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 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 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 int originalMouseButton = de.button; 103 104 de.button = initialMouseButton; 107 108 Listener l = currentListener; 109 cancelDrag(); 110 l.handleEvent(de); 111 112 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 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 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 |