Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-patch] Extending utility of CView as a drag source

Hi, all.

This patch extends the "C/C++ Projects" view to permit dragging 
non-resource selections.
This can be used to trigger refactoring operations as in the JDT, for 
example.

-Keith



Index: ChangeLog
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/ChangeLog,v
retrieving revision 1.141
diff -u -r1.141 ChangeLog
--- ChangeLog	18 Aug 2003 17:34:19 -0000	1.141
+++ ChangeLog	19 Aug 2003 14:26:02 -0000
@@ -1,3 +1,10 @@
+2003-08-19 Keith Campbell
+	Extended CView and CViewDragAdapter to use LocalSelectionTransfer.
+	Eventually this will permit dragging elements from the "C/C++ Projects" view
+	to trigger refactoring operations as in the JDT, for example.
+	* src/org/eclipse/cdt/internal/ui/cview/CView.java
+	* src/org/eclipse/cdt/internal/ui/cview/CViewDragAdapter.java
+
 2003-08-14 Sean Evoy
 	Added initial toolchain description for Solaris and Linux targets using Gnu tools.
 	* plugin.xml
Index: src/org/eclipse/cdt/internal/ui/cview/CView.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CView.java,v
retrieving revision 1.27
diff -u -r1.27 CView.java
--- src/org/eclipse/cdt/internal/ui/cview/CView.java	16 Jul 2003 14:49:38 -0000	1.27
+++ src/org/eclipse/cdt/internal/ui/cview/CView.java	19 Aug 2003 14:26:03 -0000
@@ -119,6 +119,7 @@
 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 @@
 	 */
 	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));
 	}
Index: src/org/eclipse/cdt/internal/ui/cview/CViewDragAdapter.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewDragAdapter.java,v
retrieving revision 1.4
diff -u -r1.4 CViewDragAdapter.java
--- src/org/eclipse/cdt/internal/ui/cview/CViewDragAdapter.java	8 Apr 2003 14:29:09 -0000	1.4
+++ src/org/eclipse/cdt/internal/ui/cview/CViewDragAdapter.java	19 Aug 2003 14:26:03 -0000
@@ -4,7 +4,7 @@
  * (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.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 @@
 	 */
 	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 @@
 	 * 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;
-
-		// 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 = 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];
+
+			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;
-
-		// 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);
-				}
+		List resources = new ArrayList();
+
+		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;
 	}
 }

Back to the top