KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > dnd > BasicSelectionTransferDragAdapter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.dnd;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.swt.dnd.DND;
16 import org.eclipse.swt.dnd.DragSourceAdapter;
17 import org.eclipse.swt.dnd.DragSourceEvent;
18 import org.eclipse.swt.dnd.Transfer;
19
20 import org.eclipse.jface.util.TransferDragSourceListener;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.ISelectionProvider;
23
24 import org.eclipse.ui.views.navigator.LocalSelectionTransfer;
25
26 public class BasicSelectionTransferDragAdapter extends DragSourceAdapter implements TransferDragSourceListener {
27     
28     private ISelectionProvider fProvider;
29     
30     public BasicSelectionTransferDragAdapter(ISelectionProvider provider) {
31         Assert.isNotNull(provider);
32         fProvider= provider;
33     }
34
35     /**
36      * @see TransferDragSourceListener#getTransfer
37      */

38     public Transfer getTransfer() {
39         return LocalSelectionTransfer.getInstance();
40     }
41     
42     /* non Java-doc
43      * @see org.eclipse.swt.dnd.DragSourceListener#dragStart
44      */

45     public void dragStart(DragSourceEvent event) {
46         ISelection selection= fProvider.getSelection();
47         LocalSelectionTransfer.getInstance().setSelection(selection);
48         LocalSelectionTransfer.getInstance().setSelectionSetTime(event.time & 0xFFFFFFFFL);
49         event.doit= isDragable(selection);
50     }
51     
52     /**
53      * Checks if the elements contained in the given selection can
54      * be dragged.
55      * <p>
56      * Subclasses may override.
57      *
58      * @param selection containing the elements to be dragged
59      */

60     protected boolean isDragable(ISelection selection) {
61         return true;
62     }
63
64
65     /* non Java-doc
66      * @see org.eclipse.swt.dnd.DragSourceListener#dragSetData
67      */

68     public void dragSetData(DragSourceEvent event) {
69         // For consistency set the data to the selection even though
70
// the selection is provided by the LocalSelectionTransfer
71
// to the drop target adapter.
72
event.data= LocalSelectionTransfer.getInstance().getSelection();
73     }
74
75
76     /* non Java-doc
77      * @see org.eclipse.swt.dnd.DragSourceListener#dragFinished
78      */

79     public void dragFinished(DragSourceEvent event) {
80         // Make sure we don't have to do any remaining work
81
Assert.isTrue(event.detail != DND.DROP_MOVE);
82         LocalSelectionTransfer.getInstance().setSelection(null);
83         LocalSelectionTransfer.getInstance().setSelectionSetTime(0);
84     }
85 }
86
Popular Tags