1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

support d&d non-resources in the CView.

This commit is contained in:
Alain Magloire 2003-08-20 17:34:33 +00:00
parent cee6254f9e
commit 279655a2ff
2 changed files with 55 additions and 75 deletions

View file

@ -119,6 +119,7 @@ import org.eclipse.ui.views.framelist.ForwardAction;
import org.eclipse.ui.views.framelist.FrameList; import org.eclipse.ui.views.framelist.FrameList;
import org.eclipse.ui.views.framelist.GoIntoAction; import org.eclipse.ui.views.framelist.GoIntoAction;
import org.eclipse.ui.views.framelist.UpAction; 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() { void initDragAndDrop() {
int ops = DND.DROP_COPY | DND.DROP_MOVE; int ops = DND.DROP_COPY | DND.DROP_MOVE;
Transfer[] transfers = new Transfer[] {ResourceTransfer.getInstance(), Transfer[] transfers = new Transfer[] {
FileTransfer.getInstance(), PluginTransfer.getInstance()}; ResourceTransfer.getInstance(),
FileTransfer.getInstance(),
LocalSelectionTransfer.getInstance(),
PluginTransfer.getInstance() };
viewer.addDragSupport(ops, transfers, new CViewDragAdapter((ISelectionProvider)viewer)); viewer.addDragSupport(ops, transfers, new CViewDragAdapter((ISelectionProvider)viewer));
viewer.addDropSupport(ops, transfers, new CViewDropAdapter(viewer)); viewer.addDropSupport(ops, transfers, new CViewDropAdapter(viewer));
} }

View file

@ -22,13 +22,14 @@ import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.FileTransfer; import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.part.ResourceTransfer; import org.eclipse.ui.part.ResourceTransfer;
import org.eclipse.ui.views.navigator.LocalSelectionTransfer;
/** /**
* Implements drag behaviour when items are dragged out of the * Implements drag behaviour when items are dragged out of the
* resource navigator. * resource navigator.
*/ */
class CViewDragAdapter extends DragSourceAdapter { class CViewDragAdapter extends DragSourceAdapter {
ISelectionProvider selectionProvider; private final ISelectionProvider selectionProvider;
/** /**
* Invoked when an action occurs. * Invoked when an action occurs.
@ -38,12 +39,11 @@ class CViewDragAdapter extends DragSourceAdapter {
*/ */
public void dragFinished(DragSourceEvent event) { public void dragFinished(DragSourceEvent event) {
if (event.doit && event.detail == DND.DROP_MOVE) { if (event.doit && event.detail == DND.DROP_MOVE) {
//delete the old elements // delete the old elements
final int typeMask = IResource.FOLDER | IResource.FILE;
IResource[] resources = getSelectedResources(typeMask); IResource[] resources = getSelectedResources();
if (resources == null)
return; for (int i = 0; i < resources.length; ++i) {
for (int i = 0; i < resources.length; i++) {
try { try {
resources[i].delete(true, null); resources[i].delete(true, null);
} catch (CoreException e) { } catch (CoreException e) {
@ -58,97 +58,72 @@ class CViewDragAdapter extends DragSourceAdapter {
* operation. * operation.
*/ */
public void dragSetData(DragSourceEvent event) { 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)) { if (ResourceTransfer.getInstance().isSupportedType(event.dataType)) {
event.data = resources; event.data = getSelectedResources();
return; } 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];
//resort to a file transfer for (int i = 0; i < length; ++i)
if (!FileTransfer.getInstance().isSupportedType(event.dataType))
return;
// 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(); fileNames[i] = resources[i].getLocation().toOSString();
}
event.data = fileNames; event.data = fileNames;
} else if (LocalSelectionTransfer.getInstance().isSupportedType(event.dataType)) {
event.data = LocalSelectionTransfer.getInstance().getSelection();
}
} }
/** /**
* All selection must be files or folders. * All selection must be files or folders.
*/ */
public void dragStart(DragSourceEvent event) { public void dragStart(DragSourceEvent event) {
// Workaround for 1GEUS9V // Workaround for 1GEUS9V
DragSource dragSource = (DragSource)event.widget; DragSource dragSource = (DragSource) event.widget;
Control control = dragSource.getControl(); Control control = dragSource.getControl();
if (control != control.getDisplay().getFocusControl()){
if (control != control.getDisplay().getFocusControl())
event.doit = false; event.doit = false;
return; else
LocalSelectionTransfer.getInstance().setSelection(selectionProvider.getSelection());
} }
IStructuredSelection selection = (IStructuredSelection)selectionProvider.getSelection(); private static final int typeMask = IResource.FOLDER | IResource.FILE;
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;
}
protected IResource[] getSelectedResources(int resourceTypes) {
List resources = new ArrayList();
IResource[] result = new IResource[0];
private IResource[] getSelectedResources() {
ISelection selection = selectionProvider.getSelection(); ISelection selection = selectionProvider.getSelection();
if (!(selection instanceof IStructuredSelection) || selection.isEmpty()) { List resources = new ArrayList();
return null;
} if (selection instanceof IStructuredSelection) {
IStructuredSelection structuredSelection = (IStructuredSelection)selection; IStructuredSelection structuredSelection = (IStructuredSelection) selection;
if (structuredSelection == null)
return null;
// loop through list and look for matching items // loop through list and look for matching items
Iterator enum = structuredSelection.iterator(); for (Iterator enum = structuredSelection.iterator(); enum.hasNext();) {
while (enum.hasNext()) { Object object = enum.next();
Object obj = enum.next(); IResource resource = null;
IResource res = null;
if (obj instanceof IResource) { if (object instanceof IResource)
res = (IResource)obj; resource = (IResource) object;
} else if (obj instanceof IAdaptable) { else if (object instanceof IAdaptable)
res = (IResource)((IAdaptable)obj).getAdapter(IResource.class); resource = (IResource) ((IAdaptable) object).getAdapter(IResource.class);
}
if (res != null) { if (resource != null && (resource.getType() & typeMask) != 0)
if ((res.getType() & resourceTypes) == res.getType()) { resources.add(resource);
resources.add(res);
} }
} }
}
result = new IResource[resources.size()]; IResource[] result = new IResource[resources.size()];
resources.toArray(result); resources.toArray(result);
return result; return result;
} }
/** /**
* CViewDragAction constructor comment. * CViewDragAction constructor.
*/ */
public CViewDragAdapter(ISelectionProvider provider) { public CViewDragAdapter(ISelectionProvider provider) {
super();
selectionProvider = provider; selectionProvider = provider;
} }
} }