1 11 package org.eclipse.jface.util; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.swt.dnd.DND; 18 import org.eclipse.swt.dnd.DropTargetEvent; 19 import org.eclipse.swt.dnd.DropTargetListener; 20 import org.eclipse.swt.dnd.Transfer; 21 import org.eclipse.swt.dnd.TransferData; 22 23 101 public class DelegatingDropAdapter implements DropTargetListener { 102 private List listeners = new ArrayList (); 103 104 private TransferDropTargetListener currentListener; 105 106 private int originalDropType; 107 108 113 public void addDropTargetListener(TransferDropTargetListener listener) { 114 listeners.add(listener); 115 } 116 117 124 public void dragEnter(DropTargetEvent event) { 125 originalDropType = event.detail; 128 updateCurrentListener(event); 129 } 130 131 138 public void dragLeave(final DropTargetEvent event) { 139 setCurrentListener(null, event); 142 } 143 144 152 public void dragOperationChanged(final DropTargetEvent event) { 153 originalDropType = event.detail; 156 TransferDropTargetListener oldListener = getCurrentListener(); 157 updateCurrentListener(event); 158 final TransferDropTargetListener newListener = getCurrentListener(); 159 if (newListener != null && newListener == oldListener) { 163 SafeRunnable.run(new SafeRunnable() { 164 public void run() throws Exception { 165 newListener.dragOperationChanged(event); 166 } 167 }); 168 } 169 } 170 171 180 public void dragOver(final DropTargetEvent event) { 181 TransferDropTargetListener oldListener = getCurrentListener(); 182 updateCurrentListener(event); 183 final TransferDropTargetListener newListener = getCurrentListener(); 184 185 if (newListener != null && newListener == oldListener) { 189 SafeRunnable.run(new SafeRunnable() { 190 public void run() throws Exception { 191 newListener.dragOver(event); 192 } 193 }); 194 } 195 } 196 197 204 public void drop(final DropTargetEvent event) { 205 updateCurrentListener(event); 208 if (getCurrentListener() != null) { 209 SafeRunnable.run(new SafeRunnable() { 210 public void run() throws Exception { 211 getCurrentListener().drop(event); 212 } 213 }); 214 } 215 setCurrentListener(null, event); 216 } 217 218 224 public void dropAccept(final DropTargetEvent event) { 225 if (getCurrentListener() != null) { 228 SafeRunnable.run(new SafeRunnable() { 229 public void run() throws Exception { 230 getCurrentListener().dropAccept(event); 231 } 232 }); 233 } 234 } 235 236 242 private TransferDropTargetListener getCurrentListener() { 243 return currentListener; 244 } 245 246 257 private TransferData getSupportedTransferType(TransferData[] dataTypes, 258 TransferDropTargetListener listener) { 259 for (int i = 0; i < dataTypes.length; i++) { 260 if (listener.getTransfer().isSupportedType(dataTypes[i])) { 261 return dataTypes[i]; 262 } 263 } 264 return null; 265 } 266 267 273 public Transfer[] getTransfers() { 274 Transfer[] types = new Transfer[listeners.size()]; 275 for (int i = 0; i < listeners.size(); i++) { 276 TransferDropTargetListener listener = (TransferDropTargetListener) listeners 277 .get(i); 278 types[i] = listener.getTransfer(); 279 } 280 return types; 281 } 282 283 289 public boolean isEmpty() { 290 return listeners.isEmpty(); 291 } 292 293 299 public void removeDropTargetListener(TransferDropTargetListener listener) { 300 if (currentListener == listener) { 301 currentListener = null; 302 } 303 listeners.remove(listener); 304 } 305 306 313 private boolean setCurrentListener(TransferDropTargetListener listener, 314 final DropTargetEvent event) { 315 if (currentListener == listener) { 316 return false; 317 } 318 if (currentListener != null) { 319 SafeRunnable.run(new SafeRunnable() { 320 public void run() throws Exception { 321 currentListener.dragLeave(event); 322 } 323 }); 324 } 325 currentListener = listener; 326 if (currentListener != null) { 329 SafeRunnable.run(new SafeRunnable() { 330 public void run() throws Exception { 331 currentListener.dragEnter(event); 332 } 333 }); 334 } 335 return true; 336 } 337 338 348 private void updateCurrentListener(DropTargetEvent event) { 349 int originalDetail = event.detail; 350 event.detail = originalDropType; 354 355 Iterator iter = listeners.iterator(); 356 while (iter.hasNext()) { 357 TransferDropTargetListener listener = (TransferDropTargetListener) iter 358 .next(); 359 TransferData dataType = getSupportedTransferType(event.dataTypes, 360 listener); 361 if (dataType != null) { 362 TransferData originalDataType = event.currentDataType; 363 event.currentDataType = dataType; 365 if (listener.isEnabled(event)) { 366 if (!setCurrentListener(listener, event)) { 369 event.detail = originalDetail; 370 } 371 return; 372 } 373 event.currentDataType = originalDataType; 374 } 375 } 376 setCurrentListener(null, event); 377 event.detail = DND.DROP_NONE; 378 379 event.feedback = DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL; 383 } 384 } 385 | Popular Tags |