1 11 12 package org.eclipse.ui.internal; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.jface.action.ToolBarManager; 19 import org.eclipse.jface.util.Geometry; 20 import org.eclipse.swt.SWT; 21 import org.eclipse.swt.events.DisposeEvent; 22 import org.eclipse.swt.events.DisposeListener; 23 import org.eclipse.swt.graphics.Cursor; 24 import org.eclipse.swt.graphics.Point; 25 import org.eclipse.swt.graphics.Rectangle; 26 import org.eclipse.swt.widgets.Control; 27 import org.eclipse.swt.widgets.Event; 28 import org.eclipse.swt.widgets.Listener; 29 import org.eclipse.swt.widgets.ToolBar; 30 import org.eclipse.swt.widgets.ToolItem; 31 import org.eclipse.ui.IViewReference; 32 import org.eclipse.ui.internal.dnd.AbstractDropTarget; 33 import org.eclipse.ui.internal.dnd.DragUtil; 34 import org.eclipse.ui.internal.dnd.IDragOverListener; 35 import org.eclipse.ui.internal.dnd.IDropTarget; 36 import org.eclipse.ui.presentations.PresentationUtil; 37 38 42 public class FastViewDnDHandler implements IDragOverListener { 43 private String id; 44 private ToolBarManager tbm; 45 private WorkbenchWindow wbw; 46 private ViewDropTarget dropTarget = null; 47 48 private Listener dragListener = new Listener() { 49 public void handleEvent(Event event) { 50 Point position = DragUtil.getEventLoc(event); 51 52 ToolBar toolbar = tbm.getControl(); 53 Point local = toolbar.toControl(position); 54 ToolItem item = toolbar.getItem(local); 55 IViewReference ref = (IViewReference) item 56 .getData(ShowFastViewContribution.FAST_VIEW); 57 58 if (ref != null) { 59 startDraggingFastView(ref, position, false); 60 } 61 } 62 }; 63 64 class ViewDropTarget extends AbstractDropTarget { 65 List panes; 66 ToolItem curItem; 67 68 71 public ViewDropTarget(List panesToDrop, ToolItem position) { 72 setTarget(panesToDrop, position); 73 } 74 75 public void setTarget(List panesToDrop, ToolItem position) { 76 panes = panesToDrop; 77 this.curItem = position; 78 } 79 80 83 public void drop() { 84 Perspective persp = wbw.getActiveWorkbenchPage().getActivePerspective(); 85 FastViewManager fvm = persp.getFastViewManager(); 86 87 int insertIndex = tbm.getControl().indexOf(curItem); 88 Iterator iter = panes.iterator(); 89 while (iter.hasNext()) { 90 ViewPane pane = (ViewPane) iter.next(); 91 IViewReference ref = pane.getViewReference(); 92 adoptRef(ref); 93 fvm.addViewReference(id, insertIndex++, ref, !iter.hasNext()); 94 } 95 } 96 97 private void adoptRef(IViewReference ref) { 98 Perspective persp = wbw.getActiveWorkbenchPage() 99 .getActivePerspective(); 100 PerspectiveHelper helper = persp.getPresentation(); 101 ContainerPlaceholder ourContainerPlaceholder = (ContainerPlaceholder) helper 102 .findPart(id, null); 103 LayoutPart refPart = helper.findPart(ref.getId(), ref 104 .getSecondaryId()); 105 ILayoutContainer refContainer = refPart.container; 106 if (refContainer != ourContainerPlaceholder) { 107 if (refPart instanceof PartPlaceholder) { 110 if (refContainer instanceof ContainerPlaceholder) { 111 ViewStack realContainer = (ViewStack) ((ContainerPlaceholder) refContainer) 113 .getRealContainer(); 114 realContainer.remove(refPart); 115 } 116 else if (refContainer instanceof ViewStack) { 117 refContainer.remove(refPart); 118 } 119 } 120 else { 121 helper.derefPart(refPart); 123 } 124 PartPlaceholder newPlaceholder = new PartPlaceholder(ref 125 .getId()); 126 ourContainerPlaceholder.add(newPlaceholder); 127 } 128 } 129 130 135 public Cursor getCursor() { 136 return DragCursors.getCursor(DragCursors.FASTVIEW); 137 } 138 139 public Rectangle getSnapRectangle() { 140 if (curItem == null) { 141 if (tbm.getControl().getItemCount() > 0) { 146 return getLocationOfNextIcon(); 147 } 148 return DragUtil.getDisplayBounds(tbm.getControl()); 150 } 151 152 return Geometry.toDisplay(tbm.getControl(), curItem.getBounds()); 153 } 154 } 155 156 159 public FastViewDnDHandler(String id, final ToolBarManager tbm, WorkbenchWindow wbw) { 160 this.id = id; 161 this.tbm = tbm; 162 this.wbw = wbw; 163 164 DragUtil.addDragTarget(tbm.getControl(), this); 166 PresentationUtil.addDragListener(tbm.getControl(), dragListener); 167 168 tbm.getControl().addDisposeListener(new DisposeListener() { 170 public void widgetDisposed(DisposeEvent e) { 171 DragUtil.removeDragTarget((Control)(e.widget), FastViewDnDHandler.this); 172 PresentationUtil.removeDragListener(tbm.getControl(), dragListener); 173 } 174 }); 175 } 176 177 181 private ToolItem getToolItem(Point position) { 182 ToolBar toolbar = tbm.getControl(); 183 Point local = toolbar.toControl(position); 184 return toolbar.getItem(local); 185 } 186 187 190 public IDropTarget drag(Control currentControl, Object draggedObject, 191 Point position, Rectangle dragRectangle) { 192 if (isStandaloneStack()) 194 return null; 195 196 ToolItem targetItem = getToolItem(position); 197 if (draggedObject instanceof ViewPane) { 198 ViewPane pane = (ViewPane) draggedObject; 199 200 if (pane.getWorkbenchWindow() != wbw) { 202 return null; 203 } 204 205 List newList = new ArrayList (1); 206 newList.add(draggedObject); 207 208 return createDropTarget(newList, targetItem); 209 } 210 if (draggedObject instanceof ViewStack) { 211 ViewStack folder = (ViewStack) draggedObject; 212 213 if (folder.getWorkbenchWindow() != wbw) { 214 return null; 215 } 216 217 List viewList = new ArrayList (folder.getItemCount()); 218 LayoutPart[] children = folder.getChildren(); 219 220 for (int idx = 0; idx < children.length; idx++) { 221 if (!(children[idx] instanceof PartPlaceholder)) { 222 viewList.add(children[idx]); 223 } 224 } 225 226 return createDropTarget(viewList, targetItem); 227 } 228 229 return null; 230 } 231 237 private boolean isStandaloneStack() { 238 Perspective persp = wbw.getActiveWorkbenchPage().getActivePerspective(); 239 List fvs = persp.getFastViewManager().getFastViews(id); 240 for (Iterator iterator = fvs.iterator(); iterator.hasNext();) { 241 IViewReference ref = (IViewReference) iterator.next(); 242 if (persp.isStandaloneView(ref)) 243 return true; 244 } 245 246 return false; 247 } 248 249 private IDropTarget createDropTarget(List viewList, ToolItem targetItem) { 250 if (dropTarget == null) { 251 dropTarget = new ViewDropTarget(viewList, targetItem); 252 } else { 253 dropTarget.setTarget(viewList, targetItem); 254 } 255 return dropTarget; 256 } 257 258 262 public Rectangle getLocationOfNextIcon() { 263 ToolBar control = tbm.getControl(); 264 265 Rectangle result = control.getBounds(); 266 Point size = control.computeSize(SWT.DEFAULT, SWT.DEFAULT, false); 267 result.height = size.y; 268 result.width = size.x; 269 270 boolean horizontal = (control.getStyle() & SWT.VERTICAL) == 0; 271 if (control.getItemCount() == 0) { 272 Geometry.setDimension(result, horizontal, 0); 273 } 274 275 int hoverSide = horizontal ? SWT.RIGHT : SWT.BOTTOM; 276 277 result = Geometry.getExtrudedEdge(result, -Geometry.getDimension( 278 result, !horizontal), hoverSide); 279 280 return Geometry.toDisplay(control.getParent(), result); 281 } 282 283 288 private int getIndex(IViewReference toFind) { 289 ToolItem[] items = tbm.getControl().getItems(); 290 for (int i = 0; i < items.length; i++) { 291 if (items[i].getData(ShowFastViewContribution.FAST_VIEW) == toFind) { 292 return i; 293 } 294 } 295 296 return -1; 297 } 298 299 305 protected void startDraggingFastView(IViewReference ref, Point position, 306 boolean usingKeyboard) { 307 int index = getIndex(ref); 308 if (index == -1) 309 return; 310 311 ToolItem item = tbm.getControl().getItem(index); 312 Rectangle dragRect = Geometry.toDisplay(tbm.getControl(), item.getBounds()); 313 startDrag(((WorkbenchPartReference) ref).getPane(), dragRect, position, 314 usingKeyboard); 315 } 316 317 private void startDrag(Object toDrag, Rectangle dragRect, Point position, 318 boolean usingKeyboard) { 319 WorkbenchPage page = wbw.getActiveWorkbenchPage(); 320 Perspective persp = page.getActivePerspective(); 321 322 if (toDrag instanceof ViewPane) { 324 ViewPane pane = (ViewPane) toDrag; 325 if (!persp.isMoveable(pane.getViewReference())) 326 return; 327 } 328 329 IViewReference oldFastView = null; 330 if (persp != null) { 331 oldFastView = persp.getActiveFastView(); 332 333 if (page != null) { 334 page.hideFastView(); 335 } 336 } 337 338 if (page.isPageZoomed()) { 339 page.zoomOut(); 340 } 341 342 boolean success = DragUtil.performDrag(toDrag, dragRect, position, 343 !usingKeyboard); 344 345 if (!success && oldFastView != null && page != null) { 347 page.toggleFastView(oldFastView); 348 } 349 } 350 } 351 | Popular Tags |