1 11 12 package org.eclipse.ui.internal; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.swt.dnd.DND; 19 import org.eclipse.swt.dnd.DropTarget; 20 import org.eclipse.swt.dnd.DropTargetEvent; 21 import org.eclipse.swt.dnd.DropTargetListener; 22 import org.eclipse.swt.dnd.Transfer; 23 import org.eclipse.swt.dnd.TransferData; 24 import org.eclipse.swt.widgets.Control; 25 import org.eclipse.ui.PlatformUI; 26 import org.eclipse.ui.dnd.IDragAndDropService; 27 import org.eclipse.ui.services.IDisposable; 28 29 46 public class EditorSiteDragAndDropServiceImpl implements IDragAndDropService, IDisposable { 47 58 private static class MergedDropTarget { 59 private DropTarget realDropTarget; 60 61 private Transfer[] secondaryTransfers; 62 private DropTargetListener secondaryListener; 63 64 private Transfer[] primaryTransfers; 65 private DropTargetListener primaryListener; 66 67 public MergedDropTarget(Control control, 68 int priOps, Transfer[] priTransfers, DropTargetListener priListener, 69 int secOps, Transfer[] secTransfers, DropTargetListener secListener) { 70 realDropTarget = new DropTarget(control, priOps | secOps); 71 72 primaryTransfers = priTransfers; 74 primaryListener = priListener; 75 76 WorkbenchWindow ww = (WorkbenchWindow) PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 78 WorkbenchWindowConfigurer winConfigurer = ww.getWindowConfigurer(); 79 secondaryTransfers = winConfigurer.getTransfers(); 80 secondaryListener = winConfigurer.getDropTargetListener(); 81 82 Transfer[] allTransfers = new Transfer[secondaryTransfers.length+primaryTransfers.length]; 84 int curTransfer = 0; 85 for (int i = 0; i < primaryTransfers.length; i++) { 86 allTransfers[curTransfer++] = primaryTransfers[i]; 87 } 88 for (int i = 0; i < secondaryTransfers.length; i++) { 89 allTransfers[curTransfer++] = secondaryTransfers[i]; 90 } 91 realDropTarget.setTransfer(allTransfers); 92 93 realDropTarget.addDropListener(new DropTargetListener() { 96 public void dragEnter(DropTargetEvent event) { 97 getAppropriateListener(event).dragEnter(event); 98 } 99 public void dragLeave(DropTargetEvent event) { 100 getAppropriateListener(event).dragLeave(event); 101 } 102 public void dragOperationChanged(DropTargetEvent event) { 103 getAppropriateListener(event).dragOperationChanged(event); 104 } 105 public void dragOver(DropTargetEvent event) { 106 getAppropriateListener(event).dragOver(event); 107 } 108 public void drop(DropTargetEvent event) { 109 getAppropriateListener(event).drop(event); 110 } 111 public void dropAccept(DropTargetEvent event) { 112 getAppropriateListener(event).dropAccept(event); 113 } 114 }); 115 } 116 117 private DropTargetListener getAppropriateListener(DropTargetEvent event) { 118 if (isSupportedType(primaryTransfers, event.currentDataType)) 119 return primaryListener; 120 121 return secondaryListener; 122 } 123 124 private boolean isSupportedType(Transfer[] transfers, TransferData transferType) { 125 for (int i = 0; i < transfers.length; i++) { 126 if (transfers[i].isSupportedType(transferType)) 127 return true; 128 } 129 return false; 130 } 131 132 135 public void dispose() { 136 } 137 } 138 139 List addedListeners = new ArrayList (); 141 142 145 public void addMergedDropTarget(Control control, int ops, Transfer[] transfers, 146 DropTargetListener listener) { 147 removeMergedDropTarget(control); 149 150 int editorSiteOps = DND.DROP_DEFAULT | DND.DROP_COPY | DND.DROP_LINK; 152 153 WorkbenchWindow ww = (WorkbenchWindow) PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 154 WorkbenchWindowConfigurer winConfigurer = ww.getWindowConfigurer(); 155 Transfer[] editorSiteTransfers = winConfigurer.getTransfers(); 156 DropTargetListener editorSiteListener = winConfigurer.getDropTargetListener(); 157 158 MergedDropTarget newTarget = new MergedDropTarget(control, ops, transfers, listener, 161 editorSiteOps, editorSiteTransfers, editorSiteListener); 162 addedListeners.add(newTarget); 163 } 164 165 178 private DropTarget getCurrentDropTarget(Control control) { 179 if (control == null) 180 return null; 181 182 Object curDT = control.getData("DropTarget"); return (DropTarget)curDT; 184 } 185 186 189 public void removeMergedDropTarget(Control control) { 190 DropTarget targetForControl = getCurrentDropTarget(control); 191 if (targetForControl != null) { 192 targetForControl.dispose(); 193 addedListeners.remove(targetForControl); 194 } 195 } 196 197 200 public void dispose() { 201 for (Iterator iterator = addedListeners.iterator(); iterator.hasNext();) { 203 MergedDropTarget target = (MergedDropTarget) iterator.next(); 204 target.dispose(); 205 } 206 addedListeners.clear(); 207 } 208 209 } 210 | Popular Tags |