replaced a lot of deprecated code; if someone runs into a commit conflict afterwards...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / dnd / BasicSelectionTransferDragAdapter.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.dnd;
12
13 import org.eclipse.jface.util.Assert;
14 import org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.jface.viewers.ISelectionProvider;
16 import org.eclipse.swt.dnd.DND;
17 import org.eclipse.swt.dnd.DragSourceAdapter;
18 import org.eclipse.swt.dnd.DragSourceEvent;
19 import org.eclipse.swt.dnd.Transfer;
20
21 public class BasicSelectionTransferDragAdapter extends DragSourceAdapter implements TransferDragSourceListener {
22         
23         private ISelectionProvider fProvider;
24         
25         public BasicSelectionTransferDragAdapter(ISelectionProvider provider) {
26                 Assert.isNotNull(provider);
27                 fProvider= provider;
28         }
29
30         /**
31          * @see TransferDragSourceListener#getTransfer
32          */
33         public Transfer getTransfer() {
34                 return LocalSelectionTransfer.getInstance();
35         }
36         
37         /* non Java-doc
38          * @see org.eclipse.swt.dnd.DragSourceListener#dragStart
39          */
40         public void dragStart(DragSourceEvent event) {
41                 ISelection selection= fProvider.getSelection();
42                 LocalSelectionTransfer.getInstance().setSelection(selection);
43                 LocalSelectionTransfer.getInstance().setSelectionSetTime(event.time);
44                 event.doit= isDragable(selection);
45         }
46         
47         /**
48          * Checks if the elements contained in the given selection can
49          * be dragged.
50          * <p>
51          * Subclasses may override.
52          * 
53          * @param selection containing the elements to be dragged
54          */
55         protected boolean isDragable(ISelection selection) {
56                 return true;
57         }
58
59
60         /* non Java-doc
61          * @see org.eclipse.swt.dnd.DragSourceListener#dragSetData
62          */             
63         public void dragSetData(DragSourceEvent event) {
64                 // For consistency set the data to the selection even though
65                 // the selection is provided by the LocalSelectionTransfer
66                 // to the drop target adapter.
67                 event.data= LocalSelectionTransfer.getInstance().getSelection();
68         }
69
70
71         /* non Java-doc
72          * @see org.eclipse.swt.dnd.DragSourceListener#dragFinished
73          */     
74         public void dragFinished(DragSourceEvent event) {
75                 // We assume that the drop target listener has done all
76                 // the work.
77                 Assert.isTrue(event.detail == DND.DROP_NONE);
78                 LocalSelectionTransfer.getInstance().setSelection(null);
79                 LocalSelectionTransfer.getInstance().setSelectionSetTime(0);
80         }       
81 }