| 1 |
/** |
| 2 |
* Copyright (c) 2002-2012 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v2.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v20.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM - Initial API and implementation |
| 10 |
*/ |
| 11 |
package org.eclipse.emf.edit.ui.dnd; |
| 12 |
|
| 13 |
|
| 14 |
import org.eclipse.jface.util.LocalSelectionTransfer; |
| 15 |
import org.eclipse.jface.viewers.ISelection; |
| 16 |
import org.eclipse.jface.viewers.Viewer; |
| 17 |
import org.eclipse.swt.dnd.DragSourceEvent; |
| 18 |
import org.eclipse.swt.dnd.DragSourceListener; |
| 19 |
|
| 20 |
|
| 21 |
/** |
| 22 |
* This is an implementation of {@link DragSourceListener}. |
| 23 |
* It allows the selection in effect at the start of the drag and drop interaction to be recorded, |
| 24 |
* which is especially important for a drag and drop interaction within a single view. |
| 25 |
* This is how one of these adapters is typically hooked up: |
| 26 |
* <pre> |
| 27 |
* viewer.addDragSupport |
| 28 |
* (DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK, |
| 29 |
* new Transfer [] { LocalTransfer.getInstance() }, |
| 30 |
* ViewerDragAdapter(viewer)); |
| 31 |
* </pre> |
| 32 |
* Doing so simply allows a drag operation to be initiated from the viewer |
| 33 |
* such that the viewer's selection is transferred to the drop target. |
| 34 |
* See {@link EditingDomainViewerDropAdapter} and {@link LocalTransfer} for more details. |
| 35 |
* As of EMF 2.9, {@link LocalSelectionTransfer} is also supported; |
| 36 |
* this is preferred because it's used by the platform's explorers. |
| 37 |
*/ |
| 38 |
public class ViewerDragAdapter implements DragSourceListener |
| 39 |
{ |
| 40 |
/** |
| 41 |
* This keeps track of the viewer to which we are listening. |
| 42 |
*/ |
| 43 |
protected Viewer viewer; |
| 44 |
|
| 45 |
/** |
| 46 |
* This keeps track of the selection that is in effect at the start of the drag operation |
| 47 |
*/ |
| 48 |
protected ISelection selection; |
| 49 |
|
| 50 |
/** |
| 51 |
* This creates an instance for the given viewer. |
| 52 |
*/ |
| 53 |
public ViewerDragAdapter(Viewer viewer) |
| 54 |
{ |
| 55 |
super(); |
| 56 |
|
| 57 |
// Remember the viewer and listen to SWT.DragDetect to alert the start of the drag operation. |
| 58 |
// |
| 59 |
this.viewer = viewer; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* This is called when dragging is initiated; it records the {@link #selection} of {@link #viewer}. |
| 64 |
*/ |
| 65 |
public void dragStart(DragSourceEvent event) |
| 66 |
{ |
| 67 |
selection = viewer.getSelection(); |
| 68 |
LocalSelectionTransfer.getTransfer().setSelection(selection); |
| 69 |
LocalSelectionTransfer.getTransfer().setSelectionSetTime(System.currentTimeMillis()); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* This is called when dragging is completed; it forgets the {@link #selection}. |
| 74 |
*/ |
| 75 |
public void dragFinished(DragSourceEvent event) |
| 76 |
{ |
| 77 |
selection = null; |
| 78 |
LocalTransfer.getInstance().javaToNative(null, null); |
| 79 |
LocalSelectionTransfer.getTransfer().setSelection(null); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* This is called to transfer the data. |
| 84 |
*/ |
| 85 |
public void dragSetData(DragSourceEvent event) |
| 86 |
{ |
| 87 |
if (LocalTransfer.getInstance().isSupportedType(event.dataType)) |
| 88 |
{ |
| 89 |
event.data = selection; |
| 90 |
} |
| 91 |
else if (LocalSelectionTransfer.getTransfer().isSupportedType(event.dataType)) |
| 92 |
{ |
| 93 |
event.data = LocalSelectionTransfer.getTransfer().getSelection(); |
| 94 |
} |
| 95 |
} |
| 96 |
} |