1 11 package org.eclipse.jdt.internal.ui.dnd; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.Assert; 18 19 import org.eclipse.swt.dnd.DragSource; 20 import org.eclipse.swt.dnd.DragSourceEvent; 21 import org.eclipse.swt.dnd.DragSourceListener; 22 import org.eclipse.swt.dnd.Transfer; 23 import org.eclipse.swt.dnd.TransferData; 24 25 import org.eclipse.jface.util.TransferDragSourceListener; 26 27 32 public class DelegatingDragAdapter implements DragSourceListener { 33 34 private TransferDragSourceListener[] fPossibleListeners; 35 private List fActiveListeners; 36 private TransferDragSourceListener fFinishListener; 37 38 public DelegatingDragAdapter(TransferDragSourceListener[] listeners) { 39 setPossibleListeners(listeners); 40 } 41 42 protected void setPossibleListeners(TransferDragSourceListener[] listeners) { 43 Assert.isNotNull(listeners); 44 Assert.isTrue(fActiveListeners == null, "Can only set possible listeners before drag operation has started"); fPossibleListeners= listeners; 46 } 47 48 51 public void dragStart(DragSourceEvent event) { 52 fFinishListener= null; 53 boolean saveDoit= event.doit; 54 Object saveData= event.data; 55 boolean doIt= false; 56 List transfers= new ArrayList (fPossibleListeners.length); 57 fActiveListeners= new ArrayList (fPossibleListeners.length); 58 59 for (int i= 0; i < fPossibleListeners.length; i++) { 60 TransferDragSourceListener listener= fPossibleListeners[i]; 61 event.doit= saveDoit; 62 listener.dragStart(event); 63 if (event.doit) { 64 transfers.add(listener.getTransfer()); 65 fActiveListeners.add(listener); 66 } 67 doIt= doIt || event.doit; 68 } 69 if (doIt) { 70 ((DragSource)event.widget).setTransfer((Transfer[])transfers.toArray(new Transfer[transfers.size()])); 71 } 72 event.data= saveData; 73 event.doit= doIt; 74 } 75 76 79 public void dragSetData(DragSourceEvent event) { 80 fFinishListener= getListener(event.dataType); 81 if (fFinishListener != null) 82 fFinishListener.dragSetData(event); 83 } 84 85 88 public void dragFinished(DragSourceEvent event) { 89 try{ 90 if (fFinishListener != null) { 91 fFinishListener.dragFinished(event); 92 } else { 93 fFinishListener= getListener(event.dataType); 96 if (fFinishListener != null) 97 fFinishListener.dragFinished(event); 98 } 99 } finally{ 100 fFinishListener= null; 101 fActiveListeners= null; 102 } 103 } 104 105 private TransferDragSourceListener getListener(TransferData type) { 106 if (type == null) 107 return null; 108 109 for (Iterator iter= fActiveListeners.iterator(); iter.hasNext();) { 110 TransferDragSourceListener listener= (TransferDragSourceListener)iter.next(); 111 if (listener.getTransfer().isSupportedType(type)) { 112 return listener; 113 } 114 } 115 return null; 116 } 117 } 118 | Popular Tags |