Adding new code for feature save on unfocus. This is from ticket #542. It also adds...
[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
22                 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         /*
39          * non Java-doc
40          * 
41          * @see org.eclipse.swt.dnd.DragSourceListener#dragStart
42          */
43         public void dragStart(DragSourceEvent event) {
44                 ISelection selection = fProvider.getSelection();
45                 LocalSelectionTransfer.getInstance().setSelection(selection);
46                 LocalSelectionTransfer.getInstance().setSelectionSetTime(event.time);
47                 event.doit = isDragable(selection);
48         }
49
50         /**
51          * Checks if the elements contained in the given selection can be dragged.
52          * <p>
53          * Subclasses may override.
54          * 
55          * @param selection
56          *            containing the elements to be dragged
57          */
58         protected boolean isDragable(ISelection selection) {
59                 return true;
60         }
61
62         /*
63          * non Java-doc
64          * 
65          * @see org.eclipse.swt.dnd.DragSourceListener#dragSetData
66          */
67         public void dragSetData(DragSourceEvent event) {
68                 // For consistency set the data to the selection even though
69                 // the selection is provided by the LocalSelectionTransfer
70                 // to the drop target adapter.
71                 event.data = LocalSelectionTransfer.getInstance().getSelection();
72         }
73
74         /*
75          * non Java-doc
76          * 
77          * @see org.eclipse.swt.dnd.DragSourceListener#dragFinished
78          */
79         public void dragFinished(DragSourceEvent event) {
80                 // We assume that the drop target listener has done all
81                 // the work.
82                 Assert.isTrue(event.detail == DND.DROP_NONE);
83                 LocalSelectionTransfer.getInstance().setSelection(null);
84                 LocalSelectionTransfer.getInstance().setSelectionSetTime(0);
85         }
86 }