KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > edit > ui > dnd > ViewerDragAdapter


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: ViewerDragAdapter.java,v 1.2 2005/06/08 06:20:52 nickb Exp $
16  */

17 package org.eclipse.emf.edit.ui.dnd;
18
19
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.Viewer;
22 import org.eclipse.swt.dnd.DragSourceEvent;
23 import org.eclipse.swt.dnd.DragSourceListener;
24
25
26 /**
27  * This is an implemention of {@link DragSourceListener}.
28  * It allows the selection in effect at the start of the drag and drop interaction to be recorded,
29  * which is especially important for a drag and drop interaction within a single view.
30  * This is how one of these adapters is typically hooked up:
31  * <pre>
32  * viewer.addDragSupport
33  * (DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK,
34  * new Transfer [] { LocalTransfer.getInstance() },
35  * ViewerDragAdapter(viewer));
36  * </pre>
37  * Doing so simply allows a drag operation to be initiated from the viewer
38  * such that the viewer's selection is transferred to the drop target.
39  * See {@link EditingDomainViewerDropAdapter} and {@link LocalTransfer} for more details.
40  */

41 public class ViewerDragAdapter implements DragSourceListener
42 {
43   /**
44    * This keeps track of the viewer to which we are listening.
45    */

46   protected Viewer viewer;
47
48   /**
49    * This keeps track of the selection that is in effect at the start of the drag operation
50    */

51   protected ISelection selection;
52
53   /**
54    * This creates an instance for the given viewer.
55    */

56   public ViewerDragAdapter(Viewer viewer)
57   {
58     super();
59
60     // Remember the viewer and listen to SWT.DragDetect to alert the start of the drag operation.
61
//
62
this.viewer = viewer;
63   }
64
65   /**
66    * This is called when dragging is initiated; it records the {@link #selection} of {@link #viewer}.
67    */

68   public void dragStart(DragSourceEvent event)
69   {
70     selection = viewer.getSelection();
71   }
72
73   /**
74    * This is called when dragging is completed; it forgets the {@link #selection}.
75    */

76   public void dragFinished(DragSourceEvent event)
77   {
78     selection = null;
79   }
80   
81   /**
82    * This is called to transfer the data.
83    */

84   public void dragSetData(DragSourceEvent event)
85   {
86     if (LocalTransfer.getInstance().isSupportedType(event.dataType))
87     {
88       event.data = selection;
89     }
90   }
91 }
92
Popular Tags