1 11 package org.eclipse.ui.internal; 12 13 import java.util.Iterator ; 14 import java.util.List ; 15 16 import org.eclipse.jface.util.Geometry; 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.graphics.Cursor; 19 import org.eclipse.swt.graphics.Point; 20 import org.eclipse.swt.graphics.Rectangle; 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Control; 23 import org.eclipse.ui.internal.dnd.DragBorder; 24 import org.eclipse.ui.internal.dnd.DragUtil; 25 import org.eclipse.ui.internal.dnd.IDragOverListener; 26 import org.eclipse.ui.internal.dnd.IDropTarget; 27 import org.eclipse.ui.internal.dnd.IDropTarget2; 28 import org.eclipse.ui.internal.layout.IWindowTrim; 29 import org.eclipse.ui.internal.layout.LayoutUtil; 30 import org.eclipse.ui.internal.layout.TrimDescriptor; 31 import org.eclipse.ui.internal.layout.TrimLayout; 32 import org.eclipse.ui.internal.layout.TrimToolBarBase; 33 34 36 class TrimDropTarget implements IDragOverListener { 37 38 private final class ActualTrimDropTarget implements IDropTarget2 { 39 public IWindowTrim draggedTrim; 40 41 private DragBorder border = null; 43 private int dockedArea; 44 45 private int cursorAreaId; 47 private int initialAreaId; 48 private IWindowTrim initialInsertBefore; 49 private Rectangle initialLocation; 50 51 54 private ActualTrimDropTarget() { 55 super(); 56 57 draggedTrim = null; 58 dockedArea = SWT.NONE; 59 60 initialAreaId = SWT.NONE; 61 initialInsertBefore = null; 62 } 63 64 72 public void startDrag(IWindowTrim trim) { 73 if (draggedTrim != trim) { 75 draggedTrim = trim; 77 78 initialAreaId = layout.getTrimAreaId(draggedTrim.getControl()); 81 82 initialInsertBefore = getInsertBefore(initialAreaId, draggedTrim); 84 85 initialLocation = DragUtil.getDisplayBounds(draggedTrim.getControl()); 87 88 dockedArea = initialAreaId; 90 } 91 } 92 93 101 private int getTrimArea(Point pos) { 102 int areaId = getTrimArea(pos, 0); 104 105 if (areaId == SWT.NONE) { 107 areaId = getTrimArea(pos, TrimDragPreferences.getThreshold()); 108 } 109 110 return areaId; 112 } 113 114 124 private int getTrimArea(Point pos, int extendedBoundaryWidth) { 125 int[] areaIds = layout.getAreaIds(); 126 for (int i = 0; i < areaIds.length; i++) { 127 Rectangle trimRect = layout.getTrimRect(windowComposite, areaIds[i]); 128 trimRect = Geometry.toControl(windowComposite, trimRect); 129 130 if ( (areaIds[i] & getValidSides()) != SWT.NONE) { 132 switch (areaIds[i]) { 134 case SWT.LEFT: 135 trimRect.width += extendedBoundaryWidth; 136 137 if (pos.y >= trimRect.y && 138 pos.y <= (trimRect.y+trimRect.height) && 139 pos.x <= (trimRect.x+trimRect.width)) { 140 return areaIds[i]; 141 } 142 break; 143 case SWT.RIGHT: 144 trimRect.x -= extendedBoundaryWidth; 145 trimRect.width += extendedBoundaryWidth; 146 147 if (pos.y >= trimRect.y && 148 pos.y <= (trimRect.y+trimRect.height) && 149 pos.x >= trimRect.x) { 150 return areaIds[i]; 151 } 152 break; 153 case SWT.TOP: 154 trimRect.height += extendedBoundaryWidth; 155 156 if (pos.x >= trimRect.x && 157 pos.x <= (trimRect.x+trimRect.width) && 158 pos.y <= (trimRect.y+trimRect.height)) { 159 return areaIds[i]; 160 } 161 break; 162 case SWT.BOTTOM: 163 trimRect.y -= extendedBoundaryWidth; 164 trimRect.height += extendedBoundaryWidth; 165 166 if (pos.x >= trimRect.x && 167 pos.x <= (trimRect.x+trimRect.width) && 168 pos.y >= trimRect.y) { 169 return areaIds[i]; 170 } 171 break; 172 } 173 } 174 } 175 176 return SWT.NONE; 178 } 179 180 187 private IWindowTrim getInsertBefore(int areaId, Point pos) { 188 boolean isHorizontal = (areaId == SWT.TOP) || (areaId == SWT.BOTTOM); 189 190 List tDescs = layout.getTrimArea(areaId).getDescriptors(); 193 for (Iterator iter = tDescs.iterator(); iter.hasNext();) { 194 TrimDescriptor desc = (TrimDescriptor) iter.next(); 195 196 if (desc.getTrim() == draggedTrim) { 198 continue; 199 } 200 201 Rectangle bb = desc.getCache().getControl().getBounds(); 203 Point center = Geometry.centerPoint(bb); 204 if (isHorizontal) { 205 if (pos.x < center.x) { 206 return desc.getTrim(); 207 } 208 } 209 else { 210 if (pos.y < center.y) { 211 return desc.getTrim(); 212 } 213 } 214 } 215 216 return null; 217 } 218 219 227 private IWindowTrim getInsertBefore(int areaId, IWindowTrim trim) { 228 List tDescs = layout.getTrimArea(areaId).getDescriptors(); 229 for (Iterator iter = tDescs.iterator(); iter.hasNext();) { 230 TrimDescriptor desc = (TrimDescriptor) iter.next(); 231 if (desc.getTrim() == trim) { 232 if (iter.hasNext()) { 233 desc = (TrimDescriptor) iter.next(); 234 return desc.getTrim(); 235 } 236 return null; 237 } 238 } 239 240 return null; 241 } 242 243 248 public void track(Point pos) { 249 Rectangle r = new Rectangle(pos.x, pos.y, 1,1); 251 r = Geometry.toControl(windowComposite, r); 252 pos.x = r.x; 253 pos.y = r.y; 254 255 cursorAreaId = getTrimArea(pos); 257 258 if (cursorAreaId != SWT.NONE) { 260 trackInsideTrimArea(pos); 261 } else { 262 trackOutsideTrimArea(pos); 263 } 264 } 265 266 274 private void trackInsideTrimArea(Point pos) { 275 int newArea = getTrimArea(pos); 277 IWindowTrim newInsertBefore = getInsertBefore(newArea, pos); 278 279 boolean shouldDock = dockedArea == SWT.NONE; 281 282 if (dockedArea != SWT.NONE) { 284 IWindowTrim curInsertBefore = getInsertBefore(dockedArea, draggedTrim); 286 287 shouldDock = dockedArea != newArea || curInsertBefore != newInsertBefore; 289 } 290 291 if (shouldDock) { 293 dock(newArea, newInsertBefore); 295 } 296 } 297 298 303 private void trackOutsideTrimArea(Point pos) { 304 if (dockedArea != SWT.NONE) { 306 undock(); 307 } 308 309 border.setLocation(pos, SWT.BOTTOM); 310 } 311 312 318 private int getValidSides() { 319 int result = draggedTrim.getValidSides(); 320 if (result == SWT.NONE) { 321 return result; 322 } 323 324 return SWT.TOP | SWT.BOTTOM | SWT.LEFT | SWT.RIGHT; 326 } 327 328 332 private void redock() { 333 Rectangle startRect = DragUtil.getDisplayBounds(draggedTrim.getControl()); 335 RectangleAnimation animation = new RectangleAnimation( 336 windowComposite.getShell(), startRect, initialLocation, 300); 337 animation.schedule(); 338 339 dock(initialAreaId, initialInsertBefore); 340 } 341 342 345 public void drop() { 346 if (dockedArea == SWT.NONE) { 348 redock(); 349 } 350 } 351 352 356 private void undock() { 357 layout.removeTrim(draggedTrim); 359 LayoutUtil.resize(draggedTrim.getControl()); 360 361 draggedTrim.dock(initialAreaId); 363 draggedTrim.getControl().setSize(initialLocation.width, initialLocation.height); 364 365 boolean wantsFrame = !(draggedTrim instanceof TrimToolBarBase); 368 border = new DragBorder(windowComposite, draggedTrim.getControl(), wantsFrame); 369 370 dockedArea = SWT.NONE; 371 } 372 373 376 private void dock(int areaId, IWindowTrim insertBefore) { 377 if (border != null) { 379 border.dispose(); 380 border = null; 381 } 382 383 draggedTrim.dock(areaId); 385 386 layout.addTrim(areaId, draggedTrim, insertBefore); 388 LayoutUtil.resize(draggedTrim.getControl()); 389 390 dockedArea = areaId; 392 } 393 394 397 public Cursor getCursor() { 398 if (cursorAreaId == SWT.NONE) { 400 return windowComposite.getDisplay().getSystemCursor(SWT.CURSOR_NO); 401 } 402 403 return windowComposite.getDisplay().getSystemCursor(SWT.CURSOR_SIZEALL); 405 } 406 407 410 public Rectangle getSnapRectangle() { 411 return new Rectangle(100000, 0,0,0); 414 } 415 416 419 public void dragFinished(boolean dropPerformed) { 420 if (!dropPerformed && dockedArea == SWT.NONE) { 422 redock(); 424 } 425 426 draggedTrim = null; 430 } 431 } 432 433 private ActualTrimDropTarget dropTarget; 434 435 private TrimLayout layout; 436 private Composite windowComposite; 437 438 444 public TrimDropTarget(Composite someComposite, WorkbenchWindow theWindow) { 445 layout = (TrimLayout) someComposite.getLayout(); 446 windowComposite = someComposite; 447 448 dropTarget = new ActualTrimDropTarget(); 450 } 451 452 455 public IDropTarget drag(Control currentControl, Object draggedObject, 456 Point position, final Rectangle dragRectangle) { 457 458 if (!(draggedObject instanceof IWindowTrim)) { 460 return null; 461 } 462 463 IWindowTrim trim = (IWindowTrim) draggedObject; 465 if (trim.getControl().getShell() != windowComposite.getShell()) { 466 return null; 467 } 468 469 if (dropTarget.draggedTrim == null) { 471 dropTarget.startDrag(trim); 472 } 473 474 dropTarget.track(position); 476 477 windowComposite.getDisplay().update(); 479 480 return dropTarget; 481 } 482 } 483 | Popular Tags |