1 11 package org.eclipse.jdt.internal.ui.dnd; 12 13 import org.eclipse.core.runtime.Assert; 14 import org.eclipse.core.runtime.SafeRunner; 15 16 import org.eclipse.swt.dnd.DND; 17 import org.eclipse.swt.dnd.DropTargetEvent; 18 import org.eclipse.swt.dnd.DropTargetListener; 19 import org.eclipse.swt.dnd.Transfer; 20 import org.eclipse.swt.dnd.TransferData; 21 22 import org.eclipse.jface.util.SafeRunnable; 23 import org.eclipse.jface.util.TransferDropTargetListener; 24 25 31 public class DelegatingDropAdapter implements DropTargetListener { 32 33 private TransferDropTargetListener[] fListeners; 34 private TransferDropTargetListener fCurrentListener; 35 private int fOriginalDropType; 36 37 42 public DelegatingDropAdapter(TransferDropTargetListener[] listeners) { 43 Assert.isNotNull(listeners); 44 fListeners= listeners; 45 } 46 47 55 public void dragEnter(DropTargetEvent event) { 56 fOriginalDropType= event.detail; 57 updateCurrentListener(event); 58 } 59 60 67 public void dragLeave(final DropTargetEvent event) { 68 setCurrentListener(null, event); 69 } 70 71 79 public void dragOperationChanged(final DropTargetEvent event) { 80 fOriginalDropType= event.detail; 81 TransferDropTargetListener oldListener= getCurrentListener(); 82 updateCurrentListener(event); 83 final TransferDropTargetListener newListener= getCurrentListener(); 84 if (newListener != null && newListener == oldListener) { 88 SafeRunner.run(new SafeRunnable() { 89 public void run() throws Exception { 90 newListener.dragOperationChanged(event); 91 } 92 }); 93 } 94 } 95 96 105 public void dragOver(final DropTargetEvent event) { 106 TransferDropTargetListener oldListener= getCurrentListener(); 107 updateCurrentListener(event); 108 final TransferDropTargetListener newListener= getCurrentListener(); 109 110 if (newListener != null && newListener == oldListener) { 114 SafeRunner.run(new SafeRunnable() { 115 public void run() throws Exception { 116 newListener.dragOver(event); 117 } 118 }); 119 } 120 } 121 122 129 public void drop(final DropTargetEvent event) { 130 updateCurrentListener(event); 131 if (getCurrentListener() != null) { 132 SafeRunner.run(new SafeRunnable() { 133 public void run() throws Exception { 134 getCurrentListener().drop(event); 135 } 136 }); 137 } 138 setCurrentListener(null, event); 139 } 140 141 147 public void dropAccept(final DropTargetEvent event) { 148 if (getCurrentListener() != null) { 149 SafeRunner.run(new SafeRunnable() { 150 public void run() throws Exception { 151 getCurrentListener().dropAccept(event); 152 } 153 }); 154 } 155 } 156 157 163 private TransferDropTargetListener getCurrentListener() { 164 return fCurrentListener; 165 } 166 167 178 private TransferData getSupportedTransferType(TransferData[] dataTypes, TransferDropTargetListener listener) { 179 for (int i= 0; i < dataTypes.length; i++) { 180 if (listener.getTransfer().isSupportedType(dataTypes[i])) { 181 return dataTypes[i]; 182 } 183 } 184 return null; 185 } 186 187 193 public Transfer[] getTransfers() { 194 Transfer[] types= new Transfer[fListeners.length]; 195 for (int i= 0; i < fListeners.length; i++) { 196 types[i]= fListeners[i].getTransfer(); 197 } 198 return types; 199 } 200 201 208 private boolean setCurrentListener(TransferDropTargetListener listener, final DropTargetEvent event) { 209 if (fCurrentListener == listener) 210 return false; 211 if (fCurrentListener != null) { 212 SafeRunner.run(new SafeRunnable() { 213 public void run() throws Exception { 214 fCurrentListener.dragLeave(event); 215 } 216 }); 217 } 218 fCurrentListener= listener; 219 if (fCurrentListener != null) { 220 SafeRunner.run(new SafeRunnable() { 221 public void run() throws Exception { 222 fCurrentListener.dragEnter(event); 223 } 224 }); 225 } 226 return true; 227 } 228 229 240 private void updateCurrentListener(DropTargetEvent event) { 241 int originalDetail= event.detail; 242 event.detail= fOriginalDropType; 247 248 for (int i= 0; i < fListeners.length; i++) { 249 TransferDropTargetListener listener= fListeners[i]; 250 TransferData dataType= getSupportedTransferType(event.dataTypes, listener); 251 if (dataType != null) { 252 TransferData originalDataType= event.currentDataType; 253 event.currentDataType= dataType; 255 if (listener.isEnabled(event)) { 256 if (!setCurrentListener(listener, event)) 260 event.detail= originalDetail; 261 return; 262 } else { 263 event.currentDataType= originalDataType; 264 } 265 } 266 } 267 setCurrentListener(null, event); 268 event.detail= DND.DROP_NONE; 269 } 270 } 271 | Popular Tags |