KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > view > PluginsDragAdapter


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.pde.internal.ui.view;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.jface.viewers.ISelectionProvider;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.pde.internal.core.FileAdapter;
19 import org.eclipse.swt.dnd.DragSource;
20 import org.eclipse.swt.dnd.DragSourceAdapter;
21 import org.eclipse.swt.dnd.DragSourceEvent;
22 import org.eclipse.swt.dnd.FileTransfer;
23 import org.eclipse.swt.widgets.Control;
24
25 public class PluginsDragAdapter extends DragSourceAdapter {
26     ISelectionProvider selectionProvider;
27
28     /**
29      * NavigatorDragAction constructor comment.
30      */

31     public PluginsDragAdapter(ISelectionProvider provider) {
32         selectionProvider = provider;
33     }
34
35     /**
36      * Returns the data to be transferred in a drag and drop
37      * operation.
38      */

39     public void dragSetData(DragSourceEvent event) {
40
41         //resort to a file transfer
42
if (!FileTransfer.getInstance().isSupportedType(event.dataType))
43             return;
44
45         FileAdapter[] files = getSelectedFiles();
46
47         // Get the path of each file and set as the drag data
48
final int len = files.length;
49         String JavaDoc[] fileNames = new String JavaDoc[len];
50         for (int i = 0, length = len; i < length; i++) {
51             fileNames[i] = files[i].getFile().getAbsolutePath();
52         }
53         event.data = fileNames;
54     }
55     /**
56      * All selection must be files or folders.
57      */

58     public void dragStart(DragSourceEvent event) {
59
60         // Workaround for 1GEUS9V
61
DragSource dragSource = (DragSource) event.widget;
62         Control control = dragSource.getControl();
63         if (control != control.getDisplay().getFocusControl()) {
64             event.doit = false;
65             return;
66         }
67         
68         FileAdapter [] files = getSelectedFiles();
69         
70         if (files.length==0) {
71             event.doit = false;
72             return;
73         }
74         event.doit = true;
75     }
76     private FileAdapter [] getSelectedFiles() {
77         IStructuredSelection selection = (IStructuredSelection)selectionProvider.getSelection();
78         ArrayList JavaDoc files = new ArrayList JavaDoc();
79         for (Iterator JavaDoc iter=selection.iterator(); iter.hasNext();) {
80             Object JavaDoc obj = iter.next();
81             if (obj instanceof FileAdapter)
82                 files.add(obj);
83             else
84                 return new FileAdapter[0];
85         }
86         return (FileAdapter[])files.toArray(new FileAdapter[files.size()]);
87     }
88 }
89
Popular Tags