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:
parent
cee6254f9e
commit
279655a2ff
2 changed files with 55 additions and 75 deletions
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
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];
|
||||
|
||||
//resort to a file transfer
|
||||
if (!FileTransfer.getInstance().isSupportedType(event.dataType))
|
||||
return;
|
||||
for (int i = 0; i < length; ++i)
|
||||
fileNames[i] = resources[i].getLocation().toOSString();
|
||||
|
||||
// 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();
|
||||
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()){
|
||||
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;
|
||||
if (control != control.getDisplay().getFocusControl())
|
||||
event.doit = false;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue