Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-patch] Updated drag patch

Here is an updated patch to enable dragging non-resource selections from 
the C/C++ Projects view.

The code has been reorganized to use the delegation pattern found in the 
JDT and a new LocalSelectionTransfer specific to the CDT has been 
introduced to avoid potential incompatibilities of using 
org.eclipse.ui.views.navigator.LocalSelectionTransfer.

The attachment cdt.ui-patch.txt is a patch that should be applied after 
adding the new files in cdt.ui-new-src.zip (paths are relative to cdt.ui 
project root).

-Keith




Attachment: cdt.ui-new-src.zip
Description: Zip archive

Index: src/org/eclipse/cdt/internal/ui/CUIMessages.properties
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/CUIMessages.properties,v
retrieving revision 1.1
diff -u -r1.1 CUIMessages.properties
--- src/org/eclipse/cdt/internal/ui/CUIMessages.properties	11 Sep 2003 14:58:30 -0000	1.1
+++ src/org/eclipse/cdt/internal/ui/CUIMessages.properties	12 Sep 2003 19:59:04 -0000
@@ -9,4 +9,6 @@
 # Rational Software - Initial API and implementation
 ###############################################################################
 
-ExceptionDialog.seeErrorLogMessage= See error log for more details.
\ No newline at end of file
+Drag.move.problem.title=Drag and Drop Problem
+Drag.move.problem.message={0} is read only. Do you still wish to delete it?
+ExceptionDialog.seeErrorLogMessage=See error log for more details.
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.29
diff -u -r1.29 CView.java
--- src/org/eclipse/cdt/internal/ui/cview/CView.java	11 Sep 2003 17:13:00 -0000	1.29
+++ src/org/eclipse/cdt/internal/ui/cview/CView.java	12 Sep 2003 19:59:05 -0000
@@ -25,6 +25,10 @@
 import org.eclipse.cdt.internal.core.model.TranslationUnit;
 import org.eclipse.cdt.internal.ui.IContextMenuConstants;
 import org.eclipse.cdt.internal.ui.StandardCElementLabelProvider;
+import org.eclipse.cdt.internal.ui.drag.DelegatingDragAdapter;
+import org.eclipse.cdt.internal.ui.drag.FileTransferDragAdapter;
+import org.eclipse.cdt.internal.ui.drag.ResourceTransferDragAdapter;
+import org.eclipse.cdt.internal.ui.drag.TransferDragSourceListener;
 import org.eclipse.cdt.internal.ui.editor.FileSearchAction;
 import org.eclipse.cdt.internal.ui.editor.FileSearchActionInWorkingSet;
 import org.eclipse.cdt.internal.ui.editor.OpenIncludeAction;
@@ -125,10 +129,6 @@
 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;
-
-
-
 
 public class CView extends ViewPart implements IMenuListener, ISetSelectionTarget,
 	IPropertyChangeListener {
@@ -370,14 +370,28 @@
 	 */
 	void initDragAndDrop() {
 		int ops = DND.DROP_COPY | DND.DROP_MOVE;
-		Transfer[] transfers = new Transfer[] {
+
+		Transfer[] dragTransfers = new Transfer[] {
+			ResourceTransfer.getInstance(),
+			FileTransfer.getInstance(),
+			org.eclipse.cdt.ui.LocalSelectionTransfer.getInstance(),
+			PluginTransfer.getInstance() };
+
+		TransferDragSourceListener[] dragListeners =
+			new TransferDragSourceListener[] {
+				new ResourceTransferDragAdapter(viewer),
+				new org.eclipse.cdt.internal.ui.drag.LocalSelectionTransferDragAdapter(viewer),
+				new FileTransferDragAdapter(viewer)};
+ 
+		viewer.addDragSupport(ops, dragTransfers, new DelegatingDragAdapter(viewer, dragListeners));
+
+		Transfer[] dropTransfers = new Transfer[] {
 			ResourceTransfer.getInstance(),
 			FileTransfer.getInstance(),
-			LocalSelectionTransfer.getInstance(),
+			org.eclipse.ui.views.navigator.LocalSelectionTransfer.getInstance(),
 			PluginTransfer.getInstance() };
 
-		viewer.addDragSupport(ops, transfers, new CViewDragAdapter((ISelectionProvider)viewer));
-		viewer.addDropSupport(ops, transfers, new CViewDropAdapter(viewer));
+		viewer.addDropSupport(ops, dropTransfers, new CViewDropAdapter(viewer));
 	}
 
 	/** 
Index: src/org/eclipse/cdt/internal/ui/cview/CViewDragAdapter.java
===================================================================
RCS file: src/org/eclipse/cdt/internal/ui/cview/CViewDragAdapter.java
diff -N src/org/eclipse/cdt/internal/ui/cview/CViewDragAdapter.java
--- src/org/eclipse/cdt/internal/ui/cview/CViewDragAdapter.java	1 Sep 2003 21:49:44 -0000	1.6
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,219 +0,0 @@
-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;
-
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IFolder;
-import org.eclipse.core.resources.IResource;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IAdaptable;
-import org.eclipse.core.runtime.IPath;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.jface.viewers.ISelectionProvider;
-import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.swt.dnd.DND;
-import org.eclipse.swt.dnd.DragSource;
-import org.eclipse.swt.dnd.DragSourceAdapter;
-import org.eclipse.swt.dnd.DragSourceEvent;
-import org.eclipse.swt.dnd.FileTransfer;
-import org.eclipse.swt.dnd.TransferData;
-import org.eclipse.swt.widgets.Control;
-import org.eclipse.swt.widgets.Shell;
-import org.eclipse.ui.actions.ReadOnlyStateChecker;
-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 {
-	private final ISelectionProvider selectionProvider;
-	private static final int typeMask = IResource.FOLDER | IResource.FILE;
-	private TransferData lastDataType; 	
-	private static final String CHECK_MOVE_TITLE = "Drag and Drop Problem"; //$NON-NLS-1$
-	private static final String CHECK_DELETE_MESSAGE = "{0} is read only. Do you still wish to delete it?"; //$NON-NLS-1$
-
-
-	/**
-	 * Invoked when an action occurs. 
-	 * Argument context is the Window which contains the UI from which this action was fired.
-	 * This default implementation prints the name of this class and its label.
-	 * @see DragSourceListener#dragFinished(org.eclipse.swt.dnd.DragSourceEvent)
-	 */
-	public void dragFinished(DragSourceEvent event) {
-		LocalSelectionTransfer.getInstance().setSelection(null);
-
-		if (event.doit == false)
-			return;
-
-		if (event.detail == DND.DROP_MOVE) {
-			//never delete resources when dragging outside Eclipse. 
-			//workaround for bug 30543.
-			if (lastDataType != null && FileTransfer.getInstance().isSupportedType(lastDataType))
-				return;
-			
-			IResource[] resources = getSelectedResources();	
-			DragSource dragSource = (DragSource) event.widget;
-			Control control = dragSource.getControl();
-			Shell shell = control.getShell();
-			ReadOnlyStateChecker checker;
-			
-			if (resources == null || resources.length == 0)
-				return;
-			
-			checker = new ReadOnlyStateChecker(shell, CHECK_MOVE_TITLE, CHECK_DELETE_MESSAGE);
-			resources = checker.checkReadOnlyResources(resources);		
-			//delete the old elements
-			for (int i = 0; i < resources.length; i++) {
-				try {
-					resources[i].delete(IResource.KEEP_HISTORY | IResource.FORCE, null);
-				} catch (CoreException e) {
-					e.printStackTrace();
-				}
-			}
-		} else if (event.detail == DND.DROP_TARGET_MOVE) {
-			IResource[] resources = getSelectedResources();
-
-			// file moved for us by OS, no need to delete the resources, just
-			// update the view
-			if (resources == null)
-				return;
-			for (int i = 0; i < resources.length; i++) {
-				try {
-					resources[i].refreshLocal(IResource.DEPTH_INFINITE, null);
-				} catch (CoreException e) {
-					e.printStackTrace();
-				}
-			}
-		}
-	}
-
-	/*
-	 * @see DragSourceListener#dragSetData(org.eclipse.swt.dnd.DragSourceEvent)
-	 */
-	public void dragSetData(DragSourceEvent event) {
-		IResource[] resources = getSelectedResources();
-		
-		if (resources == null || resources.length == 0)
-			return;
-
-		lastDataType = event.dataType;
-		//use local selection transfer if possible
-		if (LocalSelectionTransfer.getInstance().isSupportedType(event.dataType)) {
-			event.data = LocalSelectionTransfer.getInstance().getSelection();
-			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 length = resources.length;
-		int actualLength = 0; 
-		String[] fileNames = new String[length];
-		for (int i = 0; i < length; i++) {
-			IPath location = resources[i].getLocation();
-			// location may be null. See bug 29491.
-			if (location != null) 
-				fileNames[actualLength++] = location.toOSString();
-		}
-		if (actualLength == 0)
-			return;
-		// was one or more of the locations null?
-		if (actualLength < length) {
-			String[] tempFileNames = fileNames;
-			fileNames = new String[actualLength];
-			for (int i = 0; i < actualLength; i++)
-				fileNames[i] = tempFileNames[i];
-		}
-		event.data = fileNames;
-	}
-
-	/*
-	 * @see DragSourceListener#dragStart(org.eclipse.swt.dnd.DragSourceEvent)
-	 */
-	public void dragStart(DragSourceEvent event) {
-		lastDataType = null;
-		// Workaround for 1GEUS9V
-		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();
-			if (next instanceof IAdaptable) {
-				next = ((IAdaptable) next).getAdapter(IResource.class);
-			}
-			if (!(next instanceof IFile || next instanceof IFolder)) {
-				event.doit = false;
-				return;
-			}
-		}
-		if (selection.isEmpty()) {
-			event.doit = false;
-			return;
-		}
-		LocalSelectionTransfer.getInstance().setSelection(selection);
-		event.doit = true;
-	}
-	
-
-
-	private IResource[] getSelectedResources() {
-		ISelection selection = selectionProvider.getSelection();
-		List resources = new ArrayList();
-
-		// Sanity checks
-		if (selection == null || !(selection instanceof IStructuredSelection) || selection.isEmpty()) {
-			return null;
-		}
-
-		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);
-			}
-		}
-
-		IResource[] result = new IResource[resources.size()];
-		resources.toArray(result);
-
-		return result;
-	}
-
-	/**
-	 * CViewDragAction constructor.
-	 */
-	public CViewDragAdapter(ISelectionProvider provider) {
-		super();
-		selectionProvider = provider;
-	}
-}
Index: src/org/eclipse/cdt/ui/LocalSelectionTransfer.java
===================================================================
RCS file: src/org/eclipse/cdt/ui/LocalSelectionTransfer.java
diff -N src/org/eclipse/cdt/ui/LocalSelectionTransfer.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/cdt/ui/LocalSelectionTransfer.java	12 Sep 2003 19:59:07 -0000
@@ -0,0 +1,75 @@
+package org.eclipse.cdt.ui;
+
+import java.util.Arrays;
+
+import org.eclipse.jface.util.Assert;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.swt.dnd.ByteArrayTransfer;
+import org.eclipse.swt.dnd.TransferData;
+
+/**
+ * @author kcampbell
+ */
+public class LocalSelectionTransfer extends ByteArrayTransfer {
+	private static final LocalSelectionTransfer INSTANCE = new LocalSelectionTransfer();
+
+	private final String typeName;
+	private final int typeId;
+	private ISelection selection;
+
+	private LocalSelectionTransfer() {
+		super();
+		// Try to ensure that different Eclipse applications use different "types" of <code>LocalSelectionTransfer</code>
+		typeName = "cdt-local-selection-transfer-format" + System.currentTimeMillis(); //$NON-NLS-1$;
+		typeId = registerType(typeName);
+		selection = null;
+	}
+
+	/**
+	 * Returns the singleton.
+	 */
+	public static LocalSelectionTransfer getInstance() {
+		return INSTANCE;
+	}
+
+	/**
+	 * Sets the transfer data for local use.
+	 */
+	public void setSelection(ISelection selection) {
+		this.selection = selection;
+	}
+
+	/**
+	 * Returns the local transfer data.
+	 */
+	public ISelection getSelection() {
+		return selection;
+	}
+
+	public void javaToNative(Object object, TransferData transferData) {
+		// No encoding needed since this is a hardcoded string read and written in the same process.
+		// See nativeToJava below
+		super.javaToNative(typeName.getBytes(), transferData);
+	}
+
+	public Object nativeToJava(TransferData transferData) {
+		Object result = super.nativeToJava(transferData);
+
+		// No decoding needed: see javaToNative above.
+		Assert.isTrue(
+			result instanceof byte[] && Arrays.equals(typeName.getBytes(), (byte[]) result));
+
+		return selection;
+	}
+
+	/**
+	 * The type id used to identify this transfer.
+	 */
+	protected int[] getTypeIds() {
+		return new int[] { typeId };
+	}
+
+	protected String[] getTypeNames() {
+		return new String[] { typeName };
+	}
+}

Back to the top