fixed bug #1037094 (foreach)
[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.swt.dnd.DND;
14 import org.eclipse.swt.dnd.DragSourceAdapter;
15 import org.eclipse.swt.dnd.DragSourceEvent;
16 import org.eclipse.swt.dnd.Transfer;
17
18 import org.eclipse.jface.util.Assert;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.jface.viewers.ISelectionProvider;
21
22 public class BasicSelectionTransferDragAdapter extends DragSourceAdapter implements TransferDragSourceListener {
23         
24         private ISelectionProvider fProvider;
25         
26         public BasicSelectionTransferDragAdapter(ISelectionProvider provider) {
27                 Assert.isNotNull(provider);
28                 fProvider= provider;
29         }
30
31         /**
32          * @see TransferDragSourceListener#getTransfer
33          */
34         public Transfer getTransfer() {
35                 return LocalSelectionTransfer.getInstance();
36         }
37         
38         /* non Java-doc
39          * @see org.eclipse.swt.dnd.DragSourceListener#dragStart
40          */
41         public void dragStart(DragSourceEvent event) {
42                 ISelection selection= fProvider.getSelection();
43                 LocalSelectionTransfer.getInstance().setSelection(selection);
44                 LocalSelectionTransfer.getInstance().setSelectionSetTime(event.time);
45                 event.doit= isDragable(selection);
46         }
47         
48         /**
49          * Checks if the elements contained in the given selection can
50          * be dragged.
51          * <p>
52          * Subclasses may override.
53          * 
54          * @param selection containing the elements to be dragged
55          */
56         protected boolean isDragable(ISelection selection) {
57                 return true;
58         }
59
60
61         /* non Java-doc
62          * @see org.eclipse.swt.dnd.DragSourceListener#dragSetData
63          */             
64         public void dragSetData(DragSourceEvent event) {
65                 // For consistency set the data to the selection even though
66                 // the selection is provided by the LocalSelectionTransfer
67                 // to the drop target adapter.
68                 event.data= LocalSelectionTransfer.getInstance().getSelection();
69         }
70
71
72         /* non Java-doc
73          * @see org.eclipse.swt.dnd.DragSourceListener#dragFinished
74          */     
75         public void dragFinished(DragSourceEvent event) {
76                 // We assume that the drop target listener has done all
77                 // the work.
78                 Assert.isTrue(event.detail == DND.DROP_NONE);
79                 LocalSelectionTransfer.getInstance().setSelection(null);
80                 LocalSelectionTransfer.getInstance().setSelectionSetTime(0);
81         }       
82 }