1 11 package org.eclipse.ui.part; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.core.runtime.IConfigurationElement; 15 import org.eclipse.core.runtime.IExtension; 16 import org.eclipse.core.runtime.IExtensionPoint; 17 import org.eclipse.core.runtime.IExtensionRegistry; 18 import org.eclipse.core.runtime.Platform; 19 import org.eclipse.jface.viewers.StructuredViewer; 20 import org.eclipse.jface.viewers.ViewerDropAdapter; 21 import org.eclipse.swt.dnd.DND; 22 import org.eclipse.swt.dnd.DropTargetEvent; 23 import org.eclipse.swt.dnd.TransferData; 24 import org.eclipse.ui.PlatformUI; 25 import org.eclipse.ui.internal.WorkbenchPlugin; 26 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants; 27 28 35 public class PluginDropAdapter extends ViewerDropAdapter { 36 39 public static final String ATT_CLASS = "class"; 41 44 private TransferData currentTransfer; 45 46 51 public PluginDropAdapter(StructuredViewer viewer) { 52 super(viewer); 53 } 54 55 59 public void drop(DropTargetEvent event) { 60 try { 61 if (PluginTransfer.getInstance().isSupportedType( 62 event.currentDataType)) { 63 PluginTransferData pluginData = (PluginTransferData) event.data; 64 IDropActionDelegate delegate = getPluginAdapter(pluginData); 65 if (!delegate.run(pluginData.getData(), getCurrentTarget())) { 66 event.detail = DND.DROP_NONE; 67 } 68 } else { 69 super.drop(event); 70 } 71 } catch (CoreException e) { 72 WorkbenchPlugin.log("Drop Failed", e.getStatus()); } 74 } 75 76 79 protected TransferData getCurrentTransfer() { 80 return currentTransfer; 81 } 82 83 90 protected static IDropActionDelegate getPluginAdapter( 91 PluginTransferData data) throws CoreException { 92 93 IExtensionRegistry registry = Platform.getExtensionRegistry(); 94 String adapterName = data.getExtensionId(); 95 IExtensionPoint xpt = registry.getExtensionPoint(PlatformUI.PLUGIN_ID, 96 IWorkbenchRegistryConstants.PL_DROP_ACTIONS); 97 IExtension[] extensions = xpt.getExtensions(); 98 for (int i = 0; i < extensions.length; i++) { 99 IConfigurationElement[] configs = extensions[i] 100 .getConfigurationElements(); 101 if (configs != null && configs.length > 0) { 102 String id = configs[0].getAttribute("id"); if (id != null && id.equals(adapterName)) { 104 return (IDropActionDelegate) WorkbenchPlugin 105 .createExtension(configs[0], ATT_CLASS); 106 } 107 } 108 } 109 return null; 110 } 111 112 115 public boolean performDrop(Object data) { 116 return false; 118 } 119 120 125 public boolean validateDrop(Object target, int operation, 126 TransferData transferType) { 127 currentTransfer = transferType; 128 if (currentTransfer != null 129 && PluginTransfer.getInstance() 130 .isSupportedType(currentTransfer)) { 131 return true; 133 } 134 return false; 135 } 136 } 137 | Popular Tags |