1 11 package org.eclipse.pde.internal.ui.views.plugins; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 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 31 public PluginsDragAdapter(ISelectionProvider provider) { 32 selectionProvider = provider; 33 } 34 35 39 public void dragSetData(DragSourceEvent event) { 40 41 if (!FileTransfer.getInstance().isSupportedType(event.dataType)) 43 return; 44 45 FileAdapter[] files = getSelectedFiles(); 46 47 final int len = files.length; 49 String [] fileNames = new String [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 58 public void dragStart(DragSourceEvent event) { 59 60 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 files = new ArrayList (); 79 for (Iterator iter=selection.iterator(); iter.hasNext();) { 80 Object 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 |