KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > ui > text > ResourceTransferDragAdapter


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.search.internal.ui.text;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19
20 import org.eclipse.core.resources.IResource;
21
22 import org.eclipse.swt.dnd.DragSourceAdapter;
23 import org.eclipse.swt.dnd.DragSourceEvent;
24 import org.eclipse.swt.dnd.Transfer;
25
26 import org.eclipse.jface.util.TransferDragSourceListener;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.ISelectionProvider;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30
31 import org.eclipse.ui.part.ResourceTransfer;
32
33 /**
34  * A drag adapter that transfers the current selection as </code>
35  * IResource</code>. Only those elements in the selection are part
36  * of the transfer which can be converted into an <code>IResource
37  * </code>.
38  */

39 public class ResourceTransferDragAdapter extends DragSourceAdapter implements TransferDragSourceListener {
40
41     private ISelectionProvider fProvider;
42
43     /**
44      * Creates a new ResourceTransferDragAdapter for the given selection
45      * provider.
46      *
47      * @param provider the selection provider to access the viewer's selection
48      */

49     public ResourceTransferDragAdapter(ISelectionProvider provider) {
50         fProvider= provider;
51         Assert.isNotNull(fProvider);
52     }
53     
54     public Transfer getTransfer() {
55         return ResourceTransfer.getInstance();
56     }
57     
58     public void dragStart(DragSourceEvent event) {
59         event.doit= convertSelection().size() > 0;
60     }
61     
62     public void dragSetData(DragSourceEvent event) {
63         List JavaDoc resources= convertSelection();
64         event.data= resources.toArray(new IResource[resources.size()]);
65     }
66     
67     public void dragFinished(DragSourceEvent event) {
68         if (!event.doit)
69             return;
70     }
71     
72     private List JavaDoc convertSelection() {
73         ISelection s= fProvider.getSelection();
74         if (!(s instanceof IStructuredSelection))
75             return Collections.EMPTY_LIST;
76         IStructuredSelection selection= (IStructuredSelection) s;
77         List JavaDoc result= new ArrayList JavaDoc(selection.size());
78         for (Iterator JavaDoc iter= selection.iterator(); iter.hasNext();) {
79             Object JavaDoc element= iter.next();
80             if (element instanceof IResource) {
81                 result.add(element);
82             }
83         }
84         return result;
85     }
86 }
87
88
Popular Tags