1 11 package org.eclipse.pde.internal.ui.editor.schema; 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.ischema.ISchemaObject; 19 import org.eclipse.pde.internal.ui.editor.ModelDataTransfer; 20 import org.eclipse.swt.dnd.DragSource; 21 import org.eclipse.swt.dnd.DragSourceAdapter; 22 import org.eclipse.swt.dnd.DragSourceEvent; 23 import org.eclipse.swt.dnd.TextTransfer; 24 import org.eclipse.swt.widgets.Control; 25 26 public class ElementSectionDragAdapter extends DragSourceAdapter { 27 private ISelectionProvider fSelectionProvider; 28 private Object fDragData; 29 30 33 public ElementSectionDragAdapter(ISelectionProvider provider) { 34 fSelectionProvider = provider; 35 36 } 37 38 42 public void dragSetData(DragSourceEvent event) { 43 if (event.doit == false) 44 return; 45 if (ModelDataTransfer.getInstance().isSupportedType(event.dataType)) { 46 event.data = getSelectedModelObjects(); 47 fDragData = event.data; 48 return; 49 } 50 if (TextTransfer.getInstance().isSupportedType(event.dataType)) { 51 event.data = 52 createTextualRepresentation( 53 (IStructuredSelection) fSelectionProvider.getSelection()); 54 fDragData = null; 55 return; 56 } 57 } 58 59 private String createTextualRepresentation(IStructuredSelection sel) { 60 StringBuffer buf = new StringBuffer (); 61 for (Iterator iter = sel.iterator(); iter.hasNext();) { 62 String name = iter.next().toString(); 63 buf.append(name); 64 buf.append(" "); } 66 return buf.toString(); 67 } 68 71 public void dragStart(DragSourceEvent event) { 72 73 DragSource dragSource = (DragSource) event.widget; 75 Control control = dragSource.getControl(); 76 if (control != control.getDisplay().getFocusControl()) { 77 event.doit = false; 78 return; 79 } 80 81 event.doit = canDrag(); 82 } 83 84 public void dragFinished(DragSourceEvent event) { 85 if (event.doit == false || fDragData == null) 86 return; 87 fDragData = null; 88 } 89 90 private boolean canDrag() { 91 return canCopy((IStructuredSelection) fSelectionProvider.getSelection()); 92 } 93 94 private boolean canCopy(IStructuredSelection selection) { 95 Object prev = null; 96 for (Iterator iter = selection.iterator(); iter.hasNext();) { 97 Object obj = iter.next(); 98 if (!(obj instanceof ISchemaObject)) 99 return false; 100 if (prev != null) { 101 if (prev.getClass().equals(obj.getClass()) == false) 102 return false; 103 } else 104 prev = obj; 105 } 106 return true; 107 } 108 109 private ISchemaObject[] getSelectedModelObjects() { 110 return createObjectRepresentation( 111 (IStructuredSelection) fSelectionProvider.getSelection()); 112 } 113 114 private ISchemaObject[] createObjectRepresentation(IStructuredSelection selection) { 115 ArrayList objects = new ArrayList (); 116 for (Iterator iter = selection.iterator(); iter.hasNext();) { 117 Object obj = iter.next(); 118 if (obj instanceof ISchemaObject) 119 objects.add(obj); 120 else 121 return new ISchemaObject[0]; 122 } 123 return (ISchemaObject[]) objects.toArray( 124 new ISchemaObject[objects.size()]); 125 } 126 127 public Object [] getDragData() { 128 if (fDragData instanceof Object []) 129 return (Object [])fDragData; 130 return new Object [] { fDragData }; 131 } 132 } 133 | Popular Tags |