1 11 package org.eclipse.ui.internal.dnd; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.jface.util.Geometry; 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.graphics.Point; 20 import org.eclipse.swt.graphics.Rectangle; 21 import org.eclipse.swt.widgets.Control; 22 import org.eclipse.swt.widgets.Display; 23 import org.eclipse.swt.widgets.Event; 24 import org.eclipse.swt.widgets.Listener; 25 import org.eclipse.swt.widgets.Shell; 26 import org.eclipse.swt.widgets.Tracker; 27 import org.eclipse.ui.PlatformUI; 28 import org.eclipse.ui.internal.DragCursors; 29 30 33 public class DragUtil { 34 private static final String DROP_TARGET_ID = "org.eclipse.ui.internal.dnd.dropTarget"; 36 41 private static TestDropLocation forcedDropTarget = null; 42 43 46 private static List defaultTargets = new ArrayList (); 47 48 58 public static void addDragTarget(Control control, IDragOverListener target) { 59 if (control == null) { 60 defaultTargets.add(target); 61 } else { 62 List targetList = getTargetList(control); 63 64 if (targetList == null) { 65 targetList = new ArrayList (1); 66 } 67 targetList.add(target); 68 control.setData(DROP_TARGET_ID, targetList); 69 } 70 } 71 72 80 private static List getTargetList(Control control) { 81 List result = (List ) control.getData(DROP_TARGET_ID); 82 return result; 83 } 84 85 91 public static void removeDragTarget(Control control, 92 IDragOverListener target) { 93 if (control == null) { 94 defaultTargets.remove(target); 95 } else { 96 List targetList = getTargetList(control); 97 if (targetList != null) { 98 targetList.remove(target); 99 if (targetList.isEmpty()) { 100 control.setData(DROP_TARGET_ID, null); 101 } 102 } 103 } 104 } 105 106 115 public static Rectangle getDisplayBounds(Control boundsControl) { 116 Control parent = boundsControl.getParent(); 117 if (parent == null || boundsControl instanceof Shell) { 118 return boundsControl.getBounds(); 119 } 120 121 return Geometry.toDisplay(parent, boundsControl.getBounds()); 122 } 123 124 public static boolean performDrag(final Object draggedItem, 125 Rectangle sourceBounds, Point initialLocation, boolean allowSnapping) { 126 127 IDropTarget target = dragToTarget(draggedItem, sourceBounds, 128 initialLocation, allowSnapping); 129 130 if (target == null) { 131 return false; 132 } 133 134 target.drop(); 135 136 if (target!= null && target instanceof IDropTarget2) { 138 ((IDropTarget2)target).dragFinished(true); 139 } 140 141 return true; 142 } 143 144 152 public static boolean dragTo(Display display, Object draggedItem, 153 Point finalLocation, Rectangle dragRectangle) { 154 Control currentControl = SwtUtil.findControl(display, finalLocation); 155 156 IDropTarget target = getDropTarget(currentControl, draggedItem, 157 finalLocation, dragRectangle); 158 159 if (target == null) { 160 return false; 161 } 162 163 target.drop(); 164 165 return true; 166 } 167 168 178 public static void forceDropLocation(TestDropLocation forcedLocation) { 179 forcedDropTarget = forcedLocation; 180 } 181 182 197 static IDropTarget dragToTarget(final Object draggedItem, 198 final Rectangle sourceBounds, final Point initialLocation, 199 final boolean allowSnapping) { 200 final Display display = Display.getCurrent(); 201 202 if (forcedDropTarget != null) { 204 Point location = forcedDropTarget.getLocation(); 205 206 Control currentControl = SwtUtil.findControl(forcedDropTarget.getShells(), location); 207 return getDropTarget(currentControl, draggedItem, location, 208 sourceBounds); 209 } 210 211 final Tracker tracker = new Tracker(display, SWT.NULL); 214 tracker.setStippled(true); 215 216 tracker.addListener(SWT.Move, new Listener() { 217 public void handleEvent(final Event event) { 218 display.syncExec(new Runnable () { 219 public void run() { 220 Point location = new Point(event.x, event.y); 222 223 IDropTarget target = null; 225 226 Control targetControl = display.getCursorControl(); 227 228 target = getDropTarget(targetControl, 230 draggedItem, location, 231 tracker.getRectangles()[0]); 232 233 Rectangle snapTarget = null; 235 if (target != null) { 236 snapTarget = target.getSnapRectangle(); 237 238 tracker.setCursor(target.getCursor()); 239 } else { 240 tracker.setCursor(DragCursors 241 .getCursor(DragCursors.INVALID)); 242 } 243 244 if (allowSnapping) { 246 if (snapTarget == null) { 247 snapTarget = new Rectangle(sourceBounds.x 248 + location.x - initialLocation.x, 249 sourceBounds.y + location.y 250 - initialLocation.y, 251 sourceBounds.width, sourceBounds.height); 252 } 253 254 Rectangle[] currentRectangles = tracker.getRectangles(); 257 258 if (!(currentRectangles.length == 1 && currentRectangles[0] 259 .equals(snapTarget))) { 260 tracker.setRectangles(new Rectangle[] { snapTarget }); 261 } 262 } 263 } 264 }); 265 } 266 }); 267 268 IDropTarget target = null; 271 Control startControl = display.getCursorControl(); 272 273 if (startControl != null && allowSnapping) { 274 target = getDropTarget(startControl, 275 draggedItem, initialLocation, 276 sourceBounds); 277 } 278 279 Rectangle startRect = sourceBounds; 281 if (target != null) { 282 Rectangle rect = target.getSnapRectangle(); 283 284 if (rect != null) { 285 startRect = rect; 286 } 287 288 tracker.setCursor(target.getCursor()); 289 } 290 291 if (startRect != null) { 292 tracker.setRectangles(new Rectangle[] { Geometry.copy(startRect)}); 293 } 294 295 298 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); 302 if (shell != null) { 303 shell.setCapture(true); 304 } 305 306 boolean trackingOk = tracker.open(); 308 309 if (shell != null) { 312 shell.setCapture(false); 313 } 314 315 317 IDropTarget dropTarget = null; 319 Point finalLocation = display.getCursorLocation(); 320 Control targetControl = display.getCursorControl(); 321 dropTarget = getDropTarget(targetControl, draggedItem, 322 finalLocation, tracker.getRectangles()[0]); 323 324 tracker.dispose(); 326 327 if (trackingOk) { 330 return dropTarget; 331 } 332 else if (dropTarget!= null && dropTarget instanceof IDropTarget2) { 333 ((IDropTarget2)dropTarget).dragFinished(false); 335 } 336 337 return null; 338 } 339 340 351 private static IDropTarget getDropTarget(List toSearch, 352 Control mostSpecificControl, Object draggedObject, Point position, 353 Rectangle dragRectangle) { 354 if (toSearch == null) { 355 return null; 356 } 357 358 Iterator iter = toSearch.iterator(); 359 while (iter.hasNext()) { 360 IDragOverListener next = (IDragOverListener) iter.next(); 361 362 IDropTarget dropTarget = next.drag(mostSpecificControl, 363 draggedObject, position, dragRectangle); 364 365 if (dropTarget != null) { 366 return dropTarget; 367 } 368 } 369 370 return null; 371 } 372 373 380 public static IDropTarget getDropTarget(Control toSearch, 381 Object draggedObject, Point position, Rectangle dragRectangle) { 382 for (Control current = toSearch; current != null; current = current 384 .getParent()) { 385 IDropTarget dropTarget = getDropTarget(getTargetList(current), 386 toSearch, draggedObject, position, dragRectangle); 387 388 if (dropTarget != null) { 389 return dropTarget; 390 } 391 392 if (current instanceof Shell) { 394 break; 395 } 396 } 397 398 return getDropTarget(defaultTargets, toSearch, draggedObject, position, 400 dragRectangle); 401 } 402 403 407 public static Point getEventLoc(Event event) { 408 Control ctrl = (Control) event.widget; 409 return ctrl.toDisplay(new Point(event.x, event.y)); 410 } 411 412 } 413 | Popular Tags |