From 279655a2ffeb1bf0751423b6e68beef3451743cb Mon Sep 17 00:00:00 2001 From: Alain Magloire Date: Wed, 20 Aug 2003 17:34:33 +0000 Subject: [PATCH] support d&d non-resources in the CView. --- .../eclipse/cdt/internal/ui/cview/CView.java | 9 +- .../internal/ui/cview/CViewDragAdapter.java | 121 +++++++----------- 2 files changed, 55 insertions(+), 75 deletions(-) diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CView.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CView.java index fa569fe9c85..eec8ee996b5 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CView.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CView.java @@ -119,6 +119,7 @@ import org.eclipse.ui.views.framelist.ForwardAction; import org.eclipse.ui.views.framelist.FrameList; import org.eclipse.ui.views.framelist.GoIntoAction; import org.eclipse.ui.views.framelist.UpAction; +import org.eclipse.ui.views.navigator.LocalSelectionTransfer; @@ -358,8 +359,12 @@ public class CView extends ViewPart implements IMenuListener, ISetSelectionTarge */ void initDragAndDrop() { int ops = DND.DROP_COPY | DND.DROP_MOVE; - Transfer[] transfers = new Transfer[] {ResourceTransfer.getInstance(), - FileTransfer.getInstance(), PluginTransfer.getInstance()}; + Transfer[] transfers = new Transfer[] { + ResourceTransfer.getInstance(), + FileTransfer.getInstance(), + LocalSelectionTransfer.getInstance(), + PluginTransfer.getInstance() }; + viewer.addDragSupport(ops, transfers, new CViewDragAdapter((ISelectionProvider)viewer)); viewer.addDropSupport(ops, transfers, new CViewDropAdapter(viewer)); } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewDragAdapter.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewDragAdapter.java index 999a25b5903..ccced8f2ca6 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewDragAdapter.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewDragAdapter.java @@ -4,7 +4,7 @@ package org.eclipse.cdt.internal.ui.cview; * (c) Copyright IBM Corp. 2000, 2001. * All Rights Reserved. */ - + import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -22,13 +22,14 @@ import org.eclipse.swt.dnd.DragSourceEvent; import org.eclipse.swt.dnd.FileTransfer; import org.eclipse.swt.widgets.Control; import org.eclipse.ui.part.ResourceTransfer; +import org.eclipse.ui.views.navigator.LocalSelectionTransfer; /** * Implements drag behaviour when items are dragged out of the * resource navigator. */ class CViewDragAdapter extends DragSourceAdapter { - ISelectionProvider selectionProvider; + private final ISelectionProvider selectionProvider; /** * Invoked when an action occurs. @@ -38,12 +39,11 @@ class CViewDragAdapter extends DragSourceAdapter { */ public void dragFinished(DragSourceEvent event) { if (event.doit && event.detail == DND.DROP_MOVE) { - //delete the old elements - final int typeMask = IResource.FOLDER | IResource.FILE; - IResource[] resources = getSelectedResources(typeMask); - if (resources == null) - return; - for (int i = 0; i < resources.length; i++) { + // delete the old elements + + IResource[] resources = getSelectedResources(); + + for (int i = 0; i < resources.length; ++i) { try { resources[i].delete(true, null); } catch (CoreException e) { @@ -58,97 +58,72 @@ class CViewDragAdapter extends DragSourceAdapter { * operation. */ public void dragSetData(DragSourceEvent event) { - final int typeMask = IResource.FILE | IResource.FOLDER; - IResource[] resources = getSelectedResources(typeMask); - if (resources == null || resources.length == 0) - return; - - //use resource transfer if possible if (ResourceTransfer.getInstance().isSupportedType(event.dataType)) { - event.data = resources; - return; - } - - //resort to a file transfer - if (!FileTransfer.getInstance().isSupportedType(event.dataType)) - return; + event.data = getSelectedResources(); + } else if (FileTransfer.getInstance().isSupportedType(event.dataType)) { + // get the path of each file and set as the drag data + IResource[] resources = getSelectedResources(); + int length = resources.length; + String[] fileNames = new String[length]; - // Get the path of each file and set as the drag data - final int len = resources.length; - String[] fileNames = new String[len]; - for (int i = 0, length = len; i < length; i++) { - fileNames[i] = resources[i].getLocation().toOSString(); + for (int i = 0; i < length; ++i) + fileNames[i] = resources[i].getLocation().toOSString(); + + event.data = fileNames; + } else if (LocalSelectionTransfer.getInstance().isSupportedType(event.dataType)) { + event.data = LocalSelectionTransfer.getInstance().getSelection(); } - event.data = fileNames; } /** * All selection must be files or folders. */ public void dragStart(DragSourceEvent event) { - // Workaround for 1GEUS9V - DragSource dragSource = (DragSource)event.widget; + DragSource dragSource = (DragSource) event.widget; Control control = dragSource.getControl(); - if (control != control.getDisplay().getFocusControl()){ + + if (control != control.getDisplay().getFocusControl()) event.doit = false; - return; - } - - IStructuredSelection selection = (IStructuredSelection)selectionProvider.getSelection(); - for (Iterator i = selection.iterator(); i.hasNext();) { - Object next = i.next(); - IResource res = null; - if (next instanceof IResource) { - res = (IResource)next; - } else if (next instanceof IAdaptable) { - res = (IResource)((IAdaptable)next).getAdapter(IResource.class); - } - if (res == null) { - event.doit = false; - return; - } - } - event.doit = true; + else + LocalSelectionTransfer.getInstance().setSelection(selectionProvider.getSelection()); } - protected IResource[] getSelectedResources(int resourceTypes) { - List resources = new ArrayList(); - IResource[] result = new IResource[0]; + private static final int typeMask = IResource.FOLDER | IResource.FILE; + private IResource[] getSelectedResources() { ISelection selection = selectionProvider.getSelection(); - if (!(selection instanceof IStructuredSelection) || selection.isEmpty()) { - return null; - } - IStructuredSelection structuredSelection = (IStructuredSelection)selection; - if (structuredSelection == null) - return null; + List resources = new ArrayList(); - // loop through list and look for matching items - Iterator enum = structuredSelection.iterator(); - while (enum.hasNext()) { - Object obj = enum.next(); - IResource res = null; - if (obj instanceof IResource) { - res = (IResource)obj; - } else if (obj instanceof IAdaptable) { - res = (IResource)((IAdaptable)obj).getAdapter(IResource.class); - } - if (res != null) { - if ((res.getType() & resourceTypes) == res.getType()) { - resources.add(res); - } + if (selection instanceof IStructuredSelection) { + IStructuredSelection structuredSelection = (IStructuredSelection) selection; + + // loop through list and look for matching items + for (Iterator enum = structuredSelection.iterator(); enum.hasNext();) { + Object object = enum.next(); + IResource resource = null; + + if (object instanceof IResource) + resource = (IResource) object; + else if (object instanceof IAdaptable) + resource = (IResource) ((IAdaptable) object).getAdapter(IResource.class); + + if (resource != null && (resource.getType() & typeMask) != 0) + resources.add(resource); } } - result = new IResource[resources.size()]; + + IResource[] result = new IResource[resources.size()]; resources.toArray(result); + return result; } /** - * CViewDragAction constructor comment. + * CViewDragAction constructor. */ public CViewDragAdapter(ISelectionProvider provider) { + super(); selectionProvider = provider; } }