1 11 package org.eclipse.ui.navigator; 12 13 import java.util.LinkedHashSet ; 14 import java.util.Set ; 15 16 import org.eclipse.jface.util.LocalSelectionTransfer; 17 import org.eclipse.jface.viewers.ISelection; 18 import org.eclipse.jface.viewers.ISelectionProvider; 19 import org.eclipse.jface.viewers.IStructuredSelection; 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.Transfer; 24 import org.eclipse.swt.widgets.Control; 25 import org.eclipse.ui.internal.navigator.NavigatorPlugin; 26 import org.eclipse.ui.internal.navigator.dnd.NavigatorPluginDropAction; 27 import org.eclipse.ui.part.PluginTransfer; 28 29 48 public final class CommonDragAdapter extends DragSourceAdapter { 49 50 private static final boolean DEBUG = false; 51 52 private final INavigatorContentService contentService; 53 54 private final ISelectionProvider provider; 55 56 65 public CommonDragAdapter(INavigatorContentService aContentService, 66 ISelectionProvider aProvider) { 67 super(); 68 contentService = aContentService; 69 provider = aProvider; 70 } 71 72 83 public Transfer[] getSupportedDragTransfers() { 84 CommonDragAdapterAssistant[] assistants = contentService 85 .getDnDService().getCommonDragAssistants(); 86 87 Set supportedTypes = new LinkedHashSet (); 88 supportedTypes.add(PluginTransfer.getInstance()); 89 supportedTypes.add(LocalSelectionTransfer.getTransfer()); 90 Transfer[] transferTypes = null; 91 for (int i = 0; i < assistants.length; i++) { 92 transferTypes = assistants[i].getSupportedTransferTypes(); 93 for (int j = 0; j < transferTypes.length; j++) { 94 if (transferTypes[j] != null) { 95 supportedTypes.add(transferTypes[j]); 96 } 97 } 98 } 99 100 Transfer[] transfers = (Transfer[]) supportedTypes 101 .toArray(new Transfer[supportedTypes.size()]); 102 return transfers; 103 } 104 105 110 public void dragStart(DragSourceEvent event) { 111 if (DEBUG) { 112 System.out.println("CommonDragAdapter.dragStart (begin): " + event); } 114 try { 115 DragSource dragSource = (DragSource) event.widget; 117 Control control = dragSource.getControl(); 118 if (control == control.getDisplay().getFocusControl()) { 119 ISelection selection = provider.getSelection(); 120 if (!selection.isEmpty()) { 121 LocalSelectionTransfer.getTransfer() 122 .setSelection(selection); 123 event.doit = true; 124 } else { 125 event.doit = false; 126 } 127 } else { 128 event.doit = false; 129 } 130 } catch (RuntimeException e) { 131 NavigatorPlugin.logError(0, e.getMessage(), e); 132 } 133 134 if (DEBUG) { 135 System.out 136 .println("CommonDragAdapter.dragStart (end): doit=" + event.doit); } 138 } 139 140 145 public void dragSetData(DragSourceEvent event) { 146 147 ISelection selection = LocalSelectionTransfer.getTransfer() 148 .getSelection(); 149 150 if (DEBUG) { 151 System.out 152 .println("CommonDragAdapter.dragSetData (begin): event" + event + " selection=" + selection); } 154 155 if (LocalSelectionTransfer.getTransfer() 156 .isSupportedType(event.dataType)) { 157 event.data = selection; 158 159 if (DEBUG) { 160 System.out 161 .println("CommonDragAdapter.dragSetData set LocalSelectionTransfer"); } 163 } else if (PluginTransfer.getInstance().isSupportedType(event.dataType)) { 164 event.data = NavigatorPluginDropAction 165 .createTransferData(contentService); 166 if (DEBUG) { 167 System.out 168 .println("CommonDragAdapter.dragSetData set PluginTransfer"); } 170 } else if (selection instanceof IStructuredSelection) { 171 if (DEBUG) { 172 System.out 173 .println("CommonDragAdapter.dragSetData looking for assistants"); } 175 176 INavigatorDnDService dndService = contentService.getDnDService(); 177 CommonDragAdapterAssistant[] assistants = dndService 178 .getCommonDragAssistants(); 179 for (int i = 0; i < assistants.length; i++) { 180 181 Transfer[] supportedTransferTypes = assistants[i] 182 .getSupportedTransferTypes(); 183 for (int j = 0; j < supportedTransferTypes.length; j++) { 184 if (supportedTransferTypes[j] 185 .isSupportedType(event.dataType)) { 186 try { 187 if (DEBUG) { 188 System.out 189 .println("CommonDragAdapter.dragSetData set assistant transfer type"); } 191 if(assistants[i].setDragData(event, 192 (IStructuredSelection) selection)) { 193 return; 194 } 195 } catch (RuntimeException re) { 196 NavigatorPlugin.logError(0, re.getMessage(), re); 197 } 198 } 199 200 } 201 } 202 203 } else { 204 event.doit = false; 205 } 206 207 if (DEBUG) { 208 System.out.println("CommonDragAdapter.dragSetData (end): " + event); } 210 } 211 212 217 public void dragFinished(DragSourceEvent event) { 218 219 if (DEBUG) { 220 System.out.println("CommonDragAdapter.dragFinished()."); } 222 223 LocalSelectionTransfer.getTransfer().setSelection(null); 224 225 } 230 231 } 232 | Popular Tags |